22#if defined(SCAFACOS) or defined(SCAFACOS_DIPOLES)
33#include <boost/variant.hpp>
54 :
public boost::static_visitor<std::vector<std::string>> {
55 auto operator()(std::string
const &value)
const {
return result_type{value}; }
57 template <
typename T,
typename = std::enable_if_t<!std::is_arithmetic_v<T>>>
59 throw std::runtime_error(
"Cannot convert " + Utils::demangle<T>());
62 template <
typename T,
typename = std::enable_if_t<std::is_same_v<T,
int>>>
71 auto operator()(result_type
const &values)
const {
return values; }
73 auto operator()(std::vector<Variant>
const &values)
const {
74 result_type values_str;
75 for (
auto const &v : values) {
76 values_str.emplace_back(boost::apply_visitor(*
this, v).front());
81 template <
typename T,
typename = std::enable_if_t<std::is_arithmetic_v<T>>>
83 result_type values_str;
84 for (
auto const &v : values) {
85 values_str.emplace_back(to_str(v));
91 std::string to_str(
int const value)
const {
return std::to_string(value); }
93 std::string to_str(
double const value)
const {
94 std::ostringstream serializer;
95 serializer << std::scientific << std::setprecision(17);
97 return serializer.str();
102 :
public boost::static_visitor<std::unordered_map<std::string, Variant>> {
103 auto operator()(result_type
const &obj)
const {
return obj; }
105 template <
typename T>
106 auto operator()(std::unordered_map<T, Variant>
const &obj)
const {
108 return (obj.empty()) ? result_type{} : invalid(obj);
116 template <
typename T>
auto invalid(T
const &obj)
const {
117 return get_value<result_type>(obj);
123 if (parameters.empty()) {
124 throw std::invalid_argument(
125 "ScaFaCoS methods require at least 1 parameter");
128 std::string method_params =
"";
129 for (
auto const &kv : parameters) {
130 method_params +=
"," + kv.first;
131 for (
auto const &value : boost::apply_visitor(visitor, kv.second)) {
132 method_params +=
"," + value;
135 return method_params.substr(1);
140 auto deserializer = std::istringstream(s);
142 deserializer >> result;
143 if (deserializer.fail() or not deserializer.eof()) {
149std::unordered_map<std::string, Variant>
159 auto const numbers = std::string(
"-0123456789");
160 std::unordered_map<std::string, Variant> method_params{};
161 std::vector<std::string> flat_array;
162 std::istringstream buffer;
163 buffer.str(parameters);
164 for (std::string line; std::getline(buffer, line,
',');) {
165 flat_array.emplace_back(line);
167 for (
auto it = flat_array.begin(); it != flat_array.end();) {
168 auto const parameter_name = *it;
169 auto parameter_list = std::vector<Variant>{};
170 for (++it; it != flat_array.end(); ++it) {
171 if ((numbers.find(it->front()) == std::string::npos) and
172 not parameter_list.empty()) {
176 if (
auto converted = string_to_number<int>(*it)) {
178 }
else if (
auto converted = string_to_number<double>(*it)) {
181 parameter_list.emplace_back(result);
183 assert(not parameter_list.empty());
184 if (parameter_list.size() == 1ul) {
185 method_params[parameter_name] = parameter_list.front();
187 method_params[parameter_name] =
Variant{std::move(parameter_list)};
190 return method_params;
ScafacosContextBase provides the public interface of the ScaFaCoS bridge.
This file contains the defaults for ESPResSo.
std::unordered_map< std::string, Variant > deserialize_parameters(std::string const ¶meters)
Convert flattened parameters to a map.
std::string serialize_parameters(Variant const &pack)
Flatten a parameter map.
std::vector< std::string > available_methods()
Fetch list of methods compiled in ScaFaCoS.
std::optional< Variant > string_to_number(std::string const &s)
PackedVariant pack(const Variant &v)
Transform a Variant to a PackedVariant.
boost::make_recursive_variant< None, bool, int, std::size_t, double, std::string, ObjectRef, Utils::Vector3b, Utils::Vector3i, Utils::Vector2d, Utils::Vector3d, Utils::Vector4d, std::vector< int >, std::vector< double >, std::vector< boost::recursive_variant_ >, std::unordered_map< int, boost::recursive_variant_ >, std::unordered_map< std::string, boost::recursive_variant_ > >::type Variant
Possible types for parameters.
static std::vector< std::string > available_methods()
auto operator()(T const &value) const
auto operator()(std::vector< T > const &values) const
auto operator()(std::vector< Variant > const &values) const
auto operator()(result_type const &values) const
auto operator()(double const &value) const
result_type operator()(T const &) const
auto operator()(std::string const &value) const
auto operator()(result_type const &obj) const
auto operator()(std::unordered_map< T, Variant > const &obj) const
auto operator()(T const &obj) const