crabmeat 1.0
Loading...
Searching...
No Matches
kscratch.h
1
11
12#ifndef LDNDC_STATE_SCRATCH_H_
13#define LDNDC_STATE_SCRATCH_H_
14
15#include "crabmeat-common.h"
16
17#include "nosql/cbm_kvstore.h"
18#include "string/cbm_string.h"
19#include "time/cbm_time.h"
20
21namespace cbm {
22
23template < typename _T >
24struct state_scratch_invalid_t
25{
26 static _T const value;
27};
28template < >
29struct CBM_API state_scratch_invalid_t< bool >
30{
31 static bool const value;
32};
33
34}
35
36template < typename _T >
37_T const cbm::state_scratch_invalid_t< _T >::value =
38 ldndc::invalid_t< _T >::value;
39
40namespace cbm {
41
42struct CBM_API state_scratch_t
43{
44 typedef kvstore_t scratch_hashtable_t;
45
46 static void make_item_key(
47 std::string * _key,
48 char const * _format, ...)
49 {
50 va_list args;
51 va_start( args, _format);
52 *_key = cbm::string_t::format_t< 128 >::vmake( _format, args).str();
53 va_end( args);
54 }
55
56 state_scratch_t() : T_expire( -1) {}
57
58 lerr_t initialize( cbm::sclock_t const *);
59 lerr_t finalize( cbm::sclock_t const *);
60
61 lerr_t run_garbage_collector( cbm::td_scalar_t /*timestep*/);
62
68 void clear();
72 bool variable_exists(
73 char const * /*name*/) const;
74
89 template < typename _T >
90 lerr_t get(
91 char const * /*name*/, _T * /*buffer*/) const;
96 template < typename _T >
97 lerr_t get(
98 char const * /*name*/,
99 _T * /*buffer*/, _T const & /*default*/) const;
107 template < typename _T >
108 void set(
109 char const * /*name*/, _T const & /*value*/,
110 int = 1 /*number of timestep until expiry*/);
118 template < typename _T >
119 void addvalue(
120 char const * /*name*/, _T const & /*value*/);
125 template < typename _T >
126 lerr_t get_and_set(
127 char const * /*name*/, _T * /*buffer*/, _T const & /*value*/);
135 template < typename _T >
136 lerr_t get_and_remove(
137 char const * /*name*/, _T * /*buffer*/);
142 template < typename _T >
143 lerr_t get_and_remove(
144 char const * /*name*/,
145 _T * /*buffer*/, _T const & /*default*/);
149 void remove(
150 char const * /*name*/);
151
152 void dump() const;
153
154 private:
155 scratch_hashtable_t ht;
156 cbm::td_scalar_t T_expire;
157
158 lerr_t expire_list_enqueue(
159 char const * /*key*/, cbm::td_scalar_t /*expire time*/);
160 lerr_t expire_list_remove(
161 cbm::td_scalar_t /*timestep*/);
162};
163
164} /* namespace cbm */
165
166template < typename _T >
167lerr_t
168cbm::state_scratch_t::get(
169 char const * _key, _T * _reg)
170const
171{
172 return this->ht.fetch_pod< _T >( _reg, _key);
173}
174template < typename _T >
175lerr_t
176cbm::state_scratch_t::get(
177 char const * _key,
178 _T * _reg, _T const & _default)
179const
180{
181 lerr_t rc_get = this->get< _T >( _key, _reg);
182 if ( rc_get == LDNDC_ERR_ATTRIBUTE_NOT_FOUND)
183 {
184 *_reg = _default;
185 }
186 else if ( rc_get)
187 {
188 *_reg = state_scratch_invalid_t< _T >::value;
189 }
190 /* fall through */
191 return rc_get;
192}
193
194template < typename _T >
195void
196cbm::state_scratch_t::set(
197 char const * _key, _T const & _reg, int _expire_time)
198{
199 this->ht.store_pod< _T >( _key, _reg);
200 this->expire_list_enqueue( _key, _expire_time);
201}
202
203template < typename _T >
204void
205cbm::state_scratch_t::addvalue(
206 char const * _key, _T const & _add_value)
207{
208 _T value;
209 this->get( _key, &value, _T( 0));
210 this->set( _key, value + _add_value);
211}
212
213template < typename _T >
214lerr_t
215cbm::state_scratch_t::get_and_set(
216 char const * _key, _T * _reg,
217 _T const & _reset_value)
218{
219 lerr_t const rc_get = this->get( _key, _reg);
220 this->set( _key, _reset_value);
221 return rc_get;
222}
223template < typename _T >
224lerr_t
225cbm::state_scratch_t::get_and_remove(
226 char const * _key, _T * _reg)
227{
228 return this->ht.fetch_and_remove_pod( _reg, _key);
229}
230template < typename _T >
231lerr_t
232cbm::state_scratch_t::get_and_remove(
233 char const * _key,
234 _T * _reg, _T const & _default)
235{
236 lerr_t rc_get = this->get_and_remove( _key, _reg);
237 if ( rc_get == LDNDC_ERR_ATTRIBUTE_NOT_FOUND)
238 {
239 *_reg = _default;
240 }
241 else if ( rc_get)
242 {
243 *_reg = state_scratch_invalid_t< _T >::value;
244 }
245 /* fall through */
246 return rc_get;
247}
248
249#endif /* !LDNDC_STATE_SCRATCH_H_ */
250
The "Service Registry" holds information about available services (e.g., models, readers,...
Definition Lresources.h:51