File: config_value.hpp

package info (click to toggle)
actor-framework 0.17.6-3.2
  • links: PTS
  • area: main
  • in suites: forky, sid
  • size: 9,008 kB
  • sloc: cpp: 77,684; sh: 674; python: 309; makefile: 13
file content (860 lines) | stat: -rw-r--r-- 27,226 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
/******************************************************************************
 *                       ____    _    _____                                   *
 *                      / ___|  / \  |  ___|    C++                           *
 *                     | |     / _ \ | |_       Actor                         *
 *                     | |___ / ___ \|  _|      Framework                     *
 *                      \____/_/   \_|_|                                      *
 *                                                                            *
 * Copyright 2011-2018 Dominik Charousset                                     *
 *                                                                            *
 * Distributed under the terms and conditions of the BSD 3-Clause License or  *
 * (at your option) under the terms and conditions of the Boost Software      *
 * License 1.0. See accompanying files LICENSE and LICENSE_ALTERNATIVE.       *
 *                                                                            *
 * If you did not receive a copy of the license files, see                    *
 * http://opensource.org/licenses/BSD-3-Clause and                            *
 * http://www.boost.org/LICENSE_1_0.txt.                                      *
 ******************************************************************************/

#pragma once

#include <chrono>
#include <cstdint>
#include <iosfwd>
#include <iterator>
#include <map>
#include <string>
#include <tuple>
#include <type_traits>
#include <vector>

#include "caf/atom.hpp"
#include "caf/detail/bounds_checker.hpp"
#include "caf/detail/move_if_not_ptr.hpp"
#include "caf/detail/parse.hpp"
#include "caf/detail/type_traits.hpp"
#include "caf/dictionary.hpp"
#include "caf/fwd.hpp"
#include "caf/optional.hpp"
#include "caf/raise_error.hpp"
#include "caf/string_algorithms.hpp"
#include "caf/string_view.hpp"
#include "caf/sum_type.hpp"
#include "caf/sum_type_access.hpp"
#include "caf/sum_type_token.hpp"
#include "caf/timestamp.hpp"
#include "caf/uri.hpp"
#include "caf/variant.hpp"

namespace caf {

/// A type for config parameters with similar interface to a `variant`. This
/// type is not implemented as a simple variant alias because variants cannot
/// contain lists of themselves.
class config_value {
public:
  // -- member types -----------------------------------------------------------

  using integer = int64_t;

  using boolean = bool;

  using real = double;

  using atom = atom_value;

  using timespan = caf::timespan;

  using string = std::string;

  using list = std::vector<config_value>;

  using dictionary = caf::dictionary<config_value>;

  using types = detail::type_list<integer, boolean, real, atom, timespan, uri,
                                  string, list, dictionary>;

  using variant_type = detail::tl_apply_t<types, variant>;

  // -- constructors, destructors, and assignment operators --------------------

  config_value() = default;

  config_value(config_value&& other) = default;

  config_value(const config_value& other) = default;

  template <class T, class E = detail::enable_if_t<
                       !std::is_same<detail::decay_t<T>, config_value>::value>>
  explicit config_value(T&& x) {
    set(std::forward<T>(x));
  }

  config_value& operator=(config_value&& other) = default;

  config_value& operator=(const config_value& other) = default;

  template <class T, class E = detail::enable_if_t<
                       !std::is_same<detail::decay_t<T>, config_value>::value>>
  config_value& operator=(T&& x) {
    set(std::forward<T>(x));
    return *this;
  }

  ~config_value();

  // -- parsing ----------------------------------------------------------------

  /// Tries to parse a value from `str`.
  static expected<config_value> parse(string_view::iterator first,
                                      string_view::iterator last);

  /// Tries to parse a value from `str`.
  static expected<config_value> parse(string_view str);

  // -- properties -------------------------------------------------------------

  /// Converts the value to a list with one element. Does nothing if the value
  /// already is a list.
  void convert_to_list();

  /// Returns the value as a list, converting it to one if needed.
  list& as_list();

  /// Returns the value as a dictionary, converting it to one if needed.
  dictionary& as_dictionary();

  /// Appends `x` to a list. Converts this config value to a list first by
  /// calling `convert_to_list` if needed.
  void append(config_value x);

  /// Returns a human-readable type name of the current value.
  const char* type_name() const noexcept;

  /// Returns the underlying variant.
  variant_type& get_data() {
    return data_;
  }

