Any¶
-
template<typename ValueType>
ValueType *any_cast(Any *operand)¶ any_cast operator used to extract the ValueType from an Any*.
Will return a pointer to the stored value.
Example Usage:
Will return nullptr if the cast fails, i.e. types don’t match.MyType* pTmp = any_cast<MyType*>(pAny)
-
template<typename ValueType>
ValueType const *any_cast(Any const *operand)¶ any_cast operator used to extract a const ValueType pointer from an const Any*.
Will return a const pointer to the stored value.
Example Usage:
Will return nullptr if the cast fails, i.e. types don’t match.const MyType* pTmp = any_cast<MyType*>(pAny)
-
template<typename ValueType>
ValueType any_cast(Any const &operand)¶ any_cast operator used to extract a copy of the ValueType from an const Any&.
Example Usage:
MyType tmp = any_cast<MyType>(anAny)
Dont use an any_cast in combination with references, i.e. MyType& tmp = … or const MyType& = … Some compilers will accept this code although a copy is returned. Use the ref_any_cast in these cases.
- Throws:
BadAnyCastException – if the cast fails.
-
template<typename ValueType>
ValueType any_cast(Any &operand)¶ any_cast operator used to extract a copy of the ValueType from an Any&.
Example Usage:
MyType tmp = any_cast<MyType>(anAny)
Dont use an any_cast in combination with references, i.e. MyType& tmp = … or const MyType& tmp = … Some compilers will accept this code although a copy is returned. Use the ref_any_cast in these cases.
- Throws:
BadAnyCastException – if the cast fails.
-
template<typename ValueType>
ValueType const &ref_any_cast(Any const &operand)¶ ref_any_cast operator used to return a const reference to the internal data.
Example Usage:
const MyType& tmp = ref_any_cast<MyType>(anAny);
- Throws:
BadAnyCastException – if the cast fails.
-
template<typename ValueType>
ValueType &ref_any_cast(Any &operand)¶ ref_any_cast operator used to return a reference to the internal data.
Example Usage:
MyType& tmp = ref_any_cast<MyType>(anAny);
- Throws:
BadAnyCastException – if the cast fails.
-
class Any¶
- #include <cppmicroservices/Any.h>
An Any class represents a general type and is capable of storing any type, supporting type-safe extraction of the internally stored data.
Code taken from the Boost 1.46.1 library. Original copyright by Kevlin Henney. Modified for CppMicroServices.
Public Functions
-
Any()¶
Creates an empty any type.
-
template<typename ValueType>
inline Any(ValueType const &value)¶ Creates an Any which stores the init parameter inside.
Example:
Any a(13); Any a(string("12345"));
- Parameters:
value – The content of the Any
-
inline Any(Any const &other)¶
Copy constructor, works with empty Anys and initialized Any values.
- Parameters:
other – The Any to copy
-
template<typename ValueType>
inline bool operator==(ValueType const &val) const¶ Compares this Any with another value.
If the internal type of this any and of
val
do not match, the comparison always returns false.- Parameters:
val – The value to compare to.
- Returns:
true
if this Any contains valueval
,false
otherwise.
-
inline bool operator==(Any const &rhs) const¶
Compares this Any with another Any.
We accomplish this by forwarding the call to a virtual compare function on the Holder of the value in the _content field. The Placeholder subclass of Holder provides an implementation that invokes the above operator== with the underlying value of ValueType.
- Parameters:
rhs – an Any to compare against
- Returns:
bool return true if rhs compares equal to *this AND the underlying ValueType has an operator==, and return false otherwise.
-
template<typename ValueType>
inline bool operator!=(ValueType const &val) const¶ Compares this Any with another value for inequality.
This is the same as
!this->operator==(val)
- Parameters:
val – The value to compare to.
- Returns:
true
if this Any does not contain valueval
,false
otherwise.
-
template<typename ValueType>
inline Any &operator=(ValueType const &rhs)¶ Assignment operator for all types != Any.
Example:
Any a = 13; Any a = string("12345");
- Parameters:
rhs – The value which should be assigned to this Any.
-
std::string ToString() const¶
Returns a string representation for the content if it is not empty.
Custom types should either provide a
function or specialize the any_value_to_string template function for meaningful output.std::ostream& operator<<(std::ostream& os, const CustomType&
ct)
- Throws:
std::logic_error – if the Any is empty.
-
std::string ToStringNoExcept() const¶
Returns a string representation for the content.
If the Any is empty, an empty string is returned.
Custom types should either provide a
function or specialize the any_value_to_string template function for meaningful output.std::ostream& operator<<(std::ostream& os, const CustomType&
ct)
-
inline std::string ToJSON(uint8_t const increment, int32_t const indent) const¶
Returns a JSON representation for the content.
Custom types should specialize the any_value_to_json template function for meaningful output. The values of increment and indent are passed around to be able to be used for nicer formatting. The code that makes use of this is in the any_value_to_json specializations for the various containers.
To get pretty output, simply pass a value greater than zero in as the first argument of ToJSON and the rest of the code will take care of things.
- Parameters:
increment – The amount of extra indentation to add for each level of JSON. An increment of zero indicates no special formatting
indent – The current amount of indent to apply to the current line.
-
inline std::string ToJSON(bool prettyPrint = false) const¶
-
inline std::string ToCPP(uint8_t const increment, int32_t const indent) const¶
-
inline std::string ToCPP(bool prettyPrint = false) const¶
-
Any()¶
-
class BadAnyCastException : public std::bad_cast¶
- #include <cppmicroservices/Any.h>
The BadAnyCastException class is thrown in case of casting an Any instance.