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 ObjectHandles
40 * @tparam ManagedType Type of the managed objects, needs to be
41 * derived from @ref ObjectHandle
42 */
43template <typename ManagedType, class BaseType = ObjectHandle>
44class ObjectList : public ObjectContainer<ObjectList, ManagedType, BaseType> {
45public:
47 using Base::add_parameters;
48 using Base::context;
49
50private:
51 std::vector<std::shared_ptr<ManagedType>> m_elements;
52
53 virtual void add_in_core(const std::shared_ptr<ManagedType> &obj_ptr) = 0;
54 virtual void remove_in_core(const std::shared_ptr<ManagedType> &obj_ptr) = 0;
55 virtual bool
56 has_in_core(const std::shared_ptr<ManagedType> &obj_ptr) const = 0;
57
58public:
60 add_parameters({
61 {"_objects", AutoParameter::read_only,
62 [this]() { return make_vector_of_variants(m_elements); }},
63 });
64 }
65
66 void do_construct(VariantMap const &params) override {
67 m_elements = get_value_or<decltype(m_elements)>(params, "_objects", {});
68 for (auto const &object : m_elements) {
69 add_in_core(object);
70 }
71 }
72
73 /**
74 * @brief Add an element to the list.
75 *
76 * @param element The element to add.
77 */
78 void add(std::shared_ptr<ManagedType> const &element) {
79 if (has_in_core(element)) {
80 if (Base::context()->is_head_node()) {
81 throw std::runtime_error("This object is already present in the list");
82 }
83 throw Exception("");
84 }
85 context()->parallel_try_catch([&]() { add_in_core(element); });
86 m_elements.push_back(element);
87 }
88
89 /**
90 * @brief Removes all occurrences of an element from the list.
91 *
92 * @param element The element to remove.
93 */
94 void remove(std::shared_ptr<ManagedType> const &element) {
95 if (not has_in_core(element)) {
96 if (Base::context()->is_head_node()) {
97 throw std::runtime_error("This object is absent from the list");
98 }
99 throw Exception("");
100 }
101 remove_in_core(element);
102 std::erase(m_elements, element);
103 }
104
105 /**
106 * @brief List elements.
107 */
108 auto const &elements() const { return m_elements; }
109
110 /**
111 * @brief Clear the list.
112 */
113 void clear() {
114 for (auto const &e : m_elements) {
116 }
117
118 m_elements.clear();
119 }
120
121protected:
122 Variant do_call_method(std::string const &method,
123 VariantMap const &parameters) override {
124
125 if (method == "add") {
126 auto obj_ptr =
128
129 add(obj_ptr);
130 return none;
131 }
132
133 if (method == "remove") {
134 auto obj_ptr =
136
138 return none;
139 }
140
141 if (method == "get_elements") {
142 return make_vector_of_variants(m_elements);
143 }
144
145 if (method == "clear") {
146 clear();
147 return none;
148 }
149
150 if (method == "size") {
151 return static_cast<int>(m_elements.size());
152 }
153
154 if (method == "empty") {
155 return m_elements.empty();
156 }
157
158 return Base::do_call_method(method, parameters);
159 }
160};
161} // Namespace ScriptInterface
Owning list of ObjectHandles.
auto const & elements() const
List elements.
void add(std::shared_ptr< ManagedType > const &element)
Add an element to the list.
virtual bool has_in_core(const std::shared_ptr< ManagedType > &obj_ptr) const =0
void do_construct(VariantMap const &params) override
void remove(std::shared_ptr< ManagedType > const &element)
Removes all occurrences of an element from the list.
void clear()
Clear the list.
Variant do_call_method(std::string const &method, VariantMap const &parameters) override
virtual void remove_in_core(const std::shared_ptr< ManagedType > &obj_ptr)=0
ObjectContainer< ObjectList, ManagedType, BaseType > Base
virtual void add_in_core(const std::shared_ptr< ManagedType > &obj_ptr)=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