00001 // 00002 // Copyright (c) 2003 00003 // Paul Hamilton; pHamtec P/L 00004 // 00005 // Permission to use, copy, modify, distribute and sell this software 00006 // and its documentation for any purpose is hereby granted without fee, 00007 // provided that the above copyright notice appears in all copies and 00008 // that both the copyright notice and this permission notice appear in 00009 // supporting documentation. No representations are made about the 00010 // suitability of this software for any purpose. It is provided "as is" 00011 // without express or implied warranty. 00012 // 00013 // root.cpp 00014 // 00015 // 4-Jul-2003 phamilton Created 00016 // 00017 00018 #include "root.hpp" 00019 #include <boost/test/test_tools.hpp> 00020 #include "../member.hpp" 00021 #include "../member_visitor.hpp" 00022 #include "../object_visitor.hpp" 00023 #include "foo.hpp" 00024 #include "bar.hpp" 00025 00026 using namespace ph::reflect; 00027 using namespace reflect_test; 00028 00029 bool root::accept(ph::common::member_visitor *v) 00030 { 00031 if (!base::accept(v)) 00032 return false; 00033 pod_member<int> _x(&x); 00034 if (!v->visit(this, "x", &_x)) 00035 return false; 00036 return true; 00037 } 00038 00039 bool root::accept(ph::common::const_member_visitor *v) const 00040 { 00041 if (!base::accept(v)) 00042 return false; 00043 const_pod_member<int> _x(&x); 00044 if (!v->visit(this, "x", &_x)) 00045 return false; 00046 return true; 00047 } 00048 00049 bool root::accept(ph::common::object_visitor *v) 00050 { 00051 if (!base::accept(v)) 00052 return false; 00053 if (!v->visit(this)) 00054 return false; 00055 if (!foos.accept(v)) 00056 return false; 00057 if (!bars.accept(v)) 00058 return false; 00059 if (!a_foo.accept(v)) 00060 return false; 00061 return true; 00062 } 00063 00064 bool root::accept(ph::common::const_object_visitor *v) const 00065 { 00066 if (!base::accept(v)) 00067 return false; 00068 if (!v->visit(this)) 00069 return false; 00070 if (!foos.accept(v)) 00071 return false; 00072 if (!bars.accept(v)) 00073 return false; 00074 if (!a_foo.accept(v)) 00075 return false; 00076 return true; 00077 } 00078 00079 static void set(base *obj, const std::string &name, const std::string &val) 00080 { 00081 set_member_visitor v(name, val); 00082 obj->accept(&v); 00083 } 00084 00085 void root::addfoosandbars() 00086 { 00087 { 00088 foo *o = new foo("foo", "foo_1"); 00089 BOOST_REQUIRE_MESSAGE(o, "could not create foo object."); 00090 set(o, "x", "1"); 00091 set(o, "y", "2.1"); 00092 set(o, "z", "true"); 00093 o->addbars(); 00094 BOOST_REQUIRE(foos.add(o, true)); 00095 00096 o = new foo("foo", "foo_2"); 00097 BOOST_REQUIRE_MESSAGE(o, "could not create foo object."); 00098 set(o, "x", "4"); 00099 set(o, "y", "10"); 00100 BOOST_REQUIRE(foos.add(o, true)); 00101 } 00102 00103 { 00104 bar *o = new bar("bar", "bar_1"); 00105 BOOST_REQUIRE_MESSAGE(o, "could not create bar object."); 00106 set(o, "x", "3"); 00107 set(o, "y", "7.6"); 00108 set(o, "z", "5"); 00109 BOOST_REQUIRE(bars.add(o, true)); 00110 00111 o = new bar("bar", "bar_2"); 00112 BOOST_REQUIRE_MESSAGE(o, "could not create bar object."); 00113 set(o, "x", "22"); 00114 set(o, "y", "999.999"); 00115 set(o, "z", "53"); 00116 BOOST_REQUIRE(bars.add(o, true)); 00117 } 00118 00119 { 00120 foo *o = new foo("foo", "foo_3"); 00121 BOOST_REQUIRE_MESSAGE(o, "could not create foo object."); 00122 set(o, "x", "5"); 00123 set(o, "y", "7"); 00124 BOOST_REQUIRE(a_foo.add(o, true)); 00125 } 00126 }