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
49private:
50 std::vector<std::shared_ptr<ManagedType>> m_elements;
51
52 virtual void add_in_core(const std::shared_ptr<ManagedType> &obj_ptr) = 0;
53 virtual void remove_in_core(const std::shared_ptr<ManagedType> &obj_ptr) = 0;
54 virtual bool
55 has_in_core(const std::shared_ptr<ManagedType> &obj_ptr) const = 0;
56
57public:
59 add_parameters({
60 {"_objects", AutoParameter::read_only,
61 [this]() { return make_vector_of_variants(m_elements); }},
62 });
63 }
64
65 void do_construct(VariantMap const &params) override {
66 m_elements = get_value_or<decltype(m_elements)>(params, "_objects", {});
67 for (auto const &object : m_elements) {
68 add_in_core(object);
69 }
70 }
71
72 /**
73 * @brief Add an element to the list.
74 *
75 * @param element The element to add.
76 */
77 void add(std::shared_ptr<ManagedType> const &element) {
78 if (has_in_core(element)) {
79 if (Base::context()->is_head_node()) {
80 throw std::runtime_error("This object is already present in the list");
81 }
82 throw Exception("");
83 }
84 add_in_core(element);
85 m_elements.push_back(element);
86 }
87
88 /**
89 * @brief Removes all occurrences of an element from the list.
90 *
91 * @param element The element to remove.
92 */
93 void remove(std::shared_ptr<ManagedType> const &element) {
94 if (not has_in_core(element)) {
95 if (Base::context()->is_head_node()) {
96 throw std::runtime_error("This object is absent from the list");
97 }
98 throw Exception("");
99 }
100 remove_in_core(element);
101 m_elements.erase(std::remove(m_elements.begin(), m_elements.end(), element),
102 m_elements.end());
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 =
127 get_value<std::shared_ptr<ManagedType>>(parameters.at("object"));
128
129 add(obj_ptr);
130 return none;
131 }
132
133 if (method == "remove") {
134 auto obj_ptr =
135 get_value<std::shared_ptr<ManagedType>>(parameters.at("object"));
136
137 remove(obj_ptr);
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
std::unordered_map< std::string, Variant > VariantMap
Definition Variant.hpp:82
auto make_vector_of_variants(std::vector< T > const &v)
Definition Variant.hpp:101
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