$include_dir="/home/hyper-archives/boost-commit/include"; include("$include_dir/msg-header.inc") ?>
Subject: [Boost-commit] svn:boost r51357 - in sandbox/mirror: boost/mirror boost/mirror/detail boost/mirror/meta_classes/boost/tuples libs/mirror/example/special libs/mirror/test
From: chochlik_at_[hidden]
Date: 2009-02-20 15:43:59
Author: matus.chochlik
Date: 2009-02-20 15:43:58 EST (Fri, 20 Feb 2009)
New Revision: 51357
URL: http://svn.boost.org/trac/boost/changeset/51357
Log:
[mirror 0.3.x]
- offset/offset_of update
Added:
   sandbox/mirror/libs/mirror/test/classes_ct_08.cpp   (contents, props changed)
   sandbox/mirror/libs/mirror/test/classes_rt_08.cpp   (contents, props changed)
Text files modified: 
   sandbox/mirror/boost/mirror/detail/meta_attribs_base.hpp        |   123 +++++++++++++++++++++++++++++++-------- 
   sandbox/mirror/boost/mirror/meta_attributes.hpp                 |    48 +++++++--------                         
   sandbox/mirror/boost/mirror/meta_classes/boost/tuples/tuple.hpp |     2                                         
   sandbox/mirror/libs/mirror/example/special/boost_tuple.cpp      |    16 ++--                                    
   sandbox/mirror/libs/mirror/test/Jamfile.v2                      |     2                                         
   5 files changed, 132 insertions(+), 59 deletions(-)
Modified: sandbox/mirror/boost/mirror/detail/meta_attribs_base.hpp
==============================================================================
--- sandbox/mirror/boost/mirror/detail/meta_attribs_base.hpp	(original)
+++ sandbox/mirror/boost/mirror/detail/meta_attribs_base.hpp	2009-02-20 15:43:58 EST (Fri, 20 Feb 2009)
@@ -312,9 +312,6 @@
                 Class& instance, \
                 position_of_##NAME position \
         ){return NULL;} \
-	static inline ptrdiff_t offset_of( \
-		position_of_##NAME position \
-	){return ptrdiff_t(-1);} \
         BOOST_MIRROR_REG_TEMPLATE_OR_CLASS_ATTRIB_EPILOGUE( \
                 TYPE_SELECTOR, \
                 NAME, \
@@ -384,13 +381,6 @@
                 Class& instance, \
                 position_of_##NAME position \
         ){return &instance.NAME;} \
-	static inline ptrdiff_t offset_of(\
-		position_of_##NAME position \
-	){ \
-		unsigned char arena[sizeof(Class)]; \
-		unsigned char* attr((unsigned char*)&(((Class*)arena)->NAME));\
-		return attr - arena; \
-	} \
         BOOST_MIRROR_REG_TEMPLATE_OR_CLASS_ATTRIB_EPILOGUE( \
                 TYPE_SELECTOR, \
                 NAME, \
@@ -550,34 +540,117 @@
                 BOOST_PP_EMPTY() \
         )
 
