[d6c5faa] | 1 | #include <string> |
---|
| 2 | #include <variant> |
---|
| 3 | #include <vector> |
---|
| 4 | #include <iostream> |
---|
| 5 | |
---|
| 6 | using namespace std; |
---|
| 7 | struct Enumerator { |
---|
| 8 | string label; |
---|
| 9 | |
---|
| 10 | Enumerator(string label) : label(label) {} |
---|
| 11 | }; |
---|
| 12 | |
---|
| 13 | inline static bool operator==(const Enumerator& lhs, const Enumerator& rhs) { |
---|
| 14 | return lhs.label == rhs.label; |
---|
| 15 | } |
---|
| 16 | |
---|
| 17 | struct CFAEnum { |
---|
| 18 | vector<variant<CFAEnum, Enumerator>> members; |
---|
| 19 | string name; |
---|
| 20 | |
---|
| 21 | CFAEnum& addMember(CFAEnum inlineMember) { |
---|
| 22 | members.push_back(inlineMember); |
---|
| 23 | return *this; |
---|
| 24 | } |
---|
| 25 | |
---|
| 26 | CFAEnum& addMember(Enumerator enumerator) { |
---|
| 27 | members.push_back(enumerator); |
---|
| 28 | return *this; |
---|
| 29 | } |
---|
| 30 | |
---|
| 31 | CFAEnum( string name ): name(name) {} |
---|
| 32 | }; |
---|
| 33 | |
---|
| 34 | pair<bool, int> calculateEnumOffset(CFAEnum dst, Enumerator e) { |
---|
| 35 | int offset = 0; |
---|
| 36 | // std::cout << dst.name << " : " << e.label << std::endl; |
---|
| 37 | for (auto v : dst.members) { |
---|
| 38 | // std::cout << " offset: " << offset << std::endl; |
---|
| 39 | if (holds_alternative<Enumerator>(v)) { |
---|
| 40 | auto m = get<Enumerator>(v); |
---|
| 41 | // std::cout << " Enumerator: " << ":" << m.label << std::endl; |
---|
| 42 | if (m == e) return make_pair(true, offset); |
---|
| 43 | offset++; |
---|
| 44 | |
---|
| 45 | } else { |
---|
| 46 | auto m = get<CFAEnum>(v); |
---|
| 47 | // std::cout << " CFAEnum: " << ":" << m.name << std::endl; |
---|
| 48 | auto p = calculateEnumOffset(m, e); |
---|
| 49 | if (p.first) return make_pair(true, offset + p.second); |
---|
| 50 | offset += p.second; |
---|
| 51 | } |
---|
| 52 | } |
---|
| 53 | // std::cout << "End " << dst.name << " offset " << offset << std::endl; |
---|
| 54 | return make_pair(false, offset); |
---|
| 55 | } |
---|
| 56 | |
---|
| 57 | int main() { |
---|
| 58 | /** |
---|
| 59 | * enum() E1 { A }; // A |
---|
| 60 | enum() E2 { B, C }; // B C |
---|
| 61 | enum() E3 { D, inline E1, inline E2, E }; // D A B C E |
---|
| 62 | enum() E4 { F, inline E3, G }; // F D A B C E G |
---|
| 63 | * |
---|
| 64 | */ |
---|
| 65 | struct Enumerator A("A"), B("B"), C("C"), D("D"), E("E"), F("F"), G("G"); |
---|
| 66 | CFAEnum E1("E1"), E2("E2"), E3("E3"), E4("E4"); |
---|
| 67 | E1.addMember(A); |
---|
| 68 | E2.addMember(B).addMember(C); |
---|
| 69 | E3.addMember(D).addMember(E1).addMember(E2).addMember(E); |
---|
| 70 | E4.addMember(F).addMember(E3).addMember(G); |
---|
| 71 | std::cout << calculateEnumOffset(E3, B).first << " " << calculateEnumOffset(E3, B).second << std::endl; |
---|
| 72 | std::cout << calculateEnumOffset(E4, B).first << " " << calculateEnumOffset(E4, B).second << std::endl; |
---|
| 73 | std::cout << calculateEnumOffset(E3, E).first << " " << calculateEnumOffset(E3, E).second << std::endl; |
---|
| 74 | std::cout << calculateEnumOffset(E4, E).first << " " << calculateEnumOffset(E4, E).second << std::endl; |
---|
| 75 | } |
---|
| 76 | |
---|
| 77 | // Compile g++ -std=c++17 offsetAlgorithm.cc |
---|