Any¶
- template <typename ValueType>
-
ValueType *
cppmicroservices::
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>
-
const ValueType *
cppmicroservices::
any_cast
(const Any *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
cppmicroservices::
any_cast
(const Any &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.
- Exceptions
BadAnyCastException
: if the cast fails.
- template <typename ValueType>
-
ValueType
cppmicroservices::
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.
- Exceptions
BadAnyCastException
: if the cast fails.
- template <typename ValueType>
-
const ValueType &
cppmicroservices::
ref_any_cast
(const Any &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);
- Exceptions
BadAnyCastException
: if the cast fails.
- template <typename ValueType>
-
ValueType &
cppmicroservices::
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);
- Exceptions
BadAnyCastException
: if the cast fails.
-
class
cppmicroservices::
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>
-
Any
(const ValueType &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
-
Any
(const Any &other)¶ Copy constructor, works with empty Anys and initialized Any values.
- Parameters
other
: The Any to copy
- template <typename ValueType>
-
bool
operator==
(const ValueType &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.- Return
true
if this Any contains valueval
,false
otherwise.- Parameters
val
: The value to compare to.
-
bool
operator==
(const Any &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.
- Return
- bool return true if rhs compares equal to *this AND the underlying ValueType has an operator==, and return false otherwise.
- Parameters
rhs
: an Any to compare against
- template <typename ValueType>
-
bool
operator!=
(const ValueType &val) const¶ Compares this Any with another value for inequality.
This is the same as
!this->operator==(val)
- Return
true
if this Any does not contain valueval
,false
otherwise.- Parameters
val
: The value to compare to.
- template <typename ValueType>
-
Any &
operator=
(const ValueType &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
std::ostream& operator<<(std::ostream& os, const CustomType& ct)
function or specialize the any_value_to_string template function for meaningful output.- Exceptions
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
std::ostream& operator<<(std::ostream& os, const CustomType& ct)
function or specialize the any_value_to_string template function for meaningful output.
-
std::string
ToJSON
(const uint8_t increment, const int32_t 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 formattingindent
: The current amount of indent to apply to the current line.
-
std::string
ToJSON
(bool prettyPrint = false) const¶
-
-
class
cppmicroservices::
BadAnyCastException
¶ - #include <cppmicroservices/Any.h>
The BadAnyCastException class is thrown in case of casting an Any instance.
Inherits from std::bad_cast