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:

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

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:

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

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

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

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

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

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

Any(Any &&other)

Move constructor.

Parameters
  • other: The Any to move

Any &Swap(Any &rhs)

Swaps the content of the two Anys.

Parameters
  • rhs: The Any to swap this Any with.

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

Any &operator=(const Any &rhs)

Assignment operator for Any.

Parameters
  • rhs: The Any which should be assigned to this Any.

Any &operator=(Any &&rhs)

Move assignment operator for Any.

Return
A reference to this Any.
Parameters
  • rhs: The Any which should be moved into this Any.

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.

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 formatting
  • indent: The current amount of indent to apply to the current line.

std::string ToJSON(bool prettyPrint = false) const
const std::type_info &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 cppmicroservices::BadAnyCastException
#include <cppmicroservices/Any.h>

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

Inherits from std::bad_cast

Public Functions

BadAnyCastException(std::string msg = "")
~BadAnyCastException()
const char *what() const