Boost logo

Boost-Commit :

From: oryol_at_[hidden]
Date: 2007-08-30 22:35:07


Author: sixtrillion
Date: 2007-08-30 22:35:01 EDT (Thu, 30 Aug 2007)
New Revision: 39081
URL: http://svn.boost.org/trac/boost/changeset/39081

Log:
Boost.Reflection initial restructuring for generic usage.

Note that the Boost.Preprocess macros have not yet been added
which will allow multiple arguments for constructors/functions.

Added:
   sandbox/boost/reflection/constructor.hpp (contents, props changed)
   sandbox/boost/reflection/factory.hpp (contents, props changed)
   sandbox/boost/reflection/function.hpp (contents, props changed)
   sandbox/boost/reflection/generic_constructor.hpp (contents, props changed)
   sandbox/boost/reflection/instance.hpp (contents, props changed)
   sandbox/boost/reflection/reflector.hpp (contents, props changed)
Text files modified:
   sandbox/boost/reflection/reflection.hpp | 261 +++++++++++++++++++++++++++++++++------
   1 files changed, 221 insertions(+), 40 deletions(-)

Added: sandbox/boost/reflection/constructor.hpp
==============================================================================
--- (empty file)
+++ sandbox/boost/reflection/constructor.hpp 2007-08-30 22:35:01 EDT (Thu, 30 Aug 2007)
@@ -0,0 +1,31 @@
+/*
+ * Copyright Jeremy Pack 2007
+ * Distributed under the Boost Software License, Version 1.0. (See
+ * accompanying file LICENSE_1_0.txt or copy at
+ * http://www.boost.org/LICENSE_1_0.txt)
+ *
+ * See http://www.boost.org/ for latest version.
+ */
+
+#ifndef BOOST_EXTENSION_CONSTRUCTOR_HPP
+#define BOOST_EXTENSION_CONSTRUCTOR_HPP
+#include <boost/reflection/instance.hpp>
+namespace boost {namespace reflections {
+class constructor {
+public:
+ constructor(instance (*func)() = 0)
+ : func_(func)
+ {
+ }
+ instance call() {
+ return (*func_)();
+ }
+ instance operator()() {
+ return call();
+ }
+ bool valid() {return func_ != 0;}
+private:
+ instance (*func_)();
+};
+}}
+#endif
\ No newline at end of file

Added: sandbox/boost/reflection/factory.hpp
==============================================================================
--- (empty file)
+++ sandbox/boost/reflection/factory.hpp 2007-08-30 22:35:01 EDT (Thu, 30 Aug 2007)
@@ -0,0 +1,24 @@
+/*
+ * Copyright Jeremy Pack 2007
+ * Distributed under the Boost Software License, Version 1.0. (See
+ * accompanying file LICENSE_1_0.txt or copy at
+ * http://www.boost.org/LICENSE_1_0.txt)
+ *
+ * See http://www.boost.org/ for latest version.
+ */
+
+#ifndef BOOST_EXTENSION_FACTORY_HPP
+#define BOOST_EXTENSION_FACTORY_HPP
+#include <boost/reflection/generic_constructor.hpp>
+namespace boost {namespace reflections {
+template <class T>
+class factory : public generic_constructor<T> {
+public:
+ factory(T(*factory_func)())
+ : factory_func_(factory_func) {}
+ virtual T * create(void ** params) {return (*factory_func_)();}
+private:
+ T (*factory_func_)();
+};
+}}
+#endif
\ No newline at end of file

