From 528f932066ecfc6a9fb5477031114da5aeceeb9c Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Wed, 29 Feb 2012 11:53:22 +0200 Subject: Support for C++11 std::unique_ptr and std::shared_ptr as wrappers --- common/wrapper/driver.cxx | 24 +++++++++---------- common/wrapper/test.hxx | 60 ++++++++++++++++++++++++++++++++++------------- 2 files changed, 56 insertions(+), 28 deletions(-) diff --git a/common/wrapper/driver.cxx b/common/wrapper/driver.cxx index bb48fde..4f78a1b 100644 --- a/common/wrapper/driver.cxx +++ b/common/wrapper/driver.cxx @@ -36,9 +36,9 @@ main (int argc, char* argv[]) o.num.reset (new int (123)); o.nstrs.push_back (nullable_string ()); o.nstrs.push_back (nullable_string ("123")); -#ifdef HAVE_TR1_MEMORY - o.tr1_strs.push_back (tr1_nullable_string ()); - o.tr1_strs.push_back (tr1_nullable_string (new string ("123"))); +#if defined(HAVE_CXX11) || defined(HAVE_TR1_MEMORY) + o.sstrs.push_back (str_sptr ()); + o.sstrs.push_back (str_sptr (new string ("123"))); #endif transaction t (db->begin ()); @@ -56,10 +56,10 @@ main (int argc, char* argv[]) assert (o->nstr.null ()); assert (o->nstrs[0].null ()); assert (o->nstrs[1].get () == "123"); -#ifdef HAVE_TR1_MEMORY - assert (!o->tr1_str); - assert (!o->tr1_strs[0]); - assert (*o->tr1_strs[1] == "123"); +#if defined(HAVE_CXX11) || defined(HAVE_TR1_MEMORY) + assert (!o->sstr); + assert (!o->sstrs[0]); + assert (*o->sstrs[1] == "123"); #endif } @@ -102,10 +102,10 @@ main (int argc, char* argv[]) { cont_object co; - co.vi.reset (new vector); - co.vi->push_back (1); - co.vi->push_back (2); - co.vi->push_back (3); + co.nums.reset (new vector); + co.nums->push_back (1); + co.nums->push_back (2); + co.nums->push_back (3); co.c.num = 123; co.c.strs.reset (new vector); @@ -124,7 +124,7 @@ main (int argc, char* argv[]) auto_ptr o (db->load (id)); t.commit (); - assert (*o->vi == *co.vi); + assert (*o->nums == *co.nums); assert (o->c == co.c); } } diff --git a/common/wrapper/test.hxx b/common/wrapper/test.hxx index eeff674..ccf32b3 100644 --- a/common/wrapper/test.hxx +++ b/common/wrapper/test.hxx @@ -5,7 +5,7 @@ #ifndef TEST_HXX #define TEST_HXX -#include // HAVE_TR1_MEMORY +#include // HAVE_CXX11, HAVE_TR1_MEMORY #include #include // std::auto_ptr @@ -14,7 +14,7 @@ #include #include -#ifdef HAVE_TR1_MEMORY +#if !defined(HAVE_CXX11) && defined(HAVE_TR1_MEMORY) # include #endif @@ -26,8 +26,16 @@ using odb::nullable; typedef nullable nullable_string; -#ifdef HAVE_TR1_MEMORY -typedef std::tr1::shared_ptr tr1_nullable_string; +#ifdef HAVE_CXX11 +typedef std::unique_ptr num_uptr; +typedef std::unique_ptr str_uptr; +typedef std::shared_ptr str_sptr; +#else +typedef std::auto_ptr num_uptr; +typedef std::auto_ptr str_uptr; +# ifdef HAVE_TR1_MEMORY +typedef std::tr1::shared_ptr str_sptr; +# endif #endif #pragma db object table("obj") @@ -36,20 +44,20 @@ struct object #pragma db id auto unsigned long id_; - std::auto_ptr num; + num_uptr num; #pragma db null - std::auto_ptr str; + str_uptr str; nullable_string nstr; std::vector nstrs; -#ifdef HAVE_TR1_MEMORY +#if defined(HAVE_CXX11) || defined(HAVE_TR1_MEMORY) #pragma db null - tr1_nullable_string tr1_str; + str_sptr sstr; #pragma db value_null - std::vector tr1_strs; + std::vector sstrs; #endif }; @@ -92,15 +100,27 @@ operator== (const comp2& x, const comp2& y) return x.str == y.str && x.num == y.num && x.strs == y.strs; } +struct comp3; + +#ifdef HAVE_CXX11 +typedef std::unique_ptr comp1_uptr; +typedef std::unique_ptr comp2_uptr; +typedef std::unique_ptr comp3_uptr; +#else +typedef std::auto_ptr comp1_uptr; +typedef std::auto_ptr comp2_uptr; +typedef std::auto_ptr comp3_uptr; +#endif + #pragma db object struct comp_object { #pragma db id auto unsigned long id_; - std::auto_ptr c1; // Wrapped comp value. + comp1_uptr c1; // Wrapped comp value. std::vector > vc1; // Container of wrapped comp values. - std::auto_ptr c2; // Container inside wrapped comp value. + comp2_uptr c2; // Container inside wrapped comp value. }; // This one is just a compilation test to cover more convolute cases. @@ -108,7 +128,7 @@ struct comp_object #pragma db value struct comp3: comp2 { - std::auto_ptr c1; + comp1_uptr c1; std::vector > vc1; }; @@ -118,18 +138,26 @@ struct comp_object2 #pragma db id auto unsigned long id_; - std::auto_ptr c3; + comp3_uptr c3; }; // // Containers. // +#ifdef HAVE_CXX11 +typedef std::unique_ptr> nums_uptr; +typedef std::unique_ptr> strs_uptr; +#else +typedef std::auto_ptr > nums_uptr; +typedef std::auto_ptr > strs_uptr; +#endif + #pragma db value struct cont_comp { int num; - std::auto_ptr > strs; + strs_uptr strs; }; inline bool @@ -144,8 +172,8 @@ struct cont_object #pragma db id auto unsigned long id_; - std::auto_ptr > vi; // Wrapped container. - cont_comp c; // Wrapped container in comp value. + nums_uptr nums; // Wrapped container. + cont_comp c; // Wrapped container in comp value. }; #endif // TEST_HXX -- cgit v1.1