From 68ca8ec3227d447c6898b7c75d8ea0fb82a4af36 Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Thu, 9 Dec 2010 10:36:15 +0200 Subject: Add lazy pointer support Built-in support is provided for raw, auto, and tr1 shared/weak pointers. New test: common/lazy-ptr. --- common/inverse/tr1-memory.hxx | 19 +-- common/lazy-ptr/driver.cxx | 341 +++++++++++++++++++++++++++++++++++++ common/lazy-ptr/makefile | 108 ++++++++++++ common/lazy-ptr/test.hxx | 144 ++++++++++++++++ common/lazy-ptr/test.std | 0 common/lazy-ptr/tr1-memory.hxx | 41 +++++ common/makefile | 1 + common/relationship/tr1-memory.hxx | 19 +-- 8 files changed, 651 insertions(+), 22 deletions(-) create mode 100644 common/lazy-ptr/driver.cxx create mode 100644 common/lazy-ptr/makefile create mode 100644 common/lazy-ptr/test.hxx create mode 100644 common/lazy-ptr/test.std create mode 100644 common/lazy-ptr/tr1-memory.hxx diff --git a/common/inverse/tr1-memory.hxx b/common/inverse/tr1-memory.hxx index 76ac932..44d369a 100644 --- a/common/inverse/tr1-memory.hxx +++ b/common/inverse/tr1-memory.hxx @@ -1,15 +1,16 @@ // file : common/inverse/tr1-memory.hxx // author : Boris Kolpackov -// copyright : Copyright (c) 2009-2010 Code Synthesis Tools CC -// license : GNU GPL v2; see accompanying LICENSE file +// copyright : not copyrighted - public domain #ifndef TR1_MEMORY_HXX #define TR1_MEMORY_HXX -// Try to include TR1 in a compiler-specific manner. Define -// HAVE_TR1_MEMORY if successfull. If the compiler does not provide -// native TR1 support, fall-back on the Boost TR1 implementation if -// HAVE_BOOST_TR1 is defined. +//@@ tmp +#define HAVE_TR1_MEMORY + +// +// Try to include TR1 in a compiler-specific manner. Fall-back +// on the Boost TR1 implementation if the compiler does not support TR1. // #include // __GLIBCXX__, _HAS_TR1 @@ -18,27 +19,23 @@ // #if defined (__GNUC__) && __GNUC__ >= 4 && defined (__GLIBCXX__) # include -# define HAVE_TR1_MEMORY 1 // // IBM XL C++. // #elif defined (__xlC__) && __xlC__ >= 0x0900 # define __IBMCPP_TR1__ # include -# define HAVE_TR1_MEMORY 1 // // VC++ or Intel C++ using VC++ standard library. // #elif defined (_MSC_VER) && \ (_MSC_VER == 1500 && defined (_HAS_TR1) || _MSC_VER > 1500) # include -# define HAVE_TR1_MEMORY 1 // // Boost fall-back. // -#elif defined (HAVE_BOOST_TR1) +#else # include -# define HAVE_TR1_MEMORY 1 #endif #endif // TR1_MEMORY_HXX diff --git a/common/lazy-ptr/driver.cxx b/common/lazy-ptr/driver.cxx new file mode 100644 index 0000000..eceb515 --- /dev/null +++ b/common/lazy-ptr/driver.cxx @@ -0,0 +1,341 @@ +// file : common/lazy-ptr/driver.cxx +// author : Boris Kolpackov +// copyright : Copyright (c) 2009-2010 Code Synthesis Tools CC +// license : GNU GPL v2; see accompanying LICENSE file + +// Test lazy object pointers. +// + +#include // std::auto_ptr +#include +#include + +#include +#include + +#include + +#include "test.hxx" +#include "test-odb.hxx" + +using namespace std; +using namespace odb; + +auto_ptr +create (unsigned int id) +{ + auto_ptr r (new obj2 (id)); + return r; +} + +lazy_auto_ptr +create (database& db, unsigned int id) +{ + lazy_auto_ptr r (db, id); + return r; +} + +int +main (int argc, char* argv[]) +{ + try + { + auto_ptr db (create_database (argc, argv)); + + // Naked. + // + { + typedef cont1 cont; + typedef obj1 obj; + + // persist + // + obj* 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)); + + lazy_ptr lo1 (*db, 1); + obj* o2 (new obj (2)); + + obj* o3 (new obj (3)); + obj* o4 (new obj (4)); + + c1->o.push_back (lo1); + c1->o.push_back (o2); + c2->o.push_back (o3); + c2->o.push_back (o4); + + // Test pointer comparison. + // + assert (lazy_ptr () == lazy_ptr ()); + assert (lazy_ptr (o1) != lazy_ptr ()); + assert (lo1 != lazy_ptr ()); + assert (lazy_ptr (o1) == lazy_ptr (o1)); + assert (lo1 == lazy_ptr (*db, o1)); + assert (lo1 != lazy_ptr (*db, o2)); + + delete o1; + + { + transaction t (db->begin ()); + + db->persist (o2); + db->persist (o3); + db->persist (o4); + + db->persist (*c1); + db->persist (*c2); + + t.commit (); + } + + // load + // + { + session s; + transaction t (db->begin ()); + auto_ptr c (db->load (1)); + obj* o (db->load (1)); + + // Not loaded. + // + assert (c->o.size () == 2); + assert (!c->o[0].loaded ()); + assert (!c->o[1].loaded ()); + + assert (!o->c.loaded ()); + + // Correct object ids. + // + assert (c->o[0].object_id () == o->id); + assert (o->c.object_id () == c->id); + + // Load. + // + cont* lc (o->c.load ()); + obj* lo (c->o[0].load ()); + + assert (lc == c.get ()); + assert (lo == o); + + // Test unload/reload. + // + o->c.unload (); + assert (!o->c.loaded ()); + o->c.load (); + assert (o->c.loaded ()); + + t.commit (); + } + } + + // Auto pointer. + // + { + typedef cont2 cont; + typedef obj2 obj; + + // persist + // + { + auto_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)); + + lazy_auto_ptr lo1 = create (*db, 1); + lo1 = create (*db, 1); + + c1->o = lo1; + c2->o = create (2); + + { + transaction t (db->begin ()); + + db->persist (*c2->o); + + db->persist (*c1); + db->persist (*c2); + + t.commit (); + } + + // load + // + { + session s; + transaction t (db->begin ()); + auto_ptr c (db->load (1)); + obj* o (db->load (1)); + + // Not loaded. + // + assert (!c->o.loaded ()); + assert (!o->c.loaded ()); + + // Correct object ids. + // + assert (c->o.object_id () == o->id); + assert (o->c.object_id () == c->id); + + // Load. + // + cont* lc (o->c.load ()); + const auto_ptr& lo (c->o.load ()); + + assert (lc == c.get ()); + assert (lo.get () == o); + + t.commit (); + } + + // unload/reload + // + { + // No session. + transaction t (db->begin ()); + auto_ptr c (db->load (1)); + + assert (!c->o.loaded ()); + c->o.load (); + assert (c->o.loaded ()); + c->o.unload (); + assert (!c->o.loaded ()); + c->o.load (); + assert (c->o.loaded ()); + + t.commit (); + } + + } + + // TR1. + // +#ifdef HAVE_TR1_MEMORY + { + using namespace ::tr1; + + // persist + // + shared_ptr c1 (new cont (1)); + + { + transaction t (db->begin ()); + db->persist (c1); + t.commit (); + } + + lazy_shared_ptr lc1 (*db, 1); + shared_ptr c2 (new cont (2)); + + shared_ptr o1 (new obj (1)); + shared_ptr o2 (new obj (2)); + + shared_ptr o3 (new obj (3)); + shared_ptr o4 (new obj (4)); + + o1->c = lc1; + o2->c = lc1; + o3->c = c2; + o4->c = c2; + + // Test pointer comparison. + // + assert (lazy_shared_ptr () == lazy_shared_ptr ()); + assert (lazy_shared_ptr (c1) != lazy_shared_ptr ()); + assert (lc1 != lazy_shared_ptr ()); + assert (lazy_shared_ptr (c1) == lazy_shared_ptr (c1)); + assert (lc1 == lazy_shared_ptr (*db, c1)); + assert (lc1 != lazy_shared_ptr (*db, c2)); + + { + transaction t (db->begin ()); + + db->persist (o1); + db->persist (o2); + db->persist (o3); + db->persist (o4); + + db->persist (c2); + + t.commit (); + } + + // load + // + { + session s; + transaction t (db->begin ()); + shared_ptr c (db->load (1)); + shared_ptr o (db->load (1)); + + // Not loaded. + // + assert (c->o.size () == 2); + assert (!c->o[0].loaded ()); + assert (!c->o[1].loaded ()); + + assert (!o->c.loaded ()); + + // Correct object ids. + // + assert (c->o[0].object_id () == o->id); + assert (o->c.object_id () == c->id); + + // Load. + // + shared_ptr lc (o->c.load ()); + shared_ptr lo (c->o[0].load ()); + + assert (lc == c); + assert (lo == o); + + t.commit (); + } + + // Test lazy weak locking and reloading. + // + { + // No session. + transaction t (db->begin ()); + shared_ptr c (db->load (1)); + + // Lock. + // + assert (!c->o[1].loaded ()); + lazy_shared_ptr l (c->o[1].lock ()); + assert (!l.loaded ()); + assert (l.object_id () == c->o[1].object_id ()); + + // Reload. + // + assert (!c->o[1].loaded ()); + shared_ptr lo (c->o[1].load ()); + assert (c->o[1].loaded ()); + lo.reset (); + assert (!c->o[1].loaded ()); + lo = c->o[1].load (); + assert (c->o[1].loaded ()); + + t.commit (); + } + } +#endif + } + catch (const odb::exception& e) + { + cerr << e.what () << endl; + return 1; + } +} diff --git a/common/lazy-ptr/makefile b/common/lazy-ptr/makefile new file mode 100644 index 0000000..78fe24b --- /dev/null +++ b/common/lazy-ptr/makefile @@ -0,0 +1,108 @@ +# file : common/lazy-ptr/makefile +# author : Boris Kolpackov +# copyright : Copyright (c) 2009-2010 Code Synthesis Tools CC +# license : GNU GPL v2; see accompanying LICENSE file + +include $(dir $(lastword $(MAKEFILE_LIST)))../../build/bootstrap.make + +cxx_tun := driver.cxx +odb_hdr := test.hxx +cxx_obj := $(addprefix $(out_base)/,$(cxx_tun:.cxx=.o) $(odb_hdr:.hxx=-odb.o)) +cxx_od := $(cxx_obj:.o=.o.d) + +common.l := $(out_root)/libcommon/common/common.l +common.l.cpp-options := $(out_root)/libcommon/common/common.l.cpp-options + +driver := $(out_base)/driver +dist := $(out_base)/.dist +test := $(out_base)/.test +clean := $(out_base)/.clean + +# Import. +# +$(call import,\ + $(scf_root)/import/odb/stub.make,\ + odb: odb,odb-rules: odb_rules) + +# Build. +# +$(driver): $(cxx_obj) $(common.l) +$(cxx_obj) $(cxx_od): cpp_options := -I$(out_base) +$(cxx_obj) $(cxx_od): $(common.l.cpp-options) + +genf := $(addprefix $(odb_hdr:.hxx=-odb),.hxx .ixx .cxx) $(odb_hdr:.hxx=.sql) +gen := $(addprefix $(out_base)/,$(genf)) + +$(gen): $(odb) +$(gen): odb := $(odb) +$(gen) $(dist): export odb_options += --database $(db_id) --generate-schema +$(gen): cpp_options := -I$(out_base) +$(gen): $(common.l.cpp-options) + +$(call include-dep,$(cxx_od),$(cxx_obj),$(gen)) + +# Alias for default target. +# +$(out_base)/: $(driver) + +# Dist +# +name := $(notdir $(src_base)) + +$(dist): db_id := @database@ +$(dist): sources := $(cxx_tun) +$(dist): headers := $(odb_hdr) +$(dist): data_dist := test.std +$(dist): export name := $(name) +$(dist): export extra_dist := $(data_dist) $(call vc9projs,$(name)) \ +$(call vc10projs,$(name)) +$(dist): + $(call dist-data,$(sources) $(headers) $(data_dist)) + $(call meta-automake,../template/Makefile.am) + $(call meta-vc9projs,../template/template,$(name)) + $(call meta-vc10projs,../template/template,$(name)) + +# Test. +# +$(test): $(driver) $(src_base)/test.std + $(call message,sql $$1,$(dcf_root)/db-driver $$1, $(src_base)/test.sql) + $(call message,test $<,$< --options-file $(dcf_root)/db.options \ +>$(out_base)/test.out) + $(call message,,diff -u $(src_base)/test.std $(out_base)/test.out) + $(call message,,rm -f $(out_base)/test.out) + +# Clean. +# +$(clean): \ + $(driver).o.clean \ + $(addsuffix .cxx.clean,$(cxx_obj)) \ + $(addsuffix .cxx.clean,$(cxx_od)) \ + $(addprefix $(out_base)/,$(odb_hdr:.hxx=-odb.cxx.hxx.clean)) + $(call message,,rm -f $(out_base)/test.out) + +# Generated .gitignore. +# +ifeq ($(out_base),$(src_base)) +$(driver): | $(out_base)/.gitignore + +$(out_base)/.gitignore: files := driver $(genf) +$(clean): $(out_base)/.gitignore.clean + +$(call include,$(bld_root)/git/gitignore.make) +endif + +# How to. +# +$(call include,$(bld_root)/dist.make) +$(call include,$(bld_root)/meta/vc9proj.make) +$(call include,$(bld_root)/meta/vc10proj.make) +$(call include,$(bld_root)/meta/automake.make) + +$(call include,$(odb_rules)) +$(call include,$(bld_root)/cxx/cxx-d.make) +$(call include,$(bld_root)/cxx/cxx-o.make) +$(call include,$(bld_root)/cxx/o-e.make) + +# Dependencies. +# +$(call import,$(src_root)/libcommon/makefile) diff --git a/common/lazy-ptr/test.hxx b/common/lazy-ptr/test.hxx new file mode 100644 index 0000000..4aa8b3a --- /dev/null +++ b/common/lazy-ptr/test.hxx @@ -0,0 +1,144 @@ +// file : common/lazy-ptr/test.hxx +// author : Boris Kolpackov +// copyright : Copyright (c) 2009-2010 Code Synthesis Tools CC +// license : GNU GPL v2; see accompanying LICENSE file + +#ifndef TEST_HXX +#define TEST_HXX + +#include +#include +#include + +#include "tr1-memory.hxx" + +#include +#include + +#ifdef HAVE_TR1_MEMORY +#include +#endif + +// Naked pointer. +// +using odb::lazy_ptr; +class obj1; + +#pragma db object +class cont1 +{ +public: + cont1 () {} + cont1 (unsigned long i): id (i) {} + ~cont1 (); + + #pragma db id + unsigned long id; + + typedef std::vector > obj_list; + + #pragma db not_null + obj_list o; +}; + +#pragma db object +class obj1 +{ +public: + obj1 () {} + obj1 (unsigned long i): id (i) {} + + #pragma db id + unsigned long id; + + #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 (i->loaded ()) + delete i->get (); +} + +// Auto pointer. +// +using std::auto_ptr; +using odb::lazy_auto_ptr; + +class obj2; + +#pragma db object +class cont2 +{ +public: + cont2 () {} + cont2 (unsigned long i): id (i) {} + + #pragma db id + unsigned long id; + + #pragma db not_null + lazy_auto_ptr o; +}; + +#pragma db object +class obj2 +{ +public: + obj2 () {} + obj2 (unsigned long i): id (i) {} + + #pragma db id + unsigned long id; + + #pragma db inverse(o) not_null + lazy_ptr c; // weak +}; + +// TR1. +// +#ifdef HAVE_TR1_MEMORY +namespace tr1 +{ + using std::tr1::shared_ptr; + using odb::tr1::lazy_shared_ptr; + using odb::tr1::lazy_weak_ptr; + + class obj; + + #pragma db object pointer(std::tr1::shared_ptr) // @@ tmp std::tr1:: + class cont + { + public: + cont () {} + cont (unsigned long i): id (i) {} + + #pragma db id + unsigned long id; + + typedef std::vector > obj_list; + + #pragma db inverse(c) not_null + obj_list o; + }; + + #pragma db object pointer(std::tr1::shared_ptr) // @@ tmp std::tr1:: + class obj + { + public: + obj () {} + obj (unsigned long i): id (i) {} + + #pragma db id + unsigned long id; + + #pragma db not_null + lazy_shared_ptr c; + }; +} +#endif + +#endif // TEST_HXX diff --git a/common/lazy-ptr/test.std b/common/lazy-ptr/test.std new file mode 100644 index 0000000..e69de29 diff --git a/common/lazy-ptr/tr1-memory.hxx b/common/lazy-ptr/tr1-memory.hxx new file mode 100644 index 0000000..8965861 --- /dev/null +++ b/common/lazy-ptr/tr1-memory.hxx @@ -0,0 +1,41 @@ +// file : common/lazy-ptr/tr1-memory.hxx +// author : Boris Kolpackov +// copyright : not copyrighted - public domain + +#ifndef TR1_MEMORY_HXX +#define TR1_MEMORY_HXX + +//@@ tmp +#define HAVE_TR1_MEMORY + +// +// Try to include TR1 in a compiler-specific manner. Fall-back +// on the Boost TR1 implementation if the compiler does not support TR1. +// + +#include // __GLIBCXX__, _HAS_TR1 + +// GNU C++ or Intel C++ using libstd++. +// +#if defined (__GNUC__) && __GNUC__ >= 4 && defined (__GLIBCXX__) +# include +// +// IBM XL C++. +// +#elif defined (__xlC__) && __xlC__ >= 0x0900 +# define __IBMCPP_TR1__ +# include +// +// VC++ or Intel C++ using VC++ standard library. +// +#elif defined (_MSC_VER) && \ + (_MSC_VER == 1500 && defined (_HAS_TR1) || _MSC_VER > 1500) +# include +// +// Boost fall-back. +// +#else +# include +#endif + +#endif // TR1_MEMORY_HXX diff --git a/common/makefile b/common/makefile index 5794b15..6d95d8b 100644 --- a/common/makefile +++ b/common/makefile @@ -12,6 +12,7 @@ const \ container \ ctor \ inverse \ +lazy-ptr \ lifecycle \ query \ relationship \ diff --git a/common/relationship/tr1-memory.hxx b/common/relationship/tr1-memory.hxx index 27e9da8..a4ce28a 100644 --- a/common/relationship/tr1-memory.hxx +++ b/common/relationship/tr1-memory.hxx @@ -1,15 +1,16 @@ // file : common/relationship/tr1-memory.hxx // author : Boris Kolpackov -// copyright : Copyright (c) 2009-2010 Code Synthesis Tools CC -// license : GNU GPL v2; see accompanying LICENSE file +// copyright : not copyrighted - public domain #ifndef TR1_MEMORY_HXX #define TR1_MEMORY_HXX -// Try to include TR1 in a compiler-specific manner. Define -// HAVE_TR1_MEMORY if successfull. If the compiler does not provide -// native TR1 support, fall-back on the Boost TR1 implementation if -// HAVE_BOOST_TR1 is defined. +//@@ tmp +#define HAVE_TR1_MEMORY + +// +// Try to include TR1 in a compiler-specific manner. Fall-back +// on the Boost TR1 implementation if the compiler does not support TR1. // #include // __GLIBCXX__, _HAS_TR1 @@ -18,27 +19,23 @@ // #if defined (__GNUC__) && __GNUC__ >= 4 && defined (__GLIBCXX__) # include -# define HAVE_TR1_MEMORY 1 // // IBM XL C++. // #elif defined (__xlC__) && __xlC__ >= 0x0900 # define __IBMCPP_TR1__ # include -# define HAVE_TR1_MEMORY 1 // // VC++ or Intel C++ using VC++ standard library. // #elif defined (_MSC_VER) && \ (_MSC_VER == 1500 && defined (_HAS_TR1) || _MSC_VER > 1500) # include -# define HAVE_TR1_MEMORY 1 // // Boost fall-back. // -#elif defined (HAVE_BOOST_TR1) +#else # include -# define HAVE_TR1_MEMORY 1 #endif #endif // TR1_MEMORY_HXX -- cgit v1.1