Added: sandbox/boost/reflection/function.hpp
==============================================================================
--- (empty file)
+++ sandbox/boost/reflection/function.hpp 2007-08-30 22:35:01 EDT (Thu, 30 Aug 2007)
@@ -0,0 +1,68 @@
+/*
+ * Copyright Jeremy Pack 2007
+ * Distributed under the Boost Software License, Version 1.0. (See
+ * accompanying file LICENSE_1_0.txt or copy at
+ * http://www.boost.org/LICENSE_1_0.txt)
+ *
+ * See http://www.boost.org/ for latest version.
+ */
+
+#ifndef BOOST_EXTENSION_FUNCTION_HPP
+#define BOOST_EXTENSION_FUNCTION_HPP
+
+#include <boost/reflection/instance.hpp>
+
+namespace boost {namespace reflections {
+
+typedef void (instance::*MemberFunctionPtr)();
+template <class ReturnValue>
+class function {
+public:
+ function(ReturnValue (*func)(void *, MemberFunctionPtr) = 0,
+ MemberFunctionPtr member_function = 0)
+ : func_(func),
+ member_function_(member_function) {
+ }
+ ReturnValue call(instance & inst) {
+ return (*func_)(inst.val_, member_function_);
+ }
+ ReturnValue operator()(instance & inst) {
+ return (*func_)(inst.val_, member_function_);
+ }
+ bool valid() {
+ return member_function_ != 0 && func_ != 0;
+ }
+private:
+ ReturnValue (*func_)(void *, MemberFunctionPtr);
+ MemberFunctionPtr member_function_;
+};
+/*template <class T>
+class generic_function {
+ virtual ~generic_function() {}
+ virtual void call(void ** params) = 0;
+};
+template <class T, class ReturnValue = void>
+class function : public generic_function<T> {
+public:
+ function(ReturnValue (T::*func)())
+ : func_(func) {}
+ virtual void call(T * obj, void ** params) {
+ *static_cast<ReturnValue*>(params[0]) =
+ (obj->*func_)();
+ }
+private:
+ ReturnValue (T::*func_)();
+};
+template <class T>
+class function<T, void> : public generic_function<T> {
+public:
+ function(void (T::*func)())
+ : func_(func) {}
+ virtual void call(T * obj, void ** params) {
+ (obj->*func_)();
+ }
+private:
+ void (T::*func_)();
+}; */
+}}
+#endif
\ No newline at end of file

Added: sandbox/boost/reflection/generic_constructor.hpp
==============================================================================
--- (empty file)
+++ sandbox/boost/reflection/generic_constructor.hpp 2007-08-30 22:35:01 EDT (Thu, 30 Aug 2007)
@@ -0,0 +1,20 @@
+/*
+ * Copyright Jeremy Pack 2007
+ * Distributed under the Boost Software License, Version 1.0. (See
+ * accompanying file LICENSE_1_0.txt or copy at
+ * http://www.boost.org/LICENSE_1_0.txt)
+ *
+ * See http://www.boost.org/ for latest version.
+ */
+
+#ifndef BOOST_EXTENSION_GENERIC_CONSTRUCTOR_HPP
+#define BOOST_EXTENSION_GENERIC_CONSTRUCTOR_HPP
+namespace boost {namespace reflections {
+template <class T>
+class generic_constructor {
+public:
+ virtual ~generic_constructor() {}
+ virtual T * create(void ** params) = 0;
+};
+}}
+#endif
\ No newline at end of file

Added: sandbox/boost/reflection/instance.hpp
==============================================================================
--- (empty file)
+++ sandbox/boost/reflection/instance.hpp 2007-08-30 22:35:01 EDT (Thu, 30 Aug 2007)
@@ -0,0 +1,47 @@
+/*
+ * Copyright Jeremy Pack 2007
+ * Distributed under the Boost Software License, Version 1.0. (See
+ * accompanying file LICENSE_1_0.txt or copy at
+ * http://www.boost.org/LICENSE_1_0.txt)
+ *
+ * See http://www.boost.org/ for latest version.
+ */
+
+#ifndef BOOST_EXTENSION_INSTANCE_HPP
+#define BOOST_EXTENSION_INSTANCE_HPP
+namespace boost {namespace reflections {
+class instance {
+public:
+ instance(void * val = 0, void (*destructor)(void * val) = 0)
+ : val_(val),
+ destructor_(destructor) {}
+ ~instance() {
+ if (val_)
+ (*destructor_)(val_);
+ }
+ instance(const instance & first)
+ : val_(first.val_),
+ destructor_(first.destructor_)
+ {
+ if (this != &first) {
+ // Check for self assignment
+ first.val_ = 0;
+ }
+ }
+ instance & operator=(instance & first) {
+ if (this != &first) {
+ // Check for self assignment
+ this->val_ = first.val_;
+ this->destructor_ = first.destructor_;
+ first.val_ = 0;
+ }
+ return *this;
+ }
+private:
+ template <class ReturnValue>
+ friend class function;
+ mutable void * val_;
+ void (*destructor_)(void *);
+};
+}}
+#endif
\ No newline at end of file

