| 1 | // =============================================================== // |
|---|
| 2 | // // |
|---|
| 3 | // File : ED4_objspec.cxx // |
|---|
| 4 | // Purpose : hierarchy object specification // |
|---|
| 5 | // // |
|---|
| 6 | // Coded by Ralf Westram (coder@reallysoft.de) in October 2011 // |
|---|
| 7 | // Institute of Microbiology (Technical University Munich) // |
|---|
| 8 | // http://www.arb-home.de/ // |
|---|
| 9 | // // |
|---|
| 10 | // =============================================================== // |
|---|
| 11 | |
|---|
| 12 | #include "ed4_class.hxx" |
|---|
| 13 | |
|---|
| 14 | #define MAX_POSSIBLE_SPECIFIED_OBJECT_TYPES ((sizeof(ED4_level)*8)-1) |
|---|
| 15 | |
|---|
| 16 | STATIC_ASSERT(SPECIFIED_OBJECT_TYPES <= MAX_POSSIBLE_SPECIFIED_OBJECT_TYPES); |
|---|
| 17 | |
|---|
| 18 | inline int level2index(ED4_level lev) { |
|---|
| 19 | int index = 0; |
|---|
| 20 | e4_assert(lev); |
|---|
| 21 | while (lev) { |
|---|
| 22 | if (lev&1) { |
|---|
| 23 | e4_assert(lev == 1); |
|---|
| 24 | return index; |
|---|
| 25 | } |
|---|
| 26 | lev = ED4_level(lev>>1); |
|---|
| 27 | index++; |
|---|
| 28 | } |
|---|
| 29 | e4_assert(0); // invalid level |
|---|
| 30 | return -1U; |
|---|
| 31 | } |
|---|
| 32 | inline ED4_level index2level(int index) { return ED4_level(1<<index); } |
|---|
| 33 | |
|---|
| 34 | |
|---|
| 35 | static class ED4_objspec_registry& get_objspec_registry(); |
|---|
| 36 | |
|---|
| 37 | class ED4_objspec_registry : virtual Noncopyable { |
|---|
| 38 | ED4_objspec *known_spec[SPECIFIED_OBJECT_TYPES]; |
|---|
| 39 | int count; |
|---|
| 40 | |
|---|
| 41 | ED4_objspec_registry() : count(0) { |
|---|
| 42 | for (int i = 0; i<SPECIFIED_OBJECT_TYPES; ++i) known_spec[i] = 0; |
|---|
| 43 | } |
|---|
| 44 | friend ED4_objspec_registry& get_objspec_registry(); |
|---|
| 45 | |
|---|
| 46 | public: |
|---|
| 47 | |
|---|
| 48 | void register_objspec(ED4_objspec *ospec) { |
|---|
| 49 | int idx = level2index(ospec->level); |
|---|
| 50 | |
|---|
| 51 | e4_assert(ospec); |
|---|
| 52 | e4_assert(idx<SPECIFIED_OBJECT_TYPES); |
|---|
| 53 | e4_assert(!known_spec[idx]); // two object types have same level |
|---|
| 54 | |
|---|
| 55 | known_spec[idx] = ospec; |
|---|
| 56 | count++; |
|---|
| 57 | } |
|---|
| 58 | |
|---|
| 59 | int count_registered() const { |
|---|
| 60 | return count; |
|---|
| 61 | } |
|---|
| 62 | |
|---|
| 63 | const ED4_objspec *get_object_spec_at_index(int index) const { |
|---|
| 64 | e4_assert(index >= 0 && index<SPECIFIED_OBJECT_TYPES); |
|---|
| 65 | return known_spec[index]; |
|---|
| 66 | } |
|---|
| 67 | const ED4_objspec& get_object_spec(ED4_level lev) const { return *get_object_spec_at_index(level2index(lev)); } |
|---|
| 68 | |
|---|
| 69 | bool has_manager_that_may_contain(ED4_level lev) const { |
|---|
| 70 | for (int i = 0; i<SPECIFIED_OBJECT_TYPES; ++i) { |
|---|
| 71 | ED4_objspec *spec = known_spec[i]; |
|---|
| 72 | if (spec && spec->is_manager() && spec->allowed_to_contain(lev)) |
|---|
| 73 | return true; |
|---|
| 74 | } |
|---|
| 75 | return false; |
|---|
| 76 | } |
|---|
| 77 | |
|---|
| 78 | void init_object_specs() { |
|---|
| 79 | #if defined(DEBUG) |
|---|
| 80 | for (int i = 1; i<SPECIFIED_OBJECT_TYPES; ++i) { |
|---|
| 81 | if (known_spec[i]) { |
|---|
| 82 | ED4_objspec& spec = *known_spec[i]; |
|---|
| 83 | e4_assert(has_manager_that_may_contain(spec.level)); |
|---|
| 84 | } |
|---|
| 85 | } |
|---|
| 86 | #endif |
|---|
| 87 | } |
|---|
| 88 | }; |
|---|
| 89 | |
|---|
| 90 | static ED4_objspec_registry& get_objspec_registry() { |
|---|
| 91 | static ED4_objspec_registry objspec_registry; |
|---|
| 92 | return objspec_registry; |
|---|
| 93 | } |
|---|
| 94 | |
|---|
| 95 | bool ED4_objspec::object_specs_initialized = false; |
|---|
| 96 | bool ED4_objspec::descendants_uptodate = false; |
|---|
| 97 | |
|---|
| 98 | void ED4_objspec::init_object_specs() { |
|---|
| 99 | e4_assert(!object_specs_initialized); |
|---|
| 100 | get_objspec_registry().init_object_specs(); |
|---|
| 101 | object_specs_initialized = true; |
|---|
| 102 | } |
|---|
| 103 | |
|---|
| 104 | void ED4_objspec::calc_descendants() const { |
|---|
| 105 | if (possible_descendants == ED4_L_INVALID) { |
|---|
| 106 | possible_descendants = ED4_L_NO_LEVEL; |
|---|
| 107 | allowed_descendants = ED4_L_NO_LEVEL; |
|---|
| 108 | if (used_children || allowed_children) { |
|---|
| 109 | possible_descendants = used_children; |
|---|
| 110 | allowed_descendants = allowed_children; |
|---|
| 111 | |
|---|
| 112 | ED4_objspec_registry& objspec_registry = get_objspec_registry(); |
|---|
| 113 | |
|---|
| 114 | for (int i = 0; i<SPECIFIED_OBJECT_TYPES; ++i) { |
|---|
| 115 | const ED4_objspec *child_spec = objspec_registry.get_object_spec_at_index(i); |
|---|
| 116 | if (child_spec && child_spec != this) { |
|---|
| 117 | if ((used_children|allowed_children) & child_spec->level) { |
|---|
| 118 | child_spec->calc_descendants(); |
|---|
| 119 | if (used_children & child_spec->level) possible_descendants = ED4_level(possible_descendants|child_spec->possible_descendants); |
|---|
| 120 | if (allowed_children & child_spec->level) allowed_descendants = ED4_level(allowed_descendants |child_spec->allowed_descendants); |
|---|
| 121 | } |
|---|
| 122 | } |
|---|
| 123 | } |
|---|
| 124 | // dump(2); |
|---|
| 125 | } |
|---|
| 126 | } |
|---|
| 127 | } |
|---|
| 128 | void ED4_objspec::recalc_descendants() { |
|---|
| 129 | ED4_objspec_registry& objspec_registry = get_objspec_registry(); |
|---|
| 130 | |
|---|
| 131 | for (int i = 0; i<SPECIFIED_OBJECT_TYPES; ++i) { |
|---|
| 132 | const ED4_objspec *spec = objspec_registry.get_object_spec_at_index(i); |
|---|
| 133 | if (spec) { |
|---|
| 134 | spec->possible_descendants = ED4_L_INVALID; |
|---|
| 135 | spec->allowed_descendants = ED4_L_INVALID; |
|---|
| 136 | } |
|---|
| 137 | } |
|---|
| 138 | for (int i = 0; i<SPECIFIED_OBJECT_TYPES; ++i) { |
|---|
| 139 | const ED4_objspec *spec = objspec_registry.get_object_spec_at_index(i); |
|---|
| 140 | if (spec) spec->calc_descendants(); |
|---|
| 141 | } |
|---|
| 142 | |
|---|
| 143 | descendants_uptodate = true; |
|---|
| 144 | } |
|---|
| 145 | |
|---|
| 146 | ED4_objspec::ED4_objspec(ED4_properties static_prop_, ED4_level level_, ED4_level allowed_children_, ED4_level handled_level_, ED4_level restriction_level_) |
|---|
| 147 | : used_children(ED4_L_NO_LEVEL), |
|---|
| 148 | possible_descendants(ED4_L_INVALID), |
|---|
| 149 | allowed_descendants(ED4_L_INVALID), |
|---|
| 150 | static_prop(static_prop_), |
|---|
| 151 | level(level_), |
|---|
| 152 | allowed_children(allowed_children_), |
|---|
| 153 | handled_level(handled_level_), |
|---|
| 154 | restriction_level(restriction_level_) |
|---|
| 155 | { |
|---|
| 156 | e4_assert(!object_specs_initialized); // specs must be instaciated before they are initialized |
|---|
| 157 | |
|---|
| 158 | switch (static_prop&(ED4_P_IS_MANAGER|ED4_P_IS_TERMINAL)) { |
|---|
| 159 | case ED4_P_IS_MANAGER: // manager specific checks |
|---|
| 160 | e4_assert(static_prop & (ED4_P_HORIZONTAL|ED4_P_VERTICAL)); // each manager has to be vertical or horizontal |
|---|
| 161 | e4_assert((static_prop & (ED4_P_HORIZONTAL|ED4_P_VERTICAL)) != (ED4_P_HORIZONTAL|ED4_P_VERTICAL)); // but never both |
|---|
| 162 | e4_assert(allowed_children != ED4_L_NO_LEVEL); // managers need to allow children (what else should they manage) |
|---|
| 163 | break; |
|---|
| 164 | |
|---|
| 165 | case ED4_P_IS_TERMINAL: // terminal specific checks |
|---|
| 166 | e4_assert((static_prop & (ED4_P_HORIZONTAL|ED4_P_VERTICAL)) == 0); // terminals do not have orientation |
|---|
| 167 | e4_assert(allowed_children == ED4_L_NO_LEVEL); // terminals cannot have children |
|---|
| 168 | break; |
|---|
| 169 | |
|---|
| 170 | default : |
|---|
| 171 | e4_assert(0); |
|---|
| 172 | break; |
|---|
| 173 | } |
|---|
| 174 | |
|---|
| 175 | get_objspec_registry().register_objspec(this); |
|---|
| 176 | } |
|---|
| 177 | |
|---|
| 178 | |
|---|
| 179 | // -------------------------------------------------------------------------------- |
|---|
| 180 | |
|---|
| 181 | #ifdef UNIT_TESTS |
|---|
| 182 | #ifndef TEST_UNIT_H |
|---|
| 183 | #include <test_unit.h> |
|---|
| 184 | #endif |
|---|
| 185 | |
|---|
| 186 | |
|---|
| 187 | void TEST_objspec_registry() { |
|---|
| 188 | TEST_EXPECT_EQUAL(level2index(ED4_level(0x1)), 0); |
|---|
| 189 | TEST_EXPECT_EQUAL(level2index(ED4_level(0x2)), 1); |
|---|
| 190 | TEST_EXPECT_EQUAL(level2index(ED4_level(0x4)), 2); |
|---|
| 191 | TEST_EXPECT_EQUAL(level2index(ED4_level(0x10000)), 16); |
|---|
| 192 | |
|---|
| 193 | for (int i = 0; i<SPECIFIED_OBJECT_TYPES; ++i) { |
|---|
| 194 | TEST_EXPECT_EQUAL(level2index(index2level(i)), i); |
|---|
| 195 | } |
|---|
| 196 | |
|---|
| 197 | ED4_objspec_registry& objspec_registry = get_objspec_registry(); |
|---|
| 198 | TEST_EXPECT(objspec_registry.count_registered()>0); |
|---|
| 199 | TEST_EXPECT_EQUAL(objspec_registry.count_registered(), SPECIFIED_OBJECT_TYPES); |
|---|
| 200 | |
|---|
| 201 | TEST_EXPECT_EQUAL(objspec_registry.get_object_spec(ED4_L_ROOT).allowed_children, ED4_L_ROOTGROUP); |
|---|
| 202 | |
|---|
| 203 | ED4_objspec::init_object_specs(); |
|---|
| 204 | |
|---|
| 205 | const ED4_objspec& multi_seq = objspec_registry.get_object_spec(ED4_L_MULTI_SEQUENCE); |
|---|
| 206 | const ED4_objspec& seq = objspec_registry.get_object_spec(ED4_L_SEQUENCE); |
|---|
| 207 | |
|---|
| 208 | TEST_EXPECT_ZERO(seq.get_possible_descendants() & ED4_L_SEQUENCE_STRING); |
|---|
| 209 | TEST_EXPECT_ZERO(multi_seq.get_possible_descendants() & ED4_L_SEQUENCE_STRING); |
|---|
| 210 | |
|---|
| 211 | TEST_REJECT_ZERO(seq.get_allowed_descendants() & ED4_L_SEQUENCE_STRING); |
|---|
| 212 | TEST_REJECT_ZERO(multi_seq.get_allowed_descendants() & ED4_L_SEQUENCE_STRING); |
|---|
| 213 | |
|---|
| 214 | TEST_EXPECT_ZERO(multi_seq.get_allowed_descendants() & ED4_L_ROOTGROUP); |
|---|
| 215 | |
|---|
| 216 | // simulate adding sth in the hierarchy |
|---|
| 217 | multi_seq.announce_added(ED4_L_SEQUENCE); |
|---|
| 218 | seq.announce_added(ED4_L_SEQUENCE_STRING); |
|---|
| 219 | |
|---|
| 220 | TEST_REJECT_ZERO(seq.get_possible_descendants() & ED4_L_SEQUENCE_STRING); |
|---|
| 221 | TEST_REJECT_ZERO(multi_seq.get_possible_descendants() & ED4_L_SEQUENCE_STRING); |
|---|
| 222 | |
|---|
| 223 | TEST_EXPECT_ZERO(multi_seq.get_possible_descendants() & ED4_L_SEQUENCE_INFO); |
|---|
| 224 | |
|---|
| 225 | // add more (checks refresh) |
|---|
| 226 | seq.announce_added(ED4_L_SEQUENCE_INFO); |
|---|
| 227 | |
|---|
| 228 | TEST_REJECT_ZERO(multi_seq.get_possible_descendants() & ED4_L_SEQUENCE_INFO); |
|---|
| 229 | } |
|---|
| 230 | |
|---|
| 231 | #endif // UNIT_TESTS |
|---|
| 232 | |
|---|
| 233 | // -------------------------------------------------------------------------------- |
|---|