ESPResSo
Extensible Simulation Package for Research on Soft Matter Systems
Loading...
Searching...
No Matches
ObjectMap.hpp
Go to the documentation of this file.
1/*
2 * Copyright (C) 2010-2022 The ESPResSo project
3 * Copyright (C) 2002,2003,2004,2005,2006,2007,2008,2009,2010
4 * Max-Planck-Institute for Polymer Research, Theory Group
5 *
6 * This file is part of ESPResSo.
7 *
8 * ESPResSo is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation, either version 3 of the License, or
11 * (at your option) any later version.
12 *
13 * ESPResSo is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 */
21
22#pragma once
23
28
29#include <memory>
30#include <string>
31#include <type_traits>
32#include <unordered_map>
33#include <utility>
34
35namespace ScriptInterface {
36/**
37 * @brief Owning map of ObjectHandles
38 * @tparam ManagedType Type of the managed objects, needs to be
39 * derived from @ref ObjectHandle
40 */
41template <typename ManagedType, class BaseType = ObjectHandle,
42 class KeyType = int>
43class ObjectMap : public ObjectContainer<ObjectMap, ManagedType, BaseType> {
44public:
46 using Base::add_parameters;
47
48private:
49 std::unordered_map<KeyType, std::shared_ptr<ManagedType>> m_elements;
50
51 virtual KeyType
52 insert_in_core(std::shared_ptr<ManagedType> const &obj_ptr) = 0;
53 virtual void insert_in_core(KeyType const &key,
54 std::shared_ptr<ManagedType> const &obj_ptr) = 0;
55 virtual void erase_in_core(KeyType const &key) = 0;
56
57public:
59 add_parameters({
60 {"_objects", AutoParameter::read_only,
61 [this]() { return make_unordered_map_of_variants(m_elements); }},
62 });
63 }
64
65 void do_construct(VariantMap const &params) override = 0;
66
67 /**
68 * @brief Add an element to the map.
69 *
70 * @param key Identifier of the element to add.
71 * @param element The element to add.
72 */
73 void insert(KeyType const &key, std::shared_ptr<ManagedType> const &element) {
74 insert_in_core(key, element);
75 m_elements[key] = element;
76 }
77
78 /**
79 * @brief Add an element to the map.
80 * A free key is generated automatically.
81 *
82 * @param element The element to add.
83 */
84 KeyType insert(std::shared_ptr<ManagedType> const &element) {
85 auto const key = insert_in_core(element);
86 m_elements[key] = element;
87 return key;
88 }
89
90 /**
91 * @brief Removes all occurrences of an element from the map.
92 *
93 * @param key Identifier of the element to remove.
94 */
95 void erase(KeyType const &key) {
97 m_elements.erase(key);
98 }
99
100 /**
101 * @brief Map elements.
102 */
103 auto const &elements() const { return m_elements; }
104
105 /**
106 * @brief Clear the map.
107 */
108 void clear() {
109 for (auto const &kv : m_elements) {
110 erase_in_core(kv.first);
111 }
112
113 m_elements.clear();
114 }
115
116protected:
117 Variant do_call_method(std::string const &method,
118 VariantMap const &parameters) override {
119
120 if (method == "insert") {
121 auto obj_ptr =
123
124 if (parameters.count("key")) {
125 auto const key = get_key(parameters.at("key"));
127 return none;
128 }
129 return insert(obj_ptr);
130 }
131
132 if (method == "erase") {
133 auto const key = get_key(parameters.at("key"));
134 erase(key);
135 return none;
136 }
137
138 if (method == "get") {
139 auto const key = get_key(parameters.at("key"));
140 return Variant{m_elements.at(key)};
141 }
142
143 if (method == "get_map") {
144 return make_unordered_map_of_variants(m_elements);
145 }
146
147 if (method == "keys") {
148 std::vector<Variant> res;
149 for (auto const &kv : m_elements) {
150 res.push_back(kv.first);
151 }
152 return res;
153 }
154
155 if (method == "clear") {
156 clear();
157 return none;
158 }
159
160 if (method == "size") {
161 return static_cast<int>(m_elements.size());
162 }
163
164 if (method == "empty") {
165 return m_elements.empty();
166 }
167
168 if (method == "contains") {
169 return m_elements.find(get_key(parameters.at("key"))) != m_elements.end();
170 }
171
172 return Base::do_call_method(method, parameters);
173 }
174
176 m_elements = get_value_or<decltype(m_elements)>(params, "_objects", {});
177 for (auto const &[key, element] : m_elements) {
178 insert_in_core(key, element);
179 }
180 }
181
182 KeyType get_key(Variant const &key) const {
183 try {
184 return get_value<KeyType>(key);
185 } catch (...) {
186 using namespace detail::demangle;
187 auto const actual = simplify_symbol_variant(key);
188 auto const target = simplify_symbol(static_cast<KeyType *>(nullptr));
189 if (Base::context()->is_head_node()) {
190 throw std::invalid_argument("Key has to be of type '" + target +
191 "', got type '" + actual + "'");
192 }
193 throw;
194 }
195 }
196};
197} // Namespace ScriptInterface
Owning map of ObjectHandles.
Definition ObjectMap.hpp:43
void insert(KeyType const &key, std::shared_ptr< ManagedType > const &element)
Add an element to the map.
Definition ObjectMap.hpp:73
virtual KeyType insert_in_core(std::shared_ptr< ManagedType > const &obj_ptr)=0
ObjectContainer< ObjectMap, ManagedType, BaseType > Base
Definition ObjectMap.hpp:45
void clear()
Clear the map.
virtual void insert_in_core(KeyType const &key, std::shared_ptr< ManagedType > const &obj_ptr)=0
KeyType get_key(Variant const &key) const
KeyType insert(std::shared_ptr< ManagedType > const &element)
Add an element to the map.
Definition ObjectMap.hpp:84
void do_construct(VariantMap const &params) override=0
auto const & elements() const
Map elements.
void restore_from_checkpoint(VariantMap const &params)
void erase(KeyType const &key)
Removes all occurrences of an element from the map.
Definition ObjectMap.hpp:95
Variant do_call_method(std::string const &method, VariantMap const &parameters) override
virtual void erase_in_core(KeyType const &key)=0
T get_value(Variant const &v)
Extract value of specific type T from a Variant.
std::unordered_map< std::string, Variant > VariantMap
Definition Variant.hpp:69
auto make_unordered_map_of_variants(std::unordered_map< K, V > const &v)
Definition Variant.hpp:80
std::conditional_t< std::is_same_v< BaseType, ObjectHandle >, AutoParameters< Container< ManagedType, BaseType >, BaseType >, BaseType > ObjectContainer
Base class for containers whose BaseType might be a full specialization of AutoParameters.
boost::make_recursive_variant< None, bool, int, std::size_t, double, std::string, ObjectRef, Utils::Vector3b, Utils::Vector3i, Utils::Vector2d, Utils::Vector3d, Utils::Vector4d, std::vector< int >, std::vector< double >, std::vector< boost::recursive_variant_ >, std::unordered_map< int, boost::recursive_variant_ >, std::unordered_map< std::string, boost::recursive_variant_ > >::type Variant
Possible types for parameters.
Definition Variant.hpp:67
constexpr const None none
None-"literal".
Definition Variant.hpp:50
static SteepestDescentParameters params
Currently active steepest descent instance.
static constexpr const ReadOnly read_only