  /// Returns the underlying variant.
  const variant_type& get_data() const {
    return data_;
  }

  /// Returns a pointer to the underlying variant.
  variant_type* get_data_ptr() {
    return &data_;
  }

  /// Returns a pointer to the underlying variant.
  const variant_type* get_data_ptr() const {
    return &data_;
  }

private:
  // -- properties -------------------------------------------------------------

  static const char* type_name_at_index(size_t index) noexcept;

  // -- auto conversion of related types ---------------------------------------

  void set(bool x) {
    data_ = x;
  }

  void set(float x) {
    data_ = static_cast<double>(x);
  }

  void set(const char* x) {
    data_ = std::string{x};
  }

  void set(string_view x) {
    data_ = std::string{x.begin(), x.end()};
  }

  template <class T>
  detail::enable_if_t<detail::is_one_of<T, real, atom, timespan, uri, string,
                                        list, dictionary>::value>
  set(T x) {
    data_ = std::move(x);
  }

  template <class T>
  void set_range(T& xs, std::true_type) {
    auto& dict = as_dictionary();
    dict.clear();
    for (auto& kvp : xs)
      dict.emplace(kvp.first, std::move(kvp.second));
  }

  template <class T>
  void set_range(T& xs, std::false_type) {
    auto& ls = as_list();
    ls.clear();
    ls.insert(ls.end(), std::make_move_iterator(xs.begin()),
              std::make_move_iterator(xs.end()));
  }

  template <class T>
  detail::enable_if_t<detail::is_iterable<T>::value
                      && !detail::is_one_of<T, string, list, dictionary>::value>
  set(T xs) {
    using value_type = typename T::value_type;
    detail::bool_token<detail::is_pair<value_type>::value> is_map_type;
    set_range(xs, is_map_type);
  }

  template <class T>
  detail::enable_if_t<std::is_integral<T>::value> set(T x) {
    data_ = static_cast<int64_t>(x);
  }

  // -- member variables -------------------------------------------------------

  variant_type data_;
};

// -- convenience constants ----------------------------------------------------

/// Type of the `top_level_cli_parsing` constant.
using top_level_cli_parsing_t = std::false_type;

/// Signals parsing of top-level config values when used as third argument to
/// `parse_cli`.
constexpr auto top_level_cli_parsing = top_level_cli_parsing_t{};

/// Type of the `top_level_cli_parsing` constant.
using nested_cli_parsing_t = std::true_type;

/// Signals parsing of nested config values when used as third argument to
/// `parse_cli`.
constexpr auto nested_cli_parsing = nested_cli_parsing_t{};

// -- SumType-like access ------------------------------------------------------

template <class T>
struct default_config_value_access {
  static bool is(const config_value& x) {
    return holds_alternative<T>(x.get_data());
  }

  static const T* get_if(const config_value* x) {
    return caf::get_if<T>(&(x->get_data()));
  }

  static T get(const config_value& x) {
    return caf::get<T>(x.get_data());
  }

  static T convert(T x) {
    return x;
  }

  template <class Nested>
  static void parse_cli(string_parser_state& ps, T& x, Nested) {
    detail::parse(ps, x);
  }
};

struct config_value_access_unspecialized {};

/// @relates config_value
template <class T>
struct config_value_access : config_value_access_unspecialized {};

#define CAF_DEFAULT_CONFIG_VALUE_ACCESS(type, name)                            \
  template <>                                                                  \
  struct config_value_access<type> : default_config_value_access<type> {       \
    static std::string type_name() {                                           \
      return name;                                                             \
    }                                                                          \
  }

CAF_DEFAULT_CONFIG_VALUE_ACCESS(bool, "boolean");
CAF_DEFAULT_CONFIG_VALUE_ACCESS(double, "real64");
CAF_DEFAULT_CONFIG_VALUE_ACCESS(atom_value, "atom");
CAF_DEFAULT_CONFIG_VALUE_ACCESS(timespan, "timespan");
CAF_DEFAULT_CONFIG_VALUE_ACCESS(uri, "uri");
CAF_DEFAULT_CONFIG_VALUE_ACCESS(config_value::list, "list");
CAF_DEFAULT_CONFIG_VALUE_ACCESS(config_value::dictionary, "dictionary");

#undef CAF_DEFAULT_CONFIG_VALUE_ACCESS

template <>
struct config_value_access<std::string> {
  using super = default_config_value_access<std::string>;

