| // Copyright 2020 The Dawn & Tint Authors |
| // |
| // Redistribution and use in source and binary forms, with or without |
| // modification, are permitted provided that the following conditions are met: |
| // |
| // 1. Redistributions of source code must retain the above copyright notice, this |
| // list of conditions and the following disclaimer. |
| // |
| // 2. Redistributions in binary form must reproduce the above copyright notice, |
| // this list of conditions and the following disclaimer in the documentation |
| // and/or other materials provided with the distribution. |
| // |
| // 3. Neither the name of the copyright holder nor the names of its |
| // contributors may be used to endorse or promote products derived from |
| // this software without specific prior written permission. |
| // |
| // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| // DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE |
| // FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
| // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
| // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
| // OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| |
| #ifndef SRC_TINT_UTILS_SYMBOL_SYMBOL_H_ |
| #define SRC_TINT_UTILS_SYMBOL_SYMBOL_H_ |
| |
| #include <string> |
| |
| #include "src/tint/utils/id/generation_id.h" |
| |
| namespace tint { |
| |
| /// A symbol representing a string in the system |
| class Symbol { |
| public: |
| /// Constructor |
| /// An invalid symbol |
| Symbol(); |
| /// Constructor |
| /// @param val the symbol value |
| /// @param pid the identifier of the program that owns this Symbol |
| /// @param name the name this symbol represents |
| Symbol(uint32_t val, tint::GenerationID pid, std::string_view name); |
| |
| /// Copy constructor |
| /// @param o the symbol to copy |
| Symbol(const Symbol& o); |
| /// Move constructor |
| /// @param o the symbol to move |
| Symbol(Symbol&& o); |
| /// Destructor |
| ~Symbol(); |
| |
| /// Copy assignment |
| /// @param o the other symbol |
| /// @returns the symbol after doing the copy |
| Symbol& operator=(const Symbol& o); |
| /// Move assignment |
| /// @param o the other symbol |
| /// @returns teh symbol after doing the move |
| Symbol& operator=(Symbol&& o); |
| |
| /// Equality operator |
| /// @param o the other symbol |
| /// @returns true if the symbols are the same |
| bool operator==(const Symbol& o) const; |
| |
| /// Inequality operator |
| /// @param o the other symbol |
| /// @returns true if the symbols are the different |
| bool operator!=(const Symbol& o) const; |
| |
| /// Less-than operator |
| /// @param o the other symbol |
| /// @returns true if this symbol is ordered before symbol `o` |
| bool operator<(const Symbol& o) const; |
| |
| /// @returns true if the symbol is valid |
| bool IsValid() const { return val_ != static_cast<uint32_t>(-1); } |
| |
| /// @returns true if the symbol is valid |
| explicit operator bool() const { return IsValid(); } |
| |
| /// @returns the value for the symbol |
| uint32_t value() const { return val_; } |
| |
| /// Convert the symbol to a string |
| /// @return the string representation of the symbol |
| std::string to_str() const; |
| |
| /// Converts the symbol to the registered name |
| /// @returns the string_view representing the name of the symbol |
| std::string_view NameView() const; |
| |
| /// Converts the symbol to the registered name |
| /// @returns the string representing the name of the symbol |
| std::string Name() const; |
| |
| /// @returns the identifier of the Program that owns this symbol. |
| tint::GenerationID GenerationID() const { return generation_id_; } |
| |
| private: |
| uint32_t val_ = static_cast<uint32_t>(-1); |
| tint::GenerationID generation_id_; |
| std::string_view name_; |
| }; |
| |
| /// @param sym the Symbol |
| /// @returns the GenerationID that owns the given Symbol |
| inline GenerationID GenerationIDOf(Symbol sym) { |
| return sym.IsValid() ? sym.GenerationID() : GenerationID(); |
| } |
| |
| } // namespace tint |
| |
| namespace std { |
| |
| /// Custom std::hash specialization for tint::Symbol so symbols can be used as |
| /// keys for std::unordered_map and std::unordered_set. |
| template <> |
| class hash<tint::Symbol> { |
| public: |
| /// @param sym the symbol to return |
| /// @return the Symbol internal value |
| inline std::size_t operator()(const tint::Symbol& sym) const { |
| return static_cast<std::size_t>(sym.value()); |
| } |
| }; |
| |
| } // namespace std |
| |
| #endif // SRC_TINT_UTILS_SYMBOL_SYMBOL_H_ |