-/** Declaration of the meta_class_attributes<> template
- */
-template <class Class, class VariantTag = detail::default_meta_class_variant>
-struct meta_class_attributes
- : public meta_class_attributes_base<Class, VariantTag>
+namespace detail {
+
+template <class Class, class MetaAttribsBase>
+struct meta_class_attributes_offset_calculator : public MetaAttribsBase
 {
-private:
-	typedef meta_class_attributes_base<Class, VariantTag> base_class;
+protected:
+	typedef unsigned char byte;	
+	typedef const byte* byte_ptr;	
+
+	static inline ptrdiff_t invalid_offset(void)
+	{
+		return -1;
+	}
+
+	typedef MetaAttribsBase base_class;
         static inline ptrdiff_t calculate_offset(
-		const unsigned char* base_ptr, 
-		const unsigned char* attr_ptr
+		byte_ptr base_ptr, 
+		byte_ptr attr_ptr
         )
         {
                 // we are unable to calculate the offset
-		if(attr_ptr == 0) return -1;
+		if(attr_ptr == 0) return invalid_offset();
                 else return attr_ptr - base_ptr;
         }
+
+	template <int I>
+	static inline ptrdiff_t& get_offset_storage(mpl::int_<I> pos)
+	{
+		static ptrdiff_t offs = invalid_offset();
+		return offs;
+	}
+
+	template <int I>
+	static inline bool attrib_is_static(mpl::int_<I> position)
+	{
+		BOOST_TYPEOF_NESTED_TYPEDEF_TPL(
+			detail_traits,
+			base_class::get_traits(position)
+		)
+		typedef typename detail_traits::type traits;
+		return traits::is_static::value;
+	}
+
+	template <class T, int I>
+        static inline ptrdiff_t get_offset_of(
+		mpl::int_<I> pos,
+		T* __dummy,
+		T instance = T()
+	)
+        { 
+		return offset(instance, pos);
+	}
+
+	template <class T, int I>
+        static inline ptrdiff_t get_offset_of(mpl::int_<I> pos, ...)
+	{
+		return get_offset_storage(pos);
+	}
+
 public:
         /** Gets the byte-offset of the I-th member 
          */
-	template <class Owner, int I>
-	static inline ptrdiff_t	offset(Owner& instance, mpl::int_<I> pos)
+	template <int I>
+	static inline ptrdiff_t	offset(Class& instance, mpl::int_<I> pos)
         {
+		if(attrib_is_static(pos)) return invalid_offset();
+		ptrdiff_t& result = get_offset_storage(pos);
+		if(result == invalid_offset())
+		{
+			result = calculate_offset(
+				(byte_ptr)&instance,
+				(byte_ptr)base_class::address(instance, pos)
+			);
+		}
+		return result;
+	}
+};
+
+} // namespace detail
+
+/** Declaration of the meta_class_attributes<> template
+ */
+template <class Class, class VariantTag = detail::default_meta_class_variant>
+struct meta_class_attributes
+ : public detail::meta_class_attributes_offset_calculator<
+	Class,
+	meta_class_attributes_base<Class, VariantTag>
+>
+{
+private:
+	typedef detail::meta_class_attributes_offset_calculator<
+	        Class,
+	        meta_class_attributes_base<Class, VariantTag>
+	> offs_calc;
+	typedef typename offs_calc::byte byte;
+	typedef typename offs_calc::byte_ptr byte_ptr;
+public:
+	template <int I>
+        static inline ptrdiff_t offset_of(mpl::int_<I> pos)
+        { 
+		if(attrib_is_static(pos)) 
+			return offs_calc::invalid_offset();
+		//byte arena[sizeof(Class)*2];
+		//::std::allocator<Class> alloc;
+		//Class* ptr((Class*)alloc.allocate(1, arena));
+		Class* ptr(0);
                 return calculate_offset(
-			(const unsigned char*)&instance,
-			(const unsigned char*)base_class::address(instance, pos)
+			(byte_ptr)ptr,
+			(byte_ptr)offs_calc::address(*ptr, pos)
                 );
-	}
+        } 
 };
 
                 
Modified: sandbox/mirror/boost/mirror/meta_attributes.hpp
==============================================================================
--- sandbox/mirror/boost/mirror/meta_attributes.hpp	(original)
+++ sandbox/mirror/boost/mirror/meta_attributes.hpp	2009-02-20 15:43:58 EST (Fri, 20 Feb 2009)
@@ -52,7 +52,7 @@
                 class ReflectedType, 
                 class VariantTag
 	>
-	struct meta_class_all_attributes
+	struct meta_class_all_attributes_base
         {
                 // the scope of the MetaAttributeSequence
                 typedef boost::mirror::meta_class<ReflectedType, VariantTag> 
@@ -650,29 +650,6 @@
                                 is_inherited(pos)
                         );
                 }
