aboutsummaryrefslogtreecommitdiff
path: root/odb/pointer-traits.hxx
diff options
context:
space:
mode:
authorBoris Kolpackov <boris@codesynthesis.com>2010-07-20 11:02:07 +0200
committerBoris Kolpackov <boris@codesynthesis.com>2010-07-20 11:02:07 +0200
commit6400b736456af65176c9c1959022f1eb49fcde32 (patch)
treecdf457de88eda70b89d4cd98e83334e690c96ef1 /odb/pointer-traits.hxx
parent8b416ee938662901e1d75cf631c198cde17bb8c8 (diff)
Get rid of the session mechanism for now
Add low-level API instead.
Diffstat (limited to 'odb/pointer-traits.hxx')
-rw-r--r--odb/pointer-traits.hxx162
1 files changed, 162 insertions, 0 deletions
diff --git a/odb/pointer-traits.hxx b/odb/pointer-traits.hxx
new file mode 100644
index 0000000..a715a9d
--- /dev/null
+++ b/odb/pointer-traits.hxx
@@ -0,0 +1,162 @@
+// file : odb/pointer-traits.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 ODB_POINTER_TRAITS_HXX
+#define ODB_POINTER_TRAITS_HXX
+
+#include <new> // operators new/delete
+#include <cstddef> // std::size_t
+
+#include <odb/shared-ptr.hxx>
+
+namespace odb
+{
+ template <typename P>
+ class pointer_traits;
+
+ // Default implementation that should work for any sensible smart
+ // pointer with one template argument (object type). The only
+ // assumptions that we make are the availability of operator-> and
+ // operator*, and that the former does not throw if the pointer is
+ // NULL.
+ //
+ template <typename T, template <typename> class P>
+ class pointer_traits< P<T> >
+ {
+ public:
+ typedef T type;
+ typedef P<T> pointer;
+
+ // Return underlying pointer, including NULL.
+ //
+ static type*
+ get_ptr (const pointer& p)
+ {
+ return p.operator-> ();
+ }
+
+ // Return reference to the pointed-to object.
+ //
+ static type&
+ get_ref (const pointer& p)
+ {
+ return *p;
+ }
+
+ // Return true if the pointer is NULL.
+ //
+ static bool
+ null_ptr (const pointer& p)
+ {
+ return get_ptr () == 0;
+ }
+
+ public:
+ // Allocate memory for a shared object.
+ //
+ static void*
+ allocate (std::size_t n)
+ {
+ return operator new (n);
+ }
+
+ // Free memory allocated for a shared object. This functions is
+ // only called if the constructor of the object being created
+ // fails. Otherwise, pointer is used to delete the object
+ // and free the memory. This behavior is identical to the one
+ // used by operator delete overloading.
+ //
+ static void
+ free (void* p)
+ {
+ operator delete (p);
+ }
+ };
+
+ // Specialization for naked pointer.
+ //
+ template <typename T>
+ class pointer_traits<T*>
+ {
+ public:
+ typedef T type;
+ typedef T* pointer;
+
+ static type*
+ get_ptr (pointer p)
+ {
+ return p;
+ }
+
+ static type&
+ get_ref (pointer p)
+ {
+ return *p;
+ }
+
+ // Return true if the pointer is NULL.
+ //
+ static bool
+ null_ptr (pointer p)
+ {
+ return p == 0;
+ }
+
+ public:
+ static void*
+ allocate (std::size_t n)
+ {
+ return operator new (n);
+ }
+
+ static void
+ free (void* p)
+ {
+ operator delete (p);
+ }
+ };
+
+ // Specialization for odb::shared_ptr.
+ //
+ template <typename T>
+ class pointer_traits< shared_ptr<T> >
+ {
+ public:
+ typedef T type;
+ typedef odb::shared_ptr<T> pointer;
+
+ static type*
+ get_ptr (const pointer& p)
+ {
+ return p.get ();
+ }
+
+ static type&
+ get_ref (const pointer& p)
+ {
+ return *p;
+ }
+
+ static bool
+ null_ptr (const pointer& p)
+ {
+ return !p;
+ }
+
+ static void*
+ allocate (std::size_t n)
+ {
+ return operator new (n, shared);
+ }
+
+ static void
+ free (void* p)
+ {
+ operator delete (p, shared);
+ }
+ };
+}
+
+#endif // ODB_POINTER_TRAITS_HXX