00001
00002
00003
00004
00005
00006
00007
00008 #include "foo.hpp"
00009 #include "boost/lexical_cast.hpp"
00010
00011 using namespace persist_test;
00012
00013 ph::common::object_base *foo::get_composite_object(const std::string &name)
00014 {
00015 if (name == "templates")
00016 return &templates;
00017 else if (name == "bars")
00018 return &bars;
00019 else
00020 return 0;
00021 }
00022
00023 bool foo::has(const std::string &name) const
00024 {
00025 return name == "name" || name == "type" ||
00026 name == "x" || name == "y" || name == "z" ||
00027 name == "bars" || name == "templates";
00028 }
00029
00030 void foo::set(const std::string &name, const std::string &value)
00031 {
00032 if (name == "name")
00033 _name = value;
00034 else if (name == "type")
00035 _type = value;
00036 else if (name == "x")
00037 x = boost::lexical_cast<int>(value);
00038 else if (name == "y")
00039 y = boost::lexical_cast<double>(value);
00040 else if (name == "z")
00041 z = (value != "false");
00042 }
00043
00044 std::string foo::get(const std::string &name) const
00045 {
00046 if (name == "name")
00047 return _name;
00048 else if (name == "type")
00049 return _type;
00050 else if (name == "x")
00051 return boost::lexical_cast<std::string>(x);
00052 else if (name == "y")
00053 return boost::lexical_cast<std::string>(y);
00054 else if (name == "z")
00055 return z ? "true" : "false";
00056 else
00057 return "unknown: " + name;
00058 }
00059
00060 bool foo::write(ph::common::object_writer *writer) const
00061 {
00062 ph::common::object_writer_context obj(writer, _type);
00063 obj.attr("name", _name);
00064 {
00065 ph::common::object_writer_context _x(writer, "x");
00066 _x.data(boost::lexical_cast<std::string>(x));
00067 }
00068 {
00069 ph::common::object_writer_context _x(writer, "y");
00070 _x.data(boost::lexical_cast<std::string>(y));
00071 }
00072 {
00073 ph::common::object_writer_context _x(writer, "z");
00074 _x.data(z ? "true" : "false");
00075 }
00076 if (bars.vector()->size() > 0)
00077 {
00078 ph::common::object_writer_context c2(writer, "bars");
00079 for (std::vector<ph::common::object_base *>::const_iterator i = bars.vector()->begin();
00080 i != bars.vector()->end(); i++)
00081 if ((*i)->persistable())
00082 (*i)->persistable()->write(writer);
00083 }
00084 if (templates.vector()->size() > 0)
00085 {
00086 ph::common::object_writer_context c2(writer, "templates");
00087 for (std::vector<ph::common::object_base *>::const_iterator i = templates.vector()->begin();
00088 i != templates.vector()->end(); i++)
00089 if ((*i)->persistable())
00090 (*i)->persistable()->write(writer);
00091 }
00092 return true;
00093 }