-	private:
-		static inline ptrdiff_t calculate_offset(
-			const unsigned char* base_ptr, 
-			const unsigned char* attr_ptr
-		)
-		{
-			// we are unable to calculate the offset
-			if(attr_ptr == 0) return -1;
-			else return attr_ptr - base_ptr;
-		}
-	public:
-
-		/** Gets the byte-offset of the I-th member (including 
-		 *  the inherited ones)
-		 */
-		template <class Class, int I>
-		static inline ptrdiff_t	offset(Class& instance, mpl::int_<I> pos)
-		{
-			return calculate_offset(
-				(const unsigned char*)&instance,
-				(const unsigned char*)address(instance, pos)
-			);
-		}
 
                 /** Gets the value of the I-th member (including 
                  *  the inherited ones)
@@ -726,7 +703,28 @@
                         typename result_of_is_inherited<I>::type
 		>::type get_traits(mpl::int_<I>);
 
-	}; // all_attributes
+	}; // all_attributes_base
+
+	template < class Class, class VariantTag >
+	struct meta_class_all_attributes
+	 : public detail::meta_class_attributes_offset_calculator<
+	        Class,
+	        meta_class_all_attributes_base<Class, VariantTag>
+	>
+	{
+	private:
+		typedef detail::meta_class_attributes_offset_calculator<
+         	       Class,
+                	meta_class_all_attributes_base<Class, VariantTag>
+	        > offs_calc;
+	public:
+	        template <int I>
+	        static inline ptrdiff_t offset_of(mpl::int_<I> pos)
+	        {
+	                return offs_calc::get_offset_of(pos, (Class*)0);
+	        }
+
+	};
 
         /** Instances of this template are used to store information 
          *  about single class' member attribute and are used mainly
Modified: sandbox/mirror/boost/mirror/meta_classes/boost/tuples/tuple.hpp
==============================================================================
--- sandbox/mirror/boost/mirror/meta_classes/boost/tuples/tuple.hpp	(original)
+++ sandbox/mirror/boost/mirror/meta_classes/boost/tuples/tuple.hpp	2009-02-20 15:43:58 EST (Fri, 20 Feb 2009)
@@ -73,7 +73,7 @@
         {
                 return &::boost::tuples::get<I>(a_tuple);
         }
-	
+
         // member attrib value get
         template <int I> 
         static typename att_val_pass<I>::type
Modified: sandbox/mirror/libs/mirror/example/special/boost_tuple.cpp
==============================================================================
--- sandbox/mirror/libs/mirror/example/special/boost_tuple.cpp	(original)
+++ sandbox/mirror/libs/mirror/example/special/boost_tuple.cpp	2009-02-20 15:43:58 EST (Fri, 20 Feb 2009)
@@ -49,7 +49,7 @@
                                 " " <<
                                 ma.base_name() <<
                                 " [offset: " <<
-				ma.offset(inst) <<
+				ma.offset_of() <<
                                 "] = " <<
                                 ma.get(inst) <<
                                 endl;
@@ -132,20 +132,20 @@
         bcout << "The type name is: "<< meta_X::base_name() << endl;
         bcout << "The class has "<< size<meta_X::all_attributes>::value << " members" << endl;
         bcout << "---------------------------------------------------" << endl;
-	for_each<meta_X::all_attributes>(p);
+	for_each<meta_X::attributes>(p);
         bcout << "---------------------------------------------------" << endl;
-	reverse_for_each<meta_X::all_attributes>(p);
+	reverse_for_each<meta_X::attributes>(p);
         bcout << "---------------------------------------------------" << endl;
-	for_each<meta_X::all_attributes>(cref(select_base_name()), cref(str_printer()));
+	for_each<meta_X::attributes>(cref(select_base_name()), cref(str_printer()));
         bcout << "---------------------------------------------------" << endl;
         for_each<
-		begin<meta_X::all_attributes>::type,
-		end<meta_X::all_attributes>::type
+		begin<meta_X::attributes>::type,
+		end<meta_X::attributes>::type
 	>(cref(select_full_name()), cref(str_printer()));
         bcout << "---------------------------------------------------" << endl;
         reverse_for_each<
-		begin<meta_X::all_attributes>::type,
-		end<meta_X::all_attributes>::type
+		begin<meta_X::attributes>::type,
+		end<meta_X::attributes>::type
 	>(cref(select_base_name()), cref(str_printer()));
         bcout << "---------------------------------------------------" << endl;
         bcout << "Finished" << endl;
Modified: sandbox/mirror/libs/mirror/test/Jamfile.v2
==============================================================================
--- sandbox/mirror/libs/mirror/test/Jamfile.v2	(original)
+++ sandbox/mirror/libs/mirror/test/Jamfile.v2	2009-02-20 15:43:58 EST (Fri, 20 Feb 2009)
@@ -34,6 +34,7 @@
          [ compile classes_ct_05.cpp ]
          [ compile classes_ct_06.cpp ]
          [ compile classes_ct_07.cpp ]
+         [ compile classes_ct_08.cpp ]
          [ run classes_rt_01.cpp ]
          [ run classes_rt_02.cpp ]
          [ run classes_rt_03.cpp ]
@@ -41,6 +42,7 @@
          [ run classes_rt_05.cpp ]
          [ run classes_rt_06.cpp ]
          [ run classes_rt_07.cpp ]
+         [ run classes_rt_08.cpp ]
          [ compile concepts_ct_01.cpp ]
          [ compile concepts_ct_02.cpp ]
          [ compile concepts_ct_03.cpp ]
Added: sandbox/mirror/libs/mirror/test/classes_ct_08.cpp
==============================================================================
--- (empty file)
+++ sandbox/mirror/libs/mirror/test/classes_ct_08.cpp	2009-02-20 15:43:58 EST (Fri, 20 Feb 2009)
@@ -0,0 +1,66 @@
+/**
+ * \file test/classes_ct_08.cpp
+ *
+ * This file is part of the Mirror library testsuite.
+ *
+ * Testing class registration and reflection.
+ *
+ *  Copyright 2008 Matus Chochlik. 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)
+ */
+
+// MPL_ASSERT
+#include <boost/mpl/assert.hpp>
+#include <boost/type_traits/is_same.hpp>
+//
+#include <boost/mirror/meta_class.hpp>
+#include <boost/mirror/intrinsic/at.hpp>
+//
+#include "./namespaces.hpp"
+#include "./classes.hpp"
+#include "./test.hpp"
+
+template <class MetaAttribute>
+void test_offset_of(MetaAttribute*)
+{
+	MetaAttribute::offset_of();
+}
+
+void test_main()
+{
+	using namespace ::std;
+	using namespace ::boost;
+	using namespace ::boost::mirror;
+	//
+	typedef BOOST_MIRRORED_CLASS(::test::H) meta_H;
+	//
+	test_offset_of((at<meta_H::all_attributes, mpl::int_< 0> >::type*)0);
+	test_offset_of((at<meta_H::all_attributes, mpl::int_< 1> >::type*)0);
+	test_offset_of((at<meta_H::all_attributes, mpl::int_< 2> >::type*)0);
+	test_offset_of((at<meta_H::all_attributes, mpl::int_< 3> >::type*)0);
+	test_offset_of((at<meta_H::all_attributes, mpl::int_< 4> >::type*)0);
+	test_offset_of((at<meta_H::all_attributes, mpl::int_< 5> >::type*)0);
+	test_offset_of((at<meta_H::all_attributes, mpl::int_< 6> >::type*)0);
+	test_offset_of((at<meta_H::all_attributes, mpl::int_< 7> >::type*)0);
+	test_offset_of((at<meta_H::all_attributes, mpl::int_< 8> >::type*)0);
+	test_offset_of((at<meta_H::all_attributes, mpl::int_< 9> >::type*)0);
+	test_offset_of((at<meta_H::all_attributes, mpl::int_<10> >::type*)0);
+	test_offset_of((at<meta_H::all_attributes, mpl::int_<11> >::type*)0);
+	test_offset_of((at<meta_H::all_attributes, mpl::int_<12> >::type*)0);
+	test_offset_of((at<meta_H::all_attributes, mpl::int_<13> >::type*)0);
+	test_offset_of((at<meta_H::all_attributes, mpl::int_<14> >::type*)0);
+	test_offset_of((at<meta_H::all_attributes, mpl::int_<15> >::type*)0);
+	test_offset_of((at<meta_H::all_attributes, mpl::int_<16> >::type*)0);
+	test_offset_of((at<meta_H::all_attributes, mpl::int_<17> >::type*)0);
+	//
+}
+
+test_suite* init_unit_test_suite( int argc, char* argv[] )
+{
+    test_suite *test = BOOST_TEST_SUITE("Mirror: classes compile test 08");
+    test->add(BOOST_TEST_CASE(&test_main));
+    return test;
+}
+
+
Added: sandbox/mirror/libs/mirror/test/classes_rt_08.cpp
==============================================================================
--- (empty file)
+++ sandbox/mirror/libs/mirror/test/classes_rt_08.cpp	2009-02-20 15:43:58 EST (Fri, 20 Feb 2009)
@@ -0,0 +1,96 @@
+/**
+ * \file test/classes_ct_08.cpp
+ *
+ * This file is part of the Mirror library testsuite.
+ *
+ * Testing class registration and reflection.
+ *
+ *  Copyright 2008 Matus Chochlik. 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)
+ */
+
+// MPL_ASSERT
+#include <boost/mpl/assert.hpp>
+#include <boost/type_traits/is_same.hpp>
+//
+#include <boost/mirror/meta_class.hpp>
+#include <boost/mirror/meta_classes/boost/fusion/vector.hpp>
+#include <boost/mirror/intrinsic/at.hpp>
+//
+#include "./namespaces.hpp"
+#include "./classes.hpp"
+#include "./test.hpp"
+
+
+template <class Class, class MetaAttribute>
+void test_offset_of(Class& instance, MetaAttribute*)
+{
+	ptrdiff_t offs_1 = MetaAttribute::offset(instance);
+	ptrdiff_t offs_2 = MetaAttribute::offset_of();
+	BOOST_CHECK(offs_1 == offs_2);
+}
+
+template <class Class, class MetaAttribute>
+void test_offset_of_2(Class& instance, MetaAttribute*)
+{
+	ptrdiff_t offs_1 = MetaAttribute::offset_of();
+	ptrdiff_t offs_2 = MetaAttribute::offset(instance);
+	BOOST_CHECK(offs_1 == offs_2);
+}
+
+void test_main()
+{
+	using namespace ::std;
+	using namespace ::boost;
+	using namespace ::boost::mirror;
+	//
+	typedef BOOST_MIRRORED_CLASS(::test::H) meta_H;
+	::test::H h;
+	//
+	test_offset_of(h, (at<meta_H::all_attributes, mpl::int_< 0> >::type*)0);
+	test_offset_of(h, (at<meta_H::all_attributes, mpl::int_< 1> >::type*)0);
+	test_offset_of(h, (at<meta_H::all_attributes, mpl::int_< 2> >::type*)0);
+	test_offset_of(h, (at<meta_H::all_attributes, mpl::int_< 3> >::type*)0);
+	test_offset_of(h, (at<meta_H::all_attributes, mpl::int_< 4> >::type*)0);
+	test_offset_of(h, (at<meta_H::all_attributes, mpl::int_< 5> >::type*)0);
+	test_offset_of(h, (at<meta_H::all_attributes, mpl::int_< 6> >::type*)0);
+	test_offset_of(h, (at<meta_H::all_attributes, mpl::int_< 7> >::type*)0);
+	test_offset_of(h, (at<meta_H::all_attributes, mpl::int_< 8> >::type*)0);
+	test_offset_of(h, (at<meta_H::all_attributes, mpl::int_< 9> >::type*)0);
+	test_offset_of(h, (at<meta_H::all_attributes, mpl::int_<10> >::type*)0);
+	test_offset_of(h, (at<meta_H::all_attributes, mpl::int_<11> >::type*)0);
+	test_offset_of(h, (at<meta_H::all_attributes, mpl::int_<12> >::type*)0);
+	test_offset_of(h, (at<meta_H::all_attributes, mpl::int_<13> >::type*)0);
+	test_offset_of(h, (at<meta_H::all_attributes, mpl::int_<14> >::type*)0);
+	test_offset_of(h, (at<meta_H::all_attributes, mpl::int_<15> >::type*)0);
+	test_offset_of(h, (at<meta_H::all_attributes, mpl::int_<16> >::type*)0);
+	test_offset_of(h, (at<meta_H::all_attributes, mpl::int_<17> >::type*)0);
+	//
+	//
+        typedef fusion::vector<
+                int, int, int, int, int, int, int, int, int, int
+        > T;
+        typedef BOOST_MIRRORED_CLASS(T) meta_T;
+        T t(9, 8, 7, 6, 5, 4, 3, 2, 1, 0);
+	//
+	test_offset_of_2(t, (at<meta_T::attributes, mpl::int_< 0> >::type*)0);
+	test_offset_of_2(t, (at<meta_T::attributes, mpl::int_< 1> >::type*)0);
+	test_offset_of_2(t, (at<meta_T::attributes, mpl::int_< 2> >::type*)0);
+	test_offset_of_2(t, (at<meta_T::attributes, mpl::int_< 3> >::type*)0);
+	test_offset_of_2(t, (at<meta_T::attributes, mpl::int_< 4> >::type*)0);
+	test_offset_of_2(t, (at<meta_T::attributes, mpl::int_< 5> >::type*)0);
+	test_offset_of_2(t, (at<meta_T::attributes, mpl::int_< 6> >::type*)0);
+	test_offset_of_2(t, (at<meta_T::attributes, mpl::int_< 7> >::type*)0);
+	test_offset_of_2(t, (at<meta_T::attributes, mpl::int_< 8> >::type*)0);
+	test_offset_of_2(t, (at<meta_T::attributes, mpl::int_< 9> >::type*)0);
+}
+
+test_suite* init_unit_test_suite( int argc, char* argv[] )
+{
+    test_suite *test = BOOST_TEST_SUITE("Mirror: classes run-time test 08");
+    test->add(BOOST_TEST_CASE(&test_main));
+    return test;
+}
+
+