diff options
Diffstat (limited to 'tiger-compiler/lib/misc/endomap.hh')
| -rw-r--r-- | tiger-compiler/lib/misc/endomap.hh | 64 |
1 files changed, 64 insertions, 0 deletions
diff --git a/tiger-compiler/lib/misc/endomap.hh b/tiger-compiler/lib/misc/endomap.hh new file mode 100644 index 0000000..4a69e87 --- /dev/null +++ b/tiger-compiler/lib/misc/endomap.hh @@ -0,0 +1,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> |