  static std::string type_name() {
    return "string";
  }

  static bool is(const config_value& x) {
    return holds_alternative<std::string>(x.get_data());
  }

  static const std::string* get_if(const config_value* x) {
    return caf::get_if<std::string>(&(x->get_data()));
  }

  static std::string get(const config_value& x) {
    return caf::get<std::string>(x.get_data());
  }

  static std::string convert(std::string x) {
    return x;
  }

  template <bool IsNested>
  static void parse_cli(string_parser_state& ps, std::string& x,
                        std::integral_constant<bool, IsNested>) {
    if (IsNested)
      detail::parse_element(ps, x, ",={}[]");
    else
      detail::parse(ps, x);
  }
};

enum class select_config_value_hint {
  is_integral,
  is_map,
  is_list,
  is_custom,
  is_missing,
};

template <class T>
constexpr select_config_value_hint select_config_value_oracle() {
  return std::is_integral<T>::value && !std::is_same<T, bool>::value
           ? select_config_value_hint::is_integral
           : (detail::is_map_like<T>::value
                ? select_config_value_hint::is_map
                : (detail::is_list_like<T>::value
                     ? select_config_value_hint::is_list
                     : (!std::is_base_of<config_value_access_unspecialized,
                                         config_value_access<T>>::value
                          ? select_config_value_hint::is_custom
                          : select_config_value_hint::is_missing)));
}

/// Delegates to config_value_access for all specialized versions.
template <class T,
          select_config_value_hint Hint = select_config_value_oracle<T>()>
struct select_config_value_access {
  static_assert(Hint == select_config_value_hint::is_custom,
                "no default or specialization for config_value_access found");
  using type = config_value_access<T>;
};

template <class T>
using select_config_value_access_t = typename select_config_value_access<
  T>::type;

template <>
struct sum_type_access<config_value> {
  using types = typename config_value::types;

  using type0 = typename detail::tl_head<types>::type;

  static constexpr bool specialized = true;

  template <class U, int Pos>
  static bool is(const config_value& x, sum_type_token<U, Pos> token) {
    return x.get_data().is(pos(token));
  }

  template <class U>
  static bool is(const config_value& x, sum_type_token<U, -1>) {
    return select_config_value_access_t<U>::is(x);
  }

  template <class U, int Pos>
  static U& get(config_value& x, sum_type_token<U, Pos> token) {
    return x.get_data().get(pos(token));
  }

  template <class U>
  static U get(config_value& x, sum_type_token<U, -1>) {
    return select_config_value_access_t<U>::get(x);
  }

  template <class U, int Pos>
  static const U& get(const config_value& x, sum_type_token<U, Pos> token) {
    return x.get_data().get(pos(token));
  }

  template <class U>
  static U get(const config_value& x, sum_type_token<U, -1>) {
    return select_config_value_access_t<U>::get(x);
  }

  template <class U, int Pos>
  static U* get_if(config_value* x, sum_type_token<U, Pos> token) {
    return is(*x, token) ? &get(*x, token) : nullptr;
  }

  template <class U>
  static optional<U> get_if(config_value* x, sum_type_token<U, -1>) {
    return select_config_value_access_t<U>::get_if(x);
  }

  template <class U, int Pos>
  static const U* get_if(const config_value* x, sum_type_token<U, Pos> token) {
    return is(*x, token) ? &get(*x, token) : nullptr;
  }

  template <class U>
  static optional<U> get_if(const config_value* x, sum_type_token<U, -1>) {
    return select_config_value_access_t<U>::get_if(x);
  }

  template <class Result, class Visitor, class... Ts>
  static Result apply(config_value& x, Visitor&& visitor, Ts&&... xs) {
    return x.get_data().template apply<Result>(std::forward<Visitor>(visitor),
                                               std::forward<Ts>(xs)...);
  }

  template <class Result, class Visitor, class... Ts>
  static Result apply(const config_value& x, Visitor&& visitor, Ts&&... xs) {
    return x.get_data().template apply<Result>(std::forward<Visitor>(visitor),
                                               std::forward<Ts>(xs)...);
  }
};

/// Catches all non-specialized integer types.
template <class T>
struct select_config_value_access<T, select_config_value_hint::is_integral> {
  struct type {
    using integer_type = config_value::integer;

    static std::string type_name() {
      std::string result;
      if (std::is_signed<T>::value)
        result = "int";
      else
        result = "uint";
      result += std::to_string(sizeof(T) * 8);
      return result;
    }

