#include #include #include #include using namespace std; struct Enumerator { string label; Enumerator(string label) : label(label) {} }; inline static bool operator==(const Enumerator& lhs, const Enumerator& rhs) { return lhs.label == rhs.label; } struct CFAEnum { vector> members; string name; CFAEnum& addMember(CFAEnum inlineMember) { members.push_back(inlineMember); return *this; } CFAEnum& addMember(Enumerator enumerator) { members.push_back(enumerator); return *this; } CFAEnum( string name ): name(name) {} }; pair calculateEnumOffset(CFAEnum dst, Enumerator e) { int offset = 0; // std::cout << dst.name << " : " << e.label << std::endl; for (auto v : dst.members) { // std::cout << " offset: " << offset << std::endl; if (holds_alternative(v)) { auto m = get(v); // std::cout << " Enumerator: " << ":" << m.label << std::endl; if (m == e) return make_pair(true, offset); offset++; } else { auto m = get(v); // std::cout << " CFAEnum: " << ":" << m.name << std::endl; auto p = calculateEnumOffset(m, e); if (p.first) return make_pair(true, offset + p.second); offset += p.second; } } // std::cout << "End " << dst.name << " offset " << offset << std::endl; return make_pair(false, offset); } int main() { /** * enum() E1 { A }; // A enum() E2 { B, C }; // B C enum() E3 { D, inline E1, inline E2, E }; // D A B C E enum() E4 { F, inline E3, G }; // F D A B C E G * */ struct Enumerator A("A"), B("B"), C("C"), D("D"), E("E"), F("F"), G("G"); CFAEnum E1("E1"), E2("E2"), E3("E3"), E4("E4"); E1.addMember(A); E2.addMember(B).addMember(C); E3.addMember(D).addMember(E1).addMember(E2).addMember(E); E4.addMember(F).addMember(E3).addMember(G); std::cout << calculateEnumOffset(E3, B).first << " " << calculateEnumOffset(E3, B).second << std::endl; std::cout << calculateEnumOffset(E4, B).first << " " << calculateEnumOffset(E4, B).second << std::endl; std::cout << calculateEnumOffset(E3, E).first << " " << calculateEnumOffset(E3, E).second << std::endl; std::cout << calculateEnumOffset(E4, E).first << " " << calculateEnumOffset(E4, E).second << std::endl; } // Compile g++ -std=c++17 offsetAlgorithm.cc