Replace compat::make_unique with std::make_unique
[gromacs.git] / src / gromacs / utility / any.h
blob8e9464e4227e1f73f1e0960befcb58fe3a22b5c8
1 /*
2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 2016,2017,2018,2019, 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
36 * \brief
37 * Declares gmx::Any.
39 * \author Teemu Murtola <teemu.murtola@gmail.com>
40 * \inlibraryapi
41 * \ingroup module_utility
43 #ifndef GMX_UTILITY_ANY_H
44 #define GMX_UTILITY_ANY_H
46 #include <memory>
47 #include <string>
48 #include <type_traits>
49 #include <typeindex>
50 #include <typeinfo>
51 #include <utility>
53 #include "gromacs/utility/gmxassert.h"
55 namespace gmx
58 /*! \libinternal \brief
59 * Represents a dynamically typed value of an arbitrary type.
61 * To create a any, either initialize it as empty, or with the create()
62 * method (or the equivalent constructor, if the type parameter can be deduced
63 * and is clear to the reader from the context).
65 * To query the type of the contents in the any, use isEmpty(), type(), and
66 * isType().
68 * To access the value, you need to know the type as a compile-time constant
69 * (e.g., through branching based on isType()), and then use cast() or
70 * tryCast().
72 * Methods in this class do not throw unless otherwise indicated.
74 * This provides essentially the same functionality as boost::any.
76 * \ingroup module_utility
78 class Any
80 public:
81 /*! \brief
82 * Creates a any that holds the given value.
84 * \throws std::bad_alloc if out of memory.
86 * This method allows explicitly specifying the template argument,
87 * contrary to the templated constructor.
89 template <typename T>
90 static Any create(const T &value) { return Any(value); }
91 /*! \brief
92 * Creates a any that holds the given value.
94 * \throws std::bad_alloc if out of memory.
96 * In addition to allowing specifying the template argument, this
97 * method avoids copying when move-construction is possible.
99 template <typename T>
100 static Any create(T &&value) { return Any(std::forward<T>(value)); }
102 //! Creates an empty any value.
103 Any() {}
104 /*! \brief
105 * Creates a any that holds the given value.
107 * \throws std::bad_alloc if out of memory.
109 template <typename T, typename = typename std::enable_if<!std::is_same<T, Any>::value>::type>
110 explicit Any(T &&value)
111 : content_(new Content<typename std::decay<T>::type>(std::forward<T>(value)))
114 /*! \brief
115 * Creates a deep copy of a any.
117 * \throws std::bad_alloc if out of memory.
119 Any(const Any &other) : content_(other.cloneContent()) {}
120 //! Move-constructs a any.
121 Any(Any &&other) noexcept : content_(std::move(other.content_)) {}
122 /*! \brief
123 * Assigns the any.
125 * \throws std::bad_alloc if out of memory.
127 Any &operator=(const Any &other)
129 content_ = other.cloneContent();
130 return *this;
132 //! Move-assigns the any.
133 Any &operator=(Any &&other) noexcept
135 content_ = std::move(other.content_);
136 return *this;
139 //! Whether any value is stored.
140 bool isEmpty() const { return content_ == nullptr; }
141 //! Returns the dynamic type of the value that is currently stored.
142 std::type_index type() const
144 const std::type_info &info
145 = !isEmpty() ? content_->typeInfo() : typeid(void);
146 return std::type_index(info);
148 //! Returns whether the type stored matches the template parameter.
149 template <typename T>
150 bool isType() const
152 return !isEmpty() && content_->typeInfo() == typeid(T);
155 /*! \brief
156 * Tries to get the value as the given type.
158 * \tparam T Type to get.
159 * \returns Pointer to the value, or nullptr if the type does not match
160 * the stored value.
162 template <typename T>
163 const T *tryCast() const
165 return isType<T>() ? &static_cast<Content<T> *>(content_.get())->value_ : nullptr;
167 /*! \brief
168 * Gets the value when the type is known.
170 * \tparam T Type to get (which must match what the any stores).
172 * Asserts if the any is empty or does not contain the requested type.
174 template <typename T>
175 const T &cast() const
177 const T *value = tryCast<T>();
178 GMX_RELEASE_ASSERT(value != nullptr, "Cast to incorrect type");
179 return *value;
181 /*! \brief
182 * Tries to get the value as the given type as a non-const pointer.
184 * \tparam T Type to get.
185 * \returns Pointer to the value, or nullptr if the type does not match
186 * the stored value.
188 * This method allows modifying the value in-place, which is useful
189 * with more complicated data structures.
191 template <typename T>
192 T *tryCastRef()
194 return isType<T>() ? &static_cast<Content<T> *>(content_.get())->value_ : nullptr;
196 /*! \brief
197 * Gets the value when the type is known as a modifiable reference.
199 * \tparam T Type to get (which must match what the any stores).
201 * Asserts if the any is empty or does not contain the requested type.
203 template <typename T>
204 T &castRef()
206 T *value = tryCastRef<T>();
207 GMX_RELEASE_ASSERT(value != nullptr, "Cast to incorrect type");
208 return *value;
211 private:
212 class IContent
214 public:
215 virtual ~IContent() {}
216 virtual const std::type_info &typeInfo() const = 0;
217 virtual std::unique_ptr<IContent> clone() const = 0;
220 template <typename T>
221 class Content : public IContent
223 public:
224 explicit Content(const T &value) : value_(value) {}
225 explicit Content(T &&value) : value_(std::move(value)) {}
227 const std::type_info &typeInfo() const override { return typeid(T); }
228 std::unique_ptr<IContent> clone() const override { return std::make_unique<Content>(value_); }
230 T value_;
233 //! Creates a deep copy of the content.
234 std::unique_ptr<IContent> cloneContent() const
236 return content_ != nullptr ? content_->clone() : nullptr;
239 std::unique_ptr<IContent> content_;
242 //! \cond libapi
243 /*! \brief
244 * Converts a Any value to a string.
246 * As the name suggests, only some types of "simple" values (such as int) are
247 * supported. Asserts for unsupported types.
249 * \ingroup module_utility
251 std::string simpleValueToString(const Any &value);
252 //! \endcond
254 } // namespace gmx
256 #endif