From 6f0d40508286afc8cdd72a0b5f807d5c2a589cfc Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Fri, 4 Jun 2010 16:33:08 +0200 Subject: Initial implementation --- odb/traits.hxx | 93 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 93 insertions(+) create mode 100644 odb/traits.hxx (limited to 'odb/traits.hxx') diff --git a/odb/traits.hxx b/odb/traits.hxx new file mode 100644 index 0000000..e8e109c --- /dev/null +++ b/odb/traits.hxx @@ -0,0 +1,93 @@ +// file : odb/traits.hxx +// author : Boris Kolpackov +// copyright : Copyright (c) 2009-2010 Code Synthesis Tools CC +// license : GNU GPL v2; see accompanying LICENSE file + +#ifndef ODB_TRAITS_HXX +#define ODB_TRAITS_HXX + +#include // std::auto_ptr + +#include +#include +#include + +namespace odb +{ + enum id_source + { + ids_assigned /* Assigned by the application. */ + }; + + // Specializations should defined the following members: + // + // id_type - object id (primary key) type + // id_source - object id (primary key) source + // id_type id (const T&) - get object id + // + // void insert (database&, const T&) + // void update (database&, const T&) + // void erase (database&, const id_type&) + // memory_traits::shared_ptr find (database&, const id_type&) + // + // And inherit from object_memory and object_factory. + // + // template + // class access::object_traits; + + template + class access::object_memory + { + public: + typedef odb::shared_ptr shared_ptr; + typedef std::auto_ptr unique_ptr; + }; + + template + class access::object_factory + { + public: + static typename object_memory::shared_ptr + create () + { + // By default use shared_ptr-specific construction. + // + return shared_factory::shared_ptr>::create (); + } + }; + + template + class access::shared_factory + { + public: + typedef typename shared_ptr_traits

::type object_type; + + static P + create () + { + void* v (shared_ptr_traits

::allocate (sizeof (object_type))); + guard g (v); + P p (new (v) object_type); + g.release (); + return p; + } + private: + struct guard + { + guard (void* p): p_ (p) {} + ~guard () {if (p_) shared_ptr_traits

::free (p_);} + void release () {p_ = 0;} + void* p_; + }; + }; + + template + struct object_traits: access::object_traits + { + typedef + shared_ptr_traits::shared_ptr> + shared_ops; + }; +} + +#endif // ODB_TRAITS_HXX -- cgit v1.1