44 #ifndef TEUCHOS_ANY_HPP
45 #define TEUCHOS_ANY_HPP
52 #include <type_traits>
55 #include "Teuchos_Assert.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;
95 using type = decltype(test<T>(0));
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;
106 using type = decltype(test<T>(0));
109 template <class T, class ok = typename is_comparable<T>::type>
113 struct compare<T, std::false_type> {
114 bool operator()(T
const&, T
const&)
const {
116 "Trying to compare type " <<
typeid(T).name() <<
" which is not comparable");
124 struct compare<T, std::true_type> {
125 bool operator()(T
const& a, T
const& b)
const {
130 template <class T, class ok = typename is_printable<T>::type>
134 struct print<T, std::false_type> {
135 std::ostream& operator()(std::ostream& s, T
const&)
const {
137 "Trying to print type " <<
typeid(T).name() <<
" which is not printable");
145 struct print<T, std::true_type> {
146 std::ostream& operator()(std::ostream& a, T
const& b)
const {
154 class TEUCHOSCORE_LIB_DLL_EXPORT
any
163 template<
typename ValueType>
164 explicit any(
const ValueType & value)
165 : content(new holder<ValueType>(value))
170 : content(other.content ? other.content->clone() : 0)
182 std::swap(content, rhs.content);
187 template<
typename ValueType>
208 const std::type_info &
type()
const
210 return content ? content->type() :
typeid(void);
216 return content ? content->typeName() :
"NONE";
225 if( this->empty() && other.
empty() )
227 else if( this->empty() && !other.
empty() )
229 else if( !this->empty() && other.
empty() )
232 return content->same(*other.content);
241 if (content) content->print(os);
244 #ifndef DOXYGEN_SHOULD_SKIP_THIS
253 virtual ~placeholder() {}
255 virtual const std::type_info & type()
const = 0;
257 virtual std::string
typeName()
const = 0;
259 virtual placeholder * clone()
const = 0;
261 virtual bool same(
const placeholder &other )
const = 0;
263 virtual void print(std::ostream & os)
const = 0;
267 template<
typename ValueType>
268 class holder :
public placeholder
272 holder(
const ValueType & value)
276 const std::type_info & type()
const
277 {
return typeid(ValueType); }
282 placeholder * clone()
const
283 {
return new holder(held); }
285 bool same(
const placeholder &other )
const
287 if( type() != other.type() ) {
292 &other_held =
dynamic_cast<const holder<ValueType>&
>(other).held;
293 return ::Teuchos::compare<ValueType>{}(held, other_held);
296 void print(std::ostream & os)
const
297 { ::Teuchos::print<ValueType>{}(os, held); }
306 placeholder* access_content()
308 const placeholder* access_content()
const
317 placeholder * content;
327 bad_any_cast(
const std::string msg ) : std::runtime_error(msg) {}
338 template<
typename ValueType>
344 "any_cast<"<<ValueTypeName<<
">(operand): Error, cast to type "
345 <<
"any::holder<"<<ValueTypeName<<
"> failed since the actual underlying type is \'"
346 <<
typeName(*operand.access_content()) <<
"!"
350 ,
"any_cast<"<<ValueTypeName<<
">(operand): Error, cast to type "
351 <<
"any::holder<"<<ValueTypeName<<
"> failed because the content is NULL"
353 any::holder<ValueType>
354 *dyn_cast_content =
dynamic_cast<any::holder<ValueType>*
>(operand.access_content());
356 !dyn_cast_content, std::logic_error
357 ,
"any_cast<"<<ValueTypeName <<
">(operand): Error, cast to type "
358 <<
"any::holder<"<<ValueTypeName<<
"> failed but should not have and the actual underlying type is \'"
359 <<
typeName(*operand.access_content()) <<
"!"
360 <<
" The problem might be related to incompatible RTTI systems in static and shared libraries!"
362 return dyn_cast_content->held;
374 template<
typename ValueType>
377 return any_cast<ValueType>(const_cast<any&>(operand));
387 template<
typename ValueType>
390 return Teuchos::any_cast<ValueType>(operand);
400 std::ostringstream oss;
446 template <
typename T>
450 return any_cast<T>(rhs);
455 #endif // TEUCHOS_ANY_HPP