10#ifndef TEUCHOS_ANY_HPP
11#define TEUCHOS_ANY_HPP
21#include "Teuchos_Assert.hpp"
23#include "Teuchos_Exceptions.hpp"
58 static auto test(
int) ->
decltype(std::declval<X>() == std::declval<X>(),
59 void(), std::true_type());
61 static auto test(...) -> std::false_type;
62 using type =
decltype(test<T>(0));
69 static auto test(
int) ->
decltype(std::declval<std::ostream&>() << std::declval<X>(),
70 void(), std::true_type());
72 static auto test(...) -> std::false_type;
73 using type =
decltype(test<T>(0));
76template <class T, class ok = typename is_comparable<T>::type>
80struct compare<T, std::false_type> {
81 bool operator()(T
const&, T
const&)
const {
83 "Trying to compare type " <<
typeid(T).name() <<
" which is not comparable");
91struct compare<T, std::true_type> {
92 bool operator()(T
const& a, T
const& b)
const {
97template <class T, class ok = typename is_printable<T>::type>
101struct print<T, std::false_type> {
102 std::ostream& operator()(std::ostream& s, T
const&)
const {
105 " which is not printable (i.e. does not have operator<<() defined)!");
113struct print<T, std::true_type> {
114 std::ostream& operator()(std::ostream& a, T
const& b)
const {
122class TEUCHOSCORE_LIB_DLL_EXPORT
any
131 template<
typename ValueType,
132 std::enable_if_t<!std::is_same_v<std::decay_t<ValueType>,
any>,
int> = 0>
133 explicit any(ValueType&& value)
134 : content(new holder<std::decay_t<ValueType>>(std::forward<ValueType>(value)))
139 : content(other.content ? other.content->clone() : 0)
144 : content(std::exchange(other.content,nullptr))
156 std::swap(content, rhs.content);
161 template<
typename ValueType>
164 any(rhs).swap(*
this);
171 any(rhs).swap(*
this);
179 delete this->content;
180 this->content = std::exchange(other.content,
nullptr);
193 bool has_value()
const {
return this->content !=
nullptr; }
196 const std::type_info &
type()
const
198 return content ? content->type() :
typeid(void);
204 return content ? content->typeName() :
"NONE";
220 return content->same(*other.content);
229 if (content) content->print(os);
232#ifndef DOXYGEN_SHOULD_SKIP_THIS
241 virtual ~placeholder() {}
243 virtual const std::type_info & type()
const = 0;
245 virtual std::string typeName()
const = 0;
247 virtual placeholder * clone()
const = 0;
249 virtual bool same(
const placeholder &other )
const = 0;
251 virtual void print(std::ostream & os)
const = 0;
255 template<
typename ValueType>
256 class holder :
public placeholder
260 template <
typename U>
262 : held(std::forward<U>(value))
265 const std::type_info & type()
const
266 {
return typeid(ValueType); }
269 {
return TypeNameTraits<ValueType>::name(); }
271 placeholder * clone()
const
272 {
return new holder(held); }
274 bool same(
const placeholder &other )
const
276 if( type() != other.type() ) {
281 &other_held =
dynamic_cast<const holder<ValueType>&
>(other).held;
282 return ::Teuchos::compare<ValueType>{}(held, other_held);
285 void print(std::ostream & os)
const
286 { ::Teuchos::print<ValueType>{}(os, held); }
295 placeholder* access_content()
297 const placeholder* access_content()
const
306 placeholder * content;
313class bad_any_cast :
public std::runtime_error
316 bad_any_cast(
const std::string msg ) : std::runtime_error(msg) {}
327template<
typename ValueType>
333 "any_cast<"<<ValueTypeName<<
">(operand): Error, cast to type "
334 <<
"any::holder<"<<ValueTypeName<<
"> failed since the actual underlying type is \'"
335 <<
typeName(*operand.access_content()) <<
"!"
339 ,
"any_cast<"<<ValueTypeName<<
">(operand): Error, cast to type "
340 <<
"any::holder<"<<ValueTypeName<<
"> failed because the content is NULL"
342 any::holder<ValueType>
343 *dyn_cast_content =
dynamic_cast<any::holder<ValueType>*
>(operand.access_content());
345 !dyn_cast_content, std::logic_error
346 ,
"any_cast<"<<ValueTypeName <<
">(operand): Error, cast to type "
347 <<
"any::holder<"<<ValueTypeName<<
"> failed but should not have and the actual underlying type is \'"
348 <<
typeName(*operand.access_content()) <<
"!"
349 <<
" The problem might be related to incompatible RTTI systems in static and shared libraries!"
351 return dyn_cast_content->held;
363template<
typename ValueType>
372template <
typename ValueType>
381template <
typename ValueType>
384 using U = std::remove_cv_t<std::remove_reference_t<ValueType>>;
385 static_assert(std::is_constructible_v<ValueType, U>);
386 return static_cast<ValueType
>(std::move(*
any_cast<U>(&operand)));
396template<
typename ValueType>
409 std::ostringstream oss;
Defines basic traits returning the name of a type in a portable and readable way.
static std::string name()
Modified boost::any class, which is a container for a templated value.
void print(std::ostream &os) const
Print this value to the output stream os.
std::string toString(const any &rhs)
Converts the value in any to a std::string.
any & operator=(any &&other)
Move-assignment operator.
bool operator==(const any &a, const any &b)
Returns true if two any objects have the same value.
bool same(const any &other) const
Return if two any objects are the same or not.
std::ostream & operator<<(std::ostream &os, const any &rhs)
Writes "any" input rhs to the output stream os.
const ValueType & any_cast(const any &operand)
Used to extract the const templated value held in Teuchos::any to a given const value type.
bool operator!=(const any &a, const any &b)
Returns true if two any objects do not have the same value.
TEUCHOS_DEPRECATED bool empty() const
Return true if nothing is being stored.
void swap(Teuchos::any &a, Teuchos::any &b)
Special swap for other code to find via Argument Dependent Lookup.
any(any &&other)
Move constructor.
any(const any &other)
Copy constructor.
std::string typeName() const
Return the name of the type.
ValueType & any_cast(any &operand)
Used to extract the templated value held in Teuchos::any to a given value type.
const std::type_info & type() const
Return the type of value being stored.
ValueType & any_ref_cast(any &operand)
Keep the convenient behavior of Teuchos::any_cast w.r.t. references, but don't confuse it with the be...
any(ValueType &&value)
Templated constructor (excluded for any/any& to prevent infinite recursion via holder<any>).
T & make_any_ref(any &rhs)
Default constructs a new T value and returns a reference to it.
any & swap(any &rhs)
Method for swapping the contents of two any classes.
bool has_value() const
Checks whether the object contains a value.
any & operator=(const ValueType &rhs)
Copy the value rhs.
any & operator=(const any &rhs)
Copy the value held in rhs.
Thrown if any_cast is attempted between two incompatable types.
#define TEUCHOS_TEST_FOR_EXCEPTION_PURE_MSG(throw_exception_test, Exception, msg)
Macro for throwing an exception with breakpointing to ease debugging.
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
Macro for throwing an exception with breakpointing to ease debugging.
std::string typeName(const T &t)
Template function for returning the concrete type name of a passed-in object.
TEUCHOSCORE_LIB_DLL_EXPORT std::string demangleName(const std::string &mangledName)
Demangle a C++ name if valid.
The Teuchos namespace contains all of the classes, structs and enums used by Teuchos,...