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