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:

MyType* pTmp = any_cast<MyType*>(pAny)
Will return nullptr if the cast fails, i.e. types don’t match.

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:

const MyType* pTmp = any_cast<MyType*>(pAny)
Will return nullptr if the cast fails, i.e. types don’t match.

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

inline Any(Any &&other) noexcept

Move constructor.

Parameters:

other – The Any to move

inline Any &Swap(Any &rhs)

Swaps the content of the two Anys.

Parameters:

rhs – The Any to swap this Any with.

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 value val, 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 value val, 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.

inline Any &operator=(Any const &rhs)

Assignment operator for Any.

Parameters:

rhs – The Any which should be assigned to this Any.

inline Any &operator=(Any &&rhs) noexcept

Move assignment operator for Any.

Parameters:

rhs – The Any which should be moved into this Any.

Returns:

A reference to this Any.

inline bool Empty() const

returns true if the Any is empty

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.

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

std::ostream& operator<<(std::ostream& os, const CustomType&

ct)

function or specialize the any_value_to_string template function for meaningful output.

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
inline std::type_info const &Type() const

Returns the type information of the stored content.

If the Any is empty typeid(void) is returned. It is suggested to always query an Any for its type info before trying to extract data via an any_cast/ref_any_cast.

class BadAnyCastException : public std::bad_cast
#include <cppmicroservices/Any.h>

The BadAnyCastException class is thrown in case of casting an Any instance.

Public Functions

inline BadAnyCastException(std::string msg = "")
~BadAnyCastException() override = default
inline char const *what() const noexcept override