# t00026 - Template memento pattern ## Config ```yaml compilation_database_dir: .. output_directory: puml diagrams: t00026_class: type: class glob: - ../../tests/t00026/t00026.cc using_namespace: - clanguml::t00026 include: namespaces: - clanguml::t00026 ``` ## Source code File t00026.cc ```cpp #include #include #include namespace clanguml { namespace t00026 { template class Memento { public: Memento(T &&v) : m_value(std::forward(v)) { } T value() const { return m_value; } private: T m_value; }; template class Originator { public: Originator(T &&v) : m_value(std::forward(v)) { } Memento memoize_value() const { return Memento{m_value}; } void load(const Memento &m) { m_value = m.value(); } void print() const { std::cout << m_value << std::endl; } void set(T &&v) { m_value = std::forward(v); } private: T m_value; }; template class Caretaker { public: Memento &state(const std::string &n) { return m_mementos.at(n); } void set_state(const std::string &s, Memento &&m) { m_mementos.try_emplace(s, std::move(m)); } private: std::unordered_map> m_mementos; }; struct StringMemento { Caretaker caretaker; Originator originator; }; } } ``` ## Generated UML diagrams ![t00026_class](./t00026_class.svg "Template memento pattern")