From d706ea36e08230eb21996562e4e129495d871239 Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Wed, 29 Feb 2012 10:57:43 +0200 Subject: Support for C++11 std::unique_ptr as object pointer This includes the odb::lazy_unique_ptr implementation. --- common/lazy-ptr/driver.cxx | 51 ++++++++------- common/lazy-ptr/test.hxx | 137 +++++++++++++++++++++++------------------ common/relationship/driver.cxx | 14 +++++ common/relationship/test.hxx | 39 +++++++++++- 4 files changed, 156 insertions(+), 85 deletions(-) diff --git a/common/lazy-ptr/driver.cxx b/common/lazy-ptr/driver.cxx index 3e031d3..2e2f140 100644 --- a/common/lazy-ptr/driver.cxx +++ b/common/lazy-ptr/driver.cxx @@ -21,18 +21,21 @@ using namespace std; using namespace odb::core; -auto_ptr -create (unsigned int id) +namespace test2 { - auto_ptr r (new obj2 (id)); - return r; -} + obj_ptr + create (unsigned int id) + { + obj_ptr r (new obj (id)); + return r; + } -lazy_auto_ptr -create (database& db, unsigned int id) -{ - lazy_auto_ptr r (db, id); - return r; + lazy_obj_ptr + create (database& db, unsigned int id) + { + lazy_obj_ptr r (db, id); + return r; + } } int @@ -45,8 +48,7 @@ main (int argc, char* argv[]) // Raw. // { - typedef cont1 cont; - typedef obj1 obj; + using namespace test1; // persist // @@ -141,28 +143,31 @@ main (int argc, char* argv[]) } } - // Auto pointer. + // std::auto_ptr/std::unique_ptr // { - typedef cont2 cont; - typedef obj2 obj; + using namespace test2; // persist // { - auto_ptr o1 (new obj (1)); + obj_ptr o1 (new obj (1)); transaction t (db->begin ()); db->persist (*o1); t.commit (); } - auto_ptr c1 (new cont (1)); - auto_ptr c2 (new cont (2)); + cont_ptr c1 (new cont (1)); + cont_ptr c2 (new cont (2)); - lazy_auto_ptr lo1 = create (*db, 1); + lazy_obj_ptr lo1 = create (*db, 1); lo1 = create (*db, 1); +#ifdef HAVE_CXX11 + c1->o = std::move (lo1); +#else c1->o = lo1; +#endif c2->o = create (2); { @@ -181,7 +186,7 @@ main (int argc, char* argv[]) { session s; transaction t (db->begin ()); - auto_ptr c (db->load (1)); + cont_ptr c (db->load (1)); obj* o (db->load (1)); // Not loaded. @@ -202,7 +207,7 @@ main (int argc, char* argv[]) // Load. // cont* cl (o->c.load ()); - const auto_ptr& ol (c->o.load ()); + const obj_ptr& ol (c->o.load ()); assert (cl == c.get ()); assert (ol.get () == o); @@ -215,7 +220,7 @@ main (int argc, char* argv[]) { // No session. transaction t (db->begin ()); - auto_ptr c (db->load (1)); + cont_ptr c (db->load (1)); assert (!c->o.loaded ()); c->o.load (); @@ -233,7 +238,7 @@ main (int argc, char* argv[]) // #if defined(HAVE_CXX11) || defined(HAVE_TR1_MEMORY) { - using namespace shared; + using namespace test3; // persist // diff --git a/common/lazy-ptr/test.hxx b/common/lazy-ptr/test.hxx index 0adddf8..2813c50 100644 --- a/common/lazy-ptr/test.hxx +++ b/common/lazy-ptr/test.hxx @@ -21,87 +21,106 @@ // Raw pointer. // -using odb::lazy_ptr; -class obj1; - -#pragma db object -class cont1 +#pragma db namespace table("t1_") +namespace test1 { -public: - cont1 () {} - cont1 (unsigned long i): id (i) {} - ~cont1 (); + using odb::lazy_ptr; + class obj; - #pragma db id - unsigned long id; + #pragma db object + class cont + { + public: + cont () {} + cont (unsigned long i): id (i) {} + ~cont (); - typedef std::vector > obj_list; + #pragma db id + unsigned long id; - #pragma db value_not_null - obj_list o; -}; + typedef std::vector > obj_list; -#pragma db object -class obj1 -{ -public: - obj1 () {} - obj1 (unsigned long i): id (i) {} + #pragma db value_not_null + obj_list o; + }; - #pragma db id - unsigned long id; + #pragma db object + class obj + { + public: + obj () {} + obj (unsigned long i): id (i) {} + + #pragma db id + unsigned long id; - #pragma db inverse(o) not_null - lazy_ptr c; // weak -}; + #pragma db inverse(o) not_null + lazy_ptr c; // weak + }; -inline cont1:: -~cont1 () -{ - for (obj_list::iterator i (o.begin ()); i != o.end (); ++i) - if (obj1* p = i->get ()) - delete p; + inline cont:: + ~cont () + { + for (obj_list::iterator i (o.begin ()); i != o.end (); ++i) + if (obj* p = i->get ()) + delete p; + } } -// Auto pointer. +// std::auto_ptr/std::unique_ptr // -using std::auto_ptr; -using odb::lazy_auto_ptr; +#pragma db namespace table("t2_") +namespace test2 +{ + using odb::lazy_ptr; -class obj2; + class obj; + class cont; -#pragma db object -class cont2 -{ -public: - cont2 () {} - cont2 (unsigned long i): id (i) {} +#ifdef HAVE_CXX11 + typedef std::unique_ptr obj_ptr; + typedef std::unique_ptr cont_ptr; + typedef odb::lazy_unique_ptr lazy_obj_ptr; +#else + typedef std::auto_ptr obj_ptr; + typedef std::auto_ptr cont_ptr; + typedef odb::lazy_auto_ptr lazy_obj_ptr; +#endif - #pragma db id - unsigned long id; + #pragma db object + class cont + { + public: + cont () {} + cont (unsigned long i): id (i) {} - #pragma db not_null - lazy_auto_ptr o; -}; + #pragma db id + unsigned long id; -#pragma db object -class obj2 -{ -public: - obj2 () {} - obj2 (unsigned long i): id (i) {} + #pragma db not_null + lazy_obj_ptr o; + }; + + #pragma db object + class obj + { + public: + obj () {} + obj (unsigned long i): id (i) {} - #pragma db id - unsigned long id; + #pragma db id + unsigned long id; - #pragma db inverse(o) not_null - lazy_ptr c; // weak -}; + #pragma db inverse(o) not_null + lazy_ptr c; // weak + }; +} // shared_ptr // #if defined(HAVE_CXX11) || defined(HAVE_TR1_MEMORY) -namespace shared +#pragma db namespace table("t3_") +namespace test3 { #ifdef HAVE_CXX11 using std::shared_ptr; diff --git a/common/relationship/driver.cxx b/common/relationship/driver.cxx index 20e0f0c..c6f4b01 100644 --- a/common/relationship/driver.cxx +++ b/common/relationship/driver.cxx @@ -30,6 +30,13 @@ main (int argc, char* argv[]) aggr a ("aggr"); a.o1 = new obj1 ("o1", "obj1"); a.o2.reset (new obj2 ("obj2")); + +#ifdef HAVE_CXX11 + a.v2.push_back (obj2_ptr (new obj2 ("v1 obj2 1"))); + a.v2.push_back (0); + a.v2.push_back (obj2_ptr (new obj2 ("v1 obj2 2"))); +#endif + #if defined(HAVE_CXX11) || defined(HAVE_TR1_MEMORY) a.o3.reset (new obj3 ("obj3")); @@ -60,6 +67,13 @@ main (int argc, char* argv[]) transaction t (db->begin ()); db->persist (a.o1); db->persist (a.o2); + +#ifdef HAVE_CXX11 + for (obj2_vec::iterator i (a.v2.begin ()); i != a.v2.end (); ++i) + if (*i) + db->persist (*i); +#endif + #if defined(HAVE_CXX11) || defined(HAVE_TR1_MEMORY) db->persist (a.o3); diff --git a/common/relationship/test.hxx b/common/relationship/test.hxx index 8b40dba..e66e72c 100644 --- a/common/relationship/test.hxx +++ b/common/relationship/test.hxx @@ -118,9 +118,17 @@ operator== (const obj1_map& x, const obj1_map& y) return true; } -// auto_ptr +// auto_ptr/unique_ptr // -#pragma db object pointer(std::auto_ptr) +struct obj2; + +#ifdef HAVE_CXX11 +typedef std::unique_ptr obj2_ptr; +#else +typedef std::auto_ptr obj2_ptr; +#endif + +#pragma db object pointer(obj2_ptr) struct obj2 { obj2 () {} @@ -138,6 +146,23 @@ operator== (const obj2& x, const obj2& y) return x.id == y.id && x.str == y.str; } +#ifdef HAVE_CXX11 +typedef std::vector obj2_vec; + +inline bool +operator== (const obj2_vec& x, const obj2_vec& y) +{ + if (x.size () != y.size ()) + return false; + + for (obj2_vec::size_type i (0); i < x.size (); ++i) + if (!(x[i] ? (y[i] && *x[i] == *y[i]) : !y[i])) + return false; + + return true; +} +#endif + // shared_ptr // #if defined(HAVE_CXX11) || defined(HAVE_TR1_MEMORY) @@ -215,7 +240,12 @@ struct aggr unsigned long id; obj1* o1; - std::auto_ptr o2; + + obj2_ptr o2; // std::auto_ptr or std::unique_ptr +#ifdef HAVE_CXX11 + obj2_vec v2; +#endif + #if defined(HAVE_CXX11) || defined(HAVE_TR1_MEMORY) obj3_ptr o3; comp c; @@ -240,6 +270,9 @@ operator== (const aggr& x, const aggr& y) x.id == y.id && (x.o1 ? (y.o1 && *x.o1 == *y.o1) : !y.o1) && (x.o2.get () ? (y.o2.get () && *x.o2 == *y.o2) : !y.o2.get ()) && +#ifdef HAVE_CXX11 + x.v2 == y.v2 && +#endif #if defined(HAVE_CXX11) || defined(HAVE_TR1_MEMORY) (x.o3.get () ? (y.o3.get () && *x.o3 == *y.o3) : !y.o3.get ()) && x.c == y.c && -- cgit v1.1