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
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
|
//# This file is a part of toml++ and is subject to the the terms of the MIT license.
//# Copyright (c) Mark Gillard <mark.gillard@outlook.com.au>
//# See https://github.com/marzer/tomlplusplus/blob/master/LICENSE for the full license text.
// SPDX-License-Identifier: MIT
#pragma once
#include "std_utility.hpp"
#include "forward_declarations.hpp"
#include "source_region.hpp"
#include "header_start.hpp"
TOML_NAMESPACE_START
{
/// \brief A TOML node.
///
/// \detail A parsed TOML document forms a tree made up of tables, arrays and values.
/// This type is the base of each of those, providing a lot of the polymorphic plumbing.
class TOML_ABSTRACT_INTERFACE TOML_EXPORTED_CLASS node
{
private:
/// \cond
friend class TOML_PARSER_TYPENAME;
source_region source_{};
template <typename T>
TOML_NODISCARD
decltype(auto) get_value_exact() const noexcept(impl::value_retrieval_is_nothrow<T>);
template <typename T, typename N>
using ref_type_ = std::conditional_t< //
std::is_reference_v<T>, //
impl::copy_ref<impl::copy_cv<impl::unwrap_node<T>, std::remove_reference_t<N>>, T>, //
impl::copy_cvref<impl::unwrap_node<T>, N> //
>;
template <typename T, typename N>
using ref_type = std::conditional_t< //
std::is_reference_v<N>, //
ref_type_<T, N>, //
ref_type_<T, std::add_lvalue_reference_t<N>> //
>;
template <typename T, typename N>
TOML_PURE_GETTER
static ref_type<T, N&&> do_ref(N&& n) noexcept
{
using unwrapped_type = impl::unwrap_node<T>;
static_assert(toml::is_value<unwrapped_type> || toml::is_container<unwrapped_type>,
"The template type argument of node::ref() must be one of:" TOML_SA_UNWRAPPED_NODE_TYPE_LIST);
TOML_ASSERT_ASSUME(
n.template is<unwrapped_type>()
&& "template type argument provided to toml::node::ref() didn't match the node's actual type");
using node_ref = std::remove_volatile_t<std::remove_reference_t<N>>&;
using val_type = std::remove_volatile_t<unwrapped_type>;
using out_ref = ref_type<T, N&&>;
static_assert(std::is_reference_v<out_ref>);
if constexpr (toml::is_value<unwrapped_type>)
return static_cast<out_ref>(const_cast<node_ref>(n).template ref_cast<val_type>().get());
else
return static_cast<out_ref>(const_cast<node_ref>(n).template ref_cast<val_type>());
}
protected:
TOML_EXPORTED_MEMBER_FUNCTION
node() noexcept;
TOML_EXPORTED_MEMBER_FUNCTION
node(const node&) noexcept;
TOML_EXPORTED_MEMBER_FUNCTION
node(node&&) noexcept;
TOML_EXPORTED_MEMBER_FUNCTION
node& operator=(const node&) noexcept;
TOML_EXPORTED_MEMBER_FUNCTION
node& operator=(node&&) noexcept;
template <typename T, typename N>
using ref_cast_type_ = std::conditional_t< //
std::is_reference_v<T>, //
impl::copy_ref<impl::copy_cv<impl::wrap_node<T>, std::remove_reference_t<N>>, T>, //
impl::copy_cvref<impl::wrap_node<T>, N> //
>;
template <typename T, typename N>
using ref_cast_type = std::conditional_t< //
std::is_reference_v<N>, //
ref_cast_type_<T, N>, //
ref_cast_type_<T, std::add_lvalue_reference_t<N>> //
>;
template <typename T>
TOML_PURE_INLINE_GETTER
ref_cast_type<T, node&> ref_cast() & noexcept
{
using out_ref = ref_cast_type<T, node&>;
using out_type = std::remove_reference_t<out_ref>;
return static_cast<out_ref>(*reinterpret_cast<out_type*>(this));
}
template <typename T>
TOML_PURE_INLINE_GETTER
ref_cast_type<T, node&&> ref_cast() && noexcept
{
using out_ref = ref_cast_type<T, node&&>;
using out_type = std::remove_reference_t<out_ref>;
return static_cast<out_ref>(*reinterpret_cast<out_type*>(this));
}
template <typename T>
TOML_PURE_INLINE_GETTER
ref_cast_type<T, const node&> ref_cast() const& noexcept
{
using out_ref = ref_cast_type<T, const node&>;
using out_type = std::remove_reference_t<out_ref>;
return static_cast<out_ref>(*reinterpret_cast<out_type*>(this));
}
template <typename T>
TOML_PURE_INLINE_GETTER
ref_cast_type<T, const node&&> ref_cast() const&& noexcept
{
using out_ref = ref_cast_type<T, const node&&>;
using out_type = std::remove_reference_t<out_ref>;
return static_cast<out_ref>(*reinterpret_cast<out_type*>(this));
}
/// \endcond
public:
TOML_EXPORTED_MEMBER_FUNCTION
virtual ~node() noexcept;
/// \name Type checks
/// @{
/// \brief Checks if a node contains values/elements of only one type.
///
/// \detail \cpp
/// auto cfg = toml::parse("arr = [ 1, 2, 3, 4.0 ]");
/// toml::array& arr = *cfg["arr"].as_array();
///
/// toml::node* nonmatch{};
/// if (arr.is_homogeneous(toml::node_type::integer, nonmatch))
/// std::cout << "array was homogeneous"sv << "\n";
/// else
/// std::cout << "array was not homogeneous!\n"
/// << "first non-match was a "sv << nonmatch->type() << " at " << nonmatch->source() << "\n";
/// \ecpp
///
/// \out
/// array was not homogeneous!
/// first non-match was a floating-point at line 1, column 18
/// \eout
///
/// \param ntype A TOML node type. <br>
/// \conditional_return{toml::node_type::none}
/// "is every element the same type?"
/// \conditional_return{Anything else}
/// "is every element one of these?"
///
/// \param first_nonmatch Reference to a pointer in which the address of the first non-matching element
/// will be stored if the return value is false.
///
/// \returns True if the node was homogeneous.
///
/// \remarks Always returns `false` for empty tables and arrays.
TOML_PURE_GETTER
virtual bool is_homogeneous(node_type ntype, node*& first_nonmatch) noexcept = 0;
/// \brief Checks if a node contains values/elements of only one type (const overload).
TOML_PURE_GETTER
virtual bool is_homogeneous(node_type ntype, const node*& first_nonmatch) const noexcept = 0;
/// \brief Checks if the node contains values/elements of only one type.
///
/// \detail \cpp
/// auto arr = toml::array{ 1, 2, 3 };
/// std::cout << "homogenous: "sv << arr.is_homogeneous(toml::node_type::none) << "\n";
/// std::cout << "all floats: "sv << arr.is_homogeneous(toml::node_type::floating_point) << "\n";
/// std::cout << "all arrays: "sv << arr.is_homogeneous(toml::node_type::array) << "\n";
/// std::cout << "all ints: "sv << arr.is_homogeneous(toml::node_type::integer) << "\n";
/// \ecpp
///
/// \out
/// homogeneous: true
/// all floats: false
/// all arrays: false
/// all ints: true
/// \eout
///
/// \param ntype A TOML node type. <br>
/// \conditional_return{toml::node_type::none}
/// "is every element the same type?"
/// \conditional_return{Anything else}
/// "is every element one of these?"
///
/// \returns True if the node was homogeneous.
///
/// \remarks Always returns `false` for empty tables and arrays.
TOML_PURE_GETTER
virtual bool is_homogeneous(node_type ntype) const noexcept = 0;
/// \brief Checks if the node contains values/elements of only one type.
///
/// \detail \cpp
/// auto arr = toml::array{ 1, 2, 3 };
/// std::cout << "homogenous: "sv << arr.is_homogeneous() << "\n";
/// std::cout << "all doubles: "sv << arr.is_homogeneous<double>() << "\n";
/// std::cout << "all arrays: "sv << arr.is_homogeneous<toml::array>() << "\n";
/// std::cout << "all integers: "sv << arr.is_homogeneous<int64_t>() << "\n";
/// \ecpp
///
/// \out
/// homogeneous: true
/// all floats: false
/// all arrays: false
/// all ints: true
/// \eout
///
/// \tparam ElemType A TOML node or value type. <br>
/// \conditional_return{Left as `void`}
/// "is every element the same type?" <br>
/// \conditional_return{Explicitly specified}
/// "is every element a T?"
///
/// \returns True if the node was homogeneous.
///
/// \remarks Always returns `false` for empty tables and arrays.
template <typename ElemType = void>
TOML_PURE_GETTER
bool is_homogeneous() const noexcept
{
using type = impl::remove_cvref<impl::unwrap_node<ElemType>>;
static_assert(std::is_void_v<type> || toml::is_value<type> || toml::is_container<type>,
"The template type argument of node::is_homogeneous() must be void or one "
"of:" TOML_SA_UNWRAPPED_NODE_TYPE_LIST);
return is_homogeneous(impl::node_type_of<type>);
}
/// \brief Returns the node's type identifier.
TOML_PURE_GETTER
virtual node_type type() const noexcept = 0;
/// \brief Returns true if this node is a table.
TOML_PURE_GETTER
virtual bool is_table() const noexcept = 0;
/// \brief Returns true if this node is an array.
TOML_PURE_GETTER
virtual bool is_array() const noexcept = 0;
/// \brief Returns true if this node is an array containing only tables.
TOML_PURE_GETTER
virtual bool is_array_of_tables() const noexcept = 0;
/// \brief Returns true if this node is a value.
TOML_PURE_GETTER
virtual bool is_value() const noexcept = 0;
/// \brief Returns true if this node is a string value.
TOML_PURE_GETTER
virtual bool is_string() const noexcept = 0;
/// \brief Returns true if this node is an integer value.
TOML_PURE_GETTER
virtual bool is_integer() const noexcept = 0;
/// \brief Returns true if this node is an floating-point value.
TOML_PURE_GETTER
virtual bool is_floating_point() const noexcept = 0;
/// \brief Returns true if this node is an integer or floating-point value.
TOML_PURE_GETTER
virtual bool is_number() const noexcept = 0;
/// \brief Returns true if this node is a boolean value.
TOML_PURE_GETTER
virtual bool is_boolean() const noexcept = 0;
/// \brief Returns true if this node is a local date value.
TOML_PURE_GETTER
virtual bool is_date() const noexcept = 0;
/// \brief Returns true if this node is a local time value.
TOML_PURE_GETTER
virtual bool is_time() const noexcept = 0;
/// \brief Returns true if this node is a date-time value.
TOML_PURE_GETTER
virtual bool is_date_time() const noexcept = 0;
/// \brief Checks if a node is a specific type.
///
/// \tparam T A TOML node or value type.
///
/// \returns Returns true if this node is an instance of the specified type.
template <typename T>
TOML_PURE_INLINE_GETTER
bool is() const noexcept
{
using type = impl::remove_cvref<impl::unwrap_node<T>>;
static_assert(toml::is_value<type> || toml::is_container<type>,
"The template type argument of node::is() must be one of:" TOML_SA_UNWRAPPED_NODE_TYPE_LIST);
if constexpr (std::is_same_v<type, table>)
return is_table();
else if constexpr (std::is_same_v<type, array>)
return is_array();
else if constexpr (std::is_same_v<type, std::string>)
return is_string();
else if constexpr (std::is_same_v<type, int64_t>)
return is_integer();
else if constexpr (std::is_same_v<type, double>)
return is_floating_point();
else if constexpr (std::is_same_v<type, bool>)
return is_boolean();
else if constexpr (std::is_same_v<type, date>)
return is_date();
else if constexpr (std::is_same_v<type, time>)
return is_time();
else if constexpr (std::is_same_v<type, date_time>)
return is_date_time();
}
/// @}
/// \name Type casts
/// @{
/// \brief Returns a pointer to the node as a toml::table, if it is one.
TOML_PURE_GETTER
virtual table* as_table() noexcept = 0;
/// \brief Returns a pointer to the node as a toml::array, if it is one.
TOML_PURE_GETTER
virtual array* as_array() noexcept = 0;
/// \brief Returns a pointer to the node as a toml::value<std::string>, if it is one.
TOML_PURE_GETTER
virtual toml::value<std::string>* as_string() noexcept = 0;
/// \brief Returns a pointer to the node as a toml::value<int64_t>, if it is one.
TOML_PURE_GETTER
virtual toml::value<int64_t>* as_integer() noexcept = 0;
/// \brief Returns a pointer to the node as a toml::value<double>, if it is one.
TOML_PURE_GETTER
virtual toml::value<double>* as_floating_point() noexcept = 0;
/// \brief Returns a pointer to the node as a toml::value<bool>, if it is one.
TOML_PURE_GETTER
virtual toml::value<bool>* as_boolean() noexcept = 0;
/// \brief Returns a pointer to the node as a toml::value<toml::date>, if it is one.
TOML_PURE_GETTER
virtual toml::value<date>* as_date() noexcept = 0;
/// \brief Returns a pointer to the node as a toml::value<toml::time>, if it is one.
TOML_PURE_GETTER
virtual toml::value<time>* as_time() noexcept = 0;
/// \brief Returns a pointer to the node as a toml::value<toml::date_time>, if it is one.
TOML_PURE_GETTER
virtual toml::value<date_time>* as_date_time() noexcept = 0;
/// \brief Returns a const-qualified pointer to the node as a toml::table, if it is one.
TOML_PURE_GETTER
virtual const table* as_table() const noexcept = 0;
/// \brief Returns a const-qualified pointer to the node as a toml::array, if it is one.
TOML_PURE_GETTER
virtual const array* as_array() const noexcept = 0;
/// \brief Returns a const-qualified pointer to the node as a toml::value<std::string>, if it is one.
TOML_PURE_GETTER
virtual const toml::value<std::string>* as_string() const noexcept = 0;
/// \brief Returns a const-qualified pointer to the node as a toml::value<int64_t>, if it is one.
TOML_PURE_GETTER
virtual const toml::value<int64_t>* as_integer() const noexcept = 0;
/// \brief Returns a const-qualified pointer to the node as a toml::value<double>, if it is one.
TOML_PURE_GETTER
virtual const toml::value<double>* as_floating_point() const noexcept = 0;
/// \brief Returns a const-qualified pointer to the node as a toml::value<bool>, if it is one.
TOML_PURE_GETTER
virtual const toml::value<bool>* as_boolean() const noexcept = 0;
/// \brief Returns a const-qualified pointer to the node as a toml::value<toml::date>, if it is one.
TOML_PURE_GETTER
virtual const toml::value<date>* as_date() const noexcept = 0;
/// \brief Returns a const-qualified pointer to the node as a toml::value<toml::time>, if it is one.
TOML_PURE_GETTER
virtual const toml::value<time>* as_time() const noexcept = 0;
/// \brief Returns a const-qualified pointer to the node as a toml::value<toml::date_time>, if it is one.
TOML_PURE_GETTER
virtual const toml::value<date_time>* as_date_time() const noexcept = 0;
/// \brief Gets a pointer to the node as a more specific node type.
///
/// \details \cpp
///
/// toml::value<int64_t>* int_value = node->as<int64_t>();
/// toml::table* tbl = node->as<toml::table>();
/// if (int_value)
/// std::cout << "Node is a value<int64_t>\n";
/// else if (tbl)
/// std::cout << "Node is a table\n";
///
/// // fully-qualified value node types also work (useful for template code):
/// toml::value<int64_t>* int_value2 = node->as<toml::value<int64_t>>();
/// if (int_value2)
/// std::cout << "Node is a value<int64_t>\n";
/// \ecpp
///
/// \tparam T The node type or TOML value type to cast to.
///
/// \returns A pointer to the node as the given type, or nullptr if it was a different type.
template <typename T>
TOML_PURE_INLINE_GETTER
impl::wrap_node<T>* as() noexcept
{
using unwrapped_type = impl::unwrap_node<impl::remove_cvref<T>>;
static_assert(toml::is_value<unwrapped_type> || toml::is_container<unwrapped_type>,
"The template type argument of node::as() must be one of:" TOML_SA_UNWRAPPED_NODE_TYPE_LIST);
if constexpr (std::is_same_v<unwrapped_type, table>)
return as_table();
else if constexpr (std::is_same_v<unwrapped_type, array>)
return as_array();
else if constexpr (std::is_same_v<unwrapped_type, std::string>)
return as_string();
else if constexpr (std::is_same_v<unwrapped_type, int64_t>)
return as_integer();
else if constexpr (std::is_same_v<unwrapped_type, double>)
return as_floating_point();
else if constexpr (std::is_same_v<unwrapped_type, bool>)
return as_boolean();
else if constexpr (std::is_same_v<unwrapped_type, date>)
return as_date();
else if constexpr (std::is_same_v<unwrapped_type, time>)
return as_time();
else if constexpr (std::is_same_v<unwrapped_type, date_time>)
return as_date_time();
}
/// \brief Gets a pointer to the node as a more specific node type (const overload).
template <typename T>
TOML_PURE_INLINE_GETTER
const impl::wrap_node<T>* as() const noexcept
{
using unwrapped_type = impl::unwrap_node<impl::remove_cvref<T>>;
static_assert(toml::is_value<unwrapped_type> || toml::is_container<unwrapped_type>,
"The template type argument of node::as() must be one of:" TOML_SA_UNWRAPPED_NODE_TYPE_LIST);
if constexpr (std::is_same_v<unwrapped_type, table>)
return as_table();
else if constexpr (std::is_same_v<unwrapped_type, array>)
return as_array();
else if constexpr (std::is_same_v<unwrapped_type, std::string>)
return as_string();
else if constexpr (std::is_same_v<unwrapped_type, int64_t>)
return as_integer();
else if constexpr (std::is_same_v<unwrapped_type, double>)
return as_floating_point();
else if constexpr (std::is_same_v<unwrapped_type, bool>)
return as_boolean();
else if constexpr (std::is_same_v<unwrapped_type, date>)
return as_date();
else if constexpr (std::is_same_v<unwrapped_type, time>)
return as_time();
else if constexpr (std::is_same_v<unwrapped_type, date_time>)
return as_date_time();
}
/// @}
/// \name Value retrieval
/// @{
/// \brief Gets the value contained by this node.
///
/// \detail This function has 'exact' retrieval semantics; the only return value types allowed are the
/// TOML native value types, or types that can losslessly represent a native value type (e.g.
/// std::wstring on Windows).
///
/// \tparam T One of the native TOML value types, or a type capable of losslessly representing one.
///
/// \returns The underlying value if the node was a value of the
/// matching type (or losslessly convertible to it), or an empty optional.
///
/// \see node::value()
template <typename T>
TOML_NODISCARD
optional<T> value_exact() const noexcept(impl::value_retrieval_is_nothrow<T>);
/// \brief Gets the value contained by this node.
///
/// \detail This function has 'permissive' retrieval semantics; some value types are allowed
/// to convert to others (e.g. retrieving a boolean as an integer), and the specified return value
/// type can be any type where a reasonable conversion from a native TOML value exists
/// (e.g. std::wstring on Windows). If the source value cannot be represented by
/// the destination type, an empty optional is returned.
///
/// \godbolt{zzG81K}
///
/// \cpp
/// auto tbl = toml::parse(R"(
/// int = -10
/// flt = 25.0
/// pi = 3.14159
/// bool = false
/// huge = 9223372036854775807
/// str = "foo"
/// )"sv);
///
/// const auto print_value_with_typename =
/// [&](std::string_view key, std::string_view type_name, auto* dummy)
/// {
/// std::cout << "- " << std::setw(18) << std::left << type_name;
/// using type = std::remove_pointer_t<decltype(dummy)>;
/// if (auto val = tbl.get(key)->value<type>(); val)
/// std::cout << *val << "\n";
/// else
/// std::cout << "n/a\n";
/// };
///
/// #define print_value(key, T) print_value_with_typename(key, #T, (T*)nullptr)
///
/// for (auto key : { "int", "flt", "pi", "bool", "huge", "str" })
/// {
/// std::cout << tbl[key].type() << " value '" << key << "' as:\n";
/// print_value(key, bool);
/// print_value(key, int);
/// print_value(key, unsigned int);
/// print_value(key, long long);
/// print_value(key, float);
/// print_value(key, double);
/// print_value(key, std::string);
/// print_value(key, std::string_view);
/// print_value(key, const char*);
/// std::cout << "\n";
/// }
/// \ecpp
///
/// \out
/// integer value 'int' as:
/// - bool true
/// - int -10
/// - unsigned int n/a
/// - long long -10
/// - float -10
/// - double -10
/// - std::string n/a
/// - std::string_view n/a
/// - const char* n/a
///
/// floating-point value 'flt' as:
/// - bool n/a
/// - int 25
/// - unsigned int 25
/// - long long 25
/// - float 25
/// - double 25
/// - std::string n/a
/// - std::string_view n/a
/// - const char* n/a
///
/// floating-point value 'pi' as:
/// - bool n/a
/// - int n/a
/// - unsigned int n/a
/// - long long n/a
/// - float 3.14159
/// - double 3.14159
/// - std::string n/a
/// - std::string_view n/a
/// - const char* n/a
///
/// boolean value 'bool' as:
/// - bool false
/// - int 0
/// - unsigned int 0
/// - long long 0
/// - float n/a
/// - double n/a
/// - std::string n/a
/// - std::string_view n/a
/// - const char* n/a
///
/// integer value 'huge' as:
/// - bool true
/// - int n/a
/// - unsigned int n/a
/// - long long 9223372036854775807
/// - float n/a
/// - double n/a
/// - std::string n/a
/// - std::string_view n/a
/// - const char* n/a
///
/// string value 'str' as:
/// - bool n/a
/// - int n/a
/// - unsigned int n/a
/// - long long n/a
/// - float n/a
/// - double n/a
/// - std::string foo
/// - std::string_view foo
/// - const char* foo
/// \eout
///
/// \tparam T One of the native TOML value types, or a type capable of converting to one.
///
/// \returns The underlying value if the node was a value of the matching type (or convertible to it)
/// and within the range of the output type, or an empty optional.
///
/// \note If you want strict value retrieval semantics that do not allow for any type conversions,
/// use node::value_exact() instead.
///
/// \see node::value_exact()
template <typename T>
TOML_NODISCARD
optional<T> value() const noexcept(impl::value_retrieval_is_nothrow<T>);
/// \brief Gets the raw value contained by this node, or a default.
///
/// \tparam T Default value type. Must be one of the native TOML value types,
/// or convertible to it.
/// \param default_value The default value to return if the node wasn't a value, wasn't the
/// correct type, or no conversion was possible.
///
/// \returns The underlying value if the node was a value of the matching type (or convertible to it)
/// and within the range of the output type, or the provided default.
///
/// \note This function has the same permissive retrieval semantics as node::value(). If you want strict
/// value retrieval semantics that do not allow for any type conversions, use node::value_exact()
/// instead.
///
/// \see
/// - node::value()
/// - node::value_exact()
template <typename T>
TOML_NODISCARD
auto value_or(T&& default_value) const noexcept(impl::value_retrieval_is_nothrow<T>);
/// \brief Gets a raw reference to a node's underlying data.
///
/// \warning This function is dangerous if used carelessly and **WILL** break your code if the
/// chosen value type doesn't match the node's actual type. In debug builds an assertion
/// will fire when invalid accesses are attempted: \cpp
///
/// auto tbl = toml::parse(R"(
/// min = 32
/// max = 45
/// )"sv);
///
/// int64_t& min_ref = tbl.at("min").ref<int64_t>(); // matching type
/// double& max_ref = tbl.at("max").ref<double>(); // mismatched type, hits assert()
/// \ecpp
///
/// \note Specifying explicit ref qualifiers acts as an explicit ref-category cast,
/// whereas specifying explicit cv-ref qualifiers merges them with whatever
/// the cv qualification of the node is (to ensure cv-correctness is propagated), e.g.:
/// | node | T | return type |
/// |-------------|------------------------|------------------------------|
/// | node& | std::string | std::string& |
/// | node& | std::string&& | std::string&& |
/// | const node& | volatile std::string | const volatile std::string& |
/// | const node& | volatile std::string&& | const volatile std::string&& |
///
/// \tparam T toml::table, toml::array, or one of the TOML value types.
///
/// \returns A reference to the underlying data.
template <typename T>
TOML_PURE_GETTER
decltype(auto) ref() & noexcept
{
return do_ref<T>(*this);
}
/// \brief Gets a raw reference to a node's underlying data (rvalue overload).
template <typename T>
TOML_PURE_GETTER
decltype(auto) ref() && noexcept
{
return do_ref<T>(std::move(*this));
}
/// \brief Gets a raw reference to a node's underlying data (const lvalue overload).
template <typename T>
TOML_PURE_GETTER
decltype(auto) ref() const& noexcept
{
return do_ref<T>(*this);
}
/// \brief Gets a raw reference to a node's underlying data (const rvalue overload).
template <typename T>
TOML_PURE_GETTER
decltype(auto) ref() const&& noexcept
{
return do_ref<T>(std::move(*this));
}
/// @}
/// \name Metadata
/// @{
/// \brief Returns the source region responsible for generating this node during parsing.
TOML_PURE_INLINE_GETTER
const source_region& source() const noexcept
{
return source_;
}
/// @}
private:
/// \cond
template <typename Func, typename Node, typename T>
static constexpr bool can_visit = std::is_invocable_v<Func, ref_cast_type<T, Node>>;
template <typename Func, typename Node, typename T>
static constexpr bool can_visit_nothrow = std::is_nothrow_invocable_v<Func, ref_cast_type<T, Node>>;
template <typename Func, typename Node>
static constexpr bool can_visit_any = can_visit<Func, Node, table> //
|| can_visit<Func, Node, array> //
|| can_visit<Func, Node, std::string> //
|| can_visit<Func, Node, int64_t> //
|| can_visit<Func, Node, double> //
|| can_visit<Func, Node, bool> //
|| can_visit<Func, Node, date> //
|| can_visit<Func, Node, time> //
|| can_visit<Func, Node, date_time>;
// clang-format off
template <typename Func, typename Node>
static constexpr bool can_visit_all = can_visit<Func, Node, table> //
&& can_visit<Func, Node, array> //
&& can_visit<Func, Node, std::string> //
&& can_visit<Func, Node, int64_t> //
&& can_visit<Func, Node, double> //
&& can_visit<Func, Node, bool> //
&& can_visit<Func, Node, date> //
&& can_visit<Func, Node, time> //
&& can_visit<Func, Node, date_time>;
template <typename Func, typename Node, typename T>
static constexpr bool visit_is_nothrow_one = !can_visit<Func, Node, T> || can_visit_nothrow<Func, Node, T>;
template <typename Func, typename Node>
static constexpr bool visit_is_nothrow = visit_is_nothrow_one<Func, Node, table> //
&& visit_is_nothrow_one<Func, Node, array> //
&& visit_is_nothrow_one<Func, Node, std::string> //
&& visit_is_nothrow_one<Func, Node, int64_t> //
&& visit_is_nothrow_one<Func, Node, double> //
&& visit_is_nothrow_one<Func, Node, bool> //
&& visit_is_nothrow_one<Func, Node, date> //
&& visit_is_nothrow_one<Func, Node, time> //
&& visit_is_nothrow_one<Func, Node, date_time>;
// clang-format on
template <typename Func, typename Node, typename T, bool = can_visit<Func, Node, T>>
struct visit_return_type_
{
using type = decltype(std::declval<Func>()(std::declval<ref_cast_type<T, Node>>()));
};
template <typename Func, typename Node, typename T>
struct visit_return_type_<Func, Node, T, false>
{
using type = void;
};
template <typename Func, typename Node, typename T>
using visit_return_type = typename visit_return_type_<Func, Node, T>::type;
template <typename A, typename B>
using nonvoid = std::conditional_t<std::is_void_v<A>, B, A>;
template <typename Func, typename Node>
static decltype(auto) do_visit(Func&& visitor, Node&& n) noexcept(visit_is_nothrow<Func&&, Node&&>)
{
static_assert(can_visit_any<Func&&, Node&&>,
"TOML node visitors must be invocable for at least one of the toml::node "
"specializations:" TOML_SA_NODE_TYPE_LIST);
switch (n.type())
{
case node_type::table:
if constexpr (can_visit<Func&&, Node&&, table>)
return static_cast<Func&&>(visitor)(static_cast<Node&&>(n).template ref_cast<table>());
break;
case node_type::array:
if constexpr (can_visit<Func&&, Node&&, array>)
return static_cast<Func&&>(visitor)(static_cast<Node&&>(n).template ref_cast<array>());
break;
case node_type::string:
if constexpr (can_visit<Func&&, Node&&, std::string>)
return static_cast<Func&&>(visitor)(static_cast<Node&&>(n).template ref_cast<std::string>());
break;
case node_type::integer:
if constexpr (can_visit<Func&&, Node&&, int64_t>)
return static_cast<Func&&>(visitor)(static_cast<Node&&>(n).template ref_cast<int64_t>());
break;
case node_type::floating_point:
if constexpr (can_visit<Func&&, Node&&, double>)
return static_cast<Func&&>(visitor)(static_cast<Node&&>(n).template ref_cast<double>());
break;
case node_type::boolean:
if constexpr (can_visit<Func&&, Node&&, bool>)
return static_cast<Func&&>(visitor)(static_cast<Node&&>(n).template ref_cast<bool>());
break;
case node_type::date:
if constexpr (can_visit<Func&&, Node&&, date>)
return static_cast<Func&&>(visitor)(static_cast<Node&&>(n).template ref_cast<date>());
break;
case node_type::time:
if constexpr (can_visit<Func&&, Node&&, time>)
return static_cast<Func&&>(visitor)(static_cast<Node&&>(n).template ref_cast<time>());
break;
case node_type::date_time:
if constexpr (can_visit<Func&&, Node&&, date_time>)
return static_cast<Func&&>(visitor)(static_cast<Node&&>(n).template ref_cast<date_time>());
break;
case node_type::none: TOML_UNREACHABLE;
default: TOML_UNREACHABLE;
}
if constexpr (!can_visit_all<Func&&, Node&&>)
{
// clang-format off
using return_type =
nonvoid<visit_return_type<Func&&, Node&&, table>,
nonvoid<visit_return_type<Func&&, Node&&, array>,
nonvoid<visit_return_type<Func&&, Node&&, std::string>,
nonvoid<visit_return_type<Func&&, Node&&, int64_t>,
nonvoid<visit_return_type<Func&&, Node&&, double>,
nonvoid<visit_return_type<Func&&, Node&&, bool>,
nonvoid<visit_return_type<Func&&, Node&&, date>,
nonvoid<visit_return_type<Func&&, Node&&, time>,
visit_return_type<Func&&, Node&&, date_time>
>>>>>>>>;
// clang-format on
if constexpr (!std::is_void_v<return_type>)
{
static_assert(std::is_default_constructible_v<return_type>,
"Non-exhaustive visitors must return a default-constructible type, or void");
return return_type{};
}
}
}
/// \endcond
public:
/// \name Visitation
/// @{
/// \brief Invokes a visitor on the node based on the node's concrete type.
///
/// \details Visitation is useful when you expect
/// a node to be one of a set number of types and need
/// to handle these types differently. Using `visit()` allows
/// you to eliminate some of the casting/conversion boilerplate: \cpp
///
/// node.visit([](auto&& n)
/// {
/// if constexpr (toml::is_string<decltype(n)>)
/// do_something_with_a_string(*n)); //n is a toml::value<std::string>
/// else if constexpr (toml::is_integer<decltype(n)>)
/// do_something_with_an_int(*n); //n is a toml::value<int64_t>
/// });
/// \ecpp
///
/// Visitor functions need not be generic; specifying a concrete node type as the input argument type
/// effectively acts a 'filter', only invoking the visitor if the concrete type is compatible.
/// Thus the example above can be re-written as: \cpp
/// node.visit([](toml::value<std::string>& s) { do_something_with_a_string(*s)); });
/// node.visit([](toml::value<int64_t>& i) { do_something_with_an_int(*i)); });
/// \ecpp
///
/// \tparam Func A callable type invocable with one or more of the toml++ node types.
///
/// \param visitor The visitor object.
///
/// \returns The return value of the visitor.
/// Can be void. Non-exhaustive visitors must return a default-constructible type.
///
/// \see https://en.wikipedia.org/wiki/Visitor_pattern
template <typename Func>
decltype(auto) visit(Func&& visitor) & noexcept(visit_is_nothrow<Func&&, node&>)
{
return do_visit(static_cast<Func&&>(visitor), *this);
}
/// \brief Invokes a visitor on the node based on the node's concrete type (rvalue overload).
template <typename Func>
decltype(auto) visit(Func&& visitor) && noexcept(visit_is_nothrow<Func&&, node&&>)
{
return do_visit(static_cast<Func&&>(visitor), static_cast<node&&>(*this));
}
/// \brief Invokes a visitor on the node based on the node's concrete type (const lvalue overload).
template <typename Func>
decltype(auto) visit(Func&& visitor) const& noexcept(visit_is_nothrow<Func&&, const node&>)
{
return do_visit(static_cast<Func&&>(visitor), *this);
}
/// \brief Invokes a visitor on the node based on the node's concrete type (const rvalue overload).
template <typename Func>
decltype(auto) visit(Func&& visitor) const&& noexcept(visit_is_nothrow<Func&&, const node&&>)
{
return do_visit(static_cast<Func&&>(visitor), static_cast<const node&&>(*this));
}
/// @}
/// \name Node views
/// @{
/// \brief Creates a node_view pointing to this node.
TOML_NODISCARD
explicit operator node_view<node>() noexcept;
/// \brief Creates a node_view pointing to this node (const overload).
TOML_NODISCARD
explicit operator node_view<const node>() const noexcept;
/// \brief Returns a view of the subnode matching a fully-qualified "TOML path".
///
/// \detail \cpp
/// auto config = toml::parse(R"(
///
/// [foo]
/// bar = [ 0, 1, 2, [ 3 ], { kek = 4 } ]
///
/// )"sv);
///
/// std::cout << config.at_path("foo.bar[2]") << "\n";
/// std::cout << config.at_path("foo.bar[3][0]") << "\n";
/// std::cout << config.at_path("foo.bar[4].kek") << "\n";
/// \ecpp
///
/// \out
/// 2
/// 3
/// 4
/// \eout
///
///
/// \note Keys in paths are interpreted literally, so whitespace (or lack thereof) matters:
/// \cpp
/// config.at_path( "foo.bar") // same as node_view{ config }["foo"]["bar"]
/// config.at_path( "foo. bar") // same as node_view{ config }["foo"][" bar"]
/// config.at_path( "foo..bar") // same as node_view{ config }["foo"][""]["bar"]
/// config.at_path( ".foo.bar") // same as node_view{ config }[""]["foo"]["bar"]
/// \ecpp
/// <br>
/// Additionally, TOML allows '.' (period) characters to appear in keys if they are quoted strings.
/// This function makes no allowance for this, instead treating all period characters as sub-table delimiters.
/// If you have periods in your table keys, first consider:
/// 1. Not doing that
/// 2. Using node_view::operator[] instead.
///
/// \param path The "TOML path" to traverse.
TOML_NODISCARD
TOML_EXPORTED_MEMBER_FUNCTION
node_view<node> at_path(std::string_view path) noexcept;
/// \brief Returns a const view of the subnode matching a fully-qualified "TOML path".
///
/// \see #at_path(std::string_view)
TOML_NODISCARD
TOML_EXPORTED_MEMBER_FUNCTION
node_view<const node> at_path(std::string_view path) const noexcept;
/// \brief Returns a view of the subnode matching a fully-qualified "TOML path".
///
/// \detail \cpp
/// auto config = toml::parse(R"(
///
/// [foo]
/// bar = [ 0, 1, 2, [ 3 ], { kek = 4 } ]
///
/// )"sv);
///
/// toml::path path1("foo.bar[2]");
/// toml::path path2("foo.bar[4].kek");
/// std::cout << config.at_path(path1) << "\n";
/// std::cout << config.at_path(path1.parent_path()) << "\n";
/// std::cout << config.at_path(path2) << "\n";
/// std::cout << config.at_path(path2.parent_path()) << "\n";
/// \ecpp
///
/// \out
/// 2
/// [ 0, 1, 2, [ 3 ], { kek = 4 } ]
/// 4
/// { kek = 4 }
/// \eout
///
///
/// \note Keys in paths are interpreted literally, so whitespace (or lack thereof) matters:
/// \cpp
/// config.at_path(toml::path("foo.bar")) // same as node_view{ config }["foo"]["bar"]
/// config.at_path(toml::path("foo. bar")) // same as node_view{ config }["foo"][" bar"]
/// config.at_path(toml::path("foo..bar")) // same as node_view{ config }["foo"][""]["bar"]
/// config.at_path(toml::path(".foo.bar")) // same as node_view{ config }[""]["foo"]["bar"]
/// \ecpp
/// <br>
/// Additionally, TOML allows '.' (period) characters to appear in keys if they are quoted strings.
/// This function makes no allowance for this, instead treating all period characters as sub-table delimiters.
///
/// \param path The "TOML path" to traverse.
TOML_NODISCARD
TOML_EXPORTED_MEMBER_FUNCTION
node_view<node> at_path(const toml::path& path) noexcept;
/// \brief Returns a const view of the subnode matching a fully-qualified "TOML path".
///
/// \see #at_path(const toml::path&)
TOML_NODISCARD
TOML_EXPORTED_MEMBER_FUNCTION
node_view<const node> at_path(const toml::path& path) const noexcept;
#if TOML_ENABLE_WINDOWS_COMPAT
/// \brief Returns a view of the subnode matching a fully-qualified "TOML path".
///
/// \availability This overload is only available when #TOML_ENABLE_WINDOWS_COMPAT is enabled.
///
/// \see #at_path(std::string_view)
TOML_NODISCARD
TOML_EXPORTED_MEMBER_FUNCTION
node_view<node> at_path(std::wstring_view path);
/// \brief Returns a const view of the subnode matching a fully-qualified "TOML path".
///
/// \availability This overload is only available when #TOML_ENABLE_WINDOWS_COMPAT is enabled.
///
/// \see #at_path(std::string_view)
TOML_NODISCARD
TOML_EXPORTED_MEMBER_FUNCTION
node_view<const node> at_path(std::wstring_view path) const;
#endif // TOML_ENABLE_WINDOWS_COMPAT
/// \brief Returns a const view of the subnode matching a fully-qualified "TOML path".
///
/// \param path The "TOML path" to the desired child.
///
/// \returns A view of the child node at the given path if one existed, or an empty node view.
///
/// \see toml::node_view
TOML_NODISCARD
TOML_EXPORTED_MEMBER_FUNCTION
node_view<node> operator[](const toml::path& path) noexcept;
/// \brief Returns a const view of the subnode matching a fully-qualified "TOML path".
///
/// \param path The "TOML path" to the desired child.
///
/// \returns A view of the child node at the given path if one existed, or an empty node view.
///
/// \see toml::node_view
TOML_NODISCARD
TOML_EXPORTED_MEMBER_FUNCTION
node_view<const node> operator[](const toml::path& path) const noexcept;
/// @}
};
}
TOML_NAMESPACE_END;
/// \cond
TOML_IMPL_NAMESPACE_START
{
TOML_PURE_GETTER
TOML_EXPORTED_FREE_FUNCTION
bool TOML_CALLCONV node_deep_equality(const node*, const node*) noexcept;
}
TOML_IMPL_NAMESPACE_END;
/// \endcond
#include "header_end.hpp"
|