    static bool is(const config_value& x) {
      auto ptr = caf::get_if<integer_type>(x.get_data_ptr());
      return ptr != nullptr && detail::bounds_checker<T>::check(*ptr);
    }

    static optional<T> get_if(const config_value* x) {
      auto ptr = caf::get_if<integer_type>(x->get_data_ptr());
      if (ptr != nullptr && detail::bounds_checker<T>::check(*ptr))
        return static_cast<T>(*ptr);
      return none;
    }

    static T get(const config_value& x) {
      auto res = get_if(&x);
      CAF_ASSERT(res != none);
      return *res;
    }

    static T convert(T x) {
      return x;
    }

    template <class Nested>
    static void parse_cli(string_parser_state& ps, T& x, Nested) {
      detail::parse(ps, x);
    }
  };
};

/// Catches all non-specialized list types.
template <class T>
struct select_config_value_access<T, select_config_value_hint::is_list> {
  struct type {
    using list_type = T;

    using value_type = typename list_type::value_type;

    using value_trait = select_config_value_access_t<value_type>;

    static std::string type_name() {
      return "list of " + value_trait::type_name();
    }

    static bool is(const config_value& x) {
      auto lst = caf::get_if<config_value::list>(&x);
      return lst != nullptr
             && std::all_of(lst->begin(), lst->end(),
                            [](const config_value& y) {
                              return caf::holds_alternative<value_type>(y);
                            });
      return false;
    }

    static optional<list_type> get_if(const config_value* x) {
      list_type result;
      auto out = std::inserter(result, result.end());
      auto extract = [&](const config_value& y) {
        if (auto opt = caf::get_if<value_type>(&y)) {
          *out++ = move_if_optional(opt);
          return true;
        }
        return false;
      };
      auto lst = caf::get_if<config_value::list>(x);
      if (lst != nullptr && std::all_of(lst->begin(), lst->end(), extract))
        return result;
      return none;
    }

    static list_type get(const config_value& x) {
      auto result = get_if(&x);
      if (!result)
        CAF_RAISE_ERROR("invalid type found");
      return std::move(*result);
    }

    static config_value::list convert(const list_type& xs) {
      config_value::list result;
      for (const auto& x : xs)
        result.emplace_back(value_trait::convert(x));
      return result;
    }

    static void parse_cli(string_parser_state& ps, T& xs,
                          top_level_cli_parsing_t) {
      bool has_open_token;
      auto subtype = select_config_value_oracle<value_type>();
      if (subtype == select_config_value_hint::is_list) {
        // The outer square brackets are optional in nested lists. This means we
        // need to check for "[[" at the beginning and otherwise we assume the
        // leading '[' was omitted.
        string_parser_state tmp{ps.i, ps.e};
        has_open_token = tmp.consume('[') && tmp.consume('[');
        if (has_open_token)
          ps.consume('[');
      } else {
        has_open_token = ps.consume('[');
      }
      do {
        ps.skip_whitespaces();
        if (has_open_token) {
          if (ps.consume(']')) {
            ps.skip_whitespaces();
            ps.code = ps.at_end() ? pec::success : pec::trailing_character;
            return;
          }
        } else if (ps.at_end()) {
          // Allow trailing commas and empty strings.
          ps.code = pec::success;
          return;
        }
        value_type tmp;
        value_trait::parse_cli(ps, tmp, nested_cli_parsing);
        if (ps.code > pec::trailing_character)
          return;
        xs.insert(xs.end(), std::move(tmp));
      } while (ps.consume(','));
      if (has_open_token && !ps.consume(']')) {
        ps.code = ps.at_end() ? pec::unexpected_eof : pec::unexpected_character;
        return;
      }
      ps.skip_whitespaces();
      ps.code = ps.at_end() ? pec::success : pec::trailing_character;
    }

    static void parse_cli(string_parser_state& ps, T& xs,
                          nested_cli_parsing_t) {
      if (!ps.consume('[')) {
        ps.code = ps.at_end() ? pec::unexpected_eof : pec::unexpected_character;
        return;
      }
      do {
        if (ps.consume(']')) {
          ps.skip_whitespaces();
          ps.code = ps.at_end() ? pec::success : pec::trailing_character;
          return;
        }
        value_type tmp;
        value_trait::parse_cli(ps, tmp, nested_cli_parsing);
        if (ps.code > pec::trailing_character)
          return;
        xs.insert(xs.end(), std::move(tmp));
      } while (ps.consume(','));
      if (!ps.consume(']')) {
        ps.code = ps.at_end() ? pec::unexpected_eof : pec::unexpected_character;
        return;
      }
      ps.skip_whitespaces();
      ps.code = ps.at_end() ? pec::success : pec::trailing_character;
    }
  };
};

/// Catches all non-specialized map types.
template <class T>
struct select_config_value_access<T, select_config_value_hint::is_map> {
  struct type {
    using map_type = T;

