diff options
author | Boris Kolpackov <boris@codesynthesis.com> | 2012-02-29 11:53:22 +0200 |
---|---|---|
committer | Boris Kolpackov <boris@codesynthesis.com> | 2012-02-29 11:53:22 +0200 |
commit | 528f932066ecfc6a9fb5477031114da5aeceeb9c (patch) | |
tree | af00256e2c65e40b6b125d0f1bf9d1a6c3a57c5c /common/wrapper | |
parent | d706ea36e08230eb21996562e4e129495d871239 (diff) |
Support for C++11 std::unique_ptr and std::shared_ptr as wrappers
Diffstat (limited to 'common/wrapper')
-rw-r--r-- | common/wrapper/driver.cxx | 24 | ||||
-rw-r--r-- | 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<int>); - co.vi->push_back (1); - co.vi->push_back (2); - co.vi->push_back (3); + co.nums.reset (new vector<int>); + 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<string>); @@ -124,7 +124,7 @@ main (int argc, char* argv[]) auto_ptr<cont_object> o (db->load<cont_object> (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 <common/config.hxx> // HAVE_TR1_MEMORY +#include <common/config.hxx> // HAVE_CXX11, HAVE_TR1_MEMORY #include <string> #include <memory> // std::auto_ptr @@ -14,7 +14,7 @@ #include <odb/core.hxx> #include <odb/nullable.hxx> -#ifdef HAVE_TR1_MEMORY +#if !defined(HAVE_CXX11) && defined(HAVE_TR1_MEMORY) # include <odb/tr1/memory.hxx> #endif @@ -26,8 +26,16 @@ using odb::nullable; typedef nullable<std::string> nullable_string; -#ifdef HAVE_TR1_MEMORY -typedef std::tr1::shared_ptr<std::string> tr1_nullable_string; +#ifdef HAVE_CXX11 +typedef std::unique_ptr<int> num_uptr; +typedef std::unique_ptr<std::string> str_uptr; +typedef std::shared_ptr<std::string> str_sptr; +#else +typedef std::auto_ptr<int> num_uptr; +typedef std::auto_ptr<std::string> str_uptr; +# ifdef HAVE_TR1_MEMORY +typedef std::tr1::shared_ptr<std::string> 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<int> num; + num_uptr num; #pragma db null - std::auto_ptr<std::string> str; + str_uptr str; nullable_string nstr; std::vector<nullable_string> 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_nullable_string> tr1_strs; + std::vector<str_sptr> 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> comp1_uptr; +typedef std::unique_ptr<comp2> comp2_uptr; +typedef std::unique_ptr<comp3> comp3_uptr; +#else +typedef std::auto_ptr<comp1> comp1_uptr; +typedef std::auto_ptr<comp2> comp2_uptr; +typedef std::auto_ptr<comp3> comp3_uptr; +#endif + #pragma db object struct comp_object { #pragma db id auto unsigned long id_; - std::auto_ptr<comp1> c1; // Wrapped comp value. + comp1_uptr c1; // Wrapped comp value. std::vector<nullable<comp1> > vc1; // Container of wrapped comp values. - std::auto_ptr<comp2> 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<comp1> c1; + comp1_uptr c1; std::vector<nullable<comp1> > vc1; }; @@ -118,18 +138,26 @@ struct comp_object2 #pragma db id auto unsigned long id_; - std::auto_ptr<comp3> c3; + comp3_uptr c3; }; // // Containers. // +#ifdef HAVE_CXX11 +typedef std::unique_ptr<std::vector<int>> nums_uptr; +typedef std::unique_ptr<std::vector<std::string>> strs_uptr; +#else +typedef std::auto_ptr<std::vector<int> > nums_uptr; +typedef std::auto_ptr<std::vector<std::string> > strs_uptr; +#endif + #pragma db value struct cont_comp { int num; - std::auto_ptr<std::vector<std::string> > strs; + strs_uptr strs; }; inline bool @@ -144,8 +172,8 @@ struct cont_object #pragma db id auto unsigned long id_; - std::auto_ptr<std::vector<int> > 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 |