| e8555c89 Andy McCall | 1 | |
| 2 | |
|
| 3 | ||
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | // |
|
| 9 | // Quick and dirty reflection on C++ enums (in the future replace with c++23 reflexpr). |
|
| 10 | // |
|
| 11 | |
|
| 12 | // Print an enum type as a string, for example: |
|
| 13 | // os << EnumTypeName<SomeEnumType>() << '\n'; |
|
| 14 | ; |
|
| 15 | // You _must_ overload 'operator<<' for all enum types for which you want this to work. |
|
| 16 | std::ostream& ; |
|
| 17 | ||
| 18 | // Print an enum value as a string, for example: |
|
| 19 | // os << EnumValueName{myEnumValue} << '\n'; |
|
| 20 | |
|
| 21 | |
|
| 22 | |
|
| 23 | |
|
| 24 | ; |
|
| 25 | // You _must_ overload 'operator<<' for all enum types for which you want this to work. |
|
| 26 | std::ostream& ; |
|
| 27 | ||
| 28 | ||
| 29 | ||
| 30 | ||
| 31 | // A collection of (simple) profile counters: |
|
| 32 | // - Counters start at zero. |
|
| 33 | // - An individual counter can be incremented by 1 via 'tick(<counter-id>)'. |
|
| 34 | // - When this 'ProfileCounters' object is destroyed it prints the value of each |
|
| 35 | // counter. |
|
| 36 | // |
|
| 37 | // Template parameters: |
|
| 38 | // - bool ENABLED: |
|
| 39 | // when false, the optimizer should be able to completely optimize-out all |
|
| 40 | // profile related code |
|
| 41 | // - typename ENUM: |
|
| 42 | // Must be a c++ enum (or enum class) which satisfies the following requirements: |
|
| 43 | // * The numerical values must be 0, 1, ... IOW the values must be usable as |
|
| 44 | // indices in an array. (This is automatically the case if you don't |
|
| 45 | // manually assign numeric values). |
|
| 46 | // * There must be an enum value with the name 'NUM' which has a numerical |
|
| 47 | // value equal to the number of other values in this enum. (This is |
|
| 48 | // automatically the case if you put 'NUM' last in the list of enum |
|
| 49 | // values). |
|
| 50 | // * You must overload the following two functions for the type 'ENUM': |
|
| 51 | // std::ostream& operator<<(std::ostream& os, EnumTypeName<ENUM>); |
|
| 52 | // std::ostream& operator<<(std::ostream& os, EnumValueName<ENUM> evn); |
|
| 53 | // |
|
| 54 | // Example usage: |
|
| 55 | // enum class WidgetProfileCounter { |
|
| 56 | // CALCULATE, |
|
| 57 | // INVALIDATE, |
|
| 58 | // NUM // must be last |
|
| 59 | // }; |
|
| 60 | // std::ostream& operator<<(std::ostream& os, EnumTypeName<WidgetProfileCounter>) |
|
| 61 | // { |
|
| 62 | // return os << "WidgetProfileCounter"; |
|
| 63 | // } |
|
| 64 | // std::ostream& operator<<(std::ostream& os, EnumValueName<WidgetProfileCounter> evn) |
|
| 65 | // { |
|
| 66 | // std::string_view names[size_t(WidgetProfileCounter::NUM)] = { |
|
| 67 | // "calculate"sv, "invalidate"sv |
|
| 68 | // }; |
|
| 69 | // return os << names[size_t(evn.e)]; |
|
| 70 | // } |
|
| 71 | // |
|
| 72 | // constexpr bool PRINT_WIDGET_PROFILE = true; // possibly depending on some #define |
|
| 73 | // |
|
| 74 | // struct Widget : private ProfileCounters<PRINT_WIDGET_PROFILE, WidgetProfileCounter> |
|
| 75 | // { |
|
| 76 | // void calculate() { |
|
| 77 | // tick(WidgetProfileCounter::CALCULATE); |
|
| 78 | // // ... |
|
| 79 | // } |
|
| 80 | // void invalidate() { |
|
| 81 | // tick(WidgetProfileCounter::CALCULATE); |
|
| 82 | // // ... |
|
| 83 | // } |
|
| 84 | // }; |
|
| 85 | |
|
| 86 | |
|
| 87 | |
|
| 88 | |
|
| 89 | |
|
| 90 | |
|
| 91 | |
|
| 92 | |
|
| 93 | |
|
| 94 | |
|
| 95 | |
|
| 96 | |
|
| 97 | |
|
| 98 | |
|
| 99 | |
|
| 100 | |
|
| 101 | |
|
| 102 | |
|
| 103 | |
|
| 104 | |
|
| 105 | |
|
| 106 | |
|
| 107 | |
|
| 108 | |
|
| 109 | |
|
| 110 | |
|
| 111 | ; |
|
| 112 | ||
| 113 | ||
| 114 | // Specialization for when profiling is disabled. |
|
| 115 | // This class is simple enough so that it can be completely optimized away. |
|
| 116 | |
|
| 117 | |
|
| 118 | |
|
| 119 | |
|
| 120 | |
|
| 121 | ; |
|
| 122 | ||
| 123 | |