    using mapped_type = typename map_type::mapped_type;

    using mapped_trait = select_config_value_access_t<mapped_type>;

    static std::string type_name() {
      std::string result = "dictionary of ";
      auto nested_name = mapped_trait::type_name();
      result.insert(result.end(), nested_name.begin(), nested_name.end());
      return result;
    }

    static bool is(const config_value& x) {
      using value_type = config_value::dictionary::value_type;
      auto is_mapped_type = [](const value_type& y) {
        return caf::holds_alternative<mapped_type>(y.second);
      };
      if (auto dict = caf::get_if<config_value::dictionary>(&x))
        return std::all_of(dict->begin(), dict->end(), is_mapped_type);
      return false;
    }

    static optional<map_type> get_if(const config_value* x) {
      using value_type = config_value::dictionary::value_type;
      map_type result;
      auto extract = [&](const value_type& y) {
        if (auto opt = caf::get_if<mapped_type>(&y.second)) {
          result.emplace(y.first, move_if_optional(opt));
          return true;
        }
        return false;
      };
      if (auto dict = caf::get_if<config_value::dictionary>(x))
        if (std::all_of(dict->begin(), dict->end(), extract))
          return result;
      return none;
    }

    static map_type get(const config_value& x) {
      auto result = get_if(&x);
      if (!result)
        CAF_RAISE_ERROR("invalid type found");
      return std::move(*result);
    }

    template <class Nested>
    static void parse_cli(string_parser_state& ps, map_type& xs, Nested) {
      detail::parse(ps, xs);
    }

    static config_value::dictionary convert(const map_type& xs) {
      config_value::dictionary result;
      for (const auto& x : xs)
        result.emplace(x.first, mapped_trait::convert(x.second));
      return result;
    }
  };
};

template <>
struct config_value_access<float> {
  static std::string type_name() {
    return "real32";
  }

  static bool is(const config_value& x) {
    return holds_alternative<double>(x.get_data());
  }

  static optional<float> get_if(const config_value* x) {
    if (auto res = caf::get_if<double>(&(x->get_data())))
      return static_cast<float>(*res);
    return none;
  }

  static float get(const config_value& x) {
    return static_cast<float>(caf::get<double>(x.get_data()));
  }

  static double convert(float x) {
    return x;
  }

  template <class Nested>
  static void parse_cli(string_parser_state& ps, float& x, Nested) {
    detail::parse(ps, x);
  }
};

/// Implements automagic unboxing of `std::tuple<Ts...>` from a heterogeneous
///`config_value::list`.
/// @relates config_value
template <class... Ts>
struct config_value_access<std::tuple<Ts...>> {
  using tuple_type = std::tuple<Ts...>;

  static std::string type_name() {
    auto result = "tuple[";
    rec_name(result, true, detail::int_token<0>(), detail::type_list<Ts...>());
    result += ']';
    return result;
  }

  static bool is(const config_value& x) {
    if (auto lst = caf::get_if<config_value::list>(&x)) {
      if (lst->size() != sizeof...(Ts))
        return false;
      return rec_is(*lst, detail::int_token<0>(), detail::type_list<Ts...>());
    }
    return false;
  }

  static optional<tuple_type> get_if(const config_value* x) {
    if (auto lst = caf::get_if<config_value::list>(x)) {
      if (lst->size() != sizeof...(Ts))
        return none;
      tuple_type result;
      if (rec_get(*lst, result, detail::int_token<0>(),
                  detail::type_list<Ts...>()))
        return result;
    }
    return none;
  }

  static tuple_type get(const config_value& x) {
    if (auto result = get_if(&x))
      return std::move(*result);
    CAF_RAISE_ERROR("invalid type found");
  }

  static config_value::list convert(const tuple_type& xs) {
    config_value::list result;
    rec_convert(result, xs, detail::int_token<0>(), detail::type_list<Ts...>());
    return result;
  }

