// 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 #include #include #include namespace odb { enum id_source { ids_assigned /* Assigned by the application. */ }; // template // class access::object_traits; // // Specializations should inherit from object_memory, object_factory // and define 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 persist (database&, T&) // void store (database&, T&) // void erase (database&, const id_type&) // pointer_type find (database&, const id_type&) // bool find (database&, const id_type&, T&) // // template class access::object_memory { public: typedef T* pointer_type; }; template class access::object_factory { public: static typename object_memory::pointer_type create () { // By default use pointer-specific construction. // return pointer_factory::pointer_type>::create (); } }; template class access::pointer_factory { public: typedef typename pointer_traits

::type object_type; static P create () { void* v (pointer_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_) pointer_traits

::free (p_);} void release () {p_ = 0;} void* p_; }; }; template struct object_traits: access::object_traits { typedef pointer_traits::pointer_type> pointer_ops; }; template class value_traits { public: typedef V value_type; template static void set_value (value_type& v, I i, bool is_null) { if (!is_null) v = value_type (i); else v = value_type (); } template static void set_image (I& i, bool& is_null, value_type v) { is_null = false; i = I (v); } }; } #endif // ODB_TRAITS_HXX