summaryrefslogtreecommitdiff
path: root/tiger-compiler/lib/misc/symbol.hh
blob: 6d0a1e1bbb4f373c5467d3c41045f36b0681eb2d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
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>