  template <class Nested>
  static void parse_cli(string_parser_state& ps, tuple_type& xs, Nested) {
    rec_parse(ps, xs, detail::int_token<0>(), detail::type_list<Ts...>());
  }

private:
  template <int Pos>
  static void rec_name(std::string&, bool, detail::int_token<Pos>,
                       detail::type_list<>) {
    // nop
  }

  template <int Pos, class U, class... Us>
  static void rec_name(std::string& result, bool is_first,
                       detail::int_token<Pos>, detail::type_list<U, Us...>) {
    if (!is_first)
      result += ", ";
    using nested = config_value_access<U>;
    auto nested_name = nested::type_name();
    result.insert(result.end(), nested_name.begin(), nested_name.end());
    return rec_name(result, false, detail::int_token<Pos + 1>(),
                    detail::type_list<Us...>());
  }

  template <int Pos>
  static bool rec_is(const config_value::list&, detail::int_token<Pos>,
                     detail::type_list<>) {
    return true;
  }

  template <int Pos, class U, class... Us>
  static bool rec_is(const config_value::list& xs, detail::int_token<Pos>,
                     detail::type_list<U, Us...>) {
    if (!holds_alternative<U>(xs[Pos]))
      return false;
    return rec_is(xs, detail::int_token<Pos + 1>(), detail::type_list<Us...>());
  }

  template <int Pos>
  static bool rec_get(const config_value::list&, tuple_type&,
                      detail::int_token<Pos>, detail::type_list<>) {
    return true;
  }

  template <int Pos, class U, class... Us>
  static bool rec_get(const config_value::list& xs, tuple_type& result,
                      detail::int_token<Pos>, detail::type_list<U, Us...>) {
    if (auto value = caf::get_if<U>(&xs[Pos])) {
      std::get<Pos>(result) = detail::move_if_not_ptr(value);
      return rec_get(xs, result, detail::int_token<Pos + 1>(),
                     detail::type_list<Us...>());
    }
    return false;
  }

  template <int Pos>
  static void rec_convert(config_value::list&, const tuple_type&,
                          detail::int_token<Pos>, detail::type_list<>) {
    // nop
  }

  template <int Pos, class U, class... Us>
  static void rec_convert(config_value::list& result, const tuple_type& xs,
                          detail::int_token<Pos>, detail::type_list<U, Us...>) {
    using trait = select_config_value_access_t<U>;
    result.emplace_back(trait::convert(std::get<Pos>(xs)));
    return rec_convert(result, xs, detail::int_token<Pos + 1>(),
                       detail::type_list<Us...>());
  }

  template <int Pos>
  static void rec_parse(string_parser_state&, tuple_type&,
                        detail::int_token<Pos>, detail::type_list<>) {
    // nop
  }

  template <int Pos, class U, class... Us>
  static void rec_parse(string_parser_state& ps, tuple_type& xs,
                        detail::int_token<Pos>, detail::type_list<U, Us...>) {
    using trait = select_config_value_access_t<U>;
    trait::parse_cli(std::get<Pos>(xs), nested_cli_parsing);
    if (ps.code > pec::trailing_character)
      return;
    if (sizeof...(Us) > 0 && !ps.consume(','))
      ps.code = ps.at_end() ? pec::unexpected_eof : pec::unexpected_character;
    rec_parse(ps, xs, detail::int_token<Pos + 1>(), detail::type_list<Us...>());
  }
};

// -- SumType-like access of dictionary values ---------------------------------

/// @relates config_value
bool operator<(const config_value& x, const config_value& y);

/// @relates config_value
bool operator==(const config_value& x, const config_value& y);

/// @relates config_value
inline bool operator>=(const config_value& x, const config_value& y) {
  return !(x < y);
}

/// @relates config_value
inline bool operator!=(const config_value& x, const config_value& y) {
  return !(x == y);
}

/// @relates config_value
std::string to_string(const config_value& x);

/// @relates config_value
std::ostream& operator<<(std::ostream& out, const config_value& x);

template <class... Ts>
config_value make_config_value_list(Ts&&... xs) {
  std::vector<config_value> lst{config_value{std::forward<Ts>(xs)}...};
  return config_value{std::move(lst)};
}

/// @relates config_value
template <class Inspector>
typename Inspector::result_type inspect(Inspector& f, config_value& x) {
  return f(meta::type_name("config_value"), x.get_data());
}

} // namespace caf