Sign in to follow this  
Jesse Chounard

Looking for feedback (my new C++ TinyXML Serialization code)

Recommended Posts

Greetings, I've spent the last few days working on some code to help automate the process of loading/saving data to XML files. (Using TinyXml in C++.) The code has blown up into something much bigger than what I originally had in mind. In fact, it's probably the most complicated piece of software I've ever written. (And that's something, considering that it's not really that much code, and I've worked on some really big projects at work.) While the code is probably more complicated than it needs to be (with lots of templates and operator overloads) its use is relatively simple. Here's a quick example:
#include "XmlBind.hpp"

using namespace XmlBind;

struct Test
{
	int a;
	float b;
};

void GetBinding(OuterBinding<Test> &binding)
{
	if(!binding.Initialized())
	{
		binding.Define()
		[
			Bind("a", &Test::a),
			Bind("b", &Test::b)
		];
	}
}

int main()
{
	Test t;
	t.a = 42;
	t.b = 17.2f;

	TiXmlElement element("Test");
	ToXml(element, t);
	element.Print(stdout, 0);

	return 0;
}
And its output:
<Test>
    <a>42</a>
    <b>17.2</b>
</Test>
Note: You may notice the similarity in the registration code to how a function is registered with LuaBind. This is intentional. I really like that library, and after writing this code, I have even more respect for its authors than I did before. I figured I could do alot worse than to imitate them. (It's also why I chose the name XmlBind, though a google search showed me that there's a library already using that name. Ah well.) Here's the code (and a slightly more complicated demo): XmlBind.hpp demo.cpp It requires both TinyXml and Boost. (Though it just requires some boost headers, it does require foreach.hpp, which isn't in the latest release. You'll have to get that from CVS. You can use the Sourceforge CVS web interface to get it. It's no sweat.) What it can do: 1) Automatically handles the following types: (signed and unsigned where appropriate) char, short, int, long, float, double, bool, and std::string. 2) After registering them, it handles user defined types composed of the basic types or other registered types. (Classes inside of classes is no problem.) 3) Getter/setter class member functions. 4) Standard C++ containers (vector, list, deque.) So, here's what I'm looking for. 1) Is this useful to anyone? 2) Do you see a way to improve the code? 3) Does it compile properly in your favorite C++ compiler? (I've tried VC8 and g++/mingw.) 4) Are there any other features that you think would be worth adding? If you don't feel like commenting in this public forum for whatever reason, feel free to shoot me an email. jessechounard@gmail.com

Share this post


Link to post
Share on other sites
I've made some substantial changes to the code, based on feedback I've received.

These were the requests that I decided to work on.
1) Don't use BOOST_FOREACH, so that people who don't get Boost from CVS can use the code.
2) Separate the class binding code from the TinyXml code, so that it can be used with other XML libraries, or for other intentions completely.
3) Fix the Getter/Setter functionality so that passing/returning by const reference works properly.
4) Include a software license.

All of those are done.

1) I had some trouble getting boost::bind and std::for_each to work properly with some of the templated functions, which is why I was using BOOST_FOREACH, but I've replaced that with a simple for-loop. Looking deeper into the bind issue is on my todo list, but there's not really a problem with the way it's done now.

2) Separating the Binding code from TinyXml specific code pretty much required a rewrite, but that's okay, as the code is much cleaner for it. Now there's a set of virtual functions in the base class, called DependantFunction# (where # is 1, 2, 3, and 4) that can be overloaded in a derived class to give whatever functionality is required.

3) Fixing this one was pretty simple. Instead of function pointers, I'm now passing and storing Boost::function objects. Now if you pass (or return) by value or reference, everything should compile and run properly. Passing/returning by pointers still isn't supported, though.

4) The software is now released under the MIT (X11) License. Basically, I want people to be free to do anything they want with it. If anybody has a problem with that license and has a recommendation, please let me know.

Here's the new header files and a source for a simple test:
Binding.hpp
TinyXmlBinding.hpp
test.cpp

There's still zero documentation. I'll get to that sooner or later. :)

Thanks to everybody who sent feedback. Please feel free to tell me what you think. (Comment here or email me at jessechounard@gmail.com)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this