Boost C++ Libraries Home Libraries People FAQ More

PrevUpHomeNext

Converters

Converters are convenience functions for copying data between the dynamic variable and other containers.

The general pattern is OutputType dynamic::convert<OutputType>(InputType). The converter derives the InputType from the function parameter. The OutputType cannot be derived, so it must be specified explicitly as a template parameter.

InputType input = /* input data */;

auto output = dynamic::convert<OutputType>(input);

or...

InputType input = /* input data */;

OutputType output = dynamic::convert<decltype(output)>(input);

An error is raised when converting incompatible types. The error is either passed as an exception, or as an std::error_code if specified as output parameter.

InputType input = /* input data */;
std::error_code error;

auto output = dynamic::convert<OutputType>(input, error);
if (error)
  /* Handle errors */

The converters for each container are located in separate header files.

std::vector
[Note] Note

#include <trial/dynamic/std/vector.hpp>

// Convert std::vector into dynamic variable
std::vector<int> input = { 1, 2, 3, 4 };

auto output = dynamic::convert<dynamic::variable>(input);
assert(output.is<dynamic::array>());
assert(output.size == 4);

// Convert dynamic variable into std::vector
dynamic::variable input = { 1, 2, 3, 4 };

auto output = dynamic::convert<std::vector<int>>(input);
assert(output.size() == 4);

std::map
[Note] Note

#include <trial/dynamic/std/map.hpp>

// Convert std::map into dynamic variable
std::map<std::string, std::string> input = { { "alpha", "hydrogen" }, { "bravo", "helium" } };

auto output = dynamic::convert<dynamic::variable>(input);
assert(output.is<dynamic::map>());
assert(output.size == 2);

// Convert dynamic variable into std::map
dynamic::variable input = { { "alpha", "hydrogen" }, { "bravo", "helium" } };

auto output = dynamic::convert<std::map<std::string, std::string>>(input);
assert(output.size() == 2);

boost::any
[Note] Note

#include <trial/dynamic/boost/any.hpp>

This creates a compile-time dependency on Boost.Any.

// Convert boost::any into dynamic variable
boost::any input = 1;

auto output = dynamic::convert<dynamic::variable>(input);
assert(output.is<int>());


PrevUpHomeNext