ideas for data binding?
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@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! ]