*/
class Any
{
- public:
- /*! \brief
- * Creates a any that holds the given value.
- *
- * \throws std::bad_alloc if out of memory.
- *
- * This method allows explicitly specifying the template argument,
- * contrary to the templated constructor.
- */
- template <typename T>
- static Any create(const T &value) { return Any(value); }
- /*! \brief
- * Creates a any that holds the given value.
- *
- * \throws std::bad_alloc if out of memory.
- *
- * In addition to allowing specifying the template argument, this
- * method avoids copying when move-construction is possible.
- */
- template <typename T>
- static Any create(T &&value) { return Any(std::forward<T>(value)); }
+public:
+ /*! \brief
+ * Creates a any that holds the given value.
+ *
+ * \throws std::bad_alloc if out of memory.
+ *
+ * This method allows explicitly specifying the template argument,
+ * contrary to the templated constructor.
+ */
+ template<typename T>
+ static Any create(const T& value)
+ {
+ return Any(value);
+ }
+ /*! \brief
+ * Creates a any that holds the given value.
+ *
+ * \throws std::bad_alloc if out of memory.
+ *
+ * In addition to allowing specifying the template argument, this
+ * method avoids copying when move-construction is possible.
+ */
+ template<typename T>
+ static Any create(T&& value)
+ {
+ return Any(std::forward<T>(value));
+ }
- //! Creates an empty any value.
- Any() {}
- /*! \brief
- * Creates a any that holds the given value.
- *
- * \throws std::bad_alloc if out of memory.
- */
- template <typename T, typename = std::enable_if_t<!std::is_same<T, Any>::value> >
- explicit Any(T &&value)
- : content_(new Content < std::decay_t < T>>(std::forward<T>(value)))
- {
- }
- /*! \brief
- * Creates a deep copy of a any.
- *
- * \throws std::bad_alloc if out of memory.
- */
- Any(const Any &other) : content_(other.cloneContent()) {}
- //! Move-constructs a any.
- Any(Any &&other) noexcept : content_(std::move(other.content_)) {}
- /*! \brief
- * Assigns the any.
- *
- * \throws std::bad_alloc if out of memory.
- */
- Any &operator=(const Any &other)
- {
- content_ = other.cloneContent();
- return *this;
- }
- //! Move-assigns the any.
- Any &operator=(Any &&other) noexcept
- {
- content_ = std::move(other.content_);
- return *this;
- }
+ //! Creates an empty any value.
+ Any() {}
+ /*! \brief
+ * Creates a any that holds the given value.
+ *
+ * \throws std::bad_alloc if out of memory.
+ */
+ template<typename T, typename = std::enable_if_t<!std::is_same<T, Any>::value>>
+ explicit Any(T&& value) : content_(new Content<std::decay_t<T>>(std::forward<T>(value)))
+ {
+ }
+ /*! \brief
+ * Creates a deep copy of a any.
+ *
+ * \throws std::bad_alloc if out of memory.
+ */
+ Any(const Any& other) : content_(other.cloneContent()) {}
+ //! Move-constructs a any.
+ Any(Any&& other) noexcept : content_(std::move(other.content_)) {}
+ /*! \brief
+ * Assigns the any.
+ *
+ * \throws std::bad_alloc if out of memory.
+ */
+ Any& operator=(const Any& other)
+ {
+ content_ = other.cloneContent();
+ return *this;
+ }
+ //! Move-assigns the any.
+ Any& operator=(Any&& other) noexcept
+ {
+ content_ = std::move(other.content_);
+ return *this;
+ }
- //! Whether any value is stored.
- bool isEmpty() const { return content_ == nullptr; }
- //! Returns the dynamic type of the value that is currently stored.
- std::type_index type() const
- {
- const std::type_info &info
- = !isEmpty() ? content_->typeInfo() : typeid(void);
- return std::type_index(info);
- }
- //! Returns whether the type stored matches the template parameter.
- template <typename T>
- bool isType() const
- {
- return !isEmpty() && content_->typeInfo() == typeid(T);
- }
+ //! Whether any value is stored.
+ bool isEmpty() const { return content_ == nullptr; }
+ //! Returns the dynamic type of the value that is currently stored.
+ std::type_index type() const
+ {
+ const std::type_info& info = !isEmpty() ? content_->typeInfo() : typeid(void);
+ return std::type_index(info);
+ }
+ //! Returns whether the type stored matches the template parameter.
+ template<typename T>
+ bool isType() const
+ {
+ return !isEmpty() && content_->typeInfo() == typeid(T);
+ }
- /*! \brief
- * Tries to get the value as the given type.
- *
- * \tparam T Type to get.
- * \returns Pointer to the value, or nullptr if the type does not match
- * the stored value.
- */
- template <typename T>
- const T *tryCast() const
- {
- return isType<T>() ? &static_cast<Content<T> *>(content_.get())->value_ : nullptr;
- }
- /*! \brief
- * Gets the value when the type is known.
- *
- * \tparam T Type to get (which must match what the any stores).
- *
- * Asserts if the any is empty or does not contain the requested type.
- */
- template <typename T>
- const T &cast() const
- {
- const T *value = tryCast<T>();
- GMX_RELEASE_ASSERT(value != nullptr, "Cast to incorrect type");
- return *value;
- }
- /*! \brief
- * Tries to get the value as the given type as a non-const pointer.
- *
- * \tparam T Type to get.
- * \returns Pointer to the value, or nullptr if the type does not match
- * the stored value.
- *
- * This method allows modifying the value in-place, which is useful
- * with more complicated data structures.
- */
- template <typename T>
- T *tryCastRef()
- {
- return isType<T>() ? &static_cast<Content<T> *>(content_.get())->value_ : nullptr;
- }
- /*! \brief
- * Gets the value when the type is known as a modifiable reference.
- *
- * \tparam T Type to get (which must match what the any stores).
- *
- * Asserts if the any is empty or does not contain the requested type.
- */
- template <typename T>
- T &castRef()
- {
- T *value = tryCastRef<T>();
- GMX_RELEASE_ASSERT(value != nullptr, "Cast to incorrect type");
- return *value;
- }
-
- private:
- class IContent
- {
- public:
- virtual ~IContent() {}
- virtual const std::type_info &typeInfo() const = 0;
- virtual std::unique_ptr<IContent> clone() const = 0;
- };
+ /*! \brief
+ * Tries to get the value as the given type.
+ *
+ * \tparam T Type to get.
+ * \returns Pointer to the value, or nullptr if the type does not match
+ * the stored value.
+ */
+ template<typename T>
+ const T* tryCast() const
+ {
+ return isType<T>() ? &static_cast<Content<T>*>(content_.get())->value_ : nullptr;
+ }
+ /*! \brief
+ * Gets the value when the type is known.
+ *
+ * \tparam T Type to get (which must match what the any stores).
+ *
+ * Asserts if the any is empty or does not contain the requested type.
+ */
+ template<typename T>
+ const T& cast() const
+ {
+ const T* value = tryCast<T>();
+ GMX_RELEASE_ASSERT(value != nullptr, "Cast to incorrect type");
+ return *value;
+ }
+ /*! \brief
+ * Tries to get the value as the given type as a non-const pointer.
+ *
+ * \tparam T Type to get.
+ * \returns Pointer to the value, or nullptr if the type does not match
+ * the stored value.
+ *
+ * This method allows modifying the value in-place, which is useful
+ * with more complicated data structures.
+ */
+ template<typename T>
+ T* tryCastRef()
+ {
+ return isType<T>() ? &static_cast<Content<T>*>(content_.get())->value_ : nullptr;
+ }
+ /*! \brief
+ * Gets the value when the type is known as a modifiable reference.
+ *
+ * \tparam T Type to get (which must match what the any stores).
+ *
+ * Asserts if the any is empty or does not contain the requested type.
+ */
+ template<typename T>
+ T& castRef()
+ {
+ T* value = tryCastRef<T>();
+ GMX_RELEASE_ASSERT(value != nullptr, "Cast to incorrect type");
+ return *value;
+ }
- template <typename T>
- class Content : public IContent
- {
- public:
- explicit Content(const T &value) : value_(value) {}
- explicit Content(T &&value) : value_(std::move(value)) {}
-
- const std::type_info &typeInfo() const override { return typeid(T); }
- std::unique_ptr<IContent> clone() const override { return std::make_unique<Content>(value_); }
+private:
+ class IContent
+ {
+ public:
+ virtual ~IContent() {}
+ virtual const std::type_info& typeInfo() const = 0;
+ virtual std::unique_ptr<IContent> clone() const = 0;
+ };
- T value_;
- };
+ template<typename T>
+ class Content : public IContent
+ {
+ public:
+ explicit Content(const T& value) : value_(value) {}
+ explicit Content(T&& value) : value_(std::move(value)) {}
- //! Creates a deep copy of the content.
- std::unique_ptr<IContent> cloneContent() const
+ const std::type_info& typeInfo() const override { return typeid(T); }
+ std::unique_ptr<IContent> clone() const override
{
- return content_ != nullptr ? content_->clone() : nullptr;
+ return std::make_unique<Content>(value_);
}
- std::unique_ptr<IContent> content_;
+ T value_;
+ };
+
+ //! Creates a deep copy of the content.
+ std::unique_ptr<IContent> cloneContent() const
+ {
+ return content_ != nullptr ? content_->clone() : nullptr;
+ }
+
+ std::unique_ptr<IContent> content_;
};
//! \cond libapi
*
* \ingroup module_utility
*/
-std::string simpleValueToString(const Any &value);
+std::string simpleValueToString(const Any& value);
//! \endcond
} // namespace gmx