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>
138 : content(other.content ? other.content->clone() : 0)
155 std::swap(content, rhs.content);
160 template<
typename ValueType>
178 delete this->content;
179 this->content = std::exchange(other.content,
nullptr);
188 return ! this->has_value();
192 bool has_value()
const {
return this->content !=
nullptr; }
195 const std::type_info &
type()
const
197 return content ? content->type() :
typeid(
void);
203 return content ? content->typeName() :
"NONE";
212 if( !this->has_value() && !other.
has_value() )
214 else if( this->has_value() && !other.
has_value() )
216 else if( !this->has_value() && other.
has_value() )
219 return content->same(*other.content);
228 if (content) content->print(os);
231#ifndef DOXYGEN_SHOULD_SKIP_THIS
242 virtual const std::type_info & type()
const = 0;
244 virtual std::string typeName()
const = 0;
246 virtual placeholder * clone()
const = 0;
248 virtual bool same(
const placeholder &other )
const = 0;
250 virtual void print(std::ostream & os)
const = 0;
254 template<
typename ValueType>
255 class holder :
public placeholder
259 template <
typename U>
261 : held(std::forward<U>(value))
264 const std::type_info & type()
const
265 {
return typeid(ValueType); }
268 {
return TypeNameTraits<ValueType>::name(); }
270 placeholder * clone()
const
271 {
return new holder(held); }
273 bool same(
const placeholder &other )
const
275 if( type() != other.type() ) {
280 &other_held =
dynamic_cast<const holder<ValueType>&
>(other).held;
281 return ::Teuchos::compare<ValueType>{}(held, other_held);
284 void print(std::ostream & os)
const
294 placeholder* access_content()
296 const placeholder* access_content()
const
305 placeholder * content;
315 bad_any_cast(
const std::string msg ) : std::runtime_error(msg) {}
326template<
typename ValueType>
332 "any_cast<"<<
ValueTypeName<<
">(operand): Error, cast to type "
333 <<
"any::holder<"<<
ValueTypeName<<
"> failed since the actual underlying type is \'"
338 ,
"any_cast<"<<
ValueTypeName<<
">(operand): Error, cast to type "
339 <<
"any::holder<"<<
ValueTypeName<<
"> failed because the content is NULL"
341 any::holder<ValueType>
345 ,
"any_cast<"<<
ValueTypeName <<
">(operand): Error, cast to type "
346 <<
"any::holder<"<<
ValueTypeName<<
"> failed but should not have and the actual underlying type is \'"
348 <<
" The problem might be related to incompatible RTTI systems in static and shared libraries!"
362template<
typename ValueType>
371template <
typename ValueType>
380template <
typename ValueType>
381ValueType any_cast(any&& operand)
383 using U = std::remove_cv_t<std::remove_reference_t<ValueType>>;
384 static_assert(std::is_constructible_v<ValueType, U>);
385 return static_cast<ValueType
>(std::move(*any_cast<U>(&operand)));
395template<
typename ValueType>
408 std::ostringstream
oss;
Defines basic traits returning the name of a type in a portable and readable way.
Smart reference counting pointer class for automatic garbage collection.
void swap(RCP< T > &r_ptr)
Swap the contents with some other RCP object.
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.
ValueType & any_cast(any &operand)
Used to extract the templated value held in Teuchos::any to a given value type.
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.
any(ValueType &&value)
Templated constructor.
std::string typeName() const
Return the name of the 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...
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,...