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 virtual void before_do_construct() = 0;
57
58public:
60 add_parameters({
61 {"_objects", AutoParameter::read_only,
62 [this]() { return make_unordered_map_of_variants(m_elements); }},
63 });
64 }
65
66 void do_construct(VariantMap const &params) override {
68 m_elements = get_value_or<decltype(m_elements)>(params, "_objects", {});
69 for (auto const &[key, element] : m_elements) {
70 insert_in_core(key, element);
71 }
72 }
73
74 /**
75 * @brief Add an element to the map.
76 *
77 * @param key Identifier of the element to add.
78 * @param element The element to add.
79 */
80 void insert(KeyType const &key, std::shared_ptr<ManagedType> const &element) {
81 insert_in_core(key, element);
82 m_elements[key] = element;
83 }
84
85 /**
86 * @brief Add an element to the map.
87 * A free key is generated automatically.
88 *
89 * @param element The element to add.
90 */
91 KeyType insert(std::shared_ptr<ManagedType> const &element) {
92 auto const key = insert_in_core(element);
93 m_elements[key] = element;
94 return key;
95 }
96
97 /**
98 * @brief Removes all occurrences of an element from the map.
99 *
100 * @param key Identifier of the element to remove.
101 */
102 void erase(KeyType const &key) {
103 erase_in_core(key);
104 m_elements.erase(key);
105 }
106
107 /**
108 * @brief Map elements.
109 */
110 auto const &elements() const { return m_elements; }
111
112 /**
113 * @brief Clear the map.
114 */
115 void clear() {
116 for (auto const &kv : m_elements) {
117 erase_in_core(kv.first);
118 }
119
120 m_elements.clear();
121 }
122
123protected:
124 Variant do_call_method(std::string const &method,
125 VariantMap const &parameters) override {
126
127 if (method == "insert") {
128 auto obj_ptr =
129 get_value<std::shared_ptr<ManagedType>>(parameters.at("object"));
130
131 if (parameters.count("key")) {
132 auto const key = get_key(parameters.at("key"));
133 insert(key, obj_ptr);
134 return none;
135 }
136 return insert(obj_ptr);
137 }
138
139 if (method == "erase") {
140 auto const key = get_key(parameters.at("key"));
141 erase(key);
142 return none;
143 }
144
145 if (method == "get") {
146 auto const key = get_key(parameters.at("key"));
147 return Variant{m_elements.at(key)};
148 }
149
150 if (method == "get_map") {
151 return make_unordered_map_of_variants(m_elements);
152 }
153
154 if (method == "keys") {
155 std::vector<Variant> res;
156 for (auto const &kv : m_elements) {
157 res.push_back(kv.first);
158 }
159 return res;
160 }
161
162 if (method == "clear") {
163 clear();
164 return none;
165 }
166
167 if (method == "size") {
168 return static_cast<int>(m_elements.size());
169 }
170
171 if (method == "empty") {
172 return m_elements.empty();
173 }
174
175 if (method == "contains") {
176 return m_elements.find(get_key(parameters.at("key"))) != m_elements.end();
177 }
178
179 return Base::do_call_method(method, parameters);
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
__shared__ float res[]
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:80
virtual KeyType insert_in_core(std::shared_ptr< ManagedType > const &obj_ptr)=0
void do_construct(VariantMap const &params) override
Definition ObjectMap.hpp:66
ObjectContainer< ObjectMap, ManagedType, BaseType > Base
Definition ObjectMap.hpp:45
virtual void before_do_construct()=0
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:91
auto const & elements() const
Map elements.
void erase(KeyType const &key)
Removes all occurrences of an element from the map.
Variant do_call_method(std::string const &method, VariantMap const &parameters) override
virtual void erase_in_core(KeyType const &key)=0
std::unordered_map< std::string, Variant > VariantMap
Definition Variant.hpp:82
auto make_unordered_map_of_variants(std::unordered_map< K, V > const &v)
Definition Variant.hpp:93
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:80
constexpr const None none
None-"literal".
Definition Variant.hpp:63
static SteepestDescentParameters params
Currently active steepest descent instance.
static constexpr const ReadOnly read_only