Boost logo

Boost-Commit :

Subject: [Boost-commit] svn:boost r54532 - in sandbox/monotonic: boost/heterogenous libs/monotonic/test/clones
From: christian.schladetsch_at_[hidden]
Date: 2009-06-30 06:17:01


Author: cschladetsch
Date: 2009-06-30 06:17:00 EDT (Tue, 30 Jun 2009)
New Revision: 54532
URL: http://svn.boost.org/trac/boost/changeset/54532

Log:
added abstract_cloneable::hash, ::to_string
Text files modified:
   sandbox/monotonic/boost/heterogenous/abstract_cloneable.hpp | 25 +++++
   sandbox/monotonic/boost/heterogenous/allocator.hpp | 8 +
   sandbox/monotonic/boost/heterogenous/map.hpp | 5 +
   sandbox/monotonic/libs/monotonic/test/clones/tests.cpp | 185 ++++++++++++++++++---------------------
   4 files changed, 124 insertions(+), 99 deletions(-)

Modified: sandbox/monotonic/boost/heterogenous/abstract_cloneable.hpp
==============================================================================
--- sandbox/monotonic/boost/heterogenous/abstract_cloneable.hpp (original)
+++ sandbox/monotonic/boost/heterogenous/abstract_cloneable.hpp 2009-06-30 06:17:00 EDT (Tue, 30 Jun 2009)
@@ -6,6 +6,8 @@
 #ifndef BOOST_HETEROGENOUS_COMMON_BASE_HPP
 #define BOOST_HETEROGENOUS_COMMON_BASE_HPP
 
+#include <string>
+#include <boost/functional/hash_fwd.hpp>
 #include <boost/heterogenous/detail/prefix.hpp>
 #include <boost/heterogenous/abstract_allocator.hpp>
 
@@ -72,10 +74,33 @@
                                 // return copy;
                                 //return copy_construct(original, alloc);
                         }
+
+ /// overridable hash function
+ virtual size_t hash_value() const { return 0; }
+
+ /// return a hash value for the object. try the virtual method first, otherwise just use pointer value
+ size_t hash() const
+ {
+ if (size_t value = hash_value())
+ return value;
+ return ptrdiff_t(reinterpret_cast<const char *>(this) - 0);
+ }
+
+ virtual std::string to_string() const { return "cloneable"; }
                 };
 
         } // namespace heterogenous
 
+
+ template <class B>
+ struct hash<heterogenous::abstract_cloneable<B> >
+ {
+ size_t operator()(heterogenous::abstract_cloneable<B> const &base) const
+ {
+ return base.hash();
+ }
+ };
+
 } // namespace boost
 
 #include <boost/heterogenous/detail/suffix.hpp>

Modified: sandbox/monotonic/boost/heterogenous/allocator.hpp
==============================================================================
--- sandbox/monotonic/boost/heterogenous/allocator.hpp (original)
+++ sandbox/monotonic/boost/heterogenous/allocator.hpp 2009-06-30 06:17:00 EDT (Tue, 30 Jun 2009)
@@ -52,6 +52,14 @@
                         return ptr;
                 }
 
+ template <class T, class Alloc, class A0>
+ T *create(Alloc &alloc, A0 a0)
+ {
+ typename Alloc::template rebind<T>::other al(alloc);
+ T *ptr = al.allocate(1);
+ new (ptr) T(a0);
+ return ptr;
+ }
                 template <class T, class Alloc>
                 void release(T *ptr, Alloc &alloc)
                 {

Modified: sandbox/monotonic/boost/heterogenous/map.hpp
==============================================================================
--- sandbox/monotonic/boost/heterogenous/map.hpp (original)
+++ sandbox/monotonic/boost/heterogenous/map.hpp 2009-06-30 06:17:00 EDT (Tue, 30 Jun 2009)
@@ -182,6 +182,11 @@
                                 return impl.end();
                         }
 
+ iterator find(key_type const &key)
+ {
+ return impl.find(key);
+ }
+
                         //reference operator[](key_type const &key)
                         //{
                         // return impl[n];

