// file : odb/traits.hxx // author : Boris Kolpackov // copyright : Copyright (c) 2009-2011 Code Synthesis Tools CC // license : GNU GPL v2; see accompanying LICENSE file #ifndef ODB_TRAITS_HXX #define ODB_TRAITS_HXX #include #include #include namespace odb { // template // class access::object_traits; // // Specializations should define the following members: // // id_type - object id (primary key) type // id_type id (const T&) - get object id // // void persist (database&, T&) // void update (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_factory { public: typedef T object_type; typedef P pointer_type; static P create () { // By default use pointer-specific construction. // return pointer_factory::create (); } }; template class access::pointer_factory { public: typedef T object_type; typedef P pointer_type; static P create () { void* v (pointer_traits

::allocate (sizeof (T))); mem_guard g (v); P p (new (v) T); g.release (); return p; } private: struct mem_guard { mem_guard (void* p): p_ (p) {} ~mem_guard () {if (p_) pointer_traits

::free (p_);} void release () {p_ = 0;} void* p_; }; }; template struct object_traits: access::object_traits, access::object_factory::pointer_type> { // // If a C++ compiler issues an error pointing to this struct and // saying that it is incomplete, then you are most likely trying to // perform a database operation on a C++ type that is not a persistent // object. Or you forgot to include the corresponding -odb.hxx file. // typedef odb::pointer_traits::pointer_type> pointer_traits; typedef typename access::object_traits::object_type object_type; typedef typename access::object_traits::pointer_type pointer_type; typedef typename pointer_traits::const_pointer_type const_pointer_type; }; // Specialization for const objects. It only defines the id, object, // pointer, and const_pointer types with pointer and const_pointer // being the same. The idea is to only use this specialization in the // interfaces, with the implementations detecting this situation and // using the non-const object_traits version. // template struct object_traits { private: typedef odb::pointer_traits::pointer_type> pointer_traits; public: typedef typename access::object_traits::id_type id_type; typedef typename access::object_traits::object_type object_type; typedef typename pointer_traits::const_pointer_type const_pointer_type; typedef const_pointer_type pointer_type; }; // Specializations for pointer types to allow the C++ compiler to // instantiate persist(), etc., signatures in class database. The // overloads that use these specializations would never actually // be selected by the compiler. // template struct object_traits { struct id_type {}; }; template struct object_traits { struct id_type {}; }; template class P> struct object_traits< P > { struct id_type {}; }; template class P> struct object_traits< const P > { struct id_type {}; }; template struct composite_value_traits: access::composite_value_traits { }; } #include #endif // ODB_TRAITS_HXX