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>
26
27#include <boost/serialization/serialization.hpp>
28#include <boost/serialization/string.hpp>
29#include <boost/serialization/variant.hpp>
30#include <boost/serialization/vector.hpp>
31#include <boost/variant.hpp>
32
33#include <cstddef>
34#include <memory>
35#include <string>
36#include <type_traits>
37#include <unordered_map>
38#include <vector>
39
43
44namespace ScriptInterface {
45class ObjectHandle;
46using ObjectRef = std::shared_ptr<ObjectHandle>;
47/**
48 * @brief None-"literal".
49 */
50constexpr const None none{};
51
52/**
53 * @brief Possible types for parameters.
54 *
55 * The visitors and packing functions need to be adapted accordingly when
56 * extending this variant with new types. For the exact details, see commit
57 * <a href="https://github.com/espressomd/espresso/commit/b48ab62">b48ab62</a>.
58 * The number of types is limited by macro @c BOOST_MPL_LIMIT_LIST_SIZE
59 * (defaults to 20).
60 */
61using Variant = boost::make_recursive_variant<
62 None, bool, int, std::size_t, double, std::string, ObjectRef,
64 Utils::Vector4d, std::vector<int>, std::vector<double>,
65 std::vector<boost::recursive_variant_>,
66 std::unordered_map<int, boost::recursive_variant_>,
67 std::unordered_map<std::string, boost::recursive_variant_>>::type;
68
69using VariantMap = std::unordered_map<std::string, Variant>;
70
71/**
72 * @brief Make a Variant from argument.
73 *
74 * This is a convenience function, so that rather involved constructors from
75 * boost::variant are not needed in the script interfaces.
76 */
77template <typename T> Variant make_variant(const T &x) { return Variant(x); }
78
79template <typename K, typename V>
80auto make_unordered_map_of_variants(std::unordered_map<K, V> const &v) {
81 std::unordered_map<K, Variant> ret;
82 for (auto const &it : v) {
83 ret.insert({it.first, Variant(it.second)});
84 }
85 return ret;
86}
87
88template <typename T> auto make_vector_of_variants(std::vector<T> const &v) {
89 std::vector<Variant> ret;
90 for (auto const &item : v) {
91 ret.emplace_back(item);
92 }
93 return ret;
94}
95
96namespace detail {
97template <class T> struct is_type_visitor : boost::static_visitor<bool> {
98 template <class U> constexpr bool operator()(const U &) const {
99 return std::is_same_v<T, U>;
100 }
101};
102} // namespace detail
103
104/**
105 * @brief Check is a Variant holds a specific type.
106 *
107 * @tparam T type to check for
108 * @param v Variant to check in
109 * @return true, if v holds a T.
110 */
111template <class T> bool is_type(Variant const &v) {
112 return boost::apply_visitor(detail::is_type_visitor<T>(), v);
113}
114
115inline bool is_none(Variant const &v) { return is_type<None>(v); }
116} // 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:111
std::shared_ptr< ObjectHandle > ObjectRef
Definition Variant.hpp:46
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_unordered_map_of_variants(std::unordered_map< K, V > const &v)
Definition Variant.hpp:80
Variant make_variant(const T &x)
Make a Variant from argument.
Definition Variant.hpp:77
auto make_vector_of_variants(std::vector< T > const &v)
Definition Variant.hpp:88
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
bool is_none(Variant const &v)
Definition Variant.hpp:115
constexpr const None none
None-"literal".
Definition Variant.hpp:50
Utils::Vector< bool, 3 > Vector3b
Definition Variant.hpp:41
VectorXd< 3 > Vector3d
Definition Vector.hpp:164
VectorXd< 2 > Vector2d
Definition Vector.hpp:163
VectorXi< 3 > Vector3i
Definition Vector.hpp:173
VectorXd< 4 > Vector4d
Definition Vector.hpp:165