1 | // ================================================================= // |
---|
2 | // // |
---|
3 | // File : ad_cb.cxx // |
---|
4 | // Purpose : callbacks on DB entries // |
---|
5 | // // |
---|
6 | // Institute of Microbiology (Technical University Munich) // |
---|
7 | // http://www.arb-home.de/ // |
---|
8 | // // |
---|
9 | // ================================================================= // |
---|
10 | |
---|
11 | #include "ad_cb.h" |
---|
12 | #include "ad_hcb.h" |
---|
13 | #include "gb_compress.h" |
---|
14 | #include "gb_ta.h" |
---|
15 | #include "gb_ts.h" |
---|
16 | |
---|
17 | #include <arb_strarray.h> |
---|
18 | #include <arb_strbuf.h> |
---|
19 | |
---|
20 | static gb_triggered_callback *currently_called_back = NULp; // points to callback during callback; NULp otherwise |
---|
21 | static GB_MAIN_TYPE *inside_callback_main = NULp; // points to DB root during callback; NULp otherwise |
---|
22 | |
---|
23 | gb_hierarchy_location::gb_hierarchy_location(GBDATA *gb_main, const char *db_path) { |
---|
24 | invalidate(); |
---|
25 | if (db_path) { |
---|
26 | GB_MAIN_TYPE *Main = GB_MAIN(gb_main); |
---|
27 | GB_test_transaction(Main); |
---|
28 | |
---|
29 | ConstStrArray keys; |
---|
30 | GBT_split_string(keys, db_path, '/'); |
---|
31 | |
---|
32 | #define INVALIDATE_IF(cond) do{ if (cond) { invalidate(); return; } }while(0) |
---|
33 | |
---|
34 | int size = keys.size(); |
---|
35 | bool is_full_path = !keys[0][0]; // db_path starts with '/' |
---|
36 | |
---|
37 | depth = is_full_path ? INT_MAX : size; |
---|
38 | |
---|
39 | int implEntry = int(is_full_path); // 1 if is_full_path (the empty entry before leading slash); 0 otherwise |
---|
40 | if (size>implEntry) { |
---|
41 | int q = 0; |
---|
42 | for (int offset = size-1; offset>=implEntry; --offset, ++q) { |
---|
43 | INVALIDATE_IF(!keys[offset][0]); // empty key |
---|
44 | quark[q] = gb_find_or_create_quark(Main, keys[offset]); |
---|
45 | INVALIDATE_IF(quark[q]<1); // unknown/invalid key |
---|
46 | } |
---|
47 | quark[size-implEntry] = 0; |
---|
48 | gb_assert(is_valid()); |
---|
49 | } |
---|
50 | } |
---|
51 | #undef INVALIDATE_IF |
---|
52 | } |
---|
53 | |
---|
54 | char *gb_hierarchy_location::get_db_path(GBDATA *gb_main) const { |
---|
55 | GBS_strstruct out(MAX_HIERARCHY_DEPTH*20); |
---|
56 | GB_MAIN_TYPE *Main = GB_MAIN(gb_main); |
---|
57 | |
---|
58 | int offset = 0; |
---|
59 | while (quark[offset]) ++offset; |
---|
60 | if (!is_submatch()) out.put('/'); |
---|
61 | while (offset-->0) { |
---|
62 | out.cat(quark2key(Main, quark[offset])); |
---|
63 | out.put('/'); |
---|
64 | } |
---|
65 | out.cut_tail(1); |
---|
66 | return out.release(); |
---|
67 | } |
---|
68 | |
---|
69 | void gb_pending_callbacks::call_and_forget(GB_CB_TYPE allowedTypes) { |
---|
70 | #if defined(ASSERTION_USED) |
---|
71 | const gb_triggered_callback *tail = get_tail(); |
---|
72 | #endif |
---|
73 | |
---|
74 | for (itertype cb = callbacks.begin(); cb != callbacks.end(); ++cb) { |
---|
75 | currently_called_back = &*cb; |
---|
76 | gb_assert(currently_called_back); |
---|
77 | currently_called_back->spec(cb->gbd, allowedTypes); |
---|
78 | currently_called_back = NULp; |
---|
79 | } |
---|
80 | |
---|
81 | gb_assert(tail == get_tail()); |
---|
82 | |
---|
83 | callbacks.clear(); |
---|
84 | } |
---|
85 | |
---|
86 | void GB_MAIN_TYPE::call_pending_callbacks() { |
---|
87 | inside_callback_main = this; |
---|
88 | |
---|
89 | deleteCBs.pending.call_and_forget(GB_CB_DELETE); // first all delete callbacks: |
---|
90 | changeCBs.pending.call_and_forget(GB_CB_ALL_BUT_DELETE); // then all change callbacks: |
---|
91 | |
---|
92 | inside_callback_main = NULp; |
---|
93 | } |
---|
94 | |
---|
95 | inline void GB_MAIN_TYPE::callback_group::forget_hcbs() { |
---|
96 | delete hierarchy_cbs; |
---|
97 | hierarchy_cbs = NULp; |
---|
98 | } |
---|
99 | |
---|
100 | void GB_MAIN_TYPE::forget_hierarchy_cbs() { |
---|
101 | changeCBs.forget_hcbs(); |
---|
102 | deleteCBs.forget_hcbs(); |
---|
103 | } |
---|
104 | |
---|
105 | static void dummy_db_cb(GBDATA*, GB_CB_TYPE) { gb_assert(0); } // used as marker for deleted callbacks |
---|
106 | DatabaseCallback TypedDatabaseCallback::MARKED_DELETED = makeDatabaseCallback(dummy_db_cb); |
---|
107 | |
---|
108 | GB_MAIN_TYPE *gb_get_main_during_cb() { |
---|
109 | /* if inside a callback, return the DB root of the DB element, the callback was called for. |
---|
110 | * if not inside a callback, return NULp. |
---|
111 | */ |
---|
112 | return inside_callback_main; |
---|
113 | } |
---|
114 | |
---|
115 | NOT4PERL bool GB_inside_callback(GBDATA *of_gbd, GB_CB_TYPE cbtype) { |
---|
116 | GB_MAIN_TYPE *Main = gb_get_main_during_cb(); |
---|
117 | bool inside = false; |
---|
118 | |
---|
119 | if (Main) { // inside a callback |
---|
120 | gb_assert(currently_called_back); |
---|
121 | if (currently_called_back->gbd == of_gbd) { |
---|
122 | GB_CB_TYPE curr_cbtype; |
---|
123 | if (Main->has_pending_delete_callback()) { // delete callbacks were not all performed yet |
---|
124 | // => current callback is a delete callback |
---|
125 | curr_cbtype = GB_CB_TYPE(currently_called_back->spec.get_type() & GB_CB_DELETE); |
---|
126 | } |
---|
127 | else { |
---|
128 | gb_assert(Main->has_pending_change_callback()); |
---|
129 | curr_cbtype = GB_CB_TYPE(currently_called_back->spec.get_type() & (GB_CB_ALL-GB_CB_DELETE)); |
---|
130 | } |
---|
131 | gb_assert(curr_cbtype != GB_CB_NONE); // wtf!? are we inside callback or not? |
---|
132 | |
---|
133 | if ((cbtype&curr_cbtype) != GB_CB_NONE) { |
---|
134 | inside = true; |
---|
135 | } |
---|
136 | } |
---|
137 | } |
---|
138 | |
---|
139 | return inside; |
---|
140 | } |
---|
141 | |
---|
142 | GBDATA *GB_get_gb_main_during_cb() { |
---|
143 | GBDATA *gb_main = NULp; |
---|
144 | GB_MAIN_TYPE *Main = gb_get_main_during_cb(); |
---|
145 | |
---|
146 | if (Main) { // inside callback |
---|
147 | if (!GB_inside_callback(Main->gb_main(), GB_CB_DELETE)) { // main is not deleted |
---|
148 | gb_main = Main->gb_main(); |
---|
149 | } |
---|
150 | } |
---|
151 | return gb_main; |
---|
152 | } |
---|
153 | |
---|
154 | static GB_CSTR gb_read_pntr_ts(GBDATA *gbd, gb_transaction_save *ts) { |
---|
155 | int type = GB_TYPE_TS(ts); |
---|
156 | const char *data = GB_GETDATA_TS(ts); |
---|
157 | if (data) { |
---|
158 | if (ts->flags.compressed_data) { // uncompressed data return pntr to database entry |
---|
159 | long size = GB_GETSIZE_TS(ts) * gb_convert_type_2_sizeof[type] + gb_convert_type_2_appendix_size[type]; |
---|
160 | data = gb_uncompress_data(gbd, data, size); |
---|
161 | } |
---|
162 | } |
---|
163 | return data; |
---|
164 | } |
---|
165 | |
---|
166 | NOT4PERL const void *GB_read_old_value() { |
---|
167 | // get last array value in callbacks |
---|
168 | char *data; |
---|
169 | |
---|
170 | if (!currently_called_back) { |
---|
171 | GB_export_error("You cannot call GB_read_old_value outside a ARBDB callback"); |
---|
172 | return NULp; |
---|
173 | } |
---|
174 | if (!currently_called_back->old) { |
---|
175 | GB_export_error("No old value available in GB_read_old_value"); |
---|
176 | return NULp; |
---|
177 | } |
---|
178 | data = GB_GETDATA_TS(currently_called_back->old); |
---|
179 | if (!data) return NULp; |
---|
180 | |
---|
181 | return gb_read_pntr_ts(currently_called_back->gbd, currently_called_back->old); |
---|
182 | } |
---|
183 | long GB_read_old_size() { |
---|
184 | // same as GB_read_old_value for size |
---|
185 | if (!currently_called_back) { |
---|
186 | GB_export_error("You cannot call GB_read_old_size outside a ARBDB callback"); |
---|
187 | return -1; |
---|
188 | } |
---|
189 | if (!currently_called_back->old) { |
---|
190 | GB_export_error("No old value available in GB_read_old_size"); |
---|
191 | return -1; |
---|
192 | } |
---|
193 | return GB_GETSIZE_TS(currently_called_back->old); |
---|
194 | } |
---|
195 | |
---|
196 | inline char *cbtype2readable(GB_CB_TYPE type) { |
---|
197 | ConstStrArray septype; |
---|
198 | |
---|
199 | #define appendcbtype(cbt) do { \ |
---|
200 | if (type&cbt) { \ |
---|
201 | type = GB_CB_TYPE(type-cbt); \ |
---|
202 | septype.put(#cbt); \ |
---|
203 | } \ |
---|
204 | } while(0) |
---|
205 | |
---|
206 | appendcbtype(GB_CB_DELETE); |
---|
207 | appendcbtype(GB_CB_CHANGED); |
---|
208 | appendcbtype(GB_CB_SON_CREATED); |
---|
209 | |
---|
210 | gb_assert(type == GB_CB_NONE); |
---|
211 | |
---|
212 | return GBT_join_strings(septype, '|'); |
---|
213 | } |
---|
214 | |
---|
215 | char *TypedDatabaseCallback::get_info() const { |
---|
216 | const char *readable_fun = GBS_funptr2readable((void*)dbcb.callee(), true); |
---|
217 | char *readable_cbtype = cbtype2readable((GB_CB_TYPE)dbcb.inspect_CD2()); |
---|
218 | char *result = GBS_global_string_copy("func='%s' type=%s clientdata=%p", |
---|
219 | readable_fun, readable_cbtype, (void*)dbcb.inspect_CD1()); |
---|
220 | |
---|
221 | free(readable_cbtype); |
---|
222 | |
---|
223 | return result; |
---|
224 | } |
---|
225 | |
---|
226 | char *GB_get_callback_info(GBDATA *gbd) { |
---|
227 | // returns human-readable information about callbacks of 'gbd' or NULp |
---|
228 | char *result = NULp; |
---|
229 | if (gbd->ext) { |
---|
230 | gb_callback_list *cbl = gbd->get_callbacks(); |
---|
231 | if (cbl) { |
---|
232 | for (gb_callback_list::itertype cb = cbl->callbacks.begin(); cb != cbl->callbacks.end(); ++cb) { |
---|
233 | char *cb_info = cb->spec.get_info(); |
---|
234 | if (result) { |
---|
235 | char *new_result = GBS_global_string_copy("%s\n%s", result, cb_info); |
---|
236 | free(result); |
---|
237 | free(cb_info); |
---|
238 | result = new_result; |
---|
239 | } |
---|
240 | else { |
---|
241 | result = cb_info; |
---|
242 | } |
---|
243 | } |
---|
244 | } |
---|
245 | } |
---|
246 | |
---|
247 | return result; |
---|
248 | } |
---|
249 | |
---|
250 | #if defined(ASSERTION_USED) |
---|
251 | template<typename CB> |
---|
252 | bool CallbackList<CB>::contains_unremoved_callback(const CB& like) const { |
---|
253 | for (const_itertype cb = callbacks.begin(); cb != callbacks.end(); ++cb) { |
---|
254 | if (cb->spec.is_equal_to(like.spec) && |
---|
255 | !cb->spec.is_marked_for_removal()) |
---|
256 | { |
---|
257 | return true; |
---|
258 | } |
---|
259 | } |
---|
260 | return false; |
---|
261 | } |
---|
262 | template<> |
---|
263 | bool CallbackList<gb_hierarchy_callback>::contains_unremoved_callback(const gb_hierarchy_callback& like) const { |
---|
264 | for (const_itertype cb = callbacks.begin(); cb != callbacks.end(); ++cb) { |
---|
265 | if (cb->spec.is_equal_to(like.spec) && |
---|
266 | !cb->spec.is_marked_for_removal() && |
---|
267 | cb->get_location() == like.get_location()) // if location differs, accept duplicate callback |
---|
268 | { |
---|
269 | return true; |
---|
270 | } |
---|
271 | } |
---|
272 | return false; |
---|
273 | } |
---|
274 | #endif |
---|
275 | |
---|
276 | template <typename PRED> |
---|
277 | inline void gb_remove_callbacks_that(GBDATA *gbd, PRED shallRemove) { |
---|
278 | #if defined(ASSERTION_USED) |
---|
279 | if (GB_inside_callback(gbd, GB_CB_DELETE)) { |
---|
280 | printf("Warning: gb_remove_callback called inside delete-callback of gbd (gbd may already be freed)\n"); |
---|
281 | gb_assert(0); // fix callback-handling (never modify callbacks from inside delete callbacks) |
---|
282 | return; |
---|
283 | } |
---|
284 | #endif // DEBUG |
---|
285 | |
---|
286 | if (gbd->ext) { |
---|
287 | gb_callback_list *cbl = gbd->get_callbacks(); |
---|
288 | if (cbl) cbl->remove_callbacks_that(shallRemove); |
---|
289 | } |
---|
290 | } |
---|
291 | |
---|
292 | struct ShallBeDeleted { |
---|
293 | bool operator()(const gb_callback& cb) const { return cb.spec.is_marked_for_removal(); } |
---|
294 | }; |
---|
295 | void gb_remove_callbacks_marked_for_deletion(GBDATA *gbd) { |
---|
296 | gb_remove_callbacks_that(gbd, ShallBeDeleted()); |
---|
297 | } |
---|
298 | |
---|
299 | struct IsCallback : private TypedDatabaseCallback { |
---|
300 | IsCallback(GB_CB func_, GB_CB_TYPE type_) : TypedDatabaseCallback(makeDatabaseCallback((GB_CB)func_, (int*)NULp), type_) {} |
---|
301 | bool operator()(const gb_callback& cb) const { return sig_is_equal_to(cb.spec); } |
---|
302 | }; |
---|
303 | struct IsSpecificCallback : private TypedDatabaseCallback { |
---|
304 | IsSpecificCallback(const TypedDatabaseCallback& cb) : TypedDatabaseCallback(cb) {} |
---|
305 | bool operator()(const gb_callback& cb) const { return is_equal_to(cb.spec); } |
---|
306 | }; |
---|
307 | struct IsSpecificHierarchyCallback : private TypedDatabaseCallback { |
---|
308 | gb_hierarchy_location loc; |
---|
309 | IsSpecificHierarchyCallback(const gb_hierarchy_location& loc_, const TypedDatabaseCallback& cb) |
---|
310 | : TypedDatabaseCallback(cb), |
---|
311 | loc(loc_) |
---|
312 | {} |
---|
313 | bool operator()(const gb_callback& cb) const { |
---|
314 | const gb_hierarchy_callback& hcb = static_cast<const gb_hierarchy_callback&>(cb); |
---|
315 | return is_equal_to(cb.spec) && hcb.get_location() == loc; |
---|
316 | } |
---|
317 | }; |
---|
318 | |
---|
319 | inline void add_to_callback_chain(gb_callback_list*& head, const TypedDatabaseCallback& cbs) { |
---|
320 | if (!head) head = new gb_callback_list; |
---|
321 | head->add(gb_callback(cbs)); |
---|
322 | } |
---|
323 | inline void add_to_callback_chain(gb_hierarchy_callback_list*& head, const TypedDatabaseCallback& cbs, const gb_hierarchy_location& loc) { |
---|
324 | if (!head) head = new gb_hierarchy_callback_list; |
---|
325 | head->add(gb_hierarchy_callback(cbs, loc)); |
---|
326 | } |
---|
327 | |
---|
328 | inline GB_ERROR gb_add_callback(GBDATA *gbd, const TypedDatabaseCallback& cbs) { |
---|
329 | /* Adds a callback to a DB entry. |
---|
330 | * |
---|
331 | * Be careful when writing GB_CB_DELETE callbacks, there is a severe restriction: |
---|
332 | * |
---|
333 | * - the DB element may already be freed. The pointer is still pointing to the original |
---|
334 | * location, so you can use it to identify the DB element, but you cannot dereference |
---|
335 | * it under all circumstances. |
---|
336 | * |
---|
337 | * ARBDB internal delete-callbacks may use gb_get_main_during_cb() to access the DB root. |
---|
338 | * See also: GB_get_gb_main_during_cb() |
---|
339 | */ |
---|
340 | |
---|
341 | #if defined(DEBUG) |
---|
342 | if (GB_inside_callback(gbd, GB_CB_DELETE)) { |
---|
343 | printf("Warning: add_priority_callback called inside delete-callback of gbd (gbd may already be freed)\n"); |
---|
344 | #if defined(DEVEL_RALF) |
---|
345 | gb_assert(0); // fix callback-handling (never modify callbacks from inside delete callbacks) |
---|
346 | #endif // DEVEL_RALF |
---|
347 | } |
---|
348 | #endif // DEBUG |
---|
349 | |
---|
350 | GB_test_transaction(gbd); // may return error |
---|
351 | gbd->create_extended(); |
---|
352 | add_to_callback_chain(gbd->ext->callback, cbs); |
---|
353 | return NULp; |
---|
354 | } |
---|
355 | |
---|
356 | GB_ERROR GB_add_callback(GBDATA *gbd, GB_CB_TYPE type, const DatabaseCallback& dbcb) { |
---|
357 | return gb_add_callback(gbd, TypedDatabaseCallback(dbcb, type)); |
---|
358 | } |
---|
359 | |
---|
360 | void GB_remove_callback(GBDATA *gbd, GB_CB_TYPE type, const DatabaseCallback& dbcb) { |
---|
361 | // remove specific callback; 'type' and 'dbcb' have to match |
---|
362 | gb_remove_callbacks_that(gbd, IsSpecificCallback(TypedDatabaseCallback(dbcb, type))); |
---|
363 | } |
---|
364 | void GB_remove_all_callbacks_to(GBDATA *gbd, GB_CB_TYPE type, GB_CB func) { |
---|
365 | // removes all callbacks 'func' bound to 'gbd' with 'type' |
---|
366 | gb_remove_callbacks_that(gbd, IsCallback(func, type)); |
---|
367 | } |
---|
368 | |
---|
369 | inline void GB_MAIN_TYPE::callback_group::add_hcb(const gb_hierarchy_location& loc, const TypedDatabaseCallback& dbcb) { |
---|
370 | add_to_callback_chain(hierarchy_cbs, dbcb, loc); |
---|
371 | } |
---|
372 | inline void GB_MAIN_TYPE::callback_group::remove_hcb(const gb_hierarchy_location& loc, const TypedDatabaseCallback& dbcb) { |
---|
373 | if (hierarchy_cbs) { |
---|
374 | hierarchy_cbs->remove_callbacks_that(IsSpecificHierarchyCallback(loc, dbcb)); |
---|
375 | } |
---|
376 | } |
---|
377 | |
---|
378 | #define CHECK_HIER_CB_CONDITION() \ |
---|
379 | if (get_transaction_level()<0) return "no hierarchy callbacks allowed in NO_TRANSACTION_MODE"; \ |
---|
380 | if (!loc.is_valid()) return "invalid hierarchy location" |
---|
381 | |
---|
382 | GB_ERROR GB_MAIN_TYPE::add_hierarchy_cb(const gb_hierarchy_location& loc, const TypedDatabaseCallback& dbcb) { |
---|
383 | CHECK_HIER_CB_CONDITION(); |
---|
384 | |
---|
385 | GB_CB_TYPE type = dbcb.get_type(); |
---|
386 | if (type & GB_CB_DELETE) { |
---|
387 | deleteCBs.add_hcb(loc, dbcb.with_type_changed_to(GB_CB_DELETE)); |
---|
388 | } |
---|
389 | if (type & GB_CB_ALL_BUT_DELETE) { |
---|
390 | changeCBs.add_hcb(loc, dbcb.with_type_changed_to(GB_CB_TYPE(type&GB_CB_ALL_BUT_DELETE))); |
---|
391 | } |
---|
392 | return NULp; |
---|
393 | } |
---|
394 | |
---|
395 | GB_ERROR GB_MAIN_TYPE::remove_hierarchy_cb(const gb_hierarchy_location& loc, const TypedDatabaseCallback& dbcb) { |
---|
396 | CHECK_HIER_CB_CONDITION(); |
---|
397 | |
---|
398 | GB_CB_TYPE type = dbcb.get_type(); |
---|
399 | if (type & GB_CB_DELETE) { |
---|
400 | deleteCBs.remove_hcb(loc, dbcb.with_type_changed_to(GB_CB_DELETE)); |
---|
401 | } |
---|
402 | if (type & GB_CB_ALL_BUT_DELETE) { |
---|
403 | changeCBs.remove_hcb(loc, dbcb.with_type_changed_to(GB_CB_TYPE(type&GB_CB_ALL_BUT_DELETE))); |
---|
404 | } |
---|
405 | return NULp; |
---|
406 | } |
---|
407 | |
---|
408 | #undef CHECK_HIER_CB_CONDITION |
---|
409 | |
---|
410 | GB_ERROR GB_add_hierarchy_callback(GBDATA *gbd, GB_CB_TYPE type, const DatabaseCallback& dbcb) { |
---|
411 | /*! bind callback to ALL entries which are at the same DB-hierarchy as 'gbd'. |
---|
412 | * |
---|
413 | * Hierarchy callbacks are triggered before normal callbacks (added by GB_add_callback or GB_ensure_callback). |
---|
414 | * Nevertheless delete callbacks take precedence over change callbacks |
---|
415 | * (i.e. a normal delete callback is triggered before a hierarchical change callback). |
---|
416 | * |
---|
417 | * Hierarchy callbacks cannot be installed and will NOT be triggered in NO_TRANSACTION_MODE |
---|
418 | * (i.e. it will not work in ARBs property DBs) |
---|
419 | * |
---|
420 | * @return error if in NO_TRANSACTION_MODE or if hierarchy location is invalid |
---|
421 | */ |
---|
422 | return GB_MAIN(gbd)->add_hierarchy_cb(gb_hierarchy_location(gbd), TypedDatabaseCallback(dbcb, type)); |
---|
423 | } |
---|
424 | |
---|
425 | GB_ERROR GB_remove_hierarchy_callback(GBDATA *gbd, GB_CB_TYPE type, const DatabaseCallback& dbcb) { |
---|
426 | //! remove callback added with GB_add_hierarchy_callback() |
---|
427 | return GB_MAIN(gbd)->remove_hierarchy_cb(gb_hierarchy_location(gbd), TypedDatabaseCallback(dbcb, type)); |
---|
428 | } |
---|
429 | |
---|
430 | GB_ERROR GB_add_hierarchy_callback(GBDATA *gb_main, const char *db_path, GB_CB_TYPE type, const DatabaseCallback& dbcb) { |
---|
431 | /*! same as overloaded GB_add_hierarchy_callback(), but using db_path instead of GBDATA to define hierarchy location. |
---|
432 | * If the path starts with '/', the full path has to match to trigger the callback. |
---|
433 | * Otherwise only the given path has to match (from bottom up), |
---|
434 | * e.g. "gb_group" will trigger on any "gb_group" entry (regardless of its parent-path). |
---|
435 | */ |
---|
436 | return GB_MAIN(gb_main)->add_hierarchy_cb(gb_hierarchy_location(gb_main, db_path), TypedDatabaseCallback(dbcb, type)); |
---|
437 | } |
---|
438 | GB_ERROR GB_remove_hierarchy_callback(GBDATA *gb_main, const char *db_path, GB_CB_TYPE type, const DatabaseCallback& dbcb) { |
---|
439 | //! same as overloaded GB_remove_hierarchy_callback(), but using db_path instead of GBDATA to define hierarchy location |
---|
440 | return GB_MAIN(gb_main)->remove_hierarchy_cb(gb_hierarchy_location(gb_main, db_path), TypedDatabaseCallback(dbcb, type)); |
---|
441 | } |
---|
442 | |
---|
443 | GB_ERROR GB_ensure_callback(GBDATA *gbd, GB_CB_TYPE type, const DatabaseCallback& dbcb) { |
---|
444 | TypedDatabaseCallback newcb(dbcb, type); |
---|
445 | gb_callback_list *cbl = gbd->get_callbacks(); |
---|
446 | if (cbl) { |
---|
447 | for (gb_callback_list::itertype cb = cbl->callbacks.begin(); cb != cbl->callbacks.end(); ++cb) { |
---|
448 | if (cb->spec.is_equal_to(newcb) && !cb->spec.is_marked_for_removal()) { |
---|
449 | return NULp; // already in cb list |
---|
450 | } |
---|
451 | } |
---|
452 | } |
---|
453 | return gb_add_callback(gbd, newcb); |
---|
454 | } |
---|
455 | |
---|
456 | void GB_atclose_callback(GBDATA *gbd, const DatabaseCallback& atClose) { |
---|
457 | /*! Add a callback, which gets called directly before GB_close destroys all data. |
---|
458 | * This is the recommended way to remove all callbacks from DB elements. |
---|
459 | */ |
---|
460 | |
---|
461 | GB_MAIN_TYPE *Main = GB_MAIN(gbd); |
---|
462 | // use standard delete callback, but put in separate list: |
---|
463 | add_to_callback_chain(Main->close_callbacks, TypedDatabaseCallback(atClose, GB_CB_DELETE)); |
---|
464 | } |
---|
465 | |
---|
466 | // -------------------------------------------------------------------------------- |
---|
467 | |
---|
468 | #ifdef UNIT_TESTS |
---|
469 | |
---|
470 | #include <string> // needed b4 test_unit.h! |
---|
471 | #include <arb_file.h> |
---|
472 | |
---|
473 | #ifndef TEST_UNIT_H |
---|
474 | #include <test_unit.h> |
---|
475 | #endif |
---|
476 | |
---|
477 | |
---|
478 | // ----------------------- |
---|
479 | // test callbacks |
---|
480 | |
---|
481 | static void test_count_cb(GBDATA *, int *counter) { |
---|
482 | fprintf(stderr, "test_count_cb: var.add=%p old.val=%i ", counter, *counter); |
---|
483 | (*counter)++; |
---|
484 | fprintf(stderr, "new.val=%i\n", *counter); |
---|
485 | fflush(stderr); |
---|
486 | } |
---|
487 | |
---|
488 | static void remove_self_cb(GBDATA *gbe, GB_CB_TYPE cbtype) { |
---|
489 | GB_remove_callback(gbe, cbtype, makeDatabaseCallback(remove_self_cb)); |
---|
490 | } |
---|
491 | |
---|
492 | static void re_add_self_cb(GBDATA *gbe, int *calledCounter, GB_CB_TYPE cbtype) { |
---|
493 | ++(*calledCounter); |
---|
494 | |
---|
495 | DatabaseCallback dbcb = makeDatabaseCallback(re_add_self_cb, calledCounter); |
---|
496 | GB_remove_callback(gbe, cbtype, dbcb); |
---|
497 | |
---|
498 | ASSERT_NO_ERROR(GB_add_callback(gbe, cbtype, dbcb)); |
---|
499 | } |
---|
500 | |
---|
501 | void TEST_db_callbacks_ta_nota() { |
---|
502 | GB_shell shell; |
---|
503 | |
---|
504 | enum TAmode { |
---|
505 | NO_TA = 1, // no transaction mode |
---|
506 | WITH_TA = 2, // transaction mode |
---|
507 | |
---|
508 | BOTH_TA_MODES = (NO_TA|WITH_TA) |
---|
509 | }; |
---|
510 | |
---|
511 | for (TAmode ta_mode = NO_TA; ta_mode <= WITH_TA; ta_mode = TAmode(ta_mode+1)) { |
---|
512 | GBDATA *gb_main = GB_open("no.arb", "c"); |
---|
513 | GB_ERROR error; |
---|
514 | |
---|
515 | TEST_ANNOTATE(ta_mode == NO_TA ? "NO_TA" : "WITH_TA"); |
---|
516 | if (ta_mode == NO_TA) { |
---|
517 | error = GB_no_transaction(gb_main); TEST_EXPECT_NO_ERROR(error); |
---|
518 | } |
---|
519 | |
---|
520 | // create some DB entries |
---|
521 | GBDATA *gbc; |
---|
522 | GBDATA *gbe1; |
---|
523 | GBDATA *gbe2; |
---|
524 | GBDATA *gbe3; |
---|
525 | { |
---|
526 | GB_transaction ta(gb_main); |
---|
527 | |
---|
528 | gbc = GB_create_container(gb_main, "cont"); |
---|
529 | gbe1 = GB_create(gbc, "entry", GB_STRING); |
---|
530 | gbe2 = GB_create(gb_main, "entry", GB_INT); |
---|
531 | } |
---|
532 | |
---|
533 | // counters to detect called callbacks |
---|
534 | int e1_changed = 0; |
---|
535 | int e2_changed = 0; |
---|
536 | int c_changed = 0; |
---|
537 | int c_son_created = 0; |
---|
538 | |
---|
539 | int e1_deleted = 0; |
---|
540 | int e2_deleted = 0; |
---|
541 | int e3_deleted = 0; |
---|
542 | int c_deleted = 0; |
---|
543 | |
---|
544 | #define CHCB_COUNTERS_EXPECTATION(e1c,e2c,cc,csc) \ |
---|
545 | that(e1_changed).is_equal_to(e1c), \ |
---|
546 | that(e2_changed).is_equal_to(e2c), \ |
---|
547 | that(c_changed).is_equal_to(cc), \ |
---|
548 | that(c_son_created).is_equal_to(csc) |
---|
549 | |
---|
550 | #define DLCB_COUNTERS_EXPECTATION(e1d,e2d,e3d,cd) \ |
---|
551 | that(e1_deleted).is_equal_to(e1d), \ |
---|
552 | that(e2_deleted).is_equal_to(e2d), \ |
---|
553 | that(e3_deleted).is_equal_to(e3d), \ |
---|
554 | that(c_deleted).is_equal_to(cd) |
---|
555 | |
---|
556 | #define TEST_EXPECT_CHCB_COUNTERS(e1c,e2c,cc,csc,tam) do{ if (ta_mode & (tam)) TEST_EXPECTATION(all().of(CHCB_COUNTERS_EXPECTATION(e1c,e2c,cc,csc))); }while(0) |
---|
557 | #define TEST_EXPECT_CHCB___WANTED(e1c,e2c,cc,csc,tam) do{ if (ta_mode & (tam)) TEST_EXPECTATION__WANTED(all().of(CHCB_COUNTERS_EXPECTATION(e1c,e2c,cc,csc))); }while(0) |
---|
558 | |
---|
559 | #define TEST_EXPECT_DLCB_COUNTERS(e1d,e2d,e3d,cd,tam) do{ if (ta_mode & (tam)) TEST_EXPECTATION(all().of(DLCB_COUNTERS_EXPECTATION(e1d,e2d,e3d,cd))); }while(0) |
---|
560 | #define TEST_EXPECT_DLCB___WANTED(e1d,e2d,e3d,cd,tam) do{ if (ta_mode & (tam)) TEST_EXPECTATION__WANTED(all().of(DLCB_COUNTERS_EXPECTATION(e1d,e2d,e3d,cd))); }while(0) |
---|
561 | |
---|
562 | #define TEST_EXPECT_COUNTER(tam,cnt,expected) do{ if (ta_mode & (tam)) TEST_EXPECT_EQUAL(cnt, expected); }while(0) |
---|
563 | #define TEST_EXPECT_COUNTER__BROKEN(tam,cnt,expected,got) do{ if (ta_mode & (tam)) TEST_EXPECT_EQUAL__BROKEN(cnt, expected, got); }while(0) |
---|
564 | |
---|
565 | #define RESET_CHCB_COUNTERS() do{ e1_changed = e2_changed = c_changed = c_son_created = 0; }while(0) |
---|
566 | #define RESET_DLCB_COUNTERS() do{ e1_deleted = e2_deleted = e3_deleted = c_deleted = 0; }while(0) |
---|
567 | #define RESET_ALL_CB_COUNTERS() do{ RESET_CHCB_COUNTERS(); RESET_DLCB_COUNTERS(); }while(0) |
---|
568 | |
---|
569 | // install some DB callbacks |
---|
570 | { |
---|
571 | GB_transaction ta(gb_main); |
---|
572 | GB_add_callback(gbe1, GB_CB_CHANGED, makeDatabaseCallback(test_count_cb, &e1_changed)); |
---|
573 | GB_add_callback(gbe2, GB_CB_CHANGED, makeDatabaseCallback(test_count_cb, &e2_changed)); |
---|
574 | GB_add_callback(gbc, GB_CB_CHANGED, makeDatabaseCallback(test_count_cb, &c_changed)); |
---|
575 | GB_add_callback(gbc, GB_CB_SON_CREATED, makeDatabaseCallback(test_count_cb, &c_son_created)); |
---|
576 | } |
---|
577 | |
---|
578 | // check callbacks were not called yet |
---|
579 | TEST_EXPECT_CHCB_COUNTERS(0, 0, 0, 0, BOTH_TA_MODES); |
---|
580 | |
---|
581 | // trigger callbacks |
---|
582 | { |
---|
583 | GB_transaction ta(gb_main); |
---|
584 | |
---|
585 | error = GB_write_string(gbe1, "hi"); TEST_EXPECT_NO_ERROR(error); |
---|
586 | error = GB_write_int(gbe2, 666); TEST_EXPECT_NO_ERROR(error); |
---|
587 | |
---|
588 | TEST_EXPECT_CHCB_COUNTERS(1, 1, 1, 0, NO_TA); // callbacks triggered instantly in NO_TA mode |
---|
589 | TEST_EXPECT_CHCB_COUNTERS(0, 0, 0, 0, WITH_TA); // callbacks delayed until transaction is committed |
---|
590 | |
---|
591 | } // [Note: callbacks happen here in ta_mode] |
---|
592 | |
---|
593 | // test that GB_CB_SON_CREATED is not triggered here: |
---|
594 | TEST_EXPECT_CHCB_COUNTERS(1, 1, 1, 0, NO_TA); |
---|
595 | TEST_EXPECT_CHCB_COUNTERS(1, 1, 1, 0, WITH_TA); |
---|
596 | |
---|
597 | // really create a son |
---|
598 | RESET_CHCB_COUNTERS(); |
---|
599 | { |
---|
600 | GB_transaction ta(gb_main); |
---|
601 | gbe3 = GB_create(gbc, "e3", GB_STRING); |
---|
602 | } |
---|
603 | TEST_EXPECT_CHCB_COUNTERS(0, 0, 0, 0, NO_TA); // broken |
---|
604 | TEST_EXPECT_CHCB___WANTED(0, 0, 1, 1, NO_TA); |
---|
605 | TEST_EXPECT_CHCB_COUNTERS(0, 0, 1, 1, WITH_TA); |
---|
606 | |
---|
607 | // change that son |
---|
608 | RESET_CHCB_COUNTERS(); |
---|
609 | { |
---|
610 | GB_transaction ta(gb_main); |
---|
611 | error = GB_write_string(gbe3, "bla"); TEST_EXPECT_NO_ERROR(error); |
---|
612 | } |
---|
613 | TEST_EXPECT_CHCB_COUNTERS(0, 0, 1, 0, BOTH_TA_MODES); |
---|
614 | |
---|
615 | |
---|
616 | // test delete callbacks |
---|
617 | RESET_CHCB_COUNTERS(); |
---|
618 | { |
---|
619 | GB_transaction ta(gb_main); |
---|
620 | |
---|
621 | GB_add_callback(gbe1, GB_CB_DELETE, makeDatabaseCallback(test_count_cb, &e1_deleted)); |
---|
622 | GB_add_callback(gbe2, GB_CB_DELETE, makeDatabaseCallback(test_count_cb, &e2_deleted)); |
---|
623 | GB_add_callback(gbe3, GB_CB_DELETE, makeDatabaseCallback(test_count_cb, &e3_deleted)); |
---|
624 | GB_add_callback(gbc, GB_CB_DELETE, makeDatabaseCallback(test_count_cb, &c_deleted)); |
---|
625 | } |
---|
626 | TEST_EXPECT_CHCB_COUNTERS(0, 0, 0, 0, BOTH_TA_MODES); // adding callbacks does not trigger existing change-callbacks |
---|
627 | { |
---|
628 | GB_transaction ta(gb_main); |
---|
629 | |
---|
630 | error = GB_delete(gbe3); TEST_EXPECT_NO_ERROR(error); |
---|
631 | error = GB_delete(gbe2); TEST_EXPECT_NO_ERROR(error); |
---|
632 | |
---|
633 | TEST_EXPECT_DLCB_COUNTERS(0, 1, 1, 0, NO_TA); |
---|
634 | TEST_EXPECT_DLCB_COUNTERS(0, 0, 0, 0, WITH_TA); |
---|
635 | } |
---|
636 | |
---|
637 | TEST_EXPECT_CHCB_COUNTERS(0, 0, 1, 0, WITH_TA); // container changed by deleting a son (gbe3); no longer triggers via GB_SON_CHANGED |
---|
638 | TEST_EXPECT_CHCB_COUNTERS(0, 0, 0, 0, NO_TA); // change is not triggered in NO_TA mode (error?) |
---|
639 | TEST_EXPECT_CHCB___WANTED(0, 0, 1, 1, NO_TA); |
---|
640 | |
---|
641 | TEST_EXPECT_DLCB_COUNTERS(0, 1, 1, 0, BOTH_TA_MODES); |
---|
642 | |
---|
643 | RESET_ALL_CB_COUNTERS(); |
---|
644 | { |
---|
645 | GB_transaction ta(gb_main); |
---|
646 | error = GB_delete(gbc); TEST_EXPECT_NO_ERROR(error); // delete the container containing gbe1 and gbe3 (gbe3 alreay deleted) |
---|
647 | } |
---|
648 | TEST_EXPECT_CHCB_COUNTERS(0, 0, 0, 0, BOTH_TA_MODES); // deleting the container does not trigger any change callbacks |
---|
649 | TEST_EXPECT_DLCB_COUNTERS(1, 0, 0, 1, BOTH_TA_MODES); // deleting the container does also trigger the delete callback for gbe1 |
---|
650 | |
---|
651 | // -------------------------------------------------------------------------------- |
---|
652 | // document that a callback now can be removed while it is running |
---|
653 | // (in NO_TA mode; always worked in WITH_TA mode) |
---|
654 | { |
---|
655 | GBDATA *gbe; |
---|
656 | { |
---|
657 | GB_transaction ta(gb_main); |
---|
658 | gbe = GB_create(gb_main, "new_e1", GB_INT); // recreate |
---|
659 | GB_add_callback(gbe, GB_CB_CHANGED, makeDatabaseCallback(remove_self_cb)); |
---|
660 | } |
---|
661 | { GB_transaction ta(gb_main); GB_touch(gbe); } |
---|
662 | } |
---|
663 | |
---|
664 | // test that a callback may remove and re-add itself |
---|
665 | { |
---|
666 | GBDATA *gbn1; |
---|
667 | GBDATA *gbn2; |
---|
668 | |
---|
669 | int counter1 = 0; |
---|
670 | int counter2 = 0; |
---|
671 | |
---|
672 | { |
---|
673 | GB_transaction ta(gb_main); |
---|
674 | gbn1 = GB_create(gb_main, "new_e1", GB_INT); |
---|
675 | gbn2 = GB_create(gb_main, "new_e2", GB_INT); |
---|
676 | GB_add_callback(gbn1, GB_CB_CHANGED, makeDatabaseCallback(re_add_self_cb, &counter1)); |
---|
677 | } |
---|
678 | |
---|
679 | TEST_EXPECT_COUNTER(NO_TA, counter1, 0); // no callback triggered (trigger happens BEFORE call to GB_add_callback in NO_TA mode!) |
---|
680 | TEST_EXPECT_COUNTER(WITH_TA, counter1, 1); // callback gets triggered by GB_create |
---|
681 | TEST_EXPECT_COUNTER(BOTH_TA_MODES, counter2, 0); |
---|
682 | |
---|
683 | counter1 = 0; counter2 = 0; |
---|
684 | |
---|
685 | // test no callback is triggered by just adding a callback |
---|
686 | { |
---|
687 | GB_transaction ta(gb_main); |
---|
688 | GB_add_callback(gbn2, GB_CB_CHANGED, makeDatabaseCallback(re_add_self_cb, &counter2)); |
---|
689 | } |
---|
690 | TEST_EXPECT_COUNTER(BOTH_TA_MODES, counter1, 0); |
---|
691 | TEST_EXPECT_COUNTER(BOTH_TA_MODES, counter2, 0); |
---|
692 | |
---|
693 | { GB_transaction ta(gb_main); GB_touch(gbn1); } |
---|
694 | TEST_EXPECT_COUNTER(BOTH_TA_MODES, counter1, 1); |
---|
695 | TEST_EXPECT_COUNTER(BOTH_TA_MODES, counter2, 0); |
---|
696 | |
---|
697 | { GB_transaction ta(gb_main); GB_touch(gbn2); } |
---|
698 | TEST_EXPECT_COUNTER(BOTH_TA_MODES, counter1, 1); |
---|
699 | TEST_EXPECT_COUNTER(BOTH_TA_MODES, counter2, 1); |
---|
700 | |
---|
701 | { GB_transaction ta(gb_main); } |
---|
702 | TEST_EXPECT_COUNTER(BOTH_TA_MODES, counter1, 1); |
---|
703 | TEST_EXPECT_COUNTER(BOTH_TA_MODES, counter2, 1); |
---|
704 | } |
---|
705 | |
---|
706 | GB_close(gb_main); |
---|
707 | } |
---|
708 | } |
---|
709 | |
---|
710 | struct calledWith { |
---|
711 | RefPtr<GBDATA> gbd; |
---|
712 | GB_CB_TYPE type; |
---|
713 | int time_called; |
---|
714 | |
---|
715 | static int timer; |
---|
716 | |
---|
717 | calledWith(GBDATA *gbd_, GB_CB_TYPE type_) : gbd(gbd_), type(type_), time_called(++timer) {} |
---|
718 | }; |
---|
719 | |
---|
720 | using std::string; |
---|
721 | using std::list; |
---|
722 | |
---|
723 | class callback_trace; |
---|
724 | |
---|
725 | class ct_registry { |
---|
726 | static ct_registry *SINGLETON; |
---|
727 | |
---|
728 | typedef list<callback_trace*> ctl; |
---|
729 | |
---|
730 | ctl traces; |
---|
731 | public: |
---|
732 | ct_registry() { |
---|
733 | gb_assert(!SINGLETON); |
---|
734 | SINGLETON = this; |
---|
735 | } |
---|
736 | ~ct_registry() { |
---|
737 | gb_assert(this == SINGLETON); |
---|
738 | SINGLETON = NULp; |
---|
739 | gb_assert(traces.empty()); |
---|
740 | } |
---|
741 | |
---|
742 | static ct_registry *instance() { gb_assert(SINGLETON); return SINGLETON; } |
---|
743 | |
---|
744 | void add(callback_trace *ct) { traces.push_back(ct); } |
---|
745 | void remove(callback_trace *ct) { traces.remove(ct); } |
---|
746 | |
---|
747 | arb_test::match_expectation expect_all_calls_checked(); |
---|
748 | }; |
---|
749 | ct_registry *ct_registry::SINGLETON = NULp; |
---|
750 | |
---|
751 | int calledWith::timer = 0; |
---|
752 | |
---|
753 | class callback_trace { |
---|
754 | typedef list<calledWith> calledList; |
---|
755 | typedef calledList::iterator calledIter; |
---|
756 | |
---|
757 | calledList called; |
---|
758 | string name; |
---|
759 | |
---|
760 | calledIter find(GBDATA *gbd) { |
---|
761 | calledIter c = called.begin(); |
---|
762 | while (c != called.end()) { |
---|
763 | if (c->gbd == gbd) break; |
---|
764 | ++c; |
---|
765 | } |
---|
766 | return c; |
---|
767 | } |
---|
768 | calledIter find(GB_CB_TYPE exp_type) { |
---|
769 | calledIter c = called.begin(); |
---|
770 | while (c != called.end()) { |
---|
771 | if (c->type&exp_type) break; |
---|
772 | ++c; |
---|
773 | } |
---|
774 | return c; |
---|
775 | } |
---|
776 | calledIter find(GBDATA *gbd, GB_CB_TYPE exp_type) { |
---|
777 | calledIter c = called.begin(); |
---|
778 | while (c != called.end()) { |
---|
779 | if (c->gbd == gbd && (c->type&exp_type)) break; |
---|
780 | ++c; |
---|
781 | } |
---|
782 | return c; |
---|
783 | } |
---|
784 | |
---|
785 | bool removed(calledIter c) { |
---|
786 | if (c == called.end()) return false; |
---|
787 | called.erase(c); |
---|
788 | return true; |
---|
789 | } |
---|
790 | |
---|
791 | public: |
---|
792 | callback_trace(const char *name_) |
---|
793 | : name(name_) |
---|
794 | { |
---|
795 | called.clear(); |
---|
796 | ct_registry::instance()->add(this); |
---|
797 | } |
---|
798 | ~callback_trace() { |
---|
799 | if (was_called()) TEST_EXPECT_EQUAL(name, "has unchecked calls in dtor"); // you forgot to check some calls using TEST_EXPECT_..._TRIGGERED |
---|
800 | ct_registry::instance()->remove(this); |
---|
801 | } |
---|
802 | |
---|
803 | const string& get_name() const { return name; } |
---|
804 | |
---|
805 | void set_called_by(GBDATA *gbd, GB_CB_TYPE type) { called.push_back(calledWith(gbd, type)); } |
---|
806 | |
---|
807 | bool was_called_by(GBDATA *gbd) { return removed(find(gbd)); } |
---|
808 | bool was_called_by(GB_CB_TYPE exp_type) { return removed(find(exp_type)); } |
---|
809 | bool was_called_by(GBDATA *gbd, GB_CB_TYPE exp_type) { return removed(find(gbd, exp_type)); } |
---|
810 | |
---|
811 | int call_time(GBDATA *gbd, GB_CB_TYPE exp_type) { |
---|
812 | calledIter found = find(gbd, exp_type); |
---|
813 | if (found == called.end()) return -1; |
---|
814 | |
---|
815 | int t = found->time_called; |
---|
816 | removed(found); |
---|
817 | return t; |
---|
818 | } |
---|
819 | |
---|
820 | bool was_not_called() const { return called.empty(); } |
---|
821 | bool was_called() const { return !was_not_called(); } |
---|
822 | }; |
---|
823 | |
---|
824 | arb_test::match_expectation ct_registry::expect_all_calls_checked() { |
---|
825 | using namespace arb_test; |
---|
826 | expectation_group expected; |
---|
827 | |
---|
828 | // add failing expectations for all traces with unchecked calls |
---|
829 | for (ctl::iterator t = traces.begin(); t != traces.end(); ++t) { |
---|
830 | callback_trace *ct = *t; |
---|
831 | if (ct->was_called()) { |
---|
832 | expectation_group bad_trace; |
---|
833 | bad_trace.add(that(ct->was_called()).is_equal_to(false)); |
---|
834 | |
---|
835 | const char *failing_trace = ct->get_name().c_str(); |
---|
836 | bad_trace.add(that(failing_trace).does_differ_from(failing_trace)); // display failing_trace in failing expectation |
---|
837 | |
---|
838 | expected.add(all().ofgroup(bad_trace)); |
---|
839 | } |
---|
840 | } |
---|
841 | |
---|
842 | return all().ofgroup(expected); |
---|
843 | } |
---|
844 | |
---|
845 | |
---|
846 | static void some_cb(GBDATA *gbd, callback_trace *trace, GB_CB_TYPE cbtype) { |
---|
847 | trace->set_called_by(gbd, cbtype); |
---|
848 | } |
---|
849 | |
---|
850 | #define TRACESTRUCT(ELEM,FLAVOR) trace_##ELEM##_##FLAVOR |
---|
851 | #define HIERARCHY_TRACESTRUCT(ELEM,FLAVOR) traceHier_##ELEM##_##FLAVOR |
---|
852 | |
---|
853 | #define CONSTRUCT(name) name(#name) // pass instance-name to callback_trace-ctor as char* |
---|
854 | #define TRACECONSTRUCT(ELEM,FLAVOR) CONSTRUCT(TRACESTRUCT(ELEM,FLAVOR)) |
---|
855 | #define HIERARCHY_TRACECONSTRUCT(ELEM,FLAVOR) CONSTRUCT(HIERARCHY_TRACESTRUCT(ELEM,FLAVOR)) |
---|
856 | |
---|
857 | #define ADD_CHANGED_CALLBACK(elem) TEST_EXPECT_NO_ERROR(GB_add_callback(elem, GB_CB_CHANGED, makeDatabaseCallback(some_cb, &TRACESTRUCT(elem,changed)))) |
---|
858 | #define ADD_DELETED_CALLBACK(elem) TEST_EXPECT_NO_ERROR(GB_add_callback(elem, GB_CB_DELETE, makeDatabaseCallback(some_cb, &TRACESTRUCT(elem,deleted)))) |
---|
859 | #define ADD_NWCHILD_CALLBACK(elem) TEST_EXPECT_NO_ERROR(GB_add_callback(elem, GB_CB_SON_CREATED, makeDatabaseCallback(some_cb, &TRACESTRUCT(elem,newchild)))) |
---|
860 | |
---|
861 | #define ADD_CHANGED_HIERARCHY_CALLBACK(elem) TEST_EXPECT_NO_ERROR(GB_add_hierarchy_callback(elem, GB_CB_CHANGED, makeDatabaseCallback(some_cb, &HIERARCHY_TRACESTRUCT(elem,changed)))) |
---|
862 | #define ADD_DELETED_HIERARCHY_CALLBACK(elem) TEST_EXPECT_NO_ERROR(GB_add_hierarchy_callback(elem, GB_CB_DELETE, makeDatabaseCallback(some_cb, &HIERARCHY_TRACESTRUCT(elem,deleted)))) |
---|
863 | #define ADD_NWCHILD_HIERARCHY_CALLBACK(elem) TEST_EXPECT_NO_ERROR(GB_add_hierarchy_callback(elem, GB_CB_SON_CREATED, makeDatabaseCallback(some_cb, &HIERARCHY_TRACESTRUCT(elem,newchild)))) |
---|
864 | |
---|
865 | #define ADD_CHANGED_HIERARCHY_CALLBACK2(gbm,path,id) TEST_EXPECT_NO_ERROR(GB_add_hierarchy_callback(gbm, path, GB_CB_CHANGED, makeDatabaseCallback(some_cb, &HIERARCHY_TRACESTRUCT(id,changed)))) |
---|
866 | #define ADD_DELETED_HIERARCHY_CALLBACK2(gbm,path,id) TEST_EXPECT_NO_ERROR(GB_add_hierarchy_callback(gbm, path, GB_CB_DELETE, makeDatabaseCallback(some_cb, &HIERARCHY_TRACESTRUCT(id,deleted)))) |
---|
867 | #define ADD_NWCHILD_HIERARCHY_CALLBACK2(gbm,path,id) TEST_EXPECT_NO_ERROR(GB_add_hierarchy_callback(gbm, path, GB_CB_SON_CREATED, makeDatabaseCallback(some_cb, &HIERARCHY_TRACESTRUCT(id,newchild)))) |
---|
868 | |
---|
869 | #define ENSURE_CHANGED_CALLBACK(elem) TEST_EXPECT_NO_ERROR(GB_ensure_callback(elem, GB_CB_CHANGED, makeDatabaseCallback(some_cb, &TRACESTRUCT(elem,changed)))) |
---|
870 | #define ENSURE_DELETED_CALLBACK(elem) TEST_EXPECT_NO_ERROR(GB_ensure_callback(elem, GB_CB_DELETE, makeDatabaseCallback(some_cb, &TRACESTRUCT(elem,deleted)))) |
---|
871 | #define ENSURE_NWCHILD_CALLBACK(elem) TEST_EXPECT_NO_ERROR(GB_ensure_callback(elem, GB_CB_SON_CREATED, makeDatabaseCallback(some_cb, &TRACESTRUCT(elem,newchild)))) |
---|
872 | |
---|
873 | #define REMOVE_CHANGED_CALLBACK(elem) GB_remove_callback(elem, GB_CB_CHANGED, makeDatabaseCallback(some_cb, &TRACESTRUCT(elem,changed))) |
---|
874 | #define REMOVE_DELETED_CALLBACK(elem) GB_remove_callback(elem, GB_CB_DELETE, makeDatabaseCallback(some_cb, &TRACESTRUCT(elem,deleted))) |
---|
875 | #define REMOVE_NWCHILD_CALLBACK(elem) GB_remove_callback(elem, GB_CB_SON_CREATED, makeDatabaseCallback(some_cb, &TRACESTRUCT(elem,newchild))) |
---|
876 | |
---|
877 | #define REMOVE_CHANGED_HIERARCHY_CALLBACK(elem) TEST_EXPECT_NO_ERROR(GB_remove_hierarchy_callback(elem, GB_CB_CHANGED, makeDatabaseCallback(some_cb, &HIERARCHY_TRACESTRUCT(elem,changed)))) |
---|
878 | #define REMOVE_DELETED_HIERARCHY_CALLBACK(elem) TEST_EXPECT_NO_ERROR(GB_remove_hierarchy_callback(elem, GB_CB_DELETE, makeDatabaseCallback(some_cb, &HIERARCHY_TRACESTRUCT(elem,deleted)))) |
---|
879 | #define REMOVE_NWCHILD_HIERARCHY_CALLBACK(elem) TEST_EXPECT_NO_ERROR(GB_remove_hierarchy_callback(elem, GB_CB_SON_CREATED, makeDatabaseCallback(some_cb, &HIERARCHY_TRACESTRUCT(elem,newchild)))) |
---|
880 | |
---|
881 | #define REMOVE_CHANGED_HIERARCHY_CALLBACK2(gbm,path,id) TEST_EXPECT_NO_ERROR(GB_remove_hierarchy_callback(gbm, path, GB_CB_CHANGED, makeDatabaseCallback(some_cb, &HIERARCHY_TRACESTRUCT(id, changed)))) |
---|
882 | #define REMOVE_DELETED_HIERARCHY_CALLBACK2(gbm,path,id) TEST_EXPECT_NO_ERROR(GB_remove_hierarchy_callback(gbm, path, GB_CB_DELETE, makeDatabaseCallback(some_cb, &HIERARCHY_TRACESTRUCT(id, deleted)))) |
---|
883 | #define REMOVE_NWCHILD_HIERARCHY_CALLBACK2(gbm,path,id) TEST_EXPECT_NO_ERROR(GB_remove_hierarchy_callback(gbm, path, GB_CB_SON_CREATED, makeDatabaseCallback(some_cb, &HIERARCHY_TRACESTRUCT(id, newchild)))) |
---|
884 | |
---|
885 | #define INIT_CHANGED_CALLBACK(elem) callback_trace TRACECONSTRUCT(elem,changed); ADD_CHANGED_CALLBACK(elem) |
---|
886 | #define INIT_DELETED_CALLBACK(elem) callback_trace TRACECONSTRUCT(elem,deleted); ADD_DELETED_CALLBACK(elem) |
---|
887 | #define INIT_NWCHILD_CALLBACK(elem) callback_trace TRACECONSTRUCT(elem,newchild); ADD_NWCHILD_CALLBACK(elem) |
---|
888 | |
---|
889 | #define INIT_CHANGED_HIERARCHY_CALLBACK(elem) callback_trace HIERARCHY_TRACECONSTRUCT(elem,changed); ADD_CHANGED_HIERARCHY_CALLBACK(elem) |
---|
890 | #define INIT_DELETED_HIERARCHY_CALLBACK(elem) callback_trace HIERARCHY_TRACECONSTRUCT(elem,deleted); ADD_DELETED_HIERARCHY_CALLBACK(elem) |
---|
891 | #define INIT_NWCHILD_HIERARCHY_CALLBACK(elem) callback_trace HIERARCHY_TRACECONSTRUCT(elem,newchild); ADD_NWCHILD_HIERARCHY_CALLBACK(elem) |
---|
892 | |
---|
893 | #define INIT_CHANGED_HIERARCHY_CALLBACK2(gbm,path,id) callback_trace HIERARCHY_TRACECONSTRUCT(id,changed); ADD_CHANGED_HIERARCHY_CALLBACK2(gbm,path,id) |
---|
894 | #define INIT_DELETED_HIERARCHY_CALLBACK2(gbm,path,id) callback_trace HIERARCHY_TRACECONSTRUCT(id,deleted); ADD_DELETED_HIERARCHY_CALLBACK2(gbm,path,id) |
---|
895 | #define INIT_NWCHILD_HIERARCHY_CALLBACK2(gbm,path,id) callback_trace HIERARCHY_TRACECONSTRUCT(id,newchild); ADD_NWCHILD_HIERARCHY_CALLBACK2(gbm,path,id) |
---|
896 | |
---|
897 | #define ENSURE_ENTRY_CALLBACKS(entry) ENSURE_CHANGED_CALLBACK(entry); ENSURE_DELETED_CALLBACK(entry) |
---|
898 | #define ENSURE_CONTAINER_CALLBACKS(cont) ENSURE_CHANGED_CALLBACK(cont); ENSURE_NWCHILD_CALLBACK(cont); ENSURE_DELETED_CALLBACK(cont) |
---|
899 | |
---|
900 | #define REMOVE_ENTRY_CALLBACKS(entry) REMOVE_CHANGED_CALLBACK(entry); REMOVE_DELETED_CALLBACK(entry) |
---|
901 | #define REMOVE_CONTAINER_CALLBACKS(cont) REMOVE_CHANGED_CALLBACK(cont); REMOVE_NWCHILD_CALLBACK(cont); REMOVE_DELETED_CALLBACK(cont) |
---|
902 | |
---|
903 | #define INIT_ENTRY_CALLBACKS(entry) INIT_CHANGED_CALLBACK(entry); INIT_DELETED_CALLBACK(entry) |
---|
904 | #define INIT_CONTAINER_CALLBACKS(cont) INIT_CHANGED_CALLBACK(cont); INIT_NWCHILD_CALLBACK(cont); INIT_DELETED_CALLBACK(cont) |
---|
905 | |
---|
906 | #define TRIGGER_CHANGE(gbd) do { \ |
---|
907 | GB_initial_transaction ta(gb_main); \ |
---|
908 | if (ta.ok()) GB_touch(gbd); \ |
---|
909 | TEST_EXPECT_NO_ERROR(ta.close(NULp)); \ |
---|
910 | } while(0) |
---|
911 | |
---|
912 | #define TRIGGER_2_CHANGES(gbd1, gbd2) do { \ |
---|
913 | GB_initial_transaction ta(gb_main); \ |
---|
914 | if (ta.ok()) { \ |
---|
915 | GB_touch(gbd1); \ |
---|
916 | GB_touch(gbd2); \ |
---|
917 | } \ |
---|
918 | TEST_EXPECT_NO_ERROR(ta.close(NULp)); \ |
---|
919 | } while(0) |
---|
920 | |
---|
921 | #define TRIGGER_DELETE(gbd) do { \ |
---|
922 | GB_initial_transaction ta(gb_main); \ |
---|
923 | GB_ERROR error = NULp; \ |
---|
924 | if (ta.ok()) error = GB_delete(gbd); \ |
---|
925 | TEST_EXPECT_NO_ERROR(ta.close(error)); \ |
---|
926 | } while(0) |
---|
927 | |
---|
928 | #define TEST_EXPECT_CB_TRIGGERED(TRACE,GBD,TYPE) TEST_EXPECT(TRACE.was_called_by(GBD, TYPE)) |
---|
929 | #define TEST_EXPECT_CB_TRIGGERED_AT(TRACE,GBD,TYPE,TIME) TEST_EXPECT_EQUAL(TRACE.call_time(GBD, TYPE), TIME) |
---|
930 | |
---|
931 | #define TEST_EXPECT_CHANGE_TRIGGERED(GBD) TEST_EXPECT_CB_TRIGGERED(TRACESTRUCT(GBD, changed), GBD, GB_CB_CHANGED) |
---|
932 | #define TEST_EXPECT_DELETE_TRIGGERED(GBD) TEST_EXPECT_CB_TRIGGERED(TRACESTRUCT(GBD, deleted), GBD, GB_CB_DELETE) |
---|
933 | #define TEST_EXPECT_NCHILD_TRIGGERED(GBD) TEST_EXPECT_CB_TRIGGERED(TRACESTRUCT(GBD, newchild), GBD, GB_CB_SON_CREATED) |
---|
934 | |
---|
935 | #define TEST_EXPECT_CHANGE_HIER_TRIGGERED(NAME,GBD) TEST_EXPECT_CB_TRIGGERED(HIERARCHY_TRACESTRUCT(NAME, changed), GBD, GB_CB_CHANGED) |
---|
936 | #define TEST_EXPECT_DELETE_HIER_TRIGGERED(NAME,GBD) TEST_EXPECT_CB_TRIGGERED(HIERARCHY_TRACESTRUCT(NAME, deleted), GBD, GB_CB_DELETE) |
---|
937 | #define TEST_EXPECT_NCHILD_HIER_TRIGGERED(NAME,GBD) TEST_EXPECT_CB_TRIGGERED(HIERARCHY_TRACESTRUCT(NAME, newchild), GBD, GB_CB_SON_CREATED) |
---|
938 | |
---|
939 | |
---|
940 | #define TEST_EXPECT_CHANGE_TRIGGERED_AT(TRACE,GBD,TIME) TEST_EXPECT_CB_TRIGGERED_AT(TRACE, GBD, GB_CB_CHANGED, TIME) |
---|
941 | #define TEST_EXPECT_DELETE_TRIGGERED_AT(TRACE,GBD,TIME) TEST_EXPECT_CB_TRIGGERED_AT(TRACE, GBD, GB_CB_DELETE, TIME) |
---|
942 | |
---|
943 | #define TEST_EXPECT_TRIGGERS_CHECKED() TEST_EXPECTATION(trace_registry.expect_all_calls_checked()) |
---|
944 | |
---|
945 | void TEST_db_callbacks() { |
---|
946 | GB_shell shell; |
---|
947 | GBDATA *gb_main = GB_open("new.arb", "c"); |
---|
948 | |
---|
949 | // create some data |
---|
950 | GB_begin_transaction(gb_main); |
---|
951 | |
---|
952 | GBDATA *cont_top = GB_create_container(gb_main, "cont_top"); TEST_REJECT_NULL(cont_top); |
---|
953 | GBDATA *cont_son = GB_create_container(cont_top, "cont_son"); TEST_REJECT_NULL(cont_son); |
---|
954 | |
---|
955 | GBDATA *top = GB_create(gb_main, "top", GB_STRING); TEST_REJECT_NULL(top); |
---|
956 | GBDATA *son = GB_create(cont_top, "son", GB_INT); TEST_REJECT_NULL(son); |
---|
957 | GBDATA *grandson = GB_create(cont_son, "grandson", GB_STRING); TEST_REJECT_NULL(grandson); |
---|
958 | GBDATA *ograndson = GB_create(cont_son, "ograndson", GB_STRING); TEST_REJECT_NULL(ograndson); |
---|
959 | |
---|
960 | GB_commit_transaction(gb_main); |
---|
961 | |
---|
962 | // install callbacks |
---|
963 | GB_begin_transaction(gb_main); |
---|
964 | |
---|
965 | ct_registry trace_registry; |
---|
966 | INIT_CONTAINER_CALLBACKS(cont_top); |
---|
967 | INIT_CONTAINER_CALLBACKS(cont_son); |
---|
968 | INIT_ENTRY_CALLBACKS(top); |
---|
969 | INIT_ENTRY_CALLBACKS(son); |
---|
970 | INIT_ENTRY_CALLBACKS(grandson); |
---|
971 | INIT_ENTRY_CALLBACKS(ograndson); |
---|
972 | |
---|
973 | GB_commit_transaction(gb_main); |
---|
974 | |
---|
975 | TEST_EXPECT_TRIGGERS_CHECKED(); |
---|
976 | |
---|
977 | // trigger change callbacks via change |
---|
978 | GB_begin_transaction(gb_main); |
---|
979 | GB_write_string(top, "hello world"); |
---|
980 | GB_commit_transaction(gb_main); |
---|
981 | TEST_EXPECT_CHANGE_TRIGGERED(top); |
---|
982 | TEST_EXPECT_TRIGGERS_CHECKED(); |
---|
983 | |
---|
984 | GB_begin_transaction(gb_main); |
---|
985 | GB_write_string(top, "hello world"); // no change |
---|
986 | GB_commit_transaction(gb_main); |
---|
987 | TEST_EXPECT_TRIGGERS_CHECKED(); |
---|
988 | |
---|
989 | #if 0 |
---|
990 | // code is wrong (cannot set terminal entry to "marked") |
---|
991 | GB_begin_transaction(gb_main); |
---|
992 | GB_write_flag(son, 1); // only change "mark" |
---|
993 | GB_commit_transaction(gb_main); |
---|
994 | TEST_EXPECT_CHANGE_TRIGGERED(son); |
---|
995 | TEST_EXPECT_CHANGE_TRIGGERED(cont_top); |
---|
996 | TEST_EXPECT_TRIGGER__UNWANTED(trace_cont_top_newchild); // @@@ modifying son should not trigger newchild callback |
---|
997 | TEST_EXPECT_TRIGGERS_CHECKED(); |
---|
998 | #else |
---|
999 | // @@@ add test code similar to wrong section above |
---|
1000 | #endif |
---|
1001 | |
---|
1002 | GB_begin_transaction(gb_main); |
---|
1003 | GB_touch(grandson); |
---|
1004 | GB_commit_transaction(gb_main); |
---|
1005 | TEST_EXPECT_CHANGE_TRIGGERED(grandson); |
---|
1006 | TEST_EXPECT_CHANGE_TRIGGERED(cont_son); |
---|
1007 | TEST_EXPECT_CHANGE_TRIGGERED(cont_top); |
---|
1008 | TEST_EXPECT_TRIGGERS_CHECKED(); |
---|
1009 | |
---|
1010 | // trigger change- and soncreate-callbacks via create |
---|
1011 | |
---|
1012 | GB_begin_transaction(gb_main); |
---|
1013 | GBDATA *son2 = GB_create(cont_top, "son2", GB_INT); TEST_REJECT_NULL(son2); |
---|
1014 | GB_commit_transaction(gb_main); |
---|
1015 | TEST_EXPECT_CHANGE_TRIGGERED(cont_top); |
---|
1016 | TEST_EXPECT_NCHILD_TRIGGERED(cont_top); |
---|
1017 | TEST_EXPECT_TRIGGERS_CHECKED(); |
---|
1018 | |
---|
1019 | GB_begin_transaction(gb_main); |
---|
1020 | GBDATA *grandson2 = GB_create(cont_son, "grandson2", GB_STRING); TEST_REJECT_NULL(grandson2); |
---|
1021 | GB_commit_transaction(gb_main); |
---|
1022 | TEST_EXPECT_CHANGE_TRIGGERED(cont_son); |
---|
1023 | TEST_EXPECT_NCHILD_TRIGGERED(cont_son); |
---|
1024 | TEST_EXPECT_CHANGE_TRIGGERED(cont_top); |
---|
1025 | TEST_EXPECT_TRIGGERS_CHECKED(); |
---|
1026 | |
---|
1027 | // trigger callbacks via delete |
---|
1028 | |
---|
1029 | TRIGGER_DELETE(son2); |
---|
1030 | TEST_EXPECT_CHANGE_TRIGGERED(cont_top); |
---|
1031 | TEST_EXPECT_TRIGGERS_CHECKED(); |
---|
1032 | |
---|
1033 | TRIGGER_DELETE(grandson2); |
---|
1034 | TEST_EXPECT_CHANGE_TRIGGERED(cont_top); |
---|
1035 | TEST_EXPECT_CHANGE_TRIGGERED(cont_son); |
---|
1036 | TEST_EXPECT_TRIGGERS_CHECKED(); |
---|
1037 | |
---|
1038 | TEST_EXPECT_NO_ERROR(GB_request_undo_type(gb_main, GB_UNDO_UNDO)); |
---|
1039 | |
---|
1040 | TRIGGER_DELETE(top); |
---|
1041 | TEST_EXPECT_DELETE_TRIGGERED(top); |
---|
1042 | TEST_EXPECT_TRIGGERS_CHECKED(); |
---|
1043 | |
---|
1044 | TRIGGER_DELETE(grandson); |
---|
1045 | TEST_EXPECT_DELETE_TRIGGERED(grandson); |
---|
1046 | TEST_EXPECT_CHANGE_TRIGGERED(cont_top); |
---|
1047 | TEST_EXPECT_CHANGE_TRIGGERED(cont_son); |
---|
1048 | TEST_EXPECT_TRIGGERS_CHECKED(); |
---|
1049 | |
---|
1050 | TRIGGER_DELETE(cont_son); |
---|
1051 | TEST_EXPECT_DELETE_TRIGGERED(ograndson); |
---|
1052 | TEST_EXPECT_DELETE_TRIGGERED(cont_son); |
---|
1053 | TEST_EXPECT_CHANGE_TRIGGERED(cont_top); |
---|
1054 | TEST_EXPECT_TRIGGERS_CHECKED(); |
---|
1055 | |
---|
1056 | // trigger callbacks by undoing last 3 delete transactions |
---|
1057 | |
---|
1058 | TEST_EXPECT_NO_ERROR(GB_undo(gb_main, GB_UNDO_UNDO)); // undo delete of cont_son |
---|
1059 | TEST_EXPECT_CHANGE_TRIGGERED(cont_top); |
---|
1060 | TEST_EXPECT_NCHILD_TRIGGERED(cont_top); |
---|
1061 | TEST_EXPECT_TRIGGERS_CHECKED(); |
---|
1062 | |
---|
1063 | TEST_EXPECT_NO_ERROR(GB_undo(gb_main, GB_UNDO_UNDO)); // undo delete of grandson |
---|
1064 | // cont_son callbacks are not triggered (they are not restored by undo) |
---|
1065 | TEST_EXPECT_CHANGE_TRIGGERED(cont_top); |
---|
1066 | TEST_EXPECT_TRIGGERS_CHECKED(); |
---|
1067 | |
---|
1068 | TEST_EXPECT_NO_ERROR(GB_undo(gb_main, GB_UNDO_UNDO)); // undo delete of top |
---|
1069 | TEST_EXPECT_TRIGGERS_CHECKED(); |
---|
1070 | |
---|
1071 | // reinstall callbacks that were removed by deletes |
---|
1072 | |
---|
1073 | GB_begin_transaction(gb_main); |
---|
1074 | ENSURE_CONTAINER_CALLBACKS(cont_top); |
---|
1075 | ENSURE_CONTAINER_CALLBACKS(cont_son); |
---|
1076 | ENSURE_ENTRY_CALLBACKS(top); |
---|
1077 | ENSURE_ENTRY_CALLBACKS(son); |
---|
1078 | ENSURE_ENTRY_CALLBACKS(grandson); |
---|
1079 | GB_commit_transaction(gb_main); |
---|
1080 | TEST_EXPECT_TRIGGERS_CHECKED(); |
---|
1081 | |
---|
1082 | // trigger callbacks which will be removed |
---|
1083 | |
---|
1084 | TRIGGER_CHANGE(son); |
---|
1085 | TEST_EXPECT_CHANGE_TRIGGERED(son); |
---|
1086 | TEST_EXPECT_CHANGE_TRIGGERED(cont_top); |
---|
1087 | TEST_EXPECT_TRIGGERS_CHECKED(); |
---|
1088 | |
---|
1089 | GB_begin_transaction(gb_main); |
---|
1090 | GBDATA *son3 = GB_create(cont_top, "son3", GB_INT); TEST_REJECT_NULL(son3); |
---|
1091 | GB_commit_transaction(gb_main); |
---|
1092 | TEST_EXPECT_CHANGE_TRIGGERED(cont_top); |
---|
1093 | TEST_EXPECT_NCHILD_TRIGGERED(cont_top); |
---|
1094 | TEST_EXPECT_TRIGGERS_CHECKED(); |
---|
1095 | |
---|
1096 | // test remove callback |
---|
1097 | |
---|
1098 | GB_begin_transaction(gb_main); |
---|
1099 | REMOVE_ENTRY_CALLBACKS(son); |
---|
1100 | REMOVE_CONTAINER_CALLBACKS(cont_top); |
---|
1101 | GB_commit_transaction(gb_main); |
---|
1102 | TEST_EXPECT_TRIGGERS_CHECKED(); |
---|
1103 | |
---|
1104 | // "trigger" removed callbacks |
---|
1105 | |
---|
1106 | TRIGGER_CHANGE(son); |
---|
1107 | TEST_EXPECT_TRIGGERS_CHECKED(); |
---|
1108 | |
---|
1109 | GB_begin_transaction(gb_main); |
---|
1110 | GBDATA *son4 = GB_create(cont_top, "son4", GB_INT); TEST_REJECT_NULL(son4); |
---|
1111 | GB_commit_transaction(gb_main); |
---|
1112 | TEST_EXPECT_TRIGGERS_CHECKED(); |
---|
1113 | |
---|
1114 | // avoid that GB_close calls delete callbacks (@@@ which might be an error in GB_close!) |
---|
1115 | // by removing remaining callbacks |
---|
1116 | REMOVE_ENTRY_CALLBACKS(grandson); |
---|
1117 | REMOVE_ENTRY_CALLBACKS(top); |
---|
1118 | REMOVE_CONTAINER_CALLBACKS(cont_son); |
---|
1119 | |
---|
1120 | GB_close(gb_main); |
---|
1121 | } |
---|
1122 | |
---|
1123 | __ATTR__REDUCED_OPTIMIZE void TEST_hierarchy_callbacks() { |
---|
1124 | GB_shell shell; |
---|
1125 | const char *DBNAME = "tmp_hier_cb.arb"; |
---|
1126 | |
---|
1127 | for (int pass = 1; pass<=2; ++pass) { |
---|
1128 | bool creating = pass == 1; |
---|
1129 | TEST_ANNOTATE(GBS_global_string("%s database", creating ? "created" : "reloaded")); |
---|
1130 | |
---|
1131 | GBDATA *gb_main = pass == 1 ? GB_open(DBNAME, "cw") : GB_open(DBNAME, "r"); |
---|
1132 | TEST_REJECT_NULL(gb_main); |
---|
1133 | |
---|
1134 | // create some data |
---|
1135 | GB_begin_transaction(gb_main); |
---|
1136 | |
---|
1137 | GBDATA *cont_top1 = creating ? GB_create_container(gb_main, "cont_top") : GB_entry(gb_main, "cont_top"); TEST_REJECT_NULL(cont_top1); |
---|
1138 | GBDATA *cont_top2 = creating ? GB_create_container(gb_main, "cont_top") : GB_nextEntry(cont_top1); TEST_REJECT_NULL(cont_top2); |
---|
1139 | |
---|
1140 | GBDATA *cont_son11 = creating ? GB_create_container(cont_top1, "cont_son") : GB_entry(cont_top1, "cont_son"); TEST_REJECT_NULL(cont_son11); |
---|
1141 | GBDATA *cont_son12 = creating ? GB_create_container(cont_top1, "cont_son") : GB_nextEntry(cont_son11); TEST_REJECT_NULL(cont_son12); // leave this container empty! |
---|
1142 | GBDATA *cont_son21 = creating ? GB_create_container(cont_top2, "cont_son") : GB_entry(cont_top2, "cont_son"); TEST_REJECT_NULL(cont_son21); |
---|
1143 | GBDATA *cont_son22 = creating ? GB_create_container(cont_top2, "cont_son") : GB_nextEntry(cont_son21); TEST_REJECT_NULL(cont_son22); |
---|
1144 | |
---|
1145 | GBDATA *cson_deep = creating ? GB_create_container(cont_son11, "cont_son") : GB_entry(cont_son11, "cont_son"); TEST_REJECT_NULL(cson_deep); |
---|
1146 | |
---|
1147 | GBDATA *top1 = creating ? GB_create(gb_main, "top", GB_STRING) : GB_entry(gb_main, "top"); TEST_REJECT_NULL(top1); |
---|
1148 | GBDATA *top2 = creating ? GB_create(gb_main, "top", GB_STRING) : GB_nextEntry(top1); TEST_REJECT_NULL(top2); |
---|
1149 | |
---|
1150 | GBDATA *son11 = creating ? GB_create(cont_top1, "son", GB_INT) : GB_entry(cont_top1, "son"); TEST_REJECT_NULL(son11); |
---|
1151 | GBDATA *son12 = creating ? GB_create(cont_top1, "son", GB_INT) : GB_nextEntry(son11); TEST_REJECT_NULL(son12); |
---|
1152 | GBDATA *son21 = creating ? GB_create(cont_top2, "son", GB_INT) : GB_entry(cont_top2, "son"); TEST_REJECT_NULL(son21); |
---|
1153 | |
---|
1154 | GBDATA *grandson111 = creating ? GB_create(cont_son11, "grandson", GB_STRING) : GB_entry(cont_son11, "grandson"); TEST_REJECT_NULL(grandson111); |
---|
1155 | GBDATA *grandson112 = creating ? GB_create(cont_son11, "grandson", GB_STRING) : GB_nextEntry(grandson111); TEST_REJECT_NULL(grandson112); |
---|
1156 | GBDATA *grandson211 = creating ? GB_create(cont_son21, "grandson", GB_STRING) : GB_entry(cont_son21, "grandson"); TEST_REJECT_NULL(grandson211); |
---|
1157 | GBDATA *grandson221 = creating ? GB_create(cont_son22, "grandson", GB_STRING) : GB_entry(cont_son22, "grandson"); TEST_REJECT_NULL(grandson221); |
---|
1158 | GBDATA *grandson222 = creating ? GB_create(cont_son22, "grandson", GB_STRING) : GB_nextEntry(grandson221); TEST_REJECT_NULL(grandson222); |
---|
1159 | |
---|
1160 | // create some entries at uncommon hierarchy locations (compared to entries created above) |
---|
1161 | GBDATA *ctop_top = creating ? GB_create (cont_top2, "top", GB_STRING) : GB_entry(cont_top2, "top"); TEST_REJECT_NULL(ctop_top); |
---|
1162 | GBDATA *top_son = creating ? GB_create (gb_main, "son", GB_INT) : GB_entry(gb_main, "son"); TEST_REJECT_NULL(top_son); |
---|
1163 | GBDATA *cson = creating ? GB_create_container(gb_main, "cont_son") : GB_entry(gb_main, "cont_son"); TEST_REJECT_NULL(cson); |
---|
1164 | GBDATA *cson_gs = creating ? GB_create (cson, "grandson", GB_STRING) : GB_entry(cson, "grandson"); TEST_REJECT_NULL(cson_gs); |
---|
1165 | |
---|
1166 | GB_commit_transaction(gb_main); |
---|
1167 | |
---|
1168 | // test gb_hierarchy_location |
---|
1169 | { |
---|
1170 | GB_transaction ta(gb_main); |
---|
1171 | |
---|
1172 | gb_hierarchy_location loc_top(top1); |
---|
1173 | gb_hierarchy_location loc_son(son11); |
---|
1174 | gb_hierarchy_location loc_grandson(grandson222); |
---|
1175 | |
---|
1176 | TEST_REJECT(loc_top.is_submatch()); |
---|
1177 | TEST_REJECT(loc_son.is_submatch()); |
---|
1178 | TEST_REJECT(loc_grandson.is_submatch()); |
---|
1179 | |
---|
1180 | TEST_EXPECT(loc_top.matches(top1)); |
---|
1181 | TEST_EXPECT(loc_top.matches(top2)); |
---|
1182 | TEST_EXPECT(!loc_top.matches(cont_top1)); |
---|
1183 | TEST_EXPECT(!loc_top.matches(son12)); |
---|
1184 | TEST_EXPECT(!loc_top.matches(cont_son22)); |
---|
1185 | TEST_EXPECT(!loc_top.matches(ctop_top)); |
---|
1186 | |
---|
1187 | TEST_EXPECT(loc_son.matches(son11)); |
---|
1188 | TEST_EXPECT(loc_son.matches(son21)); |
---|
1189 | TEST_EXPECT(!loc_son.matches(top1)); |
---|
1190 | TEST_EXPECT(!loc_son.matches(grandson111)); |
---|
1191 | TEST_EXPECT(!loc_son.matches(cont_son22)); |
---|
1192 | TEST_EXPECT(!loc_son.matches(top_son)); |
---|
1193 | |
---|
1194 | TEST_EXPECT(loc_grandson.matches(grandson222)); |
---|
1195 | TEST_EXPECT(loc_grandson.matches(grandson111)); |
---|
1196 | TEST_EXPECT(!loc_grandson.matches(son11)); |
---|
1197 | TEST_EXPECT(!loc_grandson.matches(top1)); |
---|
1198 | TEST_EXPECT(!loc_grandson.matches(cont_son22)); |
---|
1199 | TEST_EXPECT(!loc_grandson.matches(cson_gs)); |
---|
1200 | TEST_EXPECT(!loc_grandson.matches(gb_main)); // nothing matches gb_main |
---|
1201 | |
---|
1202 | gb_hierarchy_location loc_ctop_top(ctop_top); |
---|
1203 | TEST_EXPECT(loc_ctop_top.matches(ctop_top)); |
---|
1204 | TEST_EXPECT(!loc_ctop_top.matches(top1)); |
---|
1205 | |
---|
1206 | gb_hierarchy_location loc_top_son(top_son); |
---|
1207 | TEST_EXPECT(loc_top_son.matches(top_son)); |
---|
1208 | TEST_EXPECT(!loc_top_son.matches(son11)); |
---|
1209 | TEST_EXPECT(!loc_top_son.matches(gb_main)); // nothing matches gb_main |
---|
1210 | |
---|
1211 | gb_hierarchy_location loc_gs(cson_gs); |
---|
1212 | TEST_EXPECT(loc_gs.matches(cson_gs)); |
---|
1213 | TEST_EXPECT(!loc_gs.matches(grandson211)); |
---|
1214 | |
---|
1215 | gb_hierarchy_location loc_root(gb_main); |
---|
1216 | TEST_REJECT(loc_root.is_valid()); // deny binding hierarchy callback to gb_main |
---|
1217 | TEST_EXPECT(!loc_root.matches(gb_main)); // nothing matches gb_main |
---|
1218 | TEST_EXPECT(!loc_root.matches(cont_top1)); // nothing matches an invalid location |
---|
1219 | |
---|
1220 | // test location from/to path |
---|
1221 | { |
---|
1222 | char *path_grandson = loc_grandson.get_db_path(gb_main); |
---|
1223 | TEST_EXPECT_EQUAL(path_grandson, "/cont_top/cont_son/grandson"); |
---|
1224 | |
---|
1225 | gb_hierarchy_location loc_grandson2(gb_main, path_grandson); |
---|
1226 | TEST_EXPECT(loc_grandson2.is_valid()); |
---|
1227 | TEST_EXPECT(loc_grandson == loc_grandson2); |
---|
1228 | |
---|
1229 | char *path_grandson2 = loc_grandson2.get_db_path(gb_main); |
---|
1230 | TEST_EXPECT_EQUAL(path_grandson, path_grandson2); |
---|
1231 | |
---|
1232 | free(path_grandson2); |
---|
1233 | free(path_grandson); |
---|
1234 | } |
---|
1235 | |
---|
1236 | gb_hierarchy_location loc_invalid(gb_main, ""); |
---|
1237 | TEST_REJECT(loc_invalid.is_valid()); |
---|
1238 | TEST_REJECT(loc_invalid == loc_invalid); // invalid locations equal nothing |
---|
1239 | |
---|
1240 | TEST_EXPECT(gb_hierarchy_location(gb_main, "/grandson/cont_top/son").is_valid()); // non-existing path with existing keys is valid |
---|
1241 | TEST_EXPECT(gb_hierarchy_location(gb_main, "/no/such/path").is_valid()); // non-existing keys generate key-quarks on-the-fly |
---|
1242 | TEST_EXPECT(gb_hierarchy_location(gb_main, "/grandson/missing/son").is_valid()); // non-existing keys generate key-quarks on-the-fly |
---|
1243 | |
---|
1244 | gb_hierarchy_location loc_submatch(gb_main, "cont_son/grandson"); // sub-location (any parents will be accepted) |
---|
1245 | TEST_EXPECT(loc_submatch.is_valid()); |
---|
1246 | TEST_EXPECT(loc_submatch.is_submatch()); |
---|
1247 | TEST_EXPECT(loc_submatch.matches(cson_gs)); |
---|
1248 | TEST_EXPECT(loc_submatch.matches(grandson111)); |
---|
1249 | TEST_REJECT(loc_submatch == loc_grandson); // but loc_grandson.matches(grandson111)! |
---|
1250 | |
---|
1251 | gb_hierarchy_location loc_anyson(gb_main, "son"); // sub-location (any 'son' location) |
---|
1252 | TEST_EXPECT(loc_anyson.is_valid()); |
---|
1253 | TEST_EXPECT(loc_anyson.is_submatch()); |
---|
1254 | TEST_EXPECT(loc_anyson.matches(top_son)); |
---|
1255 | TEST_EXPECT(loc_anyson.matches(son11)); |
---|
1256 | TEST_REJECT(loc_anyson == loc_top_son); // but loc_top_son.matches(top_son)! |
---|
1257 | TEST_REJECT(loc_anyson == loc_son); // but loc_son.matches(son11)! |
---|
1258 | |
---|
1259 | TEST_EXPECT_EQUAL(&*SmartCharPtr(loc_submatch.get_db_path(gb_main)), "cont_son/grandson"); |
---|
1260 | TEST_EXPECT_EQUAL(&*SmartCharPtr(loc_anyson .get_db_path(gb_main)), "son"); |
---|
1261 | |
---|
1262 | // test some pathological locations: |
---|
1263 | TEST_REJECT(gb_hierarchy_location(gb_main, "/") .is_valid()); |
---|
1264 | TEST_REJECT(gb_hierarchy_location(gb_main, "//") .is_valid()); |
---|
1265 | TEST_EXPECT(gb_hierarchy_location(gb_main, " / ").is_valid()); // now a valid sub-location (seems like ' ' is a valid key) |
---|
1266 | TEST_REJECT(gb_hierarchy_location(gb_main, "son//son").is_valid()); // invalid sub-location |
---|
1267 | TEST_REJECT(gb_hierarchy_location(gb_main, NULp) .is_valid()); |
---|
1268 | } |
---|
1269 | |
---|
1270 | if (pass == 1) { |
---|
1271 | TEST_EXPECT_NO_ERROR(GB_save_as(gb_main, DBNAME, "wb")); |
---|
1272 | } |
---|
1273 | |
---|
1274 | // instanciate callback_trace data and install hierarchy callbacks |
---|
1275 | GBDATA *anySon = son11; |
---|
1276 | |
---|
1277 | GBDATA *anySonContainer = cont_son11; |
---|
1278 | GBDATA *anotherSonContainer = cont_son22; |
---|
1279 | |
---|
1280 | GBDATA *anyGrandson = grandson221; |
---|
1281 | GBDATA *anotherGrandson = grandson112; |
---|
1282 | GBDATA *elimGrandson = grandson222; |
---|
1283 | GBDATA *elimGrandson2 = grandson111; |
---|
1284 | GBDATA *newGrandson = NULp; |
---|
1285 | |
---|
1286 | ct_registry trace_registry; |
---|
1287 | callback_trace HIERARCHY_TRACECONSTRUCT(anyElem,changed); // no CB added yet |
---|
1288 | INIT_CHANGED_HIERARCHY_CALLBACK(anyGrandson); |
---|
1289 | INIT_DELETED_HIERARCHY_CALLBACK(anyGrandson); |
---|
1290 | INIT_DELETED_HIERARCHY_CALLBACK(anySonContainer); |
---|
1291 | INIT_NWCHILD_HIERARCHY_CALLBACK(anySonContainer); |
---|
1292 | |
---|
1293 | GB_begin_transaction(gb_main); |
---|
1294 | INIT_CHANGED_HIERARCHY_CALLBACK2(gb_main, "son", sub_any_son); |
---|
1295 | INIT_DELETED_HIERARCHY_CALLBACK2(gb_main, "cont_son/grandson", sub_son_grandson); |
---|
1296 | INIT_DELETED_HIERARCHY_CALLBACK2(gb_main, "cont_son", sub_any_sonCont); |
---|
1297 | INIT_NWCHILD_HIERARCHY_CALLBACK2(gb_main, "cont_son", sub_contson); |
---|
1298 | GB_commit_transaction(gb_main); |
---|
1299 | |
---|
1300 | TEST_EXPECT_TRIGGERS_CHECKED(); |
---|
1301 | |
---|
1302 | // trigger change-callback using same DB entry |
---|
1303 | TRIGGER_CHANGE(anyGrandson); |
---|
1304 | TEST_EXPECT_CHANGE_HIER_TRIGGERED(anyGrandson, anyGrandson); |
---|
1305 | TEST_EXPECT_TRIGGERS_CHECKED(); |
---|
1306 | |
---|
1307 | // trigger change-callback using another DB entry (same hierarchy) |
---|
1308 | TRIGGER_CHANGE(anotherGrandson); |
---|
1309 | TEST_EXPECT_CHANGE_HIER_TRIGGERED(anyGrandson, anotherGrandson); |
---|
1310 | TEST_EXPECT_TRIGGERS_CHECKED(); |
---|
1311 | |
---|
1312 | // check only sub-hierarchy-callback is triggered by an element at different hierarchy |
---|
1313 | TRIGGER_CHANGE(anySon); |
---|
1314 | TEST_EXPECT_CHANGE_HIER_TRIGGERED(sub_any_son, anySon); |
---|
1315 | TEST_EXPECT_TRIGGERS_CHECKED(); |
---|
1316 | |
---|
1317 | // trigger change-callback using both DB entries (in two TAs) |
---|
1318 | TRIGGER_CHANGE(anyGrandson); |
---|
1319 | TRIGGER_CHANGE(anotherGrandson); |
---|
1320 | TEST_EXPECT_CHANGE_HIER_TRIGGERED(anyGrandson, anyGrandson); |
---|
1321 | TEST_EXPECT_CHANGE_HIER_TRIGGERED(anyGrandson, anotherGrandson); |
---|
1322 | TEST_EXPECT_TRIGGERS_CHECKED(); |
---|
1323 | |
---|
1324 | // trigger change-callback using both DB entries (in one TA) |
---|
1325 | TRIGGER_2_CHANGES(anyGrandson, anotherGrandson); |
---|
1326 | TEST_EXPECT_CHANGE_HIER_TRIGGERED(anyGrandson, anyGrandson); |
---|
1327 | TEST_EXPECT_CHANGE_HIER_TRIGGERED(anyGrandson, anotherGrandson); |
---|
1328 | TEST_EXPECT_TRIGGERS_CHECKED(); |
---|
1329 | |
---|
1330 | // trigger son-created-callback |
---|
1331 | { |
---|
1332 | GB_initial_transaction ta(gb_main); |
---|
1333 | if (ta.ok()) { |
---|
1334 | GBDATA *someson = GB_create(anySonContainer, "someson", GB_STRING); TEST_REJECT_NULL(someson); |
---|
1335 | } |
---|
1336 | TEST_EXPECT_NO_ERROR(ta.close(NULp)); |
---|
1337 | } |
---|
1338 | TEST_EXPECT_NCHILD_HIER_TRIGGERED(anySonContainer, anySonContainer); |
---|
1339 | TEST_EXPECT_NCHILD_HIER_TRIGGERED(sub_contson, anySonContainer); // sub-hierarchy-callback |
---|
1340 | TEST_EXPECT_TRIGGERS_CHECKED(); |
---|
1341 | |
---|
1342 | // trigger 2 son-created-callbacks (for 2 containers) and one change-callback (for a newly created son) |
---|
1343 | { |
---|
1344 | GB_initial_transaction ta(gb_main); |
---|
1345 | if (ta.ok()) { |
---|
1346 | newGrandson = GB_create(anotherSonContainer, "grandson", GB_STRING); TEST_REJECT_NULL(newGrandson); |
---|
1347 | GBDATA *someson = GB_create(anySonContainer, "someson", GB_STRING); TEST_REJECT_NULL(someson); |
---|
1348 | } |
---|
1349 | TEST_EXPECT_NO_ERROR(ta.close(NULp)); |
---|
1350 | } |
---|
1351 | TEST_EXPECT_CHANGE_HIER_TRIGGERED(anyGrandson, newGrandson); |
---|
1352 | TEST_EXPECT_NCHILD_HIER_TRIGGERED(anySonContainer, anotherSonContainer); |
---|
1353 | TEST_EXPECT_NCHILD_HIER_TRIGGERED(anySonContainer, anySonContainer); |
---|
1354 | TEST_EXPECT_NCHILD_HIER_TRIGGERED(sub_contson, anotherSonContainer); // sub-hierarchy-callback |
---|
1355 | TEST_EXPECT_NCHILD_HIER_TRIGGERED(sub_contson, anySonContainer); // sub-hierarchy-callback |
---|
1356 | TEST_EXPECT_TRIGGERS_CHECKED(); |
---|
1357 | |
---|
1358 | // trigger delete-callback |
---|
1359 | { |
---|
1360 | GB_initial_transaction ta(gb_main); |
---|
1361 | TEST_EXPECT_NO_ERROR(GB_delete(elimGrandson)); |
---|
1362 | TEST_EXPECT_NO_ERROR(ta.close(NULp)); |
---|
1363 | } |
---|
1364 | TEST_EXPECT_DELETE_HIER_TRIGGERED(anyGrandson, elimGrandson); |
---|
1365 | TEST_EXPECT_DELETE_HIER_TRIGGERED(sub_son_grandson, elimGrandson); // sub-hierarchy-callback is triggered as well |
---|
1366 | TEST_EXPECT_TRIGGERS_CHECKED(); |
---|
1367 | |
---|
1368 | // bind normal (non-hierarchical) callbacks to entries which trigger hierarchical callbacks and .. |
---|
1369 | calledWith::timer = 0; |
---|
1370 | GB_begin_transaction(gb_main); |
---|
1371 | |
---|
1372 | INIT_CHANGED_CALLBACK(anotherGrandson); |
---|
1373 | INIT_DELETED_CALLBACK(elimGrandson2); |
---|
1374 | |
---|
1375 | GB_commit_transaction(gb_main); |
---|
1376 | |
---|
1377 | TEST_EXPECT_TRIGGERS_CHECKED(); |
---|
1378 | |
---|
1379 | { |
---|
1380 | GB_initial_transaction ta(gb_main); |
---|
1381 | if (ta.ok()) { |
---|
1382 | GB_touch(anotherGrandson); |
---|
1383 | GB_touch(elimGrandson2); |
---|
1384 | TEST_EXPECT_NO_ERROR(GB_delete(elimGrandson2)); |
---|
1385 | } |
---|
1386 | } |
---|
1387 | |
---|
1388 | // .. test call-order (delete before change, hierarchical before normal): |
---|
1389 | TEST_EXPECT_DELETE_TRIGGERED_AT(traceHier_anyGrandson_deleted, elimGrandson2, 1); |
---|
1390 | TEST_EXPECT_DELETE_TRIGGERED_AT(traceHier_sub_son_grandson_deleted, elimGrandson2, 2); // sub-hierarchy-callback |
---|
1391 | TEST_EXPECT_DELETE_TRIGGERED_AT(trace_elimGrandson2_deleted, elimGrandson2, 3); |
---|
1392 | TEST_EXPECT_CHANGE_TRIGGERED_AT(traceHier_anyGrandson_changed, anotherGrandson, 4); |
---|
1393 | TEST_EXPECT_CHANGE_TRIGGERED_AT(trace_anotherGrandson_changed, anotherGrandson, 5); |
---|
1394 | |
---|
1395 | TEST_EXPECT_TRIGGERS_CHECKED(); |
---|
1396 | |
---|
1397 | // test removed hierarchy callbacks stop to trigger |
---|
1398 | REMOVE_CHANGED_HIERARCHY_CALLBACK(anyGrandson); |
---|
1399 | REMOVE_DELETED_HIERARCHY_CALLBACK(anyGrandson); |
---|
1400 | GB_begin_transaction(gb_main); |
---|
1401 | REMOVE_DELETED_HIERARCHY_CALLBACK2(gb_main, "cont_son/grandson", sub_son_grandson); |
---|
1402 | GB_commit_transaction(gb_main); |
---|
1403 | TRIGGER_CHANGE(anyGrandson); |
---|
1404 | { |
---|
1405 | GB_initial_transaction ta(gb_main); |
---|
1406 | if (ta.ok()) TEST_EXPECT_NO_ERROR(GB_delete(anyGrandson)); |
---|
1407 | } |
---|
1408 | TEST_EXPECT_TRIGGERS_CHECKED(); |
---|
1409 | |
---|
1410 | GBDATA *anyElem; |
---|
1411 | |
---|
1412 | // bind SAME callback to different hierarchy locations |
---|
1413 | anyElem = top1; ADD_CHANGED_HIERARCHY_CALLBACK(anyElem); // binds hierarchy cb to "/top" |
---|
1414 | anyElem = son11; ADD_CHANGED_HIERARCHY_CALLBACK(anyElem); // binds SAME hierarchy cb to "/cont_top/son" |
---|
1415 | |
---|
1416 | // - check both trigger independently and together |
---|
1417 | TRIGGER_CHANGE(top1); |
---|
1418 | TEST_EXPECT_CHANGE_HIER_TRIGGERED(anyElem, top1); |
---|
1419 | TEST_EXPECT_TRIGGERS_CHECKED(); |
---|
1420 | |
---|
1421 | TRIGGER_CHANGE(son11); |
---|
1422 | TEST_EXPECT_CHANGE_HIER_TRIGGERED(anyElem, son11); |
---|
1423 | TEST_EXPECT_CHANGE_HIER_TRIGGERED(sub_any_son, son11); |
---|
1424 | TEST_EXPECT_TRIGGERS_CHECKED(); |
---|
1425 | |
---|
1426 | TRIGGER_2_CHANGES(top1, son11); |
---|
1427 | TEST_EXPECT_CHANGE_HIER_TRIGGERED(anyElem, top1); |
---|
1428 | TEST_EXPECT_CHANGE_HIER_TRIGGERED(anyElem, son11); |
---|
1429 | TEST_EXPECT_CHANGE_HIER_TRIGGERED(sub_any_son, son11); |
---|
1430 | TEST_EXPECT_TRIGGERS_CHECKED(); |
---|
1431 | |
---|
1432 | // - check removing one does not disable the other |
---|
1433 | anyElem = son11; REMOVE_CHANGED_HIERARCHY_CALLBACK(anyElem); // remove hierarchy cb from "/cont_top/son" |
---|
1434 | |
---|
1435 | TRIGGER_2_CHANGES(top1, son11); |
---|
1436 | // son11 no longer triggers -> ok |
---|
1437 | TEST_EXPECT_CHANGE_HIER_TRIGGERED(sub_any_son, son11); // only sub-hierarchy-callback triggers |
---|
1438 | TEST_EXPECT_CHANGE_HIER_TRIGGERED(anyElem, top1); |
---|
1439 | TEST_EXPECT_TRIGGERS_CHECKED(); |
---|
1440 | |
---|
1441 | // test add/remove hierarchy cb by path |
---|
1442 | // (Note: sub-hierarchy-callbacks above also use paths) |
---|
1443 | { |
---|
1444 | const char *locpath = "/cont_top/son"; |
---|
1445 | DatabaseCallback dbcb = makeDatabaseCallback(some_cb, &HIERARCHY_TRACESTRUCT(anyElem,changed)); |
---|
1446 | |
---|
1447 | { |
---|
1448 | GB_transaction ta(gb_main); |
---|
1449 | TEST_EXPECT_NO_ERROR(GB_add_hierarchy_callback(gb_main, locpath, GB_CB_CHANGED, dbcb)); |
---|
1450 | } |
---|
1451 | |
---|
1452 | // now both should trigger again |
---|
1453 | TRIGGER_2_CHANGES(top1, son11); |
---|
1454 | TEST_EXPECT_CHANGE_HIER_TRIGGERED(anyElem, top1); |
---|
1455 | TEST_EXPECT_CHANGE_HIER_TRIGGERED(anyElem, son11); |
---|
1456 | TEST_EXPECT_CHANGE_HIER_TRIGGERED(sub_any_son, son11); // plus sub-hierarchy-callback |
---|
1457 | TEST_EXPECT_TRIGGERS_CHECKED(); |
---|
1458 | |
---|
1459 | { |
---|
1460 | GB_transaction ta(gb_main); |
---|
1461 | TEST_EXPECT_NO_ERROR(GB_remove_hierarchy_callback(gb_main, locpath, GB_CB_CHANGED, dbcb)); |
---|
1462 | } |
---|
1463 | |
---|
1464 | TRIGGER_2_CHANGES(top1, son11); |
---|
1465 | // son11 no longer triggers -> ok |
---|
1466 | TEST_EXPECT_CHANGE_HIER_TRIGGERED(anyElem, top1); |
---|
1467 | TEST_EXPECT_CHANGE_HIER_TRIGGERED(sub_any_son, son11); |
---|
1468 | TEST_EXPECT_TRIGGERS_CHECKED(); |
---|
1469 | |
---|
1470 | // check some failing binds |
---|
1471 | const char *invalidPath = "//such"; |
---|
1472 | { |
---|
1473 | GB_transaction ta(gb_main); |
---|
1474 | TEST_EXPECT_ERROR_CONTAINS(GB_add_hierarchy_callback(gb_main, invalidPath, GB_CB_CHANGED, dbcb), "invalid hierarchy location"); |
---|
1475 | TEST_EXPECT_ERROR_CONTAINS(GB_add_hierarchy_callback(gb_main, GB_CB_CHANGED, dbcb), "invalid hierarchy location"); |
---|
1476 | } |
---|
1477 | |
---|
1478 | // bind a hierarchy callback to a "not yet existing" path (i.e. path containing yet unused keys), |
---|
1479 | // then create an db-entry at that path and test that callback is trigger |
---|
1480 | const char *unknownPath = "/unknownPath/unknownEntry"; |
---|
1481 | { |
---|
1482 | GB_transaction ta(gb_main); |
---|
1483 | TEST_EXPECT_NO_ERROR(GB_add_hierarchy_callback(gb_main, unknownPath, GB_CB_CHANGED, dbcb)); |
---|
1484 | } |
---|
1485 | TEST_EXPECT_TRIGGERS_CHECKED(); |
---|
1486 | |
---|
1487 | GBDATA *gb_unknown; |
---|
1488 | { |
---|
1489 | GB_transaction ta(gb_main); |
---|
1490 | TEST_EXPECT_RESULT__NOERROREXPORTED(gb_unknown = GB_search(gb_main, unknownPath, GB_STRING)); |
---|
1491 | } |
---|
1492 | TEST_EXPECT_TRIGGERS_CHECKED(); // creating an entry does not trigger callback (could call a new callback-type) |
---|
1493 | |
---|
1494 | TRIGGER_CHANGE(gb_unknown); |
---|
1495 | TEST_EXPECT_CHANGE_HIER_TRIGGERED(anyElem, gb_unknown); |
---|
1496 | TEST_EXPECT_TRIGGERS_CHECKED(); |
---|
1497 | } |
---|
1498 | |
---|
1499 | // check container delete callbacks |
---|
1500 | GBDATA *emptySonContainer = cont_son12; |
---|
1501 | |
---|
1502 | TRIGGER_DELETE(emptySonContainer); |
---|
1503 | TEST_EXPECT_DELETE_HIER_TRIGGERED(anySonContainer, emptySonContainer); |
---|
1504 | TEST_EXPECT_DELETE_HIER_TRIGGERED(sub_any_sonCont, emptySonContainer); |
---|
1505 | TEST_EXPECT_TRIGGERS_CHECKED(); |
---|
1506 | |
---|
1507 | TRIGGER_DELETE(cont_top1); // father of cont_son11 |
---|
1508 | TEST_EXPECT_DELETE_HIER_TRIGGERED(anySonContainer, cont_son11); |
---|
1509 | TEST_EXPECT_DELETE_HIER_TRIGGERED(sub_any_sonCont, cont_son11); |
---|
1510 | TEST_EXPECT_DELETE_HIER_TRIGGERED(sub_any_sonCont, cson_deep); |
---|
1511 | TEST_EXPECT_TRIGGERS_CHECKED(); |
---|
1512 | |
---|
1513 | TRIGGER_DELETE(cson); // son container at top-level |
---|
1514 | TEST_EXPECT_DELETE_HIER_TRIGGERED(sub_any_sonCont, cson); |
---|
1515 | TEST_EXPECT_TRIGGERS_CHECKED(); |
---|
1516 | |
---|
1517 | // cleanup |
---|
1518 | GB_close(gb_main); |
---|
1519 | } |
---|
1520 | |
---|
1521 | GB_unlink(DBNAME); |
---|
1522 | } |
---|
1523 | |
---|
1524 | #endif // UNIT_TESTS |
---|
1525 | |
---|
1526 | // -------------------------------------------------------------------------------- |
---|