diff options
author | Boris Kolpackov <boris@codesynthesis.com> | 2010-06-04 16:33:08 +0200 |
---|---|---|
committer | Boris Kolpackov <boris@codesynthesis.com> | 2010-06-04 16:33:08 +0200 |
commit | 6f0d40508286afc8cdd72a0b5f807d5c2a589cfc (patch) | |
tree | f22a38560560664cae731c37537c2bb04bffdc2a /odb/traits.hxx | |
parent | e0f3efd9df3e7eefa06777717f23905022d1949e (diff) |
Initial implementation
Diffstat (limited to 'odb/traits.hxx')
-rw-r--r-- | odb/traits.hxx | 93 |
1 files changed, 93 insertions, 0 deletions
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 <boris@codesynthesis.com> +// 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 <memory> // std::auto_ptr + +#include <odb/forward.hxx> +#include <odb/shared-ptr.hxx> +#include <odb/shared-ptr-traits.hxx> + +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<T>::shared_ptr find (database&, const id_type&) + // + // And inherit from object_memory and object_factory. + // + // template <typename T> + // class access::object_traits; + + template <typename T> + class access::object_memory + { + public: + typedef odb::shared_ptr<T> shared_ptr; + typedef std::auto_ptr<T> unique_ptr; + }; + + template <typename T> + class access::object_factory + { + public: + static typename object_memory<T>::shared_ptr + create () + { + // By default use shared_ptr-specific construction. + // + return shared_factory<typename object_memory<T>::shared_ptr>::create (); + } + }; + + template <typename P> + class access::shared_factory + { + public: + typedef typename shared_ptr_traits<P>::type object_type; + + static P + create () + { + void* v (shared_ptr_traits<P>::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<P>::free (p_);} + void release () {p_ = 0;} + void* p_; + }; + }; + + template <typename T> + struct object_traits: access::object_traits<T> + { + typedef + shared_ptr_traits<typename access::object_traits<T>::shared_ptr> + shared_ops; + }; +} + +#endif // ODB_TRAITS_HXX |