summaryrefslogtreecommitdiff
path: root/tiger-compiler/lib/misc/endomap.hh
blob: 4a69e8735317b65d6b6e6ed1d27673e94093162b (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
/**
 ** \file misc/endomap.hh
 ** \brief Declaration of misc::endomap.
 */

#pragma once

#include <list>
#include <map>

#include <misc/map.hh>

namespace misc
{
  /** \brief A renaming mapping defaulting to identity:
      Mapping a class with itself (endomorphism).
      Unless a value is already mapped onto another, map it to itself
      if nonstrict. This is a special case of std::map. */
  template <class T> class endomap : public map<T, T>
  {
  public:
    /// Super class type.
    using super_type = map<T, T>;

    // Import overloaded virtual functions.
    using super_type::operator[];
    using super_type::operator();

    enum class strictness_type
    {
      /// Querying about an unknown identifier returns itself.
      nonstrict,
      /// Querying about an unknown identifier is an error.
      strict
    };

    /// \name Constructors and destructor.
    /// \{
    endomap();
    /// \}

    /// Return a duplicate of \a this.
    endomap* clone() const override;

    /// Return a reference to the value associated to \a t.
    ///
    /// If \a t was not inserted before, then, contrary to std::map,
    /// we insert t -> t into the map, instead of `t -> T()'.  This
    /// spares the construction of a new T, which may have nasty
    /// effects if this constructor has side effects (e.g.,
    /// incrementing a counter).
    virtual T& operator[](const T& t);

    /// Lookup for \a t, returning it if unknown.
    T operator()(const T& t) const override;

  protected:
    /// If strict, queries for an unknown (unmapped) Identifier is an error.
    strictness_type strictness_;
  };

} // namespace misc

#include <misc/endomap.hxx>