From 439bc0ff201f38025d224ce421c86ca44f3dc063 Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Tue, 28 Feb 2012 12:46:48 +0200 Subject: Test std::shared_ptr in C++11 mode and std::tr1::shared_ptr in C++98 mode --- common/inverse/driver.cxx | 44 ++-- common/inverse/test.hxx | 395 ++++++++++++++++++----------------- common/lazy-ptr/driver.cxx | 44 +++- common/lazy-ptr/test.hxx | 20 +- common/relationship-query/driver.cxx | 9 +- common/relationship-query/test.hxx | 43 ++-- common/relationship/driver.cxx | 6 +- common/relationship/test.hxx | 17 +- 8 files changed, 327 insertions(+), 251 deletions(-) diff --git a/common/inverse/driver.cxx b/common/inverse/driver.cxx index c2cc98a..cf66276 100644 --- a/common/inverse/driver.cxx +++ b/common/inverse/driver.cxx @@ -28,9 +28,11 @@ main (int argc, char* argv[]) { auto_ptr db (create_database (argc, argv)); - // Raw pointer version. + // Test raw pointers. // { + using namespace test1; + obj1_ptr o1_1 (new obj1); obj1_ptr o1_2 (new obj1); obj2_ptr o2 (new obj2); @@ -208,18 +210,20 @@ main (int argc, char* argv[]) delete o1_2; } - // TR1 pointer version. + // Test shared_ptr/weak_ptr. // -#ifdef HAVE_TR1_MEMORY +#if defined(HAVE_CXX11) || defined(HAVE_TR1_MEMORY) { - tr1_obj1_ptr o1_1 (new tr1_obj1); - tr1_obj1_ptr o1_2 (new tr1_obj1); - tr1_obj2_ptr o2 (new tr1_obj2); - tr1_obj3_ptr o3_1 (new tr1_obj3); - tr1_obj3_ptr o3_2 (new tr1_obj3); - tr1_obj4_ptr o4 (new tr1_obj4); - tr1_obj5_ptr o5_1 (new tr1_obj5); - tr1_obj5_ptr o5_2 (new tr1_obj5); + using namespace test2; + + obj1_ptr o1_1 (new obj1); + obj1_ptr o1_2 (new obj1); + obj2_ptr o2 (new obj2); + obj3_ptr o3_1 (new obj3); + obj3_ptr o3_2 (new obj3); + obj4_ptr o4 (new obj4); + obj5_ptr o5_1 (new obj5); + obj5_ptr o5_2 (new obj5); o1_1->id = "obj1 1"; o1_1->o2 = o2; @@ -230,7 +234,7 @@ main (int argc, char* argv[]) o1_1->o5.push_back (o5_2); o1_2->id = "obj1 2"; - o1_2->o2 = tr1_obj2_ptr (); + o1_2->o2 = obj2_ptr (); o1_2->o3.clear (); o1_2->o4 = o4; o1_2->o5.push_back (o5_1); @@ -279,12 +283,12 @@ main (int argc, char* argv[]) { session s; transaction t (db->begin ()); - tr1_obj2_ptr x2 (db->load (o2->id)); - tr1_obj3_ptr x3_1 (db->load (o3_1->id)); - tr1_obj3_ptr x3_2 (db->load (o3_2->id)); - tr1_obj4_ptr x4 (db->load (o4->id)); - tr1_obj5_ptr x5_1 (db->load (o5_1->id)); - tr1_obj5_ptr x5_2 (db->load (o5_2->id)); + obj2_ptr x2 (db->load (o2->id)); + obj3_ptr x3_1 (db->load (o3_1->id)); + obj3_ptr x3_2 (db->load (o3_2->id)); + obj4_ptr x4 (db->load (o4->id)); + obj5_ptr x5_1 (db->load (o5_1->id)); + obj5_ptr x5_2 (db->load (o5_2->id)); t.commit (); assert (x2->str == o2->str); @@ -301,7 +305,7 @@ main (int argc, char* argv[]) { assert (x4->str == o4->str); - tr1_obj1_ptr t1 (x4->o1[0].lock ()), t2 (x4->o1[1].lock ()); + obj1_ptr t1 (x4->o1[0].lock ()), t2 (x4->o1[1].lock ()); assert (t1->id == o1_1->id || t2->id == o1_1->id); assert (t1->id == o1_2->id || t2->id == o1_2->id); @@ -311,7 +315,7 @@ main (int argc, char* argv[]) assert (x5_1->str == o5_1->str); assert (x5_2->str == o5_2->str); - tr1_obj1_ptr t1 (x5_1->o1[0].lock ()), t2 (x5_1->o1[1].lock ()), + obj1_ptr t1 (x5_1->o1[0].lock ()), t2 (x5_1->o1[1].lock ()), t3 (x5_2->o1[0].lock ()); assert (t1->id == o1_1->id || t2->id == o1_1->id); diff --git a/common/inverse/test.hxx b/common/inverse/test.hxx index fe47eb8..83b6a3c 100644 --- a/common/inverse/test.hxx +++ b/common/inverse/test.hxx @@ -5,7 +5,7 @@ #ifndef TEST_HXX #define TEST_HXX -#include // HAVE_TR1_MEMORY +#include // HAVE_CXX11, HAVE_TR1_MEMORY #include #include @@ -14,203 +14,222 @@ #include -#ifdef HAVE_TR1_MEMORY +#if !defined(HAVE_CXX11) && defined(HAVE_TR1_MEMORY) # include #endif -struct obj1; -struct obj2; -struct obj3; -struct obj4; -struct obj5; - -typedef obj1* obj1_ptr; -typedef obj2* obj2_ptr; -typedef obj3* obj3_ptr; -typedef obj4* obj4_ptr; -typedef obj5* obj5_ptr; - -typedef std::set obj1_ptr_set; -typedef std::set obj3_ptr_set; -typedef std::set obj5_ptr_set; - -#pragma db object -struct obj1 -{ - obj1 (): o2 (0), o4 (0) {} - ~obj1 (); - - #pragma db id - std::string id; - - obj2_ptr o2; - - #pragma db id_column("obj1_id") value_column("obj3_id") - obj3_ptr_set o3; - - obj4_ptr o4; - - obj5_ptr_set o5; -}; - -#pragma db object -struct obj2 -{ - #pragma db id auto - int id; - - // one-to-one - // - #pragma db inverse(o2) - obj1_ptr o1; - - std::string str; -}; - -#pragma db object -struct obj3 -{ - std::string str; - - // one(i)-to-many - // - #pragma db inverse (o3) - obj1_ptr o1; - - #pragma db id auto - int id; -}; - -#pragma db object -struct obj4 -{ - #pragma db id auto - int id; - - std::string str; - - // many(i)-to-one - // - #pragma db inverse (o4) - obj1_ptr_set o1; -}; - -#pragma db object -struct obj5 -{ - #pragma db id auto - int id; - - std::string str; - - // many(i)-to-many - // - #pragma db inverse (o5) - obj1_ptr_set o1; -}; - -inline obj1:: -~obj1 () +// Test raw pointers. +// +#pragma db namespace table("t1_") +namespace test1 { - delete o2; - for (obj3_ptr_set::iterator i (o3.begin ()); i != o3.end (); ++i) - delete *i; - delete o4; - for (obj5_ptr_set::iterator i (o5.begin ()); i != o5.end (); ++i) - delete *i; + struct obj1; + struct obj2; + struct obj3; + struct obj4; + struct obj5; + + typedef obj1* obj1_ptr; + typedef obj2* obj2_ptr; + typedef obj3* obj3_ptr; + typedef obj4* obj4_ptr; + typedef obj5* obj5_ptr; + + typedef std::set obj1_ptr_set; + typedef std::set obj3_ptr_set; + typedef std::set obj5_ptr_set; + + #pragma db object + struct obj1 + { + obj1 (): o2 (0), o4 (0) {} + ~obj1 (); + + #pragma db id + std::string id; + + obj2_ptr o2; + + #pragma db id_column("obj1_id") value_column("obj3_id") + obj3_ptr_set o3; + + obj4_ptr o4; + + obj5_ptr_set o5; + }; + + #pragma db object + struct obj2 + { + #pragma db id auto + int id; + + // one-to-one + // + #pragma db inverse(o2) + obj1_ptr o1; + + std::string str; + }; + + #pragma db object + struct obj3 + { + std::string str; + + // one(i)-to-many + // + #pragma db inverse (o3) + obj1_ptr o1; + + #pragma db id auto + int id; + }; + + #pragma db object + struct obj4 + { + #pragma db id auto + int id; + + std::string str; + + // many(i)-to-one + // + #pragma db inverse (o4) + obj1_ptr_set o1; + }; + + #pragma db object + struct obj5 + { + #pragma db id auto + int id; + + std::string str; + + // many(i)-to-many + // + #pragma db inverse (o5) + obj1_ptr_set o1; + }; + + inline obj1:: + ~obj1 () + { + delete o2; + for (obj3_ptr_set::iterator i (o3.begin ()); i != o3.end (); ++i) + delete *i; + delete o4; + for (obj5_ptr_set::iterator i (o5.begin ()); i != o5.end (); ++i) + delete *i; + } } -// TR1 version +// Test shared_ptr/weak_ptr. // -#ifdef HAVE_TR1_MEMORY -struct tr1_obj1; -struct tr1_obj2; -struct tr1_obj3; -struct tr1_obj4; -struct tr1_obj5; - -typedef std::tr1::shared_ptr tr1_obj1_ptr; -typedef std::tr1::shared_ptr tr1_obj2_ptr; -typedef std::tr1::shared_ptr tr1_obj3_ptr; -typedef std::tr1::shared_ptr tr1_obj4_ptr; -typedef std::tr1::shared_ptr tr1_obj5_ptr; - -typedef std::tr1::weak_ptr tr1_obj1_wptr; - -typedef std::vector tr1_obj1_wptr_vec; -typedef std::vector tr1_obj3_ptr_vec; -typedef std::vector tr1_obj5_ptr_vec; - -#pragma db object pointer(tr1_obj1_ptr) -struct tr1_obj1 -{ - #pragma db id - std::string id; - - tr1_obj2_ptr o2; - - #pragma db id_column("tr1_obj1_id") value_column("tr1_obj3_id") - tr1_obj3_ptr_vec o3; - - tr1_obj4_ptr o4; - tr1_obj5_ptr_vec o5; -}; - -#pragma db object pointer(tr1_obj2_ptr) -struct tr1_obj2 -{ - #pragma db id auto - int id; - - std::string str; - - // one(i)-to-one - // - #pragma db inverse(o2) - tr1_obj1_wptr o1; -}; +#if defined(HAVE_CXX11) || defined(HAVE_TR1_MEMORY) -#pragma db object pointer(tr1_obj3_ptr) -struct tr1_obj3 +#pragma db namespace table("t2_") +namespace test2 { - #pragma db id auto - int id; - - std::string str; - - // one(i)-to-many - // - #pragma db inverse (o3) - tr1_obj1_wptr o1; -}; - -#pragma db object pointer(tr1_obj4_ptr) -struct tr1_obj4 -{ - #pragma db id auto - int id; - - std::string str; - - // many(i)-to-one - // - #pragma db inverse (o4) - tr1_obj1_wptr_vec o1; -}; - -#pragma db object pointer(tr1_obj5_ptr) -struct tr1_obj5 -{ - #pragma db id auto - int id; - - std::string str; +#ifdef HAVE_CXX11 + using std::shared_ptr; + using std::weak_ptr; +#else + using std::tr1::shared_ptr; + using std::tr1::weak_ptr; +#endif - // many(i)-to-many - // - #pragma db inverse (o5) - tr1_obj1_wptr_vec o1; -}; + struct obj1; + struct obj2; + struct obj3; + struct obj4; + struct obj5; + + typedef shared_ptr obj1_ptr; + typedef shared_ptr obj2_ptr; + typedef shared_ptr obj3_ptr; + typedef shared_ptr obj4_ptr; + typedef shared_ptr obj5_ptr; + + typedef weak_ptr obj1_wptr; + + typedef std::vector obj1_wptr_vec; + typedef std::vector obj3_ptr_vec; + typedef std::vector obj5_ptr_vec; + + #pragma db object pointer(obj1_ptr) + struct obj1 + { + #pragma db id + std::string id; + + obj2_ptr o2; + + #pragma db id_column("obj1_id") value_column("obj3_id") + obj3_ptr_vec o3; + + obj4_ptr o4; + obj5_ptr_vec o5; + }; + + #pragma db object pointer(obj2_ptr) + struct obj2 + { + #pragma db id auto + int id; + + std::string str; + + // one(i)-to-one + // + #pragma db inverse(o2) + obj1_wptr o1; + }; + + #pragma db object pointer(obj3_ptr) + struct obj3 + { + #pragma db id auto + int id; + + std::string str; + + // one(i)-to-many + // + #pragma db inverse (o3) + obj1_wptr o1; + }; + + #pragma db object pointer(obj4_ptr) + struct obj4 + { + #pragma db id auto + int id; + + std::string str; + + // many(i)-to-one + // + #pragma db inverse (o4) + obj1_wptr_vec o1; + }; + + #pragma db object pointer(obj5_ptr) + struct obj5 + { + #pragma db id auto + int id; + + std::string str; + + // many(i)-to-many + // + #pragma db inverse (o5) + obj1_wptr_vec o1; + }; +} #endif #endif // TEST_HXX diff --git a/common/lazy-ptr/driver.cxx b/common/lazy-ptr/driver.cxx index 8065e2c..3e031d3 100644 --- a/common/lazy-ptr/driver.cxx +++ b/common/lazy-ptr/driver.cxx @@ -6,6 +6,7 @@ // #include // std::auto_ptr +#include // std::move #include #include @@ -113,8 +114,13 @@ main (int argc, char* argv[]) // Correct object ids. // +#ifdef HAVE_CXX11 + assert (c->o[0].object_id () == o->id); + assert (o->c.object_id () == c->id); +#else assert (c->o[0].object_id () == o->id); assert (o->c.object_id () == c->id); +#endif // Load. // @@ -185,8 +191,13 @@ main (int argc, char* argv[]) // Correct object ids. // +#ifdef HAVE_CXX11 + assert (c->o.object_id () == o->id); + assert (o->c.object_id () == c->id); +#else assert (c->o.object_id () == o->id); assert (o->c.object_id () == c->id); +#endif // Load. // @@ -218,12 +229,11 @@ main (int argc, char* argv[]) } } - // TR1. + // Shared pointer from C++11 or TR1. // -#ifdef HAVE_TR1_MEMORY +#if defined(HAVE_CXX11) || defined(HAVE_TR1_MEMORY) { - using namespace ::tr1; - using std::tr1::shared_ptr; + using namespace shared; // persist // @@ -258,6 +268,22 @@ main (int argc, char* argv[]) assert (lc1 == lazy_shared_ptr (*db, c1)); assert (lc1 != lazy_shared_ptr (*db, c2)); + // Test move constructors. + // +#ifdef HAVE_CXX11 + { + lazy_shared_ptr tmp (*db, 1); + lazy_shared_ptr l (std::move (tmp)); + assert (lc1 == l); + } + + { + shared_ptr tmp (c1); + lazy_shared_ptr l (*db, std::move (tmp)); + assert (lc1 == l); + } +#endif + { transaction t (db->begin ()); @@ -289,8 +315,13 @@ main (int argc, char* argv[]) // Correct object ids. // +#ifdef HAVE_CXX11 + assert (c->o[0].object_id () == o->id); + assert (o->c.object_id () == c->id); +#else assert (c->o[0].object_id () == o->id); assert (o->c.object_id () == c->id); +#endif // Load. // @@ -315,7 +346,12 @@ main (int argc, char* argv[]) assert (!c->o[1].loaded ()); lazy_shared_ptr l (c->o[1].lock ()); assert (!l.loaded ()); + +#ifdef HAVE_CXX11 + assert (l.object_id () == c->o[1].object_id ()); +#else assert (l.object_id () == c->o[1].object_id ()); +#endif // Reload. // diff --git a/common/lazy-ptr/test.hxx b/common/lazy-ptr/test.hxx index f9362f8..0adddf8 100644 --- a/common/lazy-ptr/test.hxx +++ b/common/lazy-ptr/test.hxx @@ -5,7 +5,7 @@ #ifndef TEST_HXX #define TEST_HXX -#include // HAVE_TR1_MEMORY +#include // HAVE_CXX11, HAVE_TR1_MEMORY #include #include @@ -14,7 +14,7 @@ #include #include -#ifdef HAVE_TR1_MEMORY +#if !defined(HAVE_CXX11) && defined(HAVE_TR1_MEMORY) # include # include #endif @@ -59,8 +59,8 @@ inline cont1:: ~cont1 () { for (obj_list::iterator i (o.begin ()); i != o.end (); ++i) - if (i->loaded ()) - delete i->get (); + if (obj1* p = i->get ()) + delete p; } // Auto pointer. @@ -98,14 +98,20 @@ public: lazy_ptr c; // weak }; -// TR1. +// shared_ptr // -#ifdef HAVE_TR1_MEMORY -namespace tr1 +#if defined(HAVE_CXX11) || defined(HAVE_TR1_MEMORY) +namespace shared { +#ifdef HAVE_CXX11 + using std::shared_ptr; + using odb::lazy_shared_ptr; + using odb::lazy_weak_ptr; +#else using std::tr1::shared_ptr; using odb::tr1::lazy_shared_ptr; using odb::tr1::lazy_weak_ptr; +#endif class obj; diff --git a/common/relationship-query/driver.cxx b/common/relationship-query/driver.cxx index 56d1f14..58bbef5 100644 --- a/common/relationship-query/driver.cxx +++ b/common/relationship-query/driver.cxx @@ -13,7 +13,7 @@ #include #include -#include // HAVE_TR1_MEMORY +#include // HAVE_CXX11, HAVE_TR1_MEMORY #include #include "test.hxx" @@ -29,9 +29,7 @@ main (int argc, char* argv[]) { auto_ptr db (create_database (argc, argv)); -#ifdef HAVE_TR1_MEMORY - - using std::tr1::shared_ptr; +#if defined(HAVE_CXX11) || defined(HAVE_TR1_MEMORY) // // @@ -164,7 +162,8 @@ main (int argc, char* argv[]) t.commit (); } -#endif +#endif // HAVE_CXX11 || HAVE_TR1_MEMORY + } catch (const odb::exception& e) { diff --git a/common/relationship-query/test.hxx b/common/relationship-query/test.hxx index 5168ac4..c2e167c 100644 --- a/common/relationship-query/test.hxx +++ b/common/relationship-query/test.hxx @@ -5,21 +5,28 @@ #ifndef TEST_HXX #define TEST_HXX -#include // HAVE_TR1_MEMORY +#include // HAVE_CXX11, HAVE_TR1_MEMORY -#ifdef HAVE_TR1_MEMORY +#if defined(HAVE_CXX11) || defined(HAVE_TR1_MEMORY) #include #include -#include + +#ifdef HAVE_CXX11 +# include +using std::shared_ptr; +#else +# include +using std::tr1::shared_ptr; +#endif struct country; #pragma db value struct residence_info { - residence_info (bool p, std::tr1::shared_ptr l) + residence_info (bool p, shared_ptr l) : permanent (p), location (l) { } @@ -31,19 +38,19 @@ struct residence_info bool permanent; #pragma db not_null - std::tr1::shared_ptr location; + shared_ptr location; }; -#pragma db object pointer(std::tr1::shared_ptr) +#pragma db object pointer(shared_ptr) struct person { person (unsigned long i, const std::string& fn, const std::string& ln, unsigned short a, - std::tr1::shared_ptr r, + shared_ptr r, bool p, - std::tr1::shared_ptr n) + shared_ptr n) : id (i), first_name (fn), last_name (ln), @@ -71,24 +78,24 @@ struct person residence_info residence; #pragma db not_null - std::tr1::shared_ptr nationality; + shared_ptr nationality; - std::tr1::shared_ptr husband; // Self-join. + shared_ptr husband; // Self-join. }; struct employer; -#pragma db object pointer(std::tr1::shared_ptr) +#pragma db object pointer(shared_ptr) struct employee: person { employee (unsigned long i, const std::string& fn, const std::string& ln, unsigned short a, - std::tr1::shared_ptr r, + shared_ptr r, bool p, - std::tr1::shared_ptr n, - std::tr1::shared_ptr e) + shared_ptr n, + shared_ptr e) : person (i, fn, ln, a, r, p, n), employed_by (e) { @@ -98,10 +105,10 @@ struct employee: person { } - std::tr1::shared_ptr employed_by; + shared_ptr employed_by; }; -#pragma db object pointer(std::tr1::shared_ptr) +#pragma db object pointer(shared_ptr) struct employer { employer (const std::string& n) @@ -117,7 +124,7 @@ struct employer std::string name; }; -#pragma db object pointer(std::tr1::shared_ptr) +#pragma db object pointer(shared_ptr) struct country { country (const std::string& c, std::string const& n) @@ -135,5 +142,5 @@ struct country std::string name; }; -#endif // HAVE_TR1_MEMORY +#endif // HAVE_CXX11 || HAVE_TR1_MEMORY #endif // TEST_HXX diff --git a/common/relationship/driver.cxx b/common/relationship/driver.cxx index 879b123..20e0f0c 100644 --- a/common/relationship/driver.cxx +++ b/common/relationship/driver.cxx @@ -30,7 +30,7 @@ main (int argc, char* argv[]) aggr a ("aggr"); a.o1 = new obj1 ("o1", "obj1"); a.o2.reset (new obj2 ("obj2")); -#ifdef HAVE_TR1_MEMORY +#if defined(HAVE_CXX11) || defined(HAVE_TR1_MEMORY) a.o3.reset (new obj3 ("obj3")); a.c.num = 123; @@ -60,7 +60,7 @@ main (int argc, char* argv[]) transaction t (db->begin ()); db->persist (a.o1); db->persist (a.o2); -#ifdef HAVE_TR1_MEMORY +#if defined(HAVE_CXX11) || defined(HAVE_TR1_MEMORY) db->persist (a.o3); db->persist (a.c.o3); @@ -117,7 +117,7 @@ main (int argc, char* argv[]) delete a.o1; a.o1 = 0; a.o2.reset (); -#ifdef HAVE_TR1_MEMORY +#if defined(HAVE_CXX11) || defined(HAVE_TR1_MEMORY) a.o3.reset (); #endif diff --git a/common/relationship/test.hxx b/common/relationship/test.hxx index bd18747..8b40dba 100644 --- a/common/relationship/test.hxx +++ b/common/relationship/test.hxx @@ -5,7 +5,7 @@ #ifndef TEST_HXX #define TEST_HXX -#include // HAVE_TR1_MEMORY +#include // HAVE_CXX11, HAVE_TR1_MEMORY #include #include @@ -15,7 +15,7 @@ #include -#ifdef HAVE_TR1_MEMORY +#if !defined(HAVE_CXX11) && defined(HAVE_TR1_MEMORY) # include #endif @@ -138,11 +138,16 @@ operator== (const obj2& x, const obj2& y) return x.id == y.id && x.str == y.str; } -// tr1::shared_ptr +// shared_ptr // -#ifdef HAVE_TR1_MEMORY +#if defined(HAVE_CXX11) || defined(HAVE_TR1_MEMORY) struct obj3; + +#ifdef HAVE_CXX11 +typedef std::shared_ptr obj3_ptr; +#else typedef std::tr1::shared_ptr obj3_ptr; +#endif #pragma db object pointer(obj3_ptr) struct obj3 @@ -211,7 +216,7 @@ struct aggr obj1* o1; std::auto_ptr o2; -#ifdef HAVE_TR1_MEMORY +#if defined(HAVE_CXX11) || defined(HAVE_TR1_MEMORY) obj3_ptr o3; comp c; comp_vec cv; @@ -235,7 +240,7 @@ 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_TR1_MEMORY +#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 && x.cv == y.cv && -- cgit v1.1