ESPResSo
Extensible Simulation Package for Research on Soft Matter Systems
Loading...
Searching...
No Matches
Variant.hpp
Go to the documentation of this file.
1/*
2 * Copyright (C) 2010-2022 The ESPResSo project
3 *
4 * This file is part of ESPResSo.
5 *
6 * ESPResSo is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * ESPResSo is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20#pragma once
21
22#include "None.hpp"
23
24#include <utils/Vector.hpp>
25
26#include <boost/variant.hpp>
27
28/* This <boost/serialization/library_version_type.hpp> include guards against
29 * an issue in boost::serialization from boost 1.74.0 that leads to compiler
30 * error "'library_version_type' is not a member of 'boost::serialization'"
31 * when including <boost/serialization/unordered_map.hpp>. More details
32 * in ticket https://github.com/boostorg/serialization/issues/219
33 */
34#include <boost/serialization/version.hpp>
35#if BOOST_VERSION / 100000 == 1 && BOOST_VERSION / 100 % 1000 == 74
36#include <boost/serialization/library_version_type.hpp>
37#endif
38
39#include <boost/range/algorithm/transform.hpp>
40#include <boost/serialization/serialization.hpp>
41#include <boost/serialization/string.hpp>
42#include <boost/serialization/unordered_map.hpp>
43#include <boost/serialization/variant.hpp>
44#include <boost/serialization/vector.hpp>
45
46#include <cstddef>
47#include <memory>
48#include <string>
49#include <type_traits>
50#include <unordered_map>
51#include <vector>
52
56
57namespace ScriptInterface {
58class ObjectHandle;
59using ObjectRef = std::shared_ptr<ObjectHandle>;
60/**
61 * @brief None-"literal".
62 */
63constexpr const None none{};
64
65/**
66 * @brief Possible types for parameters.
67 *
68 * The visitors and packing functions need to be adapted accordingly when
69 * extending this variant with new types. For the exact details, see commit
70 * <a href="https://github.com/espressomd/espresso/commit/b48ab62">b48ab62</a>.
71 * The number of types is limited by macro @c BOOST_MPL_LIMIT_LIST_SIZE
72 * (defaults to 20).
73 */
74using Variant = boost::make_recursive_variant<
75 None, bool, int, std::size_t, double, std::string, ObjectRef,
77 Utils::Vector4d, std::vector<int>, std::vector<double>,
78 std::vector<boost::recursive_variant_>,
79 std::unordered_map<int, boost::recursive_variant_>,
80 std::unordered_map<std::string, boost::recursive_variant_>>::type;
81
82using VariantMap = std::unordered_map<std::string, Variant>;
83
84/**
85 * @brief Make a Variant from argument.
86 *
87 * This is a convenience function, so that rather involved constructors from
88 * boost::variant are not needed in the script interfaces.
89 */
90template <typename T> Variant make_variant(const T &x) { return Variant(x); }
91
92template <typename K, typename V>
93auto make_unordered_map_of_variants(std::unordered_map<K, V> const &v) {
94 std::unordered_map<K, Variant> ret;
95 for (auto const &it : v) {
96 ret.insert({it.first, Variant(it.second)});
97 }
98 return ret;
99}
100
101template <typename T> auto make_vector_of_variants(std::vector<T> const &v) {
102 std::vector<Variant> ret;
103 for (auto const &item : v) {
104 ret.emplace_back(item);
105 }
106 return ret;
107}
108
109namespace detail {
110template <class T> struct is_type_visitor : boost::static_visitor<bool> {
111 template <class U> constexpr bool operator()(const U &) const {
112 return std::is_same_v<T, U>;
113 }
114};
115} // namespace detail
116
117/**
118 * @brief Check is a Variant holds a specific type.
119 *
120 * @tparam T type to check for
121 * @param v Variant to check in
122 * @return true, if v holds a T.
123 */
124template <class T> bool is_type(Variant const &v) {
125 return boost::apply_visitor(detail::is_type_visitor<T>(), v);
126}
127
128inline bool is_none(Variant const &v) { return is_type<None>(v); }
129} // namespace ScriptInterface
Vector implementation and trait types for boost qvm interoperability.
Type to indicate no value in Variant.
bool is_type(Variant const &v)
Check is a Variant holds a specific type.
Definition Variant.hpp:124
std::shared_ptr< ObjectHandle > ObjectRef
Definition Variant.hpp:59
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
Variant make_variant(const T &x)
Make a Variant from argument.
Definition Variant.hpp:90
auto make_vector_of_variants(std::vector< T > const &v)
Definition Variant.hpp:101
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
bool is_none(Variant const &v)
Definition Variant.hpp:128
constexpr const None none
None-"literal".
Definition Variant.hpp:63
Utils::Vector< bool, 3 > Vector3b
Definition Variant.hpp:54
VectorXd< 3 > Vector3d
Definition Vector.hpp:157
VectorXd< 2 > Vector2d
Definition Vector.hpp:156
VectorXi< 3 > Vector3i
Definition Vector.hpp:166
VectorXd< 4 > Vector4d
Definition Vector.hpp:158