Boost logo

Boost-Commit :

From: oryol_at_[hidden]
Date: 2008-02-09 00:14:58


Author: jeremypack
Date: 2008-02-09 00:14:58 EST (Sat, 09 Feb 2008)
New Revision: 43184
URL: http://svn.boost.org/trac/boost/changeset/43184

Log:
Add static_reflection - a proposed new class.

Added:
   sandbox/boost/reflection/static_reflection.hpp (contents, props changed)

Added: sandbox/boost/reflection/static_reflection.hpp
==============================================================================
--- (empty file)
+++ sandbox/boost/reflection/static_reflection.hpp 2008-02-09 00:14:58 EST (Sat, 09 Feb 2008)
@@ -0,0 +1,115 @@
+/*
+ * Boost.Reflection / static_reflection:
+ * reflections of static functions
+ *
+ * (C) 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_REFLECTION_STATIC_REFLECTION_HPP
+#define BOOST_REFLECTION_STATIC_REFLECTION_HPP
+#include <map>
+#include <boost/reflection/factory.hpp>
+#include <boost/reflection/impl/typeinfo.hpp>
+
+namespace boost { namespace reflections {
+
+#define BOOST_REFLECTION_FACTORY_MAP_GET_FUNCTION(Z, N, _) \
+ template <class Info, class ReturnValue \
+ BOOST_PP_COMMA_IF(N) \
+ BOOST_PP_ENUM_PARAMS(N, class Param) > \
+ std::map<Info, \
+ ReturnValue (*)(BOOST_PP_ENUM_PARAMS(N, Param))> & get() { \
+ typedef ReturnValue (*FuncType)(BOOST_PP_ENUM_PARAMS(N, Param)); \
+ TypeInfo t = type_info_handler<TypeInfo, FuncType> \
+ ::get_class_type(); \
+ typename std::map<TypeInfo, generic_map_holder*>::iterator it = \
+ maps_.find(t); \
+ map_holder<std::map<Info, \
+ FuncType> > * holder; \
+ if (it == maps_.end()) { \
+ holder = new map_holder<std::map<Info, FuncType > >; \
+ it = maps_.insert(std::make_pair \
+ (t, holder)).first; \
+ } else { \
+ holder = \
+ static_cast<map_holder<std::map<Info, FuncType> > *> \
+ (it->second); \
+ } \
+ return *static_cast<std::map<Info, \
+ FuncType> * >(holder); \
+ }
+
+#define BOOST_REFLECTION_FACTORY_MAP_GET_FUNCTION_WITH_INFO(Z, N, _) \
+ template <class Info, class ReturnValue \
+ BOOST_PP_COMMA_IF(N) \
+ BOOST_PP_ENUM_PARAMS(N, class Param) > \
+ ReturnValue (*get(Info info))(BOOST_PP_ENUM_PARAMS(N, Param)) { \
+ typedef ReturnValue (*FuncType)(BOOST_PP_ENUM_PARAMS(N, Param)); \
+ std::map<Info, FuncType>& fm = get<Info, ReturnValue BOOST_PP_COMMA_IF(N) \
+ BOOST_PP_ENUM_PARAMS(N, Param) >(); \
+ typename std::map<Info, FuncType>::iterator it = fm.find(info); \
+ if (it == fm.end()) { \
+ return 0; \
+ } \
+ return it->second; \
+ }
+
+#define BOOST_REFLECTION_FACTORY_MAP_SET_FUNCTION(Z, N, _) \
+ template <class Info, class ReturnValue \
+ BOOST_PP_COMMA_IF(N) \
+ BOOST_PP_ENUM_PARAMS(N, class Param) > \
+ void set(Info info, ReturnValue (*func)(BOOST_PP_ENUM_PARAMS(N, Param))) { \
+ typedef ReturnValue (*FuncType)(BOOST_PP_ENUM_PARAMS(N, Param)); \
+ get<Info, ReturnValue BOOST_PP_COMMA_IF(N) \
+ BOOST_PP_ENUM_PARAMS(N, Param) >()[info] = func; \
+ }
+
+#define BOOST_REFLECTION_FACTORY_MAP_CONVERT_FUNCTION(Z, N, _) \
+ template <class Info, class ReturnValue \
+ BOOST_PP_COMMA_IF(N) \
+ BOOST_PP_ENUM_PARAMS(N, class Param) > \
+ operator std::map<Info, \
+ ReturnValue (*)(BOOST_PP_ENUM_PARAMS(N, Param))>() { \
+ return get<Info, ReturnValue BOOST_PP_COMMA_IF(N) \
+ BOOST_PP_ENUM_PARAMS(N, Param)>(); \
+ }
+
+template <class TypeInfo>
+class basic_static_reflection {
+public:
+ BOOST_PP_REPEAT(BOOST_PP_INC(BOOST_REFLECTION_MAX_FUNCTOR_PARAMS), \
+ BOOST_REFLECTION_FACTORY_MAP_GET_FUNCTION, _)
+ BOOST_PP_REPEAT(BOOST_PP_INC(BOOST_REFLECTION_MAX_FUNCTOR_PARAMS), \
+ BOOST_REFLECTION_FACTORY_MAP_GET_FUNCTION_WITH_INFO, _)
+ BOOST_PP_REPEAT(BOOST_PP_INC(BOOST_REFLECTION_MAX_FUNCTOR_PARAMS), \
+ BOOST_REFLECTION_FACTORY_MAP_SET_FUNCTION, _)
+ BOOST_PP_REPEAT(BOOST_PP_INC(BOOST_REFLECTION_MAX_FUNCTOR_PARAMS), \
+ BOOST_REFLECTION_FACTORY_MAP_CONVERT_FUNCTION, _)
+ ~basic_static_reflection() {
+ for (typename std::map<TypeInfo, generic_map_holder*>
+ ::iterator it =maps_.begin();
+ it != maps_.end(); ++it) {
+ delete it->second;
+ }
+ }
+private:
+ class generic_map_holder {
+ public:
+ virtual ~generic_map_holder() {}
+ };
+ template <class T>
+ class map_holder : public generic_map_holder, public T{
+ };
+ std::map<TypeInfo, generic_map_holder*> maps_;
+};
+typedef basic_static_reflection<default_type_info> static_reflection;
+}}
+
+#undef BOOST_REFLECTION_STATIC_REFLECTION_GET_FUNCTION
+#undef BOOST_REFLECTION_STATIC_REFLECTION_CONVERT_FUNCTION
+#endif


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