From: Frank Buss on
I want to serialize some objects and modify it with a GUI. An example:

class Library {
private:
string m_city;
vector<Book*> m_books;
};

class Book {
private:
string m_title;
int m_count;
};

My first approach was something straigtforward, which kind of works, but is
a maintenance and code-bloat nightmare:

Library::saveXml(Node* node) {
node->addTextNode("Name", m_name);
Node* books = node->addNode("Books");
for (size_t i = 0; i < m_books; i++) {
m_books[i]->saveXml(books);
}
}
Library::loadXml(Node* node) {
m_name = node->getTextNode("Name");
Node* books = node->getNode("Books");
for (size_t i = 0; i < books->size(); i++) {
Node* bookNode = node->getChild(i);
Book* book = new Book();
book->loadXml(bookNode);
m_books.push_back(book);
}
}
....

For the GUI it looks like this:

class Handler
{
public:
virtual string getValue() = 0;
virtual void setValue(string text) = 0;
}

void showBook(Book* book)
{
class TitleHandler : public Handler {
public:
TitleHandler(Book* book) : m_book(book) {}
void setValue(text) { m_book->setTitle(text); foo(); }
string getValue() { return m_book->getTitle(); }
};
private:
Book* m_book;
};
...
addTextField(new TitleHandler(book));
addNumberField(new CountHandler(book));
}

This is a simplified example, in my real project it is more complicated,
because I've implemented my own GUI system and when the user sets some
values, some fields needs to be added or removed (that's the foo-method in
the example above, which could test the value and then change the GUI).

My goal is to simplify the amount of code for the GUI application code and
the serializers. An idea would be to use some kind of special data object
class instead of plain "string" and "int", but this is for an embedded
system and I don't know if this would slow down the program too much, so
maybe I need both: plain members and a list of data objects for each
object, which references the members, maybe with some kind of template
magic and function pointers for the getters and setters.

--
Frank Buss, fb(a)frank-buss.de
http://www.frank-buss.de, http://www.it4-systems.de

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: Frank Buss on
Frank Buss wrote:

> My goal is to simplify the amount of code for the GUI application code and
> the serializers. An idea would be to use some kind of special data object
> class instead of plain "string" and "int", but this is for an embedded
> system and I don't know if this would slow down the program too much, so
> maybe I need both: plain members and a list of data objects for each
> object, which references the members, maybe with some kind of template
> magic and function pointers for the getters and setters.

I think extra objects for all data members is the way to go. A solution
would be something like this:

class Attribute {
public:
Attribute(string attributeName);
virtual void loadValue(XmlNode* node) = 0;
virtual void saveValue(XmlNode* node) = 0;
virtual string getDisplayValue() = 0;
};

class IntAttribute : public Attribute {
public:
IntAttribute(string attributeName, int value);
int getValue();
void setValue(int value);
....
};
class StringAttribute : public Attribute ...
class EnumAttribute : public Attribute ...

Then I would derive all my data classes from one class:

class Data {
public:
virtual void loadValues(XmlNode* node);
virtual void saveValues(XmlNode* node);
private:
vector<Attribute*> m_attributes;
};

The loadValues and saveValues methods can be generic, except for saving
more complex objects than Attributes, or maybe with some kind of Container
Attribute class? But I want to avoid multiple inheritance.

The constructor of the book class could create the attribute-objects, but
maybe for faster access and compile time check, I'll need a member for each
attribute:

Book::Book() {
m_countAttribute = new intAttribute("Count", 0);
m_attributes.push_back(m_countAttribute);
}

The destructor of Data can delete all attributes.

Then I can use it in the GUI like this, without the need for a Handler
class for each member:

addNumberField(aBook->getCountAttribute());

There are still some open questions:

- do you think it is fast enough to use the attributes instead of plain
fields? E.g. I could rewrite the getCount in book as getCount() { return
m_countAttribute->getValue(); } (and the same for setCount)

- how can I reduce the amount of code for defining and declaring one
attribute and the related getters, setters and initializing code? I can
think of some macros, but maybe templates would be more elegant

- how can I add some additional callback action for the GUI in an easy to
use way, when a value is changed? I'm using an old 4.1 GCC and I don't
think the nice new C++0x closures are supported

--
Frank Buss, fb(a)frank-buss.de
http://www.frank-buss.de, http://www.it4-systems.de

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: Mathias Gaunard on
On 30 avr, 12:05, Frank Buss <f...(a)frank-buss.de> wrote:
> I want to serialize some objects and modify it with a GUI.

Take a look at Boost.Serialization for an example of a nice way to do
this.

--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: Frank Buss on
Mathias Gaunard wrote:

> Take a look at Boost.Serialization for an example of a nice way to do
> this.

Thanks, there are some nice ideas in this framework. But it doesn't solve
all my problems. E.g. I need a label and a default value for each member.
The name-value-pair idea of Boost.Serialization could provide this for
serializing the data, but I don't see how I can use the same information
for displaying it in the GUI.

But looks like my concept works. I don't have the requirement of the
Boost.Serialization library to be non-intrusive, because I can create my
own data classes. Now it looks like this:

class Book : public Data {
public:
Book() { m_count = new IntAttribute(this, "Count", 0); }
int getCount() { return m_count->getValue(); }
void setCount(int count) { m_count->setValue(count); }
IntAttribute* getCountAttribute() { return m_count; }
private:
IntAttribute* m_count;
};

The Data class has a vector of Attribute* elements and the "this" parameter
in IntAttribute is used to call addAttribute on Data, so I can't forget it
for filling the attributes array in Data for automatic loading and saving.
This works nice.

I have even an EnumAttribute with a typename template parameter for
typesafe usage of enum parameters. But doing this the right way is
difficult, because sometimes the compiler error messages are difficult to
understand when using templates :-)

--
Frank Buss, fb(a)frank-buss.de
http://www.frank-buss.de, http://www.it4-systems.de

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]