Modified: sandbox/boost/reflection/reflection.hpp
==============================================================================
--- sandbox/boost/reflection/reflection.hpp (original)
+++ sandbox/boost/reflection/reflection.hpp 2007-08-30 22:35:01 EDT (Thu, 30 Aug 2007)
@@ -1,7 +1,7 @@
 /*
  * Boost.Reflection / main header
  *
- * (C) Copyright Mariano G. Consoni 2007
+ * (C) Copyright Mariano G. Consoni and Jeremy Pack 2007
  * Distributed under the Boost Software License, Version 1.0. (See
  * accompanying file LICENSE_1_0.txt or copy at
  * http://www.boost.org/LICENSE_1_0.txt)
@@ -9,57 +9,238 @@
  * See http://www.boost.org/ for latest version.
  */
 
-// TODO:
-// . implement calls with parameter maps
-// . fix FIXMEs
-
 #ifndef BOOST_EXTENSION_REFLECTION_HPP
 #define BOOST_EXTENSION_REFLECTION_HPP
+#include <map>
+#include <boost/extension/impl/typeinfo.hpp>
+#include <boost/reflection/constructor.hpp>
+#include <boost/reflection/factory.hpp>
+#include <boost/reflection/function.hpp>
+
+
+namespace boost {namespace reflections {
+
+typedef void (*FunctionPtr)();
+template<class Info, class TypeInfo>
+struct basic_function_info {
+ TypeInfo type_info_;
+ Info info_;
+ basic_function_info(TypeInfo t, Info i) : type_info_(t), info_(i)
+ {
+ }
+ basic_function_info(const basic_function_info & s)
+ : type_info_(s.type_info_), info_(s.info_) {}
+ basic_function_info & operator=(basic_function_info & s) {
+ type_info_ = s.type_info_;
+ info_ = s.info_;
+ }
+ friend inline bool operator<(const basic_function_info & t,
+ const basic_function_info & s) {
+ return t.type_info_ < s.type_info_ ||
+ (t.type_info_ == s.type_info_ &&
+ t.info_ < s.info_);
+ }
+};
+template <class Info = std::string,
+ class TypeInfo = extensions::default_type_info>
+class basic_reflection
+{
+public:
+ template <class Q, class R, class S>
+ friend class reflector;
+ constructor get_constructor() {
+ TypeInfo t = extensions::type_info_handler<TypeInfo,
+ instance (*)()>::get_class_type();
+ typename std::map<TypeInfo, FunctionPtr>::iterator it =
+ constructors_.find(t);
+ if (it == constructors_.end()) {
+ return constructor();
+ } else {
+ return reinterpret_cast<instance (*)()>(it->second);
+ }
+ }
+ template <class ReturnValue>
+ function<ReturnValue> get_function(Info info) {
+ function_info f(extensions::type_info_handler<TypeInfo,
+ ReturnValue (*)()>::get_class_type(), info);
+ std::cout << "\nGetting: " << f.type_info_;
+ typename std::map<function_info,
+ std::pair<MemberFunctionPtr,
+ FunctionPtr> >::iterator it =
+ functions_.find(f);
+ if (it == functions_.end()) {
+ return function<ReturnValue>();
+ } else {
+ return function<ReturnValue>
+ (reinterpret_cast<ReturnValue (*)(void *, MemberFunctionPtr)>
+ (it->second.second), it->second.first);
+ }
+ }
+private:
+ typedef basic_function_info<Info, TypeInfo> function_info;
+ std::map<TypeInfo, FunctionPtr> constructors_;
+ std::map<function_info,
+ std::pair<MemberFunctionPtr, FunctionPtr> > functions_;
+};
+typedef basic_reflection<> reflection;
+}}
 
