2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 2016,2017,2018, by the GROMACS development team, led by
5 * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
6 * and including many others, as listed in the AUTHORS file in the
7 * top-level source directory and at http://www.gromacs.org.
9 * GROMACS is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public License
11 * as published by the Free Software Foundation; either version 2.1
12 * of the License, or (at your option) any later version.
14 * GROMACS is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with GROMACS; if not, see
21 * http://www.gnu.org/licenses, or write to the Free Software Foundation,
22 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 * If you want to redistribute modifications to GROMACS, please
25 * consider that scientific software is very special. Version
26 * control is crucial - bugs must be traceable. We will be happy to
27 * consider code for inclusion in the official distribution, but
28 * derived work must not be called official GROMACS. Details are found
29 * in the README & COPYING files - if they are missing, get the
30 * official version at http://www.gromacs.org.
32 * To help us fund GROMACS development, we humbly ask that you cite
33 * the research papers on the package. Check out http://www.gromacs.org.
35 /*! \libinternal \file
37 * Declares gmx::Variant.
39 * \author Teemu Murtola <teemu.murtola@gmail.com>
41 * \ingroup module_utility
43 #ifndef GMX_UTILITY_VARIANT_H
44 #define GMX_UTILITY_VARIANT_H
48 #include <type_traits>
53 #include "gromacs/compat/make_unique.h"
54 #include "gromacs/utility/gmxassert.h"
59 /*! \libinternal \brief
60 * Represents a dynamically typed value of an arbitrary type.
62 * To create a variant, either initialize it as empty, or with the create()
63 * method (or the equivalent constructor, if the type parameter can be deduced
64 * and is clear to the reader from the context).
66 * To query the type of the contents in the variant, use isEmpty(), type(), and
69 * To access the value, you need to know the type as a compile-time constant
70 * (e.g., through branching based on isType()), and then use cast() or
73 * Methods in this class do not throw unless otherwise indicated.
75 * This provides essentially the same functionality as boost::any.
77 * \ingroup module_utility
83 * Creates a variant that holds the given value.
85 * \throws std::bad_alloc if out of memory.
87 * This method allows explicitly specifying the template argument,
88 * contrary to the templated constructor.
91 static Variant create(const T &value) { return Variant(value); }
93 * Creates a variant that holds the given value.
95 * \throws std::bad_alloc if out of memory.
97 * In addition to allowing specifying the template argument, this
98 * method avoids copying when move-construction is possible.
100 template <typename T>
101 static Variant create(T &&value) { return Variant(std::forward<T>(value)); }
103 //! Creates an empty variant value.
106 * Creates a variant that holds the given value.
108 * \throws std::bad_alloc if out of memory.
110 template <typename T, typename = typename std::enable_if<!std::is_same<T, Variant>::value>::type>
111 explicit Variant(T &&value)
112 : content_(new Content<typename std::decay<T>::type>(std::forward<T>(value)))
116 * Creates a deep copy of a variant.
118 * \throws std::bad_alloc if out of memory.
120 Variant(const Variant &other) : content_(other.cloneContent()) {}
121 //! Move-constructs a variant.
122 Variant(Variant &&other) noexcept : content_(std::move(other.content_)) {}
124 * Assigns the variant.
126 * \throws std::bad_alloc if out of memory.
128 Variant &operator=(const Variant &other)
130 content_ = other.cloneContent();
133 //! Move-assigns the variant.
134 Variant &operator=(Variant &&other) noexcept
136 content_ = std::move(other.content_);
140 //! Whether any value is stored.
141 bool isEmpty() const { return content_ == nullptr; }
142 //! Returns the dynamic type of the value that is currently stored.
143 std::type_index type() const
145 const std::type_info &info
146 = !isEmpty() ? content_->typeInfo() : typeid(void);
147 return std::type_index(info);
149 //! Returns whether the type stored matches the template parameter.
150 template <typename T>
153 return !isEmpty() && content_->typeInfo() == typeid(T);
157 * Tries to get the value as the given type.
159 * \tparam T Type to get.
160 * \returns Pointer to the value, or nullptr if the type does not match
163 template <typename T>
164 const T *tryCast() const
166 return isType<T>() ? &static_cast<Content<T> *>(content_.get())->value_ : nullptr;
169 * Gets the value when the type is known.
171 * \tparam T Type to get (which must match what the variant stores).
173 * Asserts if the variant is empty or does not contain the requested type.
175 template <typename T>
176 const T &cast() const
178 const T *value = tryCast<T>();
179 GMX_RELEASE_ASSERT(value != nullptr, "Cast to incorrect type");
183 * Tries to get the value as the given type as a non-const pointer.
185 * \tparam T Type to get.
186 * \returns Pointer to the value, or nullptr if the type does not match
189 * This method allows modifying the value in-place, which is useful
190 * with more complicated data structures.
192 template <typename T>
195 return isType<T>() ? &static_cast<Content<T> *>(content_.get())->value_ : nullptr;
198 * Gets the value when the type is known as a modifiable reference.
200 * \tparam T Type to get (which must match what the variant stores).
202 * Asserts if the variant is empty or does not contain the requested type.
204 template <typename T>
207 T *value = tryCastRef<T>();
208 GMX_RELEASE_ASSERT(value != nullptr, "Cast to incorrect type");
216 virtual ~IContent() {}
217 virtual const std::type_info &typeInfo() const = 0;
218 virtual std::unique_ptr<IContent> clone() const = 0;
221 template <typename T>
222 class Content : public IContent
225 explicit Content(const T &value) : value_(value) {}
226 explicit Content(T &&value) : value_(std::move(value)) {}
228 const std::type_info &typeInfo() const override { return typeid(T); }
229 std::unique_ptr<IContent> clone() const override { return compat::make_unique<Content>(value_); }
234 //! Creates a deep copy of the content.
235 std::unique_ptr<IContent> cloneContent() const
237 return content_ != nullptr ? content_->clone() : nullptr;
240 std::unique_ptr<IContent> content_;
245 * Converts a Variant value to a string.
247 * As the name suggests, only some types of "simple" values (such as int) are
248 * supported. Asserts for unsupported types.
250 * \ingroup module_utility
252 std::string simpleValueToString(const Variant &value);