diff options
Diffstat (limited to 'tiger-compiler/lib/misc/symbol.hh')
| -rw-r--r-- | tiger-compiler/lib/misc/symbol.hh | 77 |
1 files changed, 77 insertions, 0 deletions
diff --git a/tiger-compiler/lib/misc/symbol.hh b/tiger-compiler/lib/misc/symbol.hh new file mode 100644 index 0000000..6d0a1e1 --- /dev/null +++ b/tiger-compiler/lib/misc/symbol.hh @@ -0,0 +1,77 @@ +/** + ** \file misc/symbol.hh + ** \brief Definition of misc::symbol. + */ + +#pragma once + +#include <iosfwd> +#include <set> +#include <string> + +#include <misc/unique.hh> + +namespace misc +{ + /** \brief Define symbol class. + ** + ** Map any string to a unique reference. + ** This allows to avoid an "strcmp()" style comparison of strings: + ** reference comparison is much faster. + */ + class symbol : public unique<std::string> + { + using super_type = unique<std::string>; + /// The type "set of strings". + using string_set_type = super_type::object_set_type; + /// The type for the size of string map. + using string_size_type = string_set_type::size_type; + + /** \name Ctor & Dtor. + ** \{ */ + public: + /** \brief Construct a symbol. + ** \param s referenced string */ + symbol(const std::string& s); + /** \brief Construct a symbol. + ** \param s referenced string */ + symbol(const char* s = ""); + /** \brief Construct a symbol. + ** \param s symbol to copy. */ + constexpr symbol(const symbol& s) = default; + /** \} */ + + /** \name Operators. + ** \{ */ + public: + /** \brief Assign a symbol to this symbol. + ** \param rhs symbol to copy. */ + symbol& operator=(const symbol& rhs); + + /** \brief Compare two symbol for equality. + ** \param rhs string to compare with. */ + bool operator==(const symbol& rhs) const; + /** \brief Compare two symbol for inequality. + ** \param rhs string to compare with. *//* */ + bool operator!=(const symbol& rhs) const; + /** \} */ + + public: + /** \name Factory methods. + ** \{ */ + /** \brief Create a new unique symbol. */ + static symbol fresh(); + /** \brief Create a new unique symbol, forged from \a s. */ + static symbol fresh(const symbol& s); + /** \} */ + }; + + /** \brief Intercept output stream redirection. + ** \param ostr the destination output stream + ** \param the a reference to the symbol to redirect + */ + std::ostream& operator<<(std::ostream& ostr, const symbol& the); + +} // namespace misc + +#include <misc/symbol.hxx> |
