I’ve made a class, which is called RestorableObject, which was really really helpful for me, to do things faster, and maybe something like this would nice addition for Juce.
The idea is that every-Data Object/Container should be derived from RestoreableObject.
Then every RestoreableObject provides additional functions like createXMLString, createXML, saveToFile, restoreFromFile, generate Hash etc.
Internally RestoreableObject works with ValueTree.
But there is a reason why the classes not work directly with ValueTree: speed (if you do dsp-stuff you wouldn’t use a valueTree for that), typesafity, stability, simplicity (its easier to use local variables instead to do this with a value-tree).
Example you want to store this data-structure:
class MyPoint
{
public:
int x,y
}
Now adding RestoreableObject, which has three virtual functions which needed to be overwritten.
getIdentifier(),doSaveItems(),doRestoreItems()
[code]class MyPoint : public RestoreableObject
{
public:
int x,y;
Identifier getIdentifier()
{
return "MyPoint"
};
void doSaveItems()
{
saveItem("x",x);
saveItem("y",y);
};
void doRestoreItems()
{
x=restoreItem("x", defaultValue);
y=restoreItem("y", defaultValue);
};
}[/code]
If you have object which is a line, it uses two points
[code]class MyLine : public RestoreableObject
{
public:
MyPoint p1,p2;
Identifier getIdentifier()
{
return "MyLine"
};
void doSaveItems()
{
saveItemObject("p1",p1);
saveItemObject("p2",p2);
};
void doSaveItems()
{
restoreItemObject("p1",p1);
restoreItemObject("p2",p2);
};
void paintLine(Graphics &g);
}[/code]
If you have document to store, which stores a various number of lines, you can do that
[code]
class MyProject : public RestoreableObject
{
public:
OwnedArray lines;
Identifier getIdentifier()
{
return "MyProject"
};
void doSaveItems()
{
for (int i=0; i<lines.size; i++)
saveItemArrayMember("lines",i,lines[i]);
};
void doSaveItems()
{
while (restoreItemArrayMember("lines",lines))
{
};
};
}[/code]
Then if you want to save the whole document, it automatically generates a XML File.
then the file content is something like this
<Project>
<Lines>
<Member>
<MyLine>
<MyPoint x="10" y="20">
<MyPoint x="30" y="40">
</MyLine>
</Member>
<Member>
<MyLine>
<MyPoint x="100" y="200">
<MyPoint x="300" y="400">
</MyLine>
</Member>
</Lines>
</Project>