Modified: sandbox/monotonic/libs/monotonic/test/clones/tests.cpp
==============================================================================
--- sandbox/monotonic/libs/monotonic/test/clones/tests.cpp (original)
+++ sandbox/monotonic/libs/monotonic/test/clones/tests.cpp 2009-06-30 06:17:00 EDT (Tue, 30 Jun 2009)
@@ -27,41 +27,6 @@
 using namespace boost;
 using namespace heterogenous;
 
-struct my_base
-{
- virtual ~my_base() { }
-};
-
-struct T0 : cloneable<T0, my_base>
-{
- int num;
- T0() : num(0) { }
- T0(int n) : num(n) { }
-};
-
-struct T1 : cloneable<T1, my_base>
-{
- std::string str;
-
- T1() { }
- T1(std::string const &n) : str(n) { }
-};
-
-struct T2 : cloneable<T2, my_base>
-{
- float real;
- int num;
- std::string str;
-
- T2() { }
- T2(float f, int n, std::string const &s) : real(f), num(n), str(s) { }
-
- void print() const
- {
- cout << "derived3: " << real << ", " << num << ", " << str << endl;
- }
-};
-
 namespace mi_test
 {
 
@@ -74,20 +39,17 @@
         /// derive from Q1, which is also cloneable<>
         struct Q1 : Q0, cloneable<Q1>
         {
- string s;
+ string str;
                 Q1() { }
- Q1(string t) : s(t) { }
+ Q1(const char * t) : str(t) { }
         };
         
         struct my_region { };
-
 }
 
-
 BOOST_AUTO_TEST_CASE(test_clones)
 {
         using namespace mi_test;
-
         monotonic::local<my_region> local;
         monotonic::allocator<int,my_region> alloc = local.make_allocator<int>();
 
@@ -105,7 +67,6 @@
 
         Q1 *q1_c1 = dynamic_cast<Q1 *>(q1->clone_as<Q1>(alloc));
         BOOST_ASSERT(typeid(*q1_c1) == typeid(Q1));
-
 }
 
 BOOST_AUTO_TEST_CASE(test_multiple_inheritance)
@@ -113,37 +74,47 @@
         using namespace mi_test;
         typedef heterogenous::vector<> vec;
         vec v;
-
         v.emplace_back<Q0>(42);
         v.emplace_back<Q1>("foo");
-
         vec v2 = v;
- BOOST_ASSERT(v2.ref_at<Q1>(1).s == "foo");
-
+ BOOST_ASSERT(v2.ref_at<Q0>(0).num == 42);
+ BOOST_ASSERT(v2.ref_at<Q1>(1).str == "foo");
 }
 
-namespace test
+struct my_base
 {
- using namespace heterogenous;
- struct my_base
- {
- virtual ~my_base() { }
- };
+ virtual ~my_base() { }
+};
+
+struct T0 : cloneable<T0, my_base>
+{
+ int num;
+ T0() : num(0) { }
+ T0(int n) : num(n) { }
+};
 
- struct T0 : cloneable<T0, my_base> { };
- struct T1 : cloneable<T1, my_base> { };
+struct T1 : cloneable<T1, my_base>
+{
+ std::string str;
+
+ T1() { }
+ T1(std::string const &n) : str(n) { }
+};
+
+struct T2 : cloneable<T2, my_base>
+{
+ float real;
+ int num;
+ std::string str;
+
+ T2() { }
+ T2(float f, int n, std::string const &s) : real(f), num(n), str(s) { }
 
- void run()
+ void print() const
         {
- typedef heterogenous::vector<my_base> vec;
- vec v0;
- v0.emplace_back<T0>();
- v0.emplace_back<T1>();
- vec v1 = v0;
- my_base &whatever = v1[0];
- assert(v1.ptr_at<T1>(1));
+ cout << "derived3: " << real << ", " << num << ", " << str << endl;
         }
-}
+};
 
 /// some external type that we cannot change
 struct ExternalType
@@ -211,36 +182,6 @@
         }
 }
 
