CppWAMP
C++11 client library for the WAMP protocol
|
wamp::Variant has built-in support for conversions to/from the following types:
std::vector
std::map
You may register your own custom types for conversion to/from Variant
. This is done by overloading the convert function:
where TConverter
may be either wamp::ToVariantConverter or wamp::FromVariantConverter, depending on whether the custom type is being converted to a variant, or from a variant. ToVariantConverter
and FromVariantConverter
have a uniform interface, which allows you to write the conversion function once for both directions. This uniform conversion scheme was inspired by Boost.Serialization.
In the above example operator()(key, value)
is used to extract/insert a member from/to an Object
variant. operator()
supports method chaining, which allows you to write very concise conversion functions.
The overloaded convert
function should be defined in the same namespace as the custom type, or order to enable argument-dependent lookup.
After registering your custom type, you may perform conversions using wamp::Variant::from and wamp::Variant::to:
You may also use your registered custom types as RPC or pub-sub arguments:
If the type you want to make convertible contains objects that CppWAMP don't understand, all you have to do is provide the convert
functions for the nested object types.
In some circumstances, different behavior may be required when converting from and converting to variants. The CPPWAMP_CONVERSION_SPLIT_FREE macro allows you to specify different conversion functions for both directions:
Instead of writing a convert
free function for your custom type, you may write convert
as a member function. This may be useful when the required data is not available from your type's public interface.
If you want the convert
member function to be private, then you must grant friendship to the wamp::ConversionAccess class:
Intrusive conversion can be split up by using the CPPWAMP_CONVERSION_SPLIT_MEMBER macro:
Your derived types can reuse the conversion facilities of their base type. To do this, simply cast *this
to a base class reference and pass it to the converter. Then convert the additional members of your derived type.
CppWAMP currently provides converters for the following standard library and Boost types:
Supported Type | Corresponding Variant type | Converters provided in |
---|---|---|
std::map | wamp::Object | <cppwamp/variant.hpp> |
std::set | wamp::Array | <cppwamp/types/set.hpp> |
std::optional<T> | wamp::Null, or type dependent on T | <cppwamp/types/optional.hpp> |
std::tuple | wamp::Array | <cppwamp/types/tuple.hpp> |
std::unordered_map | wamp::Object | <cppwamp/types/unorderedmap.hpp> |
std::unordered_set | wamp::Array | <cppwamp/types/unorderedset.hpp> |
std::vector | wamp::Array | <cppwamp/variant.hpp> |
boost::optional<T> | wamp::Null, or type dependent on T | <cppwamp/types/boostoptional.hpp> |