44 #ifndef TEUCHOS_ANY_HPP
45 #define TEUCHOS_ANY_HPP
52 #include <type_traits>
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>
116 "Trying to compare type " <<
typeid(T).name() <<
" which is not comparable");
130 template <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(
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
306 placeholder* access_content()
308 const placeholder* access_content()
const
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
bool operator()(T const &a, T const &b) const
void print(std::ostream &os) const
Print this value to the output stream os
std::string typeName(const T &t)
Template function for returning the concrete type name of a passed-in object.
T & make_any_ref(any &rhs)
Default constructs a new T value and returns a reference to it.
bool same(const any &other) const
Return if two any objects are the same or not.
ValueType & any_cast(any &operand)
Used to extract the templated value held in Teuchos::any to a given value type.
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
Macro for throwing an exception with breakpointing to ease debugging.
bool operator()(T const &, T const &) const
decltype(test< T >(0)) type
const ValueType & any_cast(const any &operand)
Used to extract the const templated value held in Teuchos::any to a given const value type...
Modified boost::any class, which is a container for a templated value.
any & swap(any &rhs)
Method for swapping the contents of two any classes.
std::string toString(const any &rhs)
Converts the value in any to a std::string.
Thrown if any_cast is attempted between two incompatable types.
static auto test(int) -> decltype(std::declval< std::ostream & >()<< std::declval< X >(), void(), std::true_type())
#define TEUCHOSCORE_LIB_DLL_EXPORT
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 & operator=(const ValueType &rhs)
Copy the value rhs
bad_any_cast(const std::string msg)
bool operator==(const any &a, const any &b)
Returns true if two any objects have the same value.
std::string typeName() const
Return the name of the type.
std::ostream & operator()(std::ostream &s, T const &) const
const std::type_info & type() const
Return the type of value being stored.
bool operator!=(const any &a, const any &b)
Returns true if two any objects do not have the same value.
void swap(Teuchos::any &a, Teuchos::any &b)
Special swap for other code to find via Argument Dependent Lookup.
std::ostream & operator()(std::ostream &a, T const &b) const
Defines basic traits returning the name of a type in a portable and readable way. ...
any & operator=(const any &rhs)
Copy the value held in rhs
static auto test(int) -> decltype(std::declval< X >()==std::declval< X >(), void(), std::true_type())
any(const ValueType &value)
Templated constructor.
decltype(test< T >(0)) type
static std::string name()
any(const any &other)
Copy constructor.
std::ostream & operator<<(std::ostream &os, const any &rhs)
Writes "any" input rhs to the output stream os.
bool empty() const
Return true if nothing is being stored.