Serialization description file for a class containing an array and some C++ code blocks:
// bookshelf.xmlser:
[hpp]#include <book.hpp>
class Bookshelf
{
<%
int BookCount() const
{
return books.size();
}
void AddBook(const Book& book);
%>
Book[] books;
}
<%
void Bookshelf::AddBook(const Book& book)
{
books.push_back(book);
}
%>
An array is denoted by square brackets. The C++ type for it is std::vector.
You can put arbitrary C++ code in a C++ code block delimited by <% and %>. A code block within a class is placed to the generated header file. A code block after classes is placed to the generated source file.
I have added another book and placed the books to an array of books inside a bookshelf object:
// ...
Book book;
book.name = "The C++ Programming Language, 4th Edition";
book.isbn = "0-321-56384-0";
book.author = "Bjarne Stroustrup";
book.published = 2013;
book.publisher = "Pearson Education";
book.price = 61.88f;
book.color = Color::blue;
Book anotherBook;
anotherBook.name = "The Art of Computer Programming";
anotherBook.isbn = "0-201-89683-4";
anotherBook.author = "Donald E. Knuth";
anotherBook.published = 1997;
anotherBook.publisher = "Addison Wesley Longman";
anotherBook.price = 187.99f;
anotherBook.color = Color::white;
Bookshelf bookshelf;
bookshelf.AddBook(book);
bookshelf.AddBook(anotherBook);
std::unique_ptr<sngxml::dom::Element> element = bookshelf.ToXml("bookshelf");
// ...
Bookshelf bookshelfRead;
bookshelfRead.FromXml(docRead->DocumentElement());
std::cout << bookshelfRead.BookCount() << std::endl;
// ...
The program output looks like this:
<bookshelf classId="-1" className="Bookshelf" objectId="00000000-0000-0000-0000-000000000000">
<books>
<item classId="-1" className="Book" objectId="00000000-0000-0000-0000-000000000000">
<name value="The C++ Programming Language, 4th Edition" />
<isbn value="0-321-56384-0" />
<author value="Bjarne Stroustrup" />
<published value="2013" />
<publisher value="Pearson Education" />
<price value="61.880001" />
<color value="2" />
</item>
<item classId="-1" className="Book" objectId="00000000-0000-0000-0000-000000000000">
<name value="The Art of Computer Programming" />
<isbn value="0-201-89683-4" />
<author value="Donald E. Knuth" />
<published value="1997" />
<publisher value="Addison Wesley" />
<price value="187.990005" />
<color value="1" />
</item>
</books>
</bookshelf>
2