diff options
Diffstat (limited to 'common/lazy-ptr')
-rw-r--r-- | common/lazy-ptr/driver.cxx | 51 | ||||
-rw-r--r-- | common/lazy-ptr/test.hxx | 137 |
2 files changed, 106 insertions, 82 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<obj2> -create (unsigned int id) +namespace test2 { - auto_ptr<obj2> r (new obj2 (id)); - return r; -} + obj_ptr + create (unsigned int id) + { + obj_ptr r (new obj (id)); + return r; + } -lazy_auto_ptr<obj2> -create (database& db, unsigned int id) -{ - lazy_auto_ptr<obj2> 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<obj> o1 (new obj (1)); + obj_ptr o1 (new obj (1)); transaction t (db->begin ()); db->persist (*o1); t.commit (); } - auto_ptr<cont> c1 (new cont (1)); - auto_ptr<cont> c2 (new cont (2)); + cont_ptr c1 (new cont (1)); + cont_ptr c2 (new cont (2)); - lazy_auto_ptr<obj> 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<cont> c (db->load<cont> (1)); + cont_ptr c (db->load<cont> (1)); obj* o (db->load<obj> (1)); // Not loaded. @@ -202,7 +207,7 @@ main (int argc, char* argv[]) // Load. // cont* cl (o->c.load ()); - const auto_ptr<obj>& 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<cont> c (db->load<cont> (1)); + cont_ptr c (db->load<cont> (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<lazy_ptr<obj1> > obj_list; + #pragma db id + unsigned long id; - #pragma db value_not_null - obj_list o; -}; + typedef std::vector<lazy_ptr<obj> > 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<cont1> c; // weak -}; + #pragma db inverse(o) not_null + lazy_ptr<cont> 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> obj_ptr; + typedef std::unique_ptr<cont> cont_ptr; + typedef odb::lazy_unique_ptr<obj> lazy_obj_ptr; +#else + typedef std::auto_ptr<obj> obj_ptr; + typedef std::auto_ptr<cont> cont_ptr; + typedef odb::lazy_auto_ptr<obj> 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<obj2> 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<cont2> c; // weak -}; + #pragma db inverse(o) not_null + lazy_ptr<cont> 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; |