aboutsummaryrefslogtreecommitdiff
path: root/common
diff options
context:
space:
mode:
authorBoris Kolpackov <boris@codesynthesis.com>2010-12-09 10:36:15 +0200
committerBoris Kolpackov <boris@codesynthesis.com>2010-12-09 10:36:15 +0200
commit68ca8ec3227d447c6898b7c75d8ea0fb82a4af36 (patch)
tree93d2afa8e23238a87b0b2eb2f702e1910a11fb6b /common
parent0b4c59824e3b2b0411dd33835c67f6cd36d60a91 (diff)
Add lazy pointer support
Built-in support is provided for raw, auto, and tr1 shared/weak pointers. New test: common/lazy-ptr.
Diffstat (limited to 'common')
-rw-r--r--common/inverse/tr1-memory.hxx19
-rw-r--r--common/lazy-ptr/driver.cxx341
-rw-r--r--common/lazy-ptr/makefile108
-rw-r--r--common/lazy-ptr/test.hxx144
-rw-r--r--common/lazy-ptr/test.std0
-rw-r--r--common/lazy-ptr/tr1-memory.hxx41
-rw-r--r--common/makefile1
-rw-r--r--common/relationship/tr1-memory.hxx19
8 files changed, 651 insertions, 22 deletions
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 <boris@codesynthesis.com>
-// 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 <memory> 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 <memory> in a compiler-specific manner. Fall-back
+// on the Boost TR1 implementation if the compiler does not support TR1.
//
#include <cstddef> // __GLIBCXX__, _HAS_TR1
@@ -18,27 +19,23 @@
//
#if defined (__GNUC__) && __GNUC__ >= 4 && defined (__GLIBCXX__)
# include <tr1/memory>
-# define HAVE_TR1_MEMORY 1
//
// IBM XL C++.
//
#elif defined (__xlC__) && __xlC__ >= 0x0900
# define __IBMCPP_TR1__
# include <memory>
-# 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 <memory>
-# define HAVE_TR1_MEMORY 1
//
// Boost fall-back.
//
-#elif defined (HAVE_BOOST_TR1)
+#else
# include <boost/tr1/memory.hpp>
-# 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 <boris@codesynthesis.com>
+// copyright : Copyright (c) 2009-2010 Code Synthesis Tools CC
+// license : GNU GPL v2; see accompanying LICENSE file
+
+// Test lazy object pointers.
+//
+
+#include <memory> // std::auto_ptr
+#include <cassert>
+#include <iostream>
+
+#include <odb/database.hxx>
+#include <odb/transaction.hxx>
+
+#include <common/common.hxx>
+
+#include "test.hxx"
+#include "test-odb.hxx"
+
+using namespace std;
+using namespace odb;
+
+auto_ptr<obj2>
+create (unsigned int id)
+{
+ auto_ptr<obj2> r (new obj2 (id));
+ return r;
+}
+
+lazy_auto_ptr<obj2>
+create (database& db, unsigned int id)
+{
+ lazy_auto_ptr<obj2> r (db, id);
+ return r;
+}
+
+int
+main (int argc, char* argv[])
+{
+ try
+ {
+ auto_ptr<database> 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<cont> c1 (new cont (1));
+ auto_ptr<cont> c2 (new cont (2));
+
+ lazy_ptr<obj> 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<obj> () == lazy_ptr<obj> ());
+ assert (lazy_ptr<obj> (o1) != lazy_ptr<obj> ());
+ assert (lo1 != lazy_ptr<obj> ());
+ assert (lazy_ptr<obj> (o1) == lazy_ptr<obj> (o1));
+ assert (lo1 == lazy_ptr<obj> (*db, o1));
+ assert (lo1 != lazy_ptr<obj> (*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<cont> c (db->load<cont> (1));
+ obj* o (db->load<obj> (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<obj> () == o->id);
+ assert (o->c.object_id<cont> () == 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<obj> 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));
+
+ lazy_auto_ptr<obj> 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<cont> c (db->load<cont> (1));
+ obj* o (db->load<obj> (1));
+
+ // Not loaded.
+ //
+ assert (!c->o.loaded ());
+ assert (!o->c.loaded ());
+
+ // Correct object ids.
+ //
+ assert (c->o.object_id<obj> () == o->id);
+ assert (o->c.object_id<cont> () == c->id);
+
+ // Load.
+ //
+ cont* lc (o->c.load ());
+ const auto_ptr<obj>& lo (c->o.load ());
+
+ assert (lc == c.get ());
+ assert (lo.get () == o);
+
+ t.commit ();
+ }
+
+ // unload/reload
+ //
+ {
+ // No session.
+ transaction t (db->begin ());
+ auto_ptr<cont> c (db->load<cont> (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<cont> c1 (new cont (1));
+
+ {
+ transaction t (db->begin ());
+ db->persist (c1);
+ t.commit ();
+ }
+
+ lazy_shared_ptr<cont> lc1 (*db, 1);
+ shared_ptr<cont> c2 (new cont (2));
+
+ shared_ptr<obj> o1 (new obj (1));
+ shared_ptr<obj> o2 (new obj (2));
+
+ shared_ptr<obj> o3 (new obj (3));
+ shared_ptr<obj> o4 (new obj (4));
+
+ o1->c = lc1;
+ o2->c = lc1;
+ o3->c = c2;
+ o4->c = c2;
+
+ // Test pointer comparison.
+ //
+ assert (lazy_shared_ptr<cont> () == lazy_shared_ptr<cont> ());
+ assert (lazy_shared_ptr<cont> (c1) != lazy_shared_ptr<cont> ());
+ assert (lc1 != lazy_shared_ptr<cont> ());
+ assert (lazy_shared_ptr<cont> (c1) == lazy_shared_ptr<cont> (c1));
+ assert (lc1 == lazy_shared_ptr<cont> (*db, c1));
+ assert (lc1 != lazy_shared_ptr<cont> (*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<cont> c (db->load<cont> (1));
+ shared_ptr<obj> o (db->load<obj> (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<obj> () == o->id);
+ assert (o->c.object_id<cont> () == c->id);
+
+ // Load.
+ //
+ shared_ptr<cont> lc (o->c.load ());
+ shared_ptr<obj> 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<cont> c (db->load<cont> (1));
+
+ // Lock.
+ //
+ assert (!c->o[1].loaded ());
+ lazy_shared_ptr<obj> l (c->o[1].lock ());
+ assert (!l.loaded ());
+ assert (l.object_id<obj> () == c->o[1].object_id<obj> ());
+
+ // Reload.
+ //
+ assert (!c->o[1].loaded ());
+ shared_ptr<obj> 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 <boris@codesynthesis.com>
+# 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 <boris@codesynthesis.com>
+// copyright : Copyright (c) 2009-2010 Code Synthesis Tools CC
+// license : GNU GPL v2; see accompanying LICENSE file
+
+#ifndef TEST_HXX
+#define TEST_HXX
+
+#include <vector>
+#include <string>
+#include <memory>
+
+#include "tr1-memory.hxx"
+
+#include <odb/core.hxx>
+#include <odb/lazy-ptr.hxx>
+
+#ifdef HAVE_TR1_MEMORY
+#include <odb/tr1/lazy-ptr.hxx>
+#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<lazy_ptr<obj1> > 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<cont1> 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<obj2> 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<cont2> 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<cont>) // @@ tmp std::tr1::
+ class cont
+ {
+ public:
+ cont () {}
+ cont (unsigned long i): id (i) {}
+
+ #pragma db id
+ unsigned long id;
+
+ typedef std::vector<lazy_weak_ptr<obj> > obj_list;
+
+ #pragma db inverse(c) not_null
+ obj_list o;
+ };
+
+ #pragma db object pointer(std::tr1::shared_ptr<obj>) // @@ 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<cont> 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
--- /dev/null
+++ b/common/lazy-ptr/test.std
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 <boris@codesynthesis.com>
+// copyright : not copyrighted - public domain
+
+#ifndef TR1_MEMORY_HXX
+#define TR1_MEMORY_HXX
+
+//@@ tmp
+#define HAVE_TR1_MEMORY
+
+//
+// Try to include TR1 <memory> in a compiler-specific manner. Fall-back
+// on the Boost TR1 implementation if the compiler does not support TR1.
+//
+
+#include <cstddef> // __GLIBCXX__, _HAS_TR1
+
+// GNU C++ or Intel C++ using libstd++.
+//
+#if defined (__GNUC__) && __GNUC__ >= 4 && defined (__GLIBCXX__)
+# include <tr1/memory>
+//
+// IBM XL C++.
+//
+#elif defined (__xlC__) && __xlC__ >= 0x0900
+# define __IBMCPP_TR1__
+# include <memory>
+//
+// VC++ or Intel C++ using VC++ standard library.
+//
+#elif defined (_MSC_VER) && \
+ (_MSC_VER == 1500 && defined (_HAS_TR1) || _MSC_VER > 1500)
+# include <memory>
+//
+// Boost fall-back.
+//
+#else
+# include <boost/tr1/memory.hpp>
+#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 <boris@codesynthesis.com>
-// 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 <memory> 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 <memory> in a compiler-specific manner. Fall-back
+// on the Boost TR1 implementation if the compiler does not support TR1.
//
#include <cstddef> // __GLIBCXX__, _HAS_TR1
@@ -18,27 +19,23 @@
//
#if defined (__GNUC__) && __GNUC__ >= 4 && defined (__GLIBCXX__)
# include <tr1/memory>
-# define HAVE_TR1_MEMORY 1
//
// IBM XL C++.
//
#elif defined (__xlC__) && __xlC__ >= 0x0900
# define __IBMCPP_TR1__
# include <memory>
-# 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 <memory>
-# define HAVE_TR1_MEMORY 1
//
// Boost fall-back.
//
-#elif defined (HAVE_BOOST_TR1)
+#else
# include <boost/tr1/memory.hpp>
-# define HAVE_TR1_MEMORY 1
#endif
#endif // TR1_MEMORY_HXX