| 123456789101112131415161718192021222324252627282930313233 |
- //
- // flyweight.hpp
- // gameutils
- //
- // Created by Sam Jaffe on 5/19/19.
- //
- #pragma once
- #include <unordered_map>
- #include "identity.hpp"
- template <typename T> class flyweight {
- private:
- static std::unordered_map<unsigned int, T> actual_;
- public:
- const unsigned int id;
- public:
- flyweight(unsigned int id, T actual) : id(id) {
- actual_.emplace(id, std::move(actual));
- }
- flyweight(identity<T> actual) : id(actual.id) {}
- T const & actual() const { return actual_.find(this->id)->second; }
- friend bool operator==(flyweight lhs, flyweight rhs) {
- return lhs.id == rhs.id;
- }
- };
- template <typename T> std::unordered_map<unsigned int, T> flyweight<T>::actual_;
|