Changeset 14745
- Timestamp:
- 06/28/08 08:10:39 (5 years ago)
- Location:
- lang/cplusplus/reflection/trunk
- Files:
-
- 3 modified
-
memrebuild.hpp (modified) (4 diffs)
-
reflection.hpp (modified) (3 diffs)
-
test.cpp (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
-
lang/cplusplus/reflection/trunk/memrebuild.hpp
r14744 r14745 7 7 namespace mem_rebuild_ns { 8 8 9 struct type_is_t { 10 virtual ~type_is_t() {} 11 virtual bool type_is(const std::type_info &) const = 0; 12 }; 13 9 14 template <typename T> 10 15 struct builder { … … 12 17 virtual ~builder() {} 13 18 virtual func_t get(const std::type_info &) const = 0; 19 virtual func_t get(const type_is_t *) const = 0; 14 20 virtual bool equals(const std::type_info &) const = 0; 15 21 virtual bool equals(const builder*) const = 0; … … 21 27 get(const std::type_info &t) const { 22 28 if (typeid(Source) != t) 29 return NULL; 30 return instantiate; 31 } 32 virtual typename builder<Target>::func_t get(const type_is_t *t) const { 33 if (! t->type_is(typeid(Source))) 23 34 return NULL; 24 35 return instantiate; … … 108 119 109 120 #undef MEM_REBUILD_ 121 122 template <typename T, typename TI> T core(const void *p, TI t) 123 { 124 typedef mem_rebuild_ns::builder_map<T> map; 125 for (typename map::const_iterator i = map::m.begin(); 126 i != map::m.end(); 127 ++i) { 128 typename mem_rebuild_ns::builder<T>::func_t instantiate; 129 if ((instantiate = (*i)->get(t)) != NULL) 130 return (*instantiate)(p); 131 } 132 throw std::bad_cast(); 133 } 134 110 135 }; 111 136 112 137 template <typename T> T mem_rebuild(const void *p, const std::type_info &t) 113 138 { 114 typedef mem_rebuild_ns::builder_map<T> map; 115 for (typename map::const_iterator i = map::m.begin(); 116 i != map::m.end(); 117 ++i) { 118 typename mem_rebuild_ns::builder<T>::func_t instantiate; 119 if ((instantiate = (*i)->get(t)) != NULL) 120 return (*instantiate)(p); 121 } 122 throw std::bad_cast(); 139 return mem_rebuild_ns::core<T, const std::type_info&>(p, t); 140 } 141 142 template <typename T> T mem_rebuild(const void *p, 143 const mem_rebuild_ns::type_is_t *t) 144 { 145 return mem_rebuild_ns::core<T, const mem_rebuild_ns::type_is_t*>(p, t); 123 146 } 124 147 -
lang/cplusplus/reflection/trunk/reflection.hpp
r14685 r14745 5 5 #include <string> 6 6 #include <typeinfo> 7 #include "memrebuild.hpp" 7 8 8 9 namespace reflection { 9 10 10 struct type_info { 11 virtual ~type_info() {} 12 virtual type_info* clone() const = 0; 13 virtual bool equal(const std::type_info &) const = 0; 14 }; 15 16 bool operator==(const type_info* x, const std::type_info &y) { 17 return x->equal(y); 18 } 19 20 bool operator==(const std::type_info &x, const type_info* y) { 21 return y == x; 22 } 23 24 bool operator!=(const type_info *x, const std::type_info &y) { 25 return ! (x == y); 26 } 27 28 bool operator!=(const std::type_info &x, const type_info* y) { 29 return ! (x == y); 30 } 31 32 template <class T> struct type_info_t : public type_info { 33 virtual type_info_t *clone() const { return new type_info_t<T>(); } 34 virtual bool equal(const std::type_info &x) const { return typeid(T) == x; } 11 template <class T> struct member : public mem_rebuild_ns::type_is_t { 12 virtual ~member() {} 13 virtual char T::*ptr() const = 0; 14 virtual void set(T *, const void *, const std::type_info &) const = 0; 15 template <typename Vt> Vt T::*ptr(Vt*) const { 16 return reinterpret_cast<Vt T::*>(ptr()); 17 } 18 template <typename Vt> Vt get(T *obj) const { 19 return mem_rebuild<Vt>(&(obj->*ptr()), this); 20 } 21 template <typename Vt> void set(T *obj, const Vt &v) const { 22 set(obj, &v, typeid(Vt)); 23 } 35 24 }; 36 25 37 template <class T> struct member { 38 char T::*_ptr; 39 type_info *type; 40 member(char T::*p, const type_info &t) : _ptr(p), type(t.clone()) {} 41 member(const member<T>& x) : _ptr(x._ptr), type(x.type->clone()) {} 42 ~member() { delete type; } 43 member& operator=(const member<T>& x) { 44 if (this == &x) return *this; 45 _ptr = x._ptr; 46 delete type; 47 type = x.type->clone(); 48 return *this; 26 template <class T, typename Vt> struct member_t : public member<T> { 27 Vt T::*p; 28 member_t(Vt T::*_p) : p(_p) {} 29 virtual bool type_is(const std::type_info &x) const { 30 return typeid(Vt) == x; 49 31 } 50 template <typename Vt> Vt T::*ptr(Vt*) const { 51 return reinterpret_cast<Vt T::*>(_ptr); 32 virtual char T::*ptr() const { 33 return reinterpret_cast<char T::*>(p); 34 } 35 virtual void set(T * obj, const void *s, const std::type_info &t) const { 36 obj->*p = mem_rebuild<Vt>(s, t); 52 37 } 53 38 }; 54 39 55 40 template <class T, class C = typename T::reflection_definition> struct base 56 : public std::map<std::string, member<T> > {41 : public std::map<std::string, member<T>*> { 57 42 typedef T Klass; 58 43 static C map; 59 44 template <typename Vt> 60 45 void register_(const std::string &name, Vt T::*p) { 61 insert(std::make_pair(name, 62 member<T>(reinterpret_cast<char T::*>(p), 63 type_info_t<Vt>()))); 46 insert(std::pair<std::string, member<T>*>(name, new member_t<T, Vt>(p))); 64 47 } 65 48 }; … … 72 55 = T::reflection_definition::map.find(n); 73 56 if (i == T::reflection_definition::map.end() 74 || typeid(*v) != i->second.type)57 || ! i->second->type_is(typeid(*v))) 75 58 return false; 76 *v = obj.*i->second .ptr((Vt*)NULL);59 *v = obj.*i->second->ptr((Vt*)NULL); 77 60 return true; 78 61 } … … 82 65 typename T::reflection_definition::const_iterator i = 83 66 T::reflection_definition::map.find(n); 84 if (i == T::reflection_definition::map.end() || typeid(v) != i->second.type) 67 if (i == T::reflection_definition::map.end() 68 || ! i->second->type_is(typeid(v))) 85 69 return false; 86 obj.*i->second .ptr((Vt*)NULL) = v;70 obj.*i->second->ptr((Vt*)NULL) = v; 87 71 return true; 88 72 } -
lang/cplusplus/reflection/trunk/test.cpp
r14571 r14745 3 3 4 4 using namespace std; 5 using namespace boost; 5 6 6 7 struct Foo { … … 23 24 f.i = 3; 24 25 int i = 0; 25 reflection::get(f, "i", &i); 26 ++i; 27 reflection::set(f, "i", i); 28 reflection::set(f, "s", string("hello world!")); 26 i = Foo::reflection_definition::map["i"]->get<int>(&f); 27 Foo::reflection_definition::map["i"]->set(&f, lexical_cast<string>(i + 1)); 28 Foo::reflection_definition::map["s"]->set(&f, string("hello world!")); 29 29 30 30 for (Foo::reflection_definition::const_iterator i 31 31 = Foo::reflection_definition::map.begin(); 32 32 i != Foo::reflection_definition::map.end(); 33 ++i) { 34 cout << i->first << " = "; 35 if (i->second.type == typeid(int)) { 36 cout << f.*i->second.ptr((int*)NULL); 37 } else if (i->second.type == typeid(std::string)) { 38 cout << f.*i->second.ptr((std::string*)NULL); 39 } 40 cout << endl; 41 } 33 ++i) 34 cout << i->first << " = " << i->second->get<std::string>(&f) << endl; 42 35 43 return f.i;36 return 0; 44 37 } 45 38
![(please configure the [header_logo] section in trac.ini)](/share/chrome/site/your_project_logo.png)