Commit ab7535c7 authored by Dominik Charousset's avatar Dominik Charousset

Implement new get_as function for config_value

parent dc04d789
This diff is collapsed.
......@@ -33,6 +33,13 @@ struct bounds_checker {
}
};
template <>
struct bounds_checker<int64_t, false> {
static constexpr bool check(int64_t) noexcept {
return true;
}
};
template <class To>
struct bounds_checker<To, true> {
static constexpr bool check(int64_t x) noexcept {
......
......@@ -222,8 +222,15 @@ template <class First, class Second, size_t N>
void parse_element(string_parser_state& ps, std::pair<First, Second>& kvp,
const char (&char_blacklist)[N]);
template <class T>
enable_if_tt<is_iterable<T>> parse(string_parser_state& ps, T& xs) {
struct require_opening_char_t {};
constexpr auto require_opening_char = require_opening_char_t{};
struct allow_omitting_opening_char_t {};
constexpr auto allow_omitting_opening_char = allow_omitting_opening_char_t{};
template <class T, class Policy = allow_omitting_opening_char_t>
enable_if_tt<is_iterable<T>>
parse(string_parser_state& ps, T& xs, Policy = {}) {
using value_type = deconst_kvp_t<typename T::value_type>;
static constexpr auto is_map_type = is_pair<value_type>::value;
static constexpr auto opening_char = is_map_type ? '{' : '[';
......@@ -252,6 +259,9 @@ enable_if_tt<is_iterable<T>> parse(string_parser_state& ps, T& xs) {
}
return;
}
if constexpr (std::is_same<Policy, require_opening_char_t>::value) {
ps.code = pec::unexpected_character;
} else {
// An empty string simply results in an empty list/map.
if (ps.at_end())
return;
......@@ -265,6 +275,7 @@ enable_if_tt<is_iterable<T>> parse(string_parser_state& ps, T& xs) {
*out++ = std::move(tmp);
} while (ps.consume(','));
ps.code = ps.at_end() ? pec::success : pec::trailing_character;
}
}
template <class T>
......@@ -306,4 +317,11 @@ auto parse(string_view str, T& x) {
return parse_result(ps, str);
}
template <class T, class Policy>
auto parse(string_view str, T& x, Policy policy) {
string_parser_state ps{str.begin(), str.end()};
parse(ps, x, policy);
return parse_result(ps, str);
}
} // namespace caf::detail
......@@ -18,6 +18,7 @@
#pragma once
#include "caf/none.hpp"
#include "caf/string_view.hpp"
#include <chrono>
......@@ -60,6 +61,13 @@ void print_escaped(Buffer& buf, string_view str) {
buf.push_back('"');
}
template <class Buffer>
void print(Buffer& buf, none_t) {
using namespace caf::literals;
auto str = "null"_sv;
buf.insert(buf.end(), str.begin(), str.end());
}
template <class Buffer>
void print(Buffer& buf, bool x) {
using namespace caf::literals;
......
......@@ -330,14 +330,6 @@ private:
map_type xs_;
};
// -- free functions -----------------------------------------------------------
// @relates dictionary
template <class T>
std::string to_string(const dictionary<T>& xs) {
return deep_to_string(xs.container());
}
// -- operators ----------------------------------------------------------------
// @relates dictionary
......@@ -376,10 +368,4 @@ bool operator>=(const dictionary<T>& xs, const dictionary<T>& ys) {
return xs.container() >= ys.container();
}
// @relates dictionary
template <class T>
std::ostream& operator<<(std::ostream& out, const dictionary<T>& xs) {
return out << to_string(xs);
}
} // namespace caf
......@@ -33,6 +33,9 @@ namespace caf {
/// @relates config_value
using settings = dictionary<config_value>;
/// @relates config_value
CAF_CORE_EXPORT std::string to_string(const settings& xs);
/// Tries to retrieve the value associated to `name` from `xs`.
/// @relates config_value
CAF_CORE_EXPORT const config_value*
......
This diff is collapsed.
......@@ -130,15 +130,13 @@ bool stringification_inspector::value(float x) {
bool stringification_inspector::value(double x) {
sep();
auto str = std::to_string(x);
result_ += str;
detail::print(result_, x);
return true;
}
bool stringification_inspector::value(long double x) {
sep();
auto str = std::to_string(x);
result_ += str;
detail::print(result_, x);
return true;
}
......
......@@ -22,6 +22,8 @@
namespace caf {
// note: to_string is implemented in config_value.cpp
const config_value* get_if(const settings* xs, string_view name) {
// Access the key directly unless the user specified a dot-separated path.
auto pos = name.find('.');
......
This diff is collapsed.
......@@ -108,8 +108,12 @@ struct i64_wrapper {
};
struct my_request {
int32_t a;
int32_t b;
int32_t a = 0;
int32_t b = 0;
my_request() = default;
my_request(int a, int b) : a(a), b(b) {
// nop
}
};
[[maybe_unused]] inline bool operator==(const my_request& x,
......
......@@ -145,7 +145,7 @@ const auto conf0_log = make_log(
"key: foo=bar",
"{",
"key: foo",
"value (string): \"bar\"",
"value (string): bar",
"}",
"key: 1group",
"{",
......@@ -162,7 +162,7 @@ const auto conf0_log = make_log(
"key: padding",
"value (integer): 10",
"key: file-name",
"value (string): \"foobar.ini\"",
"value (string): foobar.ini",
"}",
"key: scheduler",
"{",
......@@ -184,7 +184,7 @@ const auto conf0_log = make_log(
"value (integer): 23",
"value (integer): 2",
"value (integer): 4",
"value (string): \"abc\"",
"value (string): abc",
"]",
"key: some-map",
"{",
......@@ -193,7 +193,7 @@ const auto conf0_log = make_log(
"key: entry2",
"value (integer): 23",
"key: entry3",
"value (string): \"abc\"",
"value (string): abc",
"}",
"key: middleman",
"{",
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment