44 #ifndef TEUCHOS_ANY_HPP 45 #define TEUCHOS_ANY_HPP 51 #include "Teuchos_Assert.hpp" 86 class TEUCHOSCORE_LIB_DLL_EXPORT
any 95 template<
typename ValueType>
96 explicit any(
const ValueType & value)
97 : content(new holder<ValueType>(value))
102 : content(other.content ? other.content->clone() : 0)
114 std::swap(content, rhs.content);
119 template<
typename ValueType>
140 const std::type_info &
type()
const 142 return content ? content->type() :
typeid(void);
148 return content ? content->typeName() :
"NONE";
154 if( this->empty() && other.
empty() )
156 else if( this->empty() && !other.
empty() )
158 else if( !this->empty() && other.
empty() )
161 return content->same(*other.content);
167 if (content) content->print(os);
170 #ifndef DOXYGEN_SHOULD_SKIP_THIS 179 virtual ~placeholder() {}
181 virtual const std::type_info & type()
const = 0;
183 virtual std::string
typeName()
const = 0;
185 virtual placeholder * clone()
const = 0;
187 virtual bool same(
const placeholder &other )
const = 0;
189 virtual void print(std::ostream & os)
const = 0;
193 template<
typename ValueType>
194 class holder :
public placeholder
198 holder(
const ValueType & value)
202 const std::type_info & type()
const 203 {
return typeid(ValueType); }
208 placeholder * clone()
const 209 {
return new holder(held); }
211 bool same(
const placeholder &other )
const 213 if( type() != other.type() ) {
218 &other_held =
dynamic_cast<const holder<ValueType>&
>(other).held;
219 return held == other_held;
222 void print(std::ostream & os)
const 232 placeholder* access_content()
234 const placeholder* access_content()
const 243 placeholder * content;
253 bad_any_cast(
const std::string msg ) : std::runtime_error(msg) {}
264 template<
typename ValueType>
270 "any_cast<"<<ValueTypeName<<
">(operand): Error, cast to type " 271 <<
"any::holder<"<<ValueTypeName<<
"> failed since the actual underlying type is \'" 272 <<
typeName(*operand.access_content()) <<
"!" 276 ,
"any_cast<"<<ValueTypeName<<
">(operand): Error, cast to type " 277 <<
"any::holder<"<<ValueTypeName<<
"> failed because the content is NULL" 279 any::holder<ValueType>
280 *dyn_cast_content =
dynamic_cast<any::holder<ValueType>*
>(operand.access_content());
282 !dyn_cast_content, std::logic_error
283 ,
"any_cast<"<<ValueTypeName <<
">(operand): Error, cast to type " 284 <<
"any::holder<"<<ValueTypeName<<
"> failed but should not have and the actual underlying type is \'" 285 <<
typeName(*operand.access_content()) <<
"!" 286 <<
" The problem might be related to incompatible RTTI systems in static and shared libraries!" 288 return dyn_cast_content->held;
300 template<
typename ValueType>
303 return any_cast<ValueType>(
const_cast<any&
>(operand));
311 std::ostringstream oss;
343 #endif // TEUCHOS_ANY_HPP 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.
void print(std::ostream &os) const
Print this value to the output stream os
bool same(const any &other) const
Return if two any objects are the same or not.
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.
bool empty() const
Return true if nothing is being stored.
any & operator=(const ValueType &rhs)
Copy the value rhs
const std::type_info & type() const
Return the type of value being stored.
The Teuchos namespace contains all of the classes, structs and enums used by Teuchos, as well as a number of utility routines.
std::string typeName() const
Return the name of the type.
bool operator==(const any &a, const any &b)
Returns true if two any objects have the same value.
bool operator!=(const any &a, const any &b)
Returns true if two any objects do not have the same value.
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
any(const ValueType &value)
Templated constructor.
static std::string name()
any(const any &other)
Copy constructor.
std::string typeName(const T &t)
Template function for returning the concrete type name of a passed-in object.
std::ostream & operator<<(std::ostream &os, const any &rhs)
Writes "any" input rhs to the output stream os.