-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbean_ptr_impl.hpp
138 lines (116 loc) · 2.58 KB
/
bean_ptr_impl.hpp
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
namespace hiberlite{
template<class C>
real_bean<C>::real_bean(const bean_key _key, C* _obj) : key(_key), obj(_obj), forgotten(false)
{}
template<class C>
real_bean<C>::~real_bean()
{
save();
delete obj;
}
template<class C>
void real_bean<C>::destroy() {
if(forgotten)
return;
Database::dbDelete(key, *obj);
delete obj;
forgotten=true;
obj=NULL;
key.id=Database::NULL_ID;
}
template<class C>
void real_bean<C>::save() {
if(forgotten)
return;
if(!obj)
return;
Database::dbUpdate(key, *obj);
}
template<class C>
C* real_bean<C>::operator->()
{
if(forgotten)
return NULL;
loadLazy();
if(!obj)
throw std::runtime_error("NULL pointer exception!");
return obj;
}
template<class C>
C* real_bean<C>::get()
{
if(forgotten)
return NULL;
loadLazy();
if(!obj)
throw std::runtime_error("NULL pointer exception!");
return obj;
}
template<class C>
inline void real_bean<C>::loadLazy()
{
if(!obj && key.id!=Database::NULL_ID)
obj=Database::dbLoad<C>(key);
}
template<class C>
bean_ptr<C>::bean_ptr(bean_key k, rb_pair<C>* para)
{
this->takeRes(para);
}
template<class C>
bean_ptr<C>::bean_ptr(const bean_ptr<C>& other) : shared_res< real_bean<C> >(other)
{
}
template<class C>
bean_ptr<C>& bean_ptr<C>::operator=(const bean_ptr<C>& other)
{
shared_res< real_bean<C> >::operator=( other );
return *this;
}
template<class C>
bean_ptr<C>::bean_ptr(bean_key k)
{
*this=Registry<C>::get(k);
}
template<class C>
bean_ptr<C>::bean_ptr()
{
}
template<class C>
bean_ptr<C>::operator bool() const {
return get_id()!=Database::NULL_ID;
}
template<class C> template<class Archive>
void bean_ptr<C>::hibernate(Archive & ar)
{
tmp_id=get_id();
if(tmp_id!=Database::NULL_ID
&& ar.getConnection()!=shared_res< real_bean<C> >::get_object()->get_key().con)
throw std::logic_error("saving the bean from different database");
ar & hiberlite::sql_nvp< sqlid_t > ("id", tmp_id );
if(ar.is_loading())
*this=Registry<C>::get( bean_key(ar.getConnection(), tmp_id) );
}
template<class C>
C& bean_ptr<C>::operator*() {
return *(shared_res< real_bean<C> >::get_object()->get());
}
template<class C>
C* bean_ptr<C>::operator->() {
return shared_res< real_bean<C> >::get_object()->get();
}
template<class C>
void bean_ptr<C>::destroy() {
shared_res< real_bean<C> >::get_object()->destroy();
}
template<class C>
void bean_ptr<C>::save() {
shared_res< real_bean<C> >::get_object()->save();
}
template<class C>
sqlid_t bean_ptr<C>::get_id() {
if( !shared_res< real_bean<C> >::get_object() )
return Database::NULL_ID;
return shared_res< real_bean<C> >::get_object()->get_key().id;
}
} //namespace hiberlite