-BOOST_AUTO_TEST_CASE(test_any)
-{
- // this works, after changing boost::any<> to take an allocator type argument
- typedef any<monotonic::allocator<char> > any_type;
- typedef std::vector<any_type, monotonic::allocator<any_type> > vec;
- vec v;
-
- // an issue here is that instances are copy-constructed into the container.
- // another issue is that this is effectively typeless.
- // but, types added do not have to derive from anything in order for duplication to work.
- v.push_back(T0(42));
- v.push_back(T1("foo"));
-
- vec v2 = v;
-
- BOOST_ASSERT(any_cast<T1 &>(v2[1]).str == "foo");
-}
-
-BOOST_AUTO_TEST_CASE(test_variant)
-{
- // need to declare all the possible types that could be used at the point of declaration
- typedef variant<T0, T1, T2> var;
- typedef std::vector<var, monotonic::allocator<var> > vec;
- vec v0;
- v0.push_back(T0(42));
- v0.push_back(T1("foo"));
- vec v1 = v0;
- BOOST_ASSERT(boost::get<T1>(v1[1]).str == "foo");
-}
-
 struct my_base2
 {
         int number;
@@ -248,19 +189,23 @@
         virtual ~my_base2() { }
 };
 
-struct M0 : heterogenous::cloneable<M0, my_base2>
+struct M0 : cloneable<M0, my_base2>
 {
+ M0(int n = 0) : my_base2(n) {}
 };
 
-struct M1 : heterogenous::cloneable<M1, my_base2>
+struct M1 : cloneable<M1, my_base2>
 {
+ string str;
+ M1() { }
+ M1(const char *s) : str(s) { }
 };
 
-struct M2 : heterogenous::cloneable<M2, my_base2>
+struct M2 : cloneable<M2, my_base2>
 {
 };
 
-struct M3 : heterogenous::cloneable<M3, my_base2>
+struct M3 : cloneable<M3, my_base2>
 {
 };
 
@@ -274,12 +219,54 @@
 
 BOOST_AUTO_TEST_CASE(test_map)
 {
- heterogenous::map<my_base2,my_less> map;
-
- map .key<M0>().value<M1>()
+ typedef heterogenous::map<my_base2,my_less> map_type;
+ map_type map;
+ map .key<M0>(42).value<M1>("foo")
                 .key<M2>().value<M3>()
                 ;
+ M0 *m0 = create<M0>(map.get_allocator(), 42);
+ map_type::iterator iter = map.find(m0);
+ BOOST_ASSERT(iter!= map.end());
+ M1 *m1 = dynamic_cast<M1 *>(iter->second);
+ BOOST_ASSERT(m1 != 0);
+ BOOST_ASSERT(m1->str == "foo");
+}
+
+BOOST_AUTO_TEST_CASE(test_hash)
+{
+ M0 a, b;
+ BOOST_ASSERT(a.hash() != b.hash());
+}
+
+
+BOOST_AUTO_TEST_CASE(test_any)
+{
+ // this works, after changing boost::any<> to take an allocator type argument
+ typedef any<monotonic::allocator<char> > any_type;
+ typedef std::vector<any_type, monotonic::allocator<any_type> > vec;
+ vec v;
+
+ // an issue here is that instances are copy-constructed into the container.
+ // another issue is that this is effectively typeless.
+ // but, types added do not have to derive from anything in order for duplication to work.
+ v.push_back(T0(42));
+ v.push_back(T1("foo"));
 
+ vec v2 = v;
+
+ BOOST_ASSERT(any_cast<T1 &>(v2[1]).str == "foo");
+}
+
+BOOST_AUTO_TEST_CASE(test_variant)
+{
+ // need to declare all the possible types that could be used at the point of declaration
+ typedef variant<T0, T1, T2> var;
+ typedef std::vector<var, monotonic::allocator<var> > vec;
+ vec v0;
+ v0.push_back(T0(42));
+ v0.push_back(T1("foo"));
+ vec v1 = v0;
+ BOOST_ASSERT(boost::get<T1>(v1[1]).str == "foo");
 }
 
 //EOF


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