-#include <boost/function.hpp>
-
-#include <list>
-
-
-#ifdef BOOST_EXTENSION_USE_PP
-
-#include <boost/preprocessor/arithmetic/inc.hpp>
-#include <boost/preprocessor/if.hpp>
-#include <boost/preprocessor/punctuation/comma_if.hpp>
-#include <boost/preprocessor/repetition.hpp>
-
-#ifndef BOOST_EXTENSION_MAX_FUNCTOR_PARAMS
-#define BOOST_EXTENSION_MAX_FUNCTOR_PARAMS 2
-#endif
-
-#endif
-
-#include "method_info.hpp"
-#include "parameter_map.hpp"
-
-
-
-namespace boost {
- namespace extension {
-
- template<class Implementation, class Info>
+#if 0
+ template<class Info>
     class reflection
     {
     public:
       reflection(Info info) : info_(info) {}
-
       class generic_method_container
       {
       public:
+ template <class ReturnValue, class Param1, class Param2, class Param3,
+ class Param4, class Param5, class Param6>
+ ReturnValue call(Param1 p1, Param2 p2, Param3 p3, Param4 p4,
+ Param5 p5, Param6 p6) {
+ ReturnValue val;
+ call_virtual(static_cast<void*>(&p1),
+ static_cast<void*>(&p2),
+ static_cast<void*>(&p3),
+ static_cast<void*>(&p4),
+ static_cast<void*>(&p5),
+ static_cast<void*>(&p6));
+ get_return_value(static_cast<void*>(&val));
+ return val;
+ }
+ template <class Param1, class Param2, class Param3,
+ class Param4, class Param5, class Param6>
+ void call_void(Param1 p1, Param2 p2, Param3 p3, Param4 p4,
+ Param5 p5, Param6 p6) {
+ call_virtual(static_cast<void*>(&p1),
+ static_cast<void*>(&p2),
+ static_cast<void*>(&p3),
+ static_cast<void*>(&p4),
+ static_cast<void*>(&p5),
+ static_cast<void*>(&p6));
+ }
+ template <class ReturnValue, class Param1, class Param2, class Param3,
+ class Param4, class Param5>
+ ReturnValue call(Param1 p1, Param2 p2, Param3 p3, Param4 p4,
+ Param5 p5) {
+ ReturnValue val;
+ call_virtual(static_cast<void*>(&p1),
+ static_cast<void*>(&p2),
+ static_cast<void*>(&p3),
+ static_cast<void*>(&p4),
+ static_cast<void*>(&p5));
+ get_return_value(static_cast<void*>(&val));
+ return val;
+ }
+ template <class Param1, class Param2, class Param3,
+ class Param4, class Param5>
+ void call_void(Param1 p1, Param2 p2, Param3 p3, Param4 p4,
+ Param5 p5) {
+ call_virtual(static_cast<void*>(&p1),
+ static_cast<void*>(&p2),
+ static_cast<void*>(&p3),
+ static_cast<void*>(&p4),
+ static_cast<void*>(&p5));
+ }
+ template <class ReturnValue, class Param1, class Param2, class Param3,
+ class Param4>
+ ReturnValue call(Param1 p1, Param2 p2, Param3 p3, Param4 p4) {
+ ReturnValue val;
+ call_virtual(static_cast<void*>(&p1),
+ static_cast<void*>(&p2),
+ static_cast<void*>(&p3),
+ static_cast<void*>(&p4));
+ get_return_value(static_cast<void*>(&val));
+ return val;
+ }
+ template <class Param1, class Param2, class Param3,
+ class Param4>
+ void call_void(Param1 p1, Param2 p2, Param3 p3, Param4 p4) {
+ call_virtual(static_cast<void*>(&p1),
+ static_cast<void*>(&p2),
+ static_cast<void*>(&p3),
+ static_cast<void*>(&p4));
+ }
+ template <class ReturnValue, class Param1, class Param2, class Param3>
+ ReturnValue call(Param1 p1, Param2 p2, Param3 p3) {
+ ReturnValue val;
+ call_virtual(static_cast<void*>(&p1),
+ static_cast<void*>(&p2),
+ static_cast<void*>(&p3));
+ get_return_value(static_cast<void*>(&val));
+ return val;
+ }
+ template <class Param1, class Param2, class Param3>
+ void call_void(Param1 p1, Param2 p2, Param3 p3) {
+ call_virtual(static_cast<void*>(&p1),
+ static_cast<void*>(&p2),
+ static_cast<void*>(&p3));
+ }
+ template <class ReturnValue, class Param1, class Param2>
+ ReturnValue call(Param1 p1, Param2 p2) {
+ ReturnValue val;
+ call_virtual(static_cast<void*>(&p1),
+ static_cast<void*>(&p2));
+ get_return_value(static_cast<void*>(&val));
+ return val;
+ }
+ template <class Param1, class Param2>
+ void call_void(Param1 p1, Param2 p2) {
+ call_virtual(static_cast<void*>(&p1),
+ static_cast<void*>(&p2));
+ }
+ template <class ReturnValue, class Param1>
+ ReturnValue call(Param1 p1) {
+ ReturnValue val;
+ call_virtual(static_cast<void*>(&p1));
+ get_return_value(static_cast<void*>(&val));
+ return val;
+ }
+ template <class Param1>
+ void call_void(Param1 p1) {
+ call_virtual(static_cast<void*>(&p1));
+ }
+ template <class ReturnValue>
+ ReturnValue call() {
+ ReturnValue val;
+ call_virtual();
+ get_return_value(static_cast<void*>(&val));
+ return val;
+ }
+ void call_void() {
+ call_virtual();
+ }
+
         virtual ~generic_method_container(){}
+ protected:
+ virtual void get_return_value(void * val) = 0;
+ virtual void call_virtual(void * p1 = 0, void * p2 = 0, void * p3 = 0,
+ void * p4 = 0, void * p5 = 0, void * p6 = 0)
+ = 0;
       };
-
-
- template <class MethodID, class MethodReturnValue,
+ template <class Object, class ReturnValue, class Param1 = void,
+ class Param2 = void, class Param3 = void,
+ class Param4 = void, class Param5 = void,
+ class Param6 = void>
+ class method_container : public virtual_method_container {
+ public:
+ virtual void get_return_value(void * val) {
+ *static_cast<ReturnValue*>(val) = return_value_;
+ }
+ virtual void call_virtual(void * p1 = 0, void * p2 = 0,
+ void * p3 = 0, void * p4 = 0,
+ void * p5 = 0, void * p6 = 0) {
+
+ }
+ private:
+ ReturnValue
+ ReturnValue return_value_;
+ };
+ /*template <class MethodID, class MethodReturnValue,
                 class MethodParam0 = void, class MethodParamID0 = void,
- class MethodParam1 = void, class MethodParamID1 = void>
+ class MethodParam1 = void, class MethodParamID1 = void>*/
 
- class method_container
+ /*class method_container
         : public std::list<method_info<Implementation, MethodID,
                                        MethodReturnValue,
                                        MethodParam0, MethodParamID0,
@@ -69,7 +250,7 @@
           public:
             method_container() {}
             virtual ~method_container(){}
- };
+ };*/
 
 
       typedef std::list<generic_method_container *> MethodList;
@@ -444,5 +625,5 @@
 
   } // extension
 } // boost
-
+#endif
 #endif // BOOST_EXTENSION_REFLECTION_HPP

Added: sandbox/boost/reflection/reflector.hpp
==============================================================================
--- (empty file)
+++ sandbox/boost/reflection/reflector.hpp 2007-08-30 22:35:01 EDT (Thu, 30 Aug 2007)
@@ -0,0 +1,144 @@
+/*
+ * Copyright Jeremy Pack 2007
+ * Distributed under the Boost Software License, Version 1.0. (See
+ * accompanying file LICENSE_1_0.txt or copy at
+ * http://www.boost.org/LICENSE_1_0.txt)
+ *
+ * See http://www.boost.org/ for latest version.
+ */
+
+#ifndef BOOST_EXTENSION_REFLECTOR_HPP
+#define BOOST_EXTENSION_REFLECTOR_HPP
+
+#include <boost/reflection/factory.hpp>
+#include <boost/reflection/function.hpp>
+#include <boost/reflection/constructor.hpp>
+#include <boost/reflection/instance.hpp>
+#include <boost/reflection/reflection.hpp>
+namespace boost {namespace reflections {
+
+
+
+/* template <class Info = std::string,
+ class TypeInfo = extensions::default_type_info>
+ class generic_reflector
+{
+public:
+ typedef basic_function_info<Info, TypeInfo> function_info;
+ virtual ~generic_reflector() {}
+
+protected:
+
+};*/
+template <class T, class Info = std::string,
+ class TypeInfo = extensions::default_type_info>
+class reflector
+{
+public:
+ // Initialize with a pointer to the reflection
+ // this reflector will be reflecting into
+ reflector(basic_reflection<Info, TypeInfo> * current_reflection)
+ : reflection_(current_reflection) {
+ }
+ typedef basic_function_info<Info, TypeInfo> function_info;
+ void reflect_constructor() {
+ add_constructor(&construct);
+ }
+ template <class ReturnValue>
+ void reflect(ReturnValue (T::*func)(), Info info) {
+ function_info f(extensions::type_info_handler<TypeInfo,
+ ReturnValue (*)()>::get_class_type(), info);
+ ReturnValue (*f2)(void *, MemberFunctionPtr) = &call_member<ReturnValue>;
+ std::pair<MemberFunctionPtr, FunctionPtr>
+ p(reinterpret_cast<MemberFunctionPtr>(func),
+ reinterpret_cast<FunctionPtr>(f2));
+ std::pair<function_info, std::pair<MemberFunctionPtr, FunctionPtr> >
+ p2(f, p);
+ reflection_->functions_.insert(p2);
+ }
+private:
+ void add_constructor(instance (*func)()) {
+ reflection_->constructors_.insert(std::make_pair<TypeInfo, FunctionPtr>(
+ extensions::type_info_handler<TypeInfo, instance (*)()>::get_class_type(),
+ reinterpret_cast<FunctionPtr>(func)));
+ }
+ static instance construct() {
+ return instance(static_cast<void*>(new T()), &destruct);
+ }
+ template <class ReturnValue>
+ static ReturnValue call_member(void * val,
+ MemberFunctionPtr member_function) {
+ T * actual = static_cast<T*>(val);
+ ReturnValue (T::*func)() =
+ reinterpret_cast<ReturnValue (T::*)()>(member_function);
+ return (actual->*func)();
+ }
+ static void destruct(void * val) {
+ delete static_cast<T*>(val);
+ }
+ basic_reflection<Info, TypeInfo> * reflection_;
+};
+ /*
+template <class TypeInfo, class Info>
+class function_info
+{
+public:
+ TypeInfo type_info_;
+ Info info_;
+ bool operator<(function_info & second) {
+ return type_info_ < second.type_info_ ||
+ (type_info_ == second.type_info_ &&
+ info_ < second.type_info_);
+ }
+};
+template <class Info = std::string,
+ class TypeInfo = extensions::default_type_info>
+class generic_reflector
+{
+public:
+
+};
+template <class T, class Info = std::string,
+ class TypeInfo = extensions::default_type_info>
+class reflector : public generic_reflector<Info, TypeInfo>
+{
+public:
+ void reflect_constructor() {
+ constructors_.insert(std::pair<TypeInfo, generic_constructor*>
+ (type_info_handler::get_class_type<TypeInfo, constructor>,
+ new constructor()));
+ }
+ template <class ReturnValue>
+ void reflect(ReturnValue (T::*func)(), Info info) {
+ functions_.insert(std::pair<std::pair<TypeInfo, Info>, generic_function*>
+ (std::pair<TypeInfo, Info>
+ (type_info_handler::get_class_type<function<ReturnValue> >, info),
+ (new function<ReturnValue>(func))));
+ }
+ ~reflector() {
+ for (ConstructorIterator it = contructors_.begin();
+ it != constructors_.end(); ++it) {
+ delete it->second;
+ }
+ for (FactoryIterator it = factories_.begin();
+ it != factories_.end(); ++it) {
+ delete it->second;
+ }
+ for (FunctionIterator it = functions_.begin();
+ it != functions_.end(); ++it) {
+ delete it->second;
+ }
+ }
+private:
+ typedef std::map<TypeInfo, generic_constructor<T>*>::iterator
+ ConstructorIterator;
+ typedef std::map<function_info<TypeInfo, Info>,
+ generic_constructor<T>*>::iterator FactoryIterator;
+ typedef std::map<function_info<TypeInfo, Info>,
+ generic_function<T>*>::iterator FunctionIterator;
+ std::map<TypeInfo, generic_constructor<T>*> constructors_;
+ std::map<function_info<TypeInfo, Info>, generic_constructor<T>*> factories_;
+ std::map<function_info<TypeInfo, Info>, generic_function<T>*> functions_;
+};*/
+}}
+#endif
\ No newline at end of file


Boost-Commit list run by bdawes at acm.org, david.abrahams at rcn.com, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk