// Copyright 2019 The Chromium Authors // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. #ifdef UNSAFE_BUFFERS_BUILD // TODO(crbug.com/40285824): Remove this and convert code to safer constructs. #pragma allow_unsafe_buffers #endif #ifndef COMPONENTS_MEDIA_ROUTER_COMMON_PROVIDERS_CAST_CHANNEL_ENUM_TABLE_H_ #define COMPONENTS_MEDIA_ROUTER_COMMON_PROVIDERS_CAST_CHANNEL_ENUM_TABLE_H_ #include <cstdint> #include <cstring> #include <optional> #include <ostream> #include <string_view> #include "base/check_op.h" #include "base/notreached.h" #include "build/build_config.h" // TODO(crbug.com/1291730): Move this file to a more appropriate directory. // // // A bidirectional mapping between enum values and strings. // // Typical Usage // ------------- // // The typical way to use this class is to define a specializtion of // EnumTable<E>::instance for a specific enum type, which is initialized with // a table of enum values and their corresponding strings: // // // In .h file: // enum class MyEnum { kFoo, kBar, ..., kOther, kMaxValue = kOther }; // // // In .cc file: // // template <> // constexpr EnumTable<MyEnum> EnumTable<MyEnum>::instance({ // {MyEnum::kFoo, "FOO"}, // {MyEnum::kBar, "BAR"}, // ... // {MyEnum::kOther}, // Not all values need strings. // }, // MyEnum::kMaxValue); // Needed to detect missing table entries. // // The functions EnumToString() and StringToEnum() are used to look up values // in the table. In some cases, it may be useful to wrap these functions for // special handling of unknown values, etc.: // // const char* MyEnumToString(MyEnum value) { // return EnumToString(value).value_or(nullptr).data(); // } // // MyEnum StringToMyEnum(const std::string& name) { // return StringToEnum<MyEnum>(name).value_or(MyEnum::kOther); // } // // NOTE: Because it's a template specialization, the definition of // EnumTable<E>::instance has to be in the cast_util namespace. Alternatively, // you can declare your own table with any name you want and call its members // functions directly. // // To get a the string for an enum value that is known at compile time, there is // a special zero-argument form of EnumToString(), (and a corresponding member // function in EnumTable): // // // Compiles to std::string_view("FOO"). // EnumToString<MyEnum, MyEnum::kFoo>(); // // The syntax is a little awkward, but it saves you from having to duplicate // string literals or define each enum string as a named constant. // // // Consecutive Enum Tables // ----------------------- // // When using an EnumTable, it's generally best for the following conditions // to be true: // // - The enum is defined with "enum class" syntax. // // - The members have the default values assigned by the compiler. // // - There is an extra member named kMaxValue which is set equal to the highest // ordinary value. (The Chromium style checker will verify that kMaxValue // really is the maximum value.) // // - The values in the EnumTable constructor appear in sorted order. // // - Every member of the enum (other than kMaxValue) is included in the table. // // When these conditions are met, the enum's |kMaxValue| member should be passed // as the second argument of the EnumTable. This will create a table where enum // values can be converted to strings in constant time. It will also reliably // detect an incomplete enum table during startup of a debug build. // // // Non-Consecutive Enum Tables // --------------------------- // // When the conditions in the previous section cannot be satisfied, the second // argument of the EnumTable constructor should be the special constant // |NonConsecutiveEnumTable|. Doing so has some unfortunate side-effects: there // is no automatic way to detect when an enum value is missing from the table, // and looking up a non-constant enum value requires a linear search. // // // Why use an EnumTable? // --------------------- // // If you're only doing one-way conversions from an enum to a string, using an // EnumTable is almost identical to a switch statement. Typically a switch // statement that converts an enum value to a string will compile to a simple // constant-time lookup in a static array, and a call to an EnumTable's // GetString() method will compile to almost exactly the same code (with one // additional shlq instruction compared to the switch statement). // // If your enum values are non-consecutive, the GetString() method will perform // a linear search instead of an array lookup. The search should be competitve // in performance with a switch statement in this case. To avoid getting this // sub-optimal behavior by accident, you must explicitly request it with an // extra argument to EnumTable's constructor. If you don't do this, failure to // put the values in the correct order will cause your unit tests to fail on // startup with a decriptive error message. // // EnumTable really shines when you need to convert strings to enum values. A // typical implementation without EnumTable looks like this: // // constexpr char kMyEnumFooString[] = "FOO"; // constexpr char kMyEnumBarString[] = "BAR"; // ... // // MyEnum StringToMyEnum(const std::string& str) { // if (str == kMyEnumFooString) return MyEnum::kFoo; // if (str == kMyEnumBarString) return MyEnum::kBar; // ... // return kOther; // } // // If you roll your own solution, you can't do much better than this without // jumping through some hoops. Obvious improvements, like storing the data in a // global base::flat_map, are off-limits because Chromium requires all global // variables to have trivial destructors. A simple chain of "if" statements // works fine, but it has a number of drawbacks compared to an EnumTable: // // - You have to write and maintain a function separate from the one that // converts enum values to strings. With an EnumTable, a single declaration // lets you convert in both directions. // // - Since you will almost certainly be using the strings in more than one // place, you have to either define a named constant for each one, or live // with the fact that the string literals are duplicated, causing maintenance // headaches. With an EnumTable, each string naturally appears in only one // place, so there's no need for supplementary named constants. When you need // to refer to the string for a particular enum value, you can get it directly // from the table at compile time using the zero-argument EnumToString() // function or the zero-argument EnumTable<E>::ToString() method. // // - If you want to follow best practices, you'll need to write unit tests for // your functions that convert between strings and enums. When you use an // EnumTable, you don't write any functions, so there's nothing to test; all // the necessary testing is done by EnumTable's own unit tests. // // - The conversion function is essentially a fully-unrolled linear search // through all the possible string values. This bloats your code and causes // unnecessary churn in the instruction cache. Using an EnumTable, this // search is compiled into a single loop shared by all enums, which the // compiler can unroll, or not, as appropriate. On 64-bit platforms, the main // table uses only 16 bytes per entry, so memory locality is excellent and // impact on the data cache is minimal. // // - The hand-written function performs a function call for each candidate // string. EnumTable's search loop compares the lengths of the strings before // doing anything else, so most iterations don't call any functions and don't // access any memory aside from the lookup table itself. // // - If you accidentally duplicate any lines in the function, you're on your // own. EnumTable detects duplicate strings on startup (in debug builds), so // you'll never accidentally try to map a string to two different enum values. namespace cast_util { class #ifdef ARCH_CPU_64_BITS alignas(16) #endif GenericEnumTableEntry { … }; // Yes, these constructors really needs to be inlined. Even though they look // "complex" to the style checker, everything is executed at compile time, so an // EnumTable instance can be fully initialized without executing any code. inline constexpr GenericEnumTableEntry::GenericEnumTableEntry(int32_t value) : … { … } inline constexpr GenericEnumTableEntry::GenericEnumTableEntry( int32_t value, std::string_view str) : … { … } struct NonConsecutiveEnumTable_t { … }; constexpr NonConsecutiveEnumTable_t NonConsecutiveEnumTable; // A table for associating enum values with string literals. This class is // designed for use as an initialized global variable, so it has a trivial // destructor and a simple constant-time constructor. template <typename E> class EnumTable { … }; // Converts an enum value to a string using the default table // (EnumTable<E>::instance) for the given enum type. template <typename E> inline std::optional<std::string_view> EnumToString(E value) { … } // Converts a literal enum value to a string at compile time using the default // table (EnumTable<E>::GetInstance()) for the given enum type. // // TODO(crbug.com/1291730): Once C++17 features are allowed, change this // function to have only one template parameter: // // template <auto Value> // inline std::string_view EnumToString() { // return EnumTable<decltype(Value) // >::GetInstance().template GetString<Value>(); // } // template <typename E, E Value> inline std::string_view EnumToString() { … } // Converts a string to an enum value using the default table // (EnumTable<E>::instance) for the given enum type. template <typename E> inline std::optional<E> StringToEnum(std::string_view str) { … } } // namespace cast_util #endif // COMPONENTS_MEDIA_ROUTER_COMMON_PROVIDERS_CAST_CHANNEL_ENUM_TABLE_H_