From 967be9e750221ab2ab783f95df79bb26d290a45e Mon Sep 17 00:00:00 2001 From: Martial Simon Date: Mon, 15 Sep 2025 01:07:58 +0200 Subject: add: added projects --- tiger-compiler/lib/misc/unique.hh | 85 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 85 insertions(+) create mode 100644 tiger-compiler/lib/misc/unique.hh (limited to 'tiger-compiler/lib/misc/unique.hh') diff --git a/tiger-compiler/lib/misc/unique.hh b/tiger-compiler/lib/misc/unique.hh new file mode 100644 index 0000000..27cd05e --- /dev/null +++ b/tiger-compiler/lib/misc/unique.hh @@ -0,0 +1,85 @@ +/** + ** \file misc/unique.hh + ** \brief Definition of misc::unique. + */ + +#pragma once + +#include +#include + +namespace misc +{ + /** \brief Define \c unique class. + ** + ** Implementation of the flyweight pattern. + ** Map identical objects to a unique reference. + */ + template > class unique + { + protected: + /// The unique's object type. + using object_set_type = std::set; + /// The type for the size of the unique set. + using object_size_type = typename object_set_type::size_type; + + public: + using value_type = unique; + using data_type = T; + + /** \name Ctor & Dtor. + ** \{ */ + /** \brief Construct a \c unique. + ** \param s referenced object */ + unique(const data_type& s); + /** \brief Construct a \c unique. + ** \param u \c unique to copy. */ + constexpr unique(const unique& u) = default; + virtual ~unique() = default; + /** \} */ + + /** \name Accessors. + ** \{ */ + /// The object referenced by \c this. + const data_type& get() const; + operator const data_type&() const; + + /// The number of referenced objects. + static object_size_type object_map_size(); + /** \} */ + + /** \name Operators. + ** \{ */ + /** \brief Assign a \c unique to this \c unique. + ** \param rhs \c unique to copy. */ + value_type& operator=(const value_type& rhs); + + /** \brief Compare two \c unique for equality. + ** \param rhs \c unique to compare with. */ + bool operator==(const value_type& rhs) const; + /** \brief Compare two \c unique for inequality. + ** \param rhs \c unique to compare with. */ + bool operator!=(const value_type& rhs) const; + /** \brief Compare two \c unique for order. + ** \param rhs \c unique to compare with. */ + bool operator<(const value_type& rhs) const; + /** \} */ + + protected: + /// Return the set of uniques. + static object_set_type& object_set_instance(); + + /// Pointer to the unique referenced object. + const data_type* obj_; + }; + + /** \brief Intercept output stream redirection. + ** \param ostr the destination output stream + ** \param u a reference to the unique to redirect + */ + template + std::ostream& operator<<(std::ostream& ostr, const unique& u); + +} // namespace misc + +#include -- cgit v1.2.3