| 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 | void invalidate() { quark[0] = 0; } |
|---|
| 36 | |
|---|
| 37 | public: |
|---|
| 38 | explicit gb_hierarchy_location(GBDATA *gbd) { |
|---|
| 39 | for (int offset = 0; gbd; ++offset) { |
|---|
| 40 | gb_assert(offset<MAX_HIERARCHY_DEPTH); // increase MAX_HIERARCHY_DEPTH (or use dynamic mem) |
|---|
| 41 | quark[offset] = GB_KEY_QUARK(gbd); |
|---|
| 42 | if (!quark[offset]) return; |
|---|
| 43 | |
|---|
| 44 | gbd = gbd->get_father(); |
|---|
| 45 | } |
|---|
| 46 | gb_assert(0); // did not reach DB-root (invalid entry?) |
|---|
| 47 | } |
|---|
| 48 | gb_hierarchy_location(GBDATA *gb_main, const char *db_path); |
|---|
| 49 | |
|---|
| 50 | bool is_valid() const { return quark[0] != 0; } |
|---|
| 51 | |
|---|
| 52 | bool matches(GBDATA *gbd) const { |
|---|
| 53 | //! return true if 'gbd' is at 'this' hierarchy location |
|---|
| 54 | if (is_valid()) { |
|---|
| 55 | for (int offset = 0; gbd; ++offset) { |
|---|
| 56 | GBQUARK q = GB_KEY_QUARK(gbd); |
|---|
| 57 | if (!quark[offset]) return !q; |
|---|
| 58 | if (q != quark[offset]) return false; |
|---|
| 59 | |
|---|
| 60 | gbd = gbd->get_father(); |
|---|
| 61 | } |
|---|
| 62 | gb_assert(0); // went beyond root |
|---|
| 63 | } |
|---|
| 64 | return false; |
|---|
| 65 | } |
|---|
| 66 | |
|---|
| 67 | bool operator == (const gb_hierarchy_location& other) const { |
|---|
| 68 | if (is_valid() && other.is_valid()) { |
|---|
| 69 | int offset; |
|---|
| 70 | for (offset = 0; quark[offset]; ++offset) { |
|---|
| 71 | if (quark[offset] != other.quark[offset]) return false; |
|---|
| 72 | } |
|---|
| 73 | return other.quark[offset] == 0; |
|---|
| 74 | } |
|---|
| 75 | return false; |
|---|
| 76 | } |
|---|
| 77 | |
|---|
| 78 | char *get_db_path(GBDATA *gb_main) const; |
|---|
| 79 | }; |
|---|
| 80 | |
|---|
| 81 | class gb_hierarchy_callback : public gb_callback { |
|---|
| 82 | gb_hierarchy_location loc; |
|---|
| 83 | public: |
|---|
| 84 | gb_hierarchy_callback(const TypedDatabaseCallback& spec_, const gb_hierarchy_location& loc_) |
|---|
| 85 | : gb_callback(spec_), |
|---|
| 86 | loc(loc_) |
|---|
| 87 | {} |
|---|
| 88 | bool triggered_by(GBDATA *gbd) const { return loc.matches(gbd); } |
|---|
| 89 | const gb_hierarchy_location& get_location() const { return loc; } |
|---|
| 90 | }; |
|---|
| 91 | |
|---|
| 92 | struct gb_hierarchy_callback_list : public CallbackList<gb_hierarchy_callback> { |
|---|
| 93 | // need forward decl for gb_hierarchy_callback_list, i.e. cant use a simple typedef here |
|---|
| 94 | }; |
|---|
| 95 | |
|---|
| 96 | #else |
|---|
| 97 | #error ad_hcb.h included twice |
|---|
| 98 | #endif // AD_HCB_H |
|---|
| 99 | |
|---|
| 100 | |
|---|