| 1 | // =============================================================== // |
|---|
| 2 | // // |
|---|
| 3 | // File : ad_hcb.h // |
|---|
| 4 | // Purpose : hierarchical callbacks // |
|---|
| 5 | // // |
|---|
| 6 | // Coded by Ralf Westram (coder@reallysoft.de) in January 2014 // |
|---|
| 7 | // Institute of Microbiology (Technical University Munich) // |
|---|
| 8 | // http://www.arb-home.de/ // |
|---|
| 9 | // // |
|---|
| 10 | // =============================================================== // |
|---|
| 11 | |
|---|
| 12 | #ifndef AD_HCB_H |
|---|
| 13 | #define AD_HCB_H |
|---|
| 14 | |
|---|
| 15 | #ifndef ARBDB_BASE_H |
|---|
| 16 | #include "arbdb_base.h" |
|---|
| 17 | #endif |
|---|
| 18 | #ifndef GB_LOCAL_H |
|---|
| 19 | #include "gb_local.h" |
|---|
| 20 | #endif |
|---|
| 21 | #ifndef GB_KEY_H |
|---|
| 22 | #include "gb_key.h" |
|---|
| 23 | #endif |
|---|
| 24 | |
|---|
| 25 | // -------------------------------- |
|---|
| 26 | // hierarchical callbacks |
|---|
| 27 | |
|---|
| 28 | |
|---|
| 29 | /*! Stores path to a specific location in DB hierarchy as list of GBQUARKs |
|---|
| 30 | */ |
|---|
| 31 | class gb_hierarchy_location { |
|---|
| 32 | static const int MAX_HIERARCHY_DEPTH = 10; // no real limit, just avoids dynamic allocation |
|---|
| 33 | GBQUARK quark[MAX_HIERARCHY_DEPTH]; |
|---|
| 34 | |
|---|
| 35 | public: |
|---|
| 36 | gb_hierarchy_location(GBDATA *gbd) { |
|---|
| 37 | for (int offset = 0; gbd; ++offset) { |
|---|
| 38 | gb_assert(offset<MAX_HIERARCHY_DEPTH); // increase MAX_HIERARCHY_DEPTH (or use dynamic mem) |
|---|
| 39 | quark[offset] = GB_KEY_QUARK(gbd); |
|---|
| 40 | if (!quark[offset]) return; |
|---|
| 41 | |
|---|
| 42 | gbd = gbd->get_father(); |
|---|
| 43 | } |
|---|
| 44 | gb_assert(0); // did not reach DB-root (invalid entry?) |
|---|
| 45 | } |
|---|
| 46 | |
|---|
| 47 | bool matches(GBDATA *gbd) const { |
|---|
| 48 | //! return true if 'gbd' is at 'this' hierarchy location |
|---|
| 49 | for (int offset = 0; gbd; ++offset) { |
|---|
| 50 | GBQUARK q = GB_KEY_QUARK(gbd); |
|---|
| 51 | if (!quark[offset]) return !q; |
|---|
| 52 | if (q != quark[offset]) return false; |
|---|
| 53 | |
|---|
| 54 | gbd = gbd->get_father(); |
|---|
| 55 | } |
|---|
| 56 | |
|---|
| 57 | gb_assert(0); |
|---|
| 58 | return false; |
|---|
| 59 | } |
|---|
| 60 | }; |
|---|
| 61 | |
|---|
| 62 | class gb_hierarchy_callback : public gb_callback { |
|---|
| 63 | gb_hierarchy_location loc; |
|---|
| 64 | public: |
|---|
| 65 | gb_hierarchy_callback(const TypedDatabaseCallback& spec_, GBDATA *gbd_representative) |
|---|
| 66 | : gb_callback(spec_), |
|---|
| 67 | loc(gbd_representative) |
|---|
| 68 | {} |
|---|
| 69 | |
|---|
| 70 | bool triggered_by(GBDATA *gbd) const { return loc.matches(gbd); } |
|---|
| 71 | }; |
|---|
| 72 | |
|---|
| 73 | struct gb_hierarchy_callback_list : public CallbackList<gb_hierarchy_callback> { |
|---|
| 74 | // need forward decl for gb_hierarchy_callback_list, i.e. cant use a simple typedef here |
|---|
| 75 | }; |
|---|
| 76 | |
|---|
| 77 | #else |
|---|
| 78 | #error ad_hcb.h included twice |
|---|
| 79 | #endif // AD_HCB_H |
|---|