C++ XML Objects Use: Changing the objects

Previous Next Usage Home Installation Docs


Now we have the objects, we just manipulate them like any others.


Move along, there's really nothing to see here except for the manipulation of the vectors and references, which have been defined differently.

When looping through a vector, you need to replace:

for (std::vector<foo *>::iterator i=foos.begin(); i != foos.end(); i++)

With this:

for (ph::xmlobj::xmlobj_typed_vector<foo>::iterator i=foos.begin(); i != foos.end(); i++)

You can even use an algorithm from STL because the iterators are actually real bidrectional ones.

ph::xmlobj::xmlobj_typed_vector<foo>::iterator i=std::find_if(foos.begin(), foos.end(), 
	boost::bind(&foo::name, _1), "foo_2"));

Which searches for an object with a name of "foo_2".

One thing that's possible now, but not before is that you can define a visitor which will visit every object in your hierarchy like this:

class my_visitor : public ph::common::object_visitor
	// object_visitor overides.
	virtual bool recurse() { return true; }
	virtual bool visit(const ph::common::object_base *obj);
	virtual bool visit_composite(const ph::common::object_base *obj)
		{ return true; }

bool my_visitor::visit(const ph::common::object_base *obj)
	... do anything with the object in here. Dynamic cast it to get the
		data or something. And when your finished (even if you 
		are in the middle of the tree) return false and it
		will all terminate ...

There are a whole lot of interfaces that you can query off "ph::common::object_base" to manipulate it in some way OTHER than dynamic casting it. Like to get the value of a member with a name, I could query for the object's "persistable_object_base" object, and then use it to get a named member:

if (obj->persistable())
	std::string value = obj->persistable()->get("x");

Or to see if the parent of my object is a composite, and then add a new object to it:

if (obj->outerable())
	ph::common::object_base *outer = obj->outerable()->outer();
	if (outer->composition())
		outer->composition()->add(new MyObj(), true);

But we digress...

Previous Next Usage Home Installation Docs

Generated: Wed Apr 5 23:00:19 EST 2006

Copyright (c) 2005; Paul Hamilton; pHamtec P/L.

Use, modification, and distribution is provided free of any limitations.