blob: e8e109c90a3b662809fe6facea310563b06c4f2a (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
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
|