44#ifndef TEUCHOS_ANY_HPP
45#define TEUCHOS_ANY_HPP
91 static auto test(
int) ->
decltype(std::declval<X>() == std::declval<X>(),
92 void(), std::true_type());
94 static auto test(...) -> std::false_type;
102 static auto test(
int) ->
decltype(std::declval<std::ostream&>() << std::declval<X>(),
103 void(), std::true_type());
105 static auto test(...) -> std::false_type;
109template <class T, class ok = typename is_comparable<T>::type>
116 "Trying to compare type " <<
typeid(T).name() <<
" which is not comparable");
130template <class T, class ok = typename is_printable<T>::type>
137 "Trying to print type " <<
typeid(T).name() <<
" which is not printable");
146 std::ostream&
operator()(std::ostream& a, T
const& b)
const {
163 template<
typename ValueType>
164 explicit any(ValueType&& value)
165 :
content(new holder<
std::decay_t<ValueType>>(
std::forward<ValueType>(value)))
192 template<
typename ValueType>
195 any(rhs).swap(*
this);
202 any(rhs).swap(*
this);
211 this->
content = std::exchange(other.content,
nullptr);
227 const std::type_info &
type()
const
263#ifndef DOXYGEN_SHOULD_SKIP_THIS
272 virtual ~placeholder() {}
274 virtual const std::type_info & type()
const = 0;
276 virtual std::string typeName()
const = 0;
278 virtual placeholder * clone()
const = 0;
280 virtual bool same(
const placeholder &other )
const = 0;
282 virtual void print(std::ostream & os)
const = 0;
286 template<
typename ValueType>
287 class holder :
public placeholder
291 template <
typename U>
293 : held(std::forward<U>(value))
296 const std::type_info & type()
const
297 {
return typeid(ValueType); }
300 {
return TypeNameTraits<ValueType>::name(); }
302 placeholder * clone()
const
303 {
return new holder(held); }
305 bool same(
const placeholder &other )
const
307 if( type() != other.type() ) {
312 &other_held =
dynamic_cast<const holder<ValueType>&
>(other).held;
313 return ::Teuchos::compare<ValueType>{}(held, other_held);
316 void print(std::ostream & os)
const
317 { ::Teuchos::print<ValueType>{}(os, held); }
326 placeholder* access_content()
328 const placeholder* access_content()
const
358template<
typename ValueType>
364 "any_cast<"<<ValueTypeName<<
">(operand): Error, cast to type "
365 <<
"any::holder<"<<ValueTypeName<<
"> failed since the actual underlying type is \'"
366 <<
typeName(*operand.access_content()) <<
"!"
370 ,
"any_cast<"<<ValueTypeName<<
">(operand): Error, cast to type "
371 <<
"any::holder<"<<ValueTypeName<<
"> failed because the content is NULL"
373 any::holder<ValueType>
374 *dyn_cast_content =
dynamic_cast<any::holder<ValueType>*
>(operand.access_content());
376 !dyn_cast_content, std::logic_error
377 ,
"any_cast<"<<ValueTypeName <<
">(operand): Error, cast to type "
378 <<
"any::holder<"<<ValueTypeName<<
"> failed but should not have and the actual underlying type is \'"
379 <<
typeName(*operand.access_content()) <<
"!"
380 <<
" The problem might be related to incompatible RTTI systems in static and shared libraries!"
382 return dyn_cast_content->held;
394template<
typename ValueType>
403template <
typename ValueType>
412template <
typename ValueType>
415 using U = std::remove_cv_t<std::remove_reference_t<ValueType>>;
416 static_assert(std::is_constructible_v<ValueType, U>);
417 return static_cast<ValueType
>(std::move(*
any_cast<U>(&operand)));
427template<
typename ValueType>
440 std::ostringstream oss;
#define TEUCHOSCORE_LIB_DLL_EXPORT
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.
ValueType * any_cast(any *operand)
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.
ValueType any_cast(any &&operand)
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.
bad_any_cast(const std::string msg)
#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.
bool operator()(T const &, T const &) const
bool operator()(T const &a, T const &b) const
static auto test(int) -> decltype(std::declval< X >()==std::declval< X >(), void(), std::true_type())
decltype(test< T >(0)) type
static auto test(...) -> std::false_type
static auto test(...) -> std::false_type
decltype(test< T >(0)) type
static auto test(int) -> decltype(std::declval< std::ostream & >()<< std::declval< X >(), void(), std::true_type())
std::ostream & operator()(std::ostream &s, T const &) const
std::ostream & operator()(std::ostream &a, T const &b) const