ESPResSo
Extensible Simulation Package for Research on Soft Matter Systems
Loading...
Searching...
No Matches
ObjectList.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
27
29
30#include <memory>
31#include <string>
32#include <type_traits>
33#include <utility>
34#include <vector>
35
36namespace ScriptInterface {
37
38/**
39 * @brief Owning list of object handles.
40 *
41 * Stored elements are cleared from the core during destruction.
42 * Due to how dynamic dispatch works, derived types must mark
43 * @ref remove_in_core as @c final` and call @ref do_destruct in
44 * their virtual destructor. This is to ensure that pure virtual
45 * functions called by @ref clear cannot be executed in a type
46 * derived from the type currently being destructed, since derived
47 * types no longer exist at this point of the destruction sequence.
48 *
49 * @tparam ManagedType Type of the managed objects, needs to be
50 * derived from @ref ObjectHandle
51 */
52template <typename ManagedType, class BaseType = ObjectHandle>
53class ObjectList : public ObjectContainer<ObjectList, ManagedType, BaseType> {
54public:
56 using Base::add_parameters;
57 using Base::context;
58 using value_type = std::shared_ptr<ManagedType>;
59
60private:
61 std::vector<value_type> m_elements;
62 bool dtor_sequence_initiated = false;
63
64 virtual void add_in_core(value_type const &obj_ptr) = 0;
65 virtual void remove_in_core(value_type const &obj_ptr) = 0;
66 virtual bool has_in_core(value_type const &obj_ptr) const = 0;
67
68protected:
69 void do_destruct() {
70 assert(not dtor_sequence_initiated);
71 clear();
72 dtor_sequence_initiated = true;
73 }
74
75public:
77 add_parameters({
78 {"_objects", AutoParameter::read_only,
79 [this]() { return make_vector_of_variants(m_elements); }},
80 });
81 }
82
83 ~ObjectList() override { assert(dtor_sequence_initiated); }
84
85 void do_construct(VariantMap const &params) override {
86 m_elements = get_value_or<decltype(m_elements)>(params, "_objects", {});
87 for (auto const &object : m_elements) {
88 add_in_core(object);
89 }
90 }
91
92 /**
93 * @brief Add an element to the list.
94 *
95 * @param element The element to add.
96 */
97 void add(value_type const &element) {
98 if (has_in_core(element)) {
99 if (Base::context()->is_head_node()) {
100 throw std::runtime_error("This object is already present in the list");
101 }
102 throw Exception("");
103 }
104 context()->parallel_try_catch([&]() { add_in_core(element); });
105 m_elements.push_back(element);
106 }
107
108 /**
109 * @brief Removes all occurrences of an element from the list.
110 *
111 * @param element The element to remove.
112 */
113 void remove(value_type const &element) {
114 if (not has_in_core(element)) {
115 if (Base::context()->is_head_node()) {
116 throw std::runtime_error("This object is absent from the list");
117 }
118 throw Exception("");
119 }
120 remove_in_core(element);
121 std::erase(m_elements, element);
122 }
123
124 /**
125 * @brief List elements.
126 */
127 auto const &elements() const { return m_elements; }
128
129 /**
130 * @brief Clear the list.
131 */
132 void clear() {
133 for (auto const &e : m_elements) {
135 }
136
137 m_elements.clear();
138 }
139
140protected:
141 Variant do_call_method(std::string const &method,
142 VariantMap const &parameters) override {
143
144 if (method == "add") {
145 auto obj_ptr = get_value<value_type>(parameters.at("object"));
146
147 add(obj_ptr);
148 return none;
149 }
150
151 if (method == "remove") {
152 auto obj_ptr = get_value<value_type>(parameters.at("object"));
153
155 return none;
156 }
157
158 if (method == "get_elements") {
159 return make_vector_of_variants(m_elements);
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 return Base::do_call_method(method, parameters);
176 }
177};
178} // Namespace ScriptInterface
Owning list of object handles.
auto const & elements() const
List elements.
void remove(value_type const &element)
Removes all occurrences of an element from the list.
void do_construct(VariantMap const &params) override
virtual void add_in_core(value_type const &obj_ptr)=0
void clear()
Clear the list.
void add(value_type const &element)
Add an element to the list.
Variant do_call_method(std::string const &method, VariantMap const &parameters) override
std::shared_ptr< ManagedType > value_type
virtual void remove_in_core(value_type const &obj_ptr)=0
ObjectContainer< ObjectList, ManagedType, BaseType > Base
virtual bool has_in_core(value_type const &obj_ptr) const =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_vector_of_variants(std::vector< T > const &v)
Definition Variant.hpp:88
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