-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcpptoml.h
2495 lines (2186 loc) · 63 KB
/
cpptoml.h
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
/**
* @file cpptoml.h
* @author Chase Geigle
* @date May 2013
*/
#ifndef _CPPTOML_H_
#define _CPPTOML_H_
#include <algorithm>
#if !CPPTOML_HAS_STD_PUT_TIME
#include <array>
#endif
#include <cassert>
#include <cstdint>
#include <cstring>
#include <fstream>
#include <iomanip>
#include <memory>
#if CPPTOML_HAS_STD_REGEX
#include <regex>
#endif
#include <sstream>
#include <stdexcept>
#include <string>
#include <unordered_map>
#include <map>
#include <vector>
namespace cpptoml
{
class writer; // forward declaration
class base; // forward declaration
#if defined(CPPTOML_USE_MAP)
// a std::map will ensure that entries a sorted, albeit at a slight
// performance penalty relative to the (default) unordered_map
using string_to_base_map = std::map<std::string, std::shared_ptr<base>>;
#else
// by default an unordered_map is used for best performance as the
// toml specification does not require entries to be sorted
using string_to_base_map
= std::unordered_map<std::string, std::shared_ptr<base>>;
#endif
template <class T>
class option
{
public:
option() : empty_{true}
{
// nothing
}
option(T value) : empty_{false}, value_{std::move(value)}
{
// nothing
}
explicit operator bool() const
{
return !empty_;
}
const T& operator*() const
{
return value_;
}
const T& value_or(const T& alternative) const
{
if (!empty_)
return value_;
return alternative;
}
private:
bool empty_;
T value_;
};
struct datetime
{
int year = 0;
int month = 0;
int day = 0;
int hour = 0;
int minute = 0;
int second = 0;
int microsecond = 0;
int hour_offset = 0;
int minute_offset = 0;
static inline struct datetime from_local(const struct tm& t)
{
datetime dt;
dt.year = t.tm_year + 1900;
dt.month = t.tm_mon + 1;
dt.day = t.tm_mday;
dt.hour = t.tm_hour;
dt.minute = t.tm_min;
dt.second = t.tm_sec;
char buf[16];
strftime(buf, 16, "%z", &t);
int offset = std::stoi(buf);
dt.hour_offset = offset / 100;
dt.minute_offset = offset % 100;
return dt;
}
static inline struct datetime from_utc(const struct tm& t)
{
datetime dt;
dt.year = t.tm_year + 1900;
dt.month = t.tm_mon + 1;
dt.day = t.tm_mday;
dt.hour = t.tm_hour;
dt.minute = t.tm_min;
dt.second = t.tm_sec;
return dt;
}
};
inline std::ostream& operator<<(std::ostream& os, const datetime& dt)
{
using std::setw;
auto fill = os.fill();
os.fill('0');
os << setw(4) << dt.year << "-" << setw(2) << dt.month << "-" << setw(2)
<< dt.day << "T" << setw(2) << dt.hour << ":" << setw(2) << dt.minute
<< ":" << setw(2) << dt.second;
if (dt.microsecond > 0)
{
os << "." << setw(6) << dt.microsecond;
}
if (dt.hour_offset != 0 || dt.minute_offset != 0)
{
if (dt.hour_offset > 0)
os << "+";
else
os << "-";
os << setw(2) << std::abs(dt.hour_offset) << ":" << setw(2)
<< std::abs(dt.minute_offset);
}
else
os << "Z";
os.fill(fill);
return os;
}
template <class T>
class value;
template <class T>
struct valid_value
{
const static bool value
= std::is_same<T, std::string>::value || std::is_same<T, int64_t>::value
|| std::is_same<T, double>::value || std::is_same<T, bool>::value
|| std::is_same<T, datetime>::value;
};
template <class T, bool Valid = valid_value<typename std::decay<T>::type>::value
|| std::is_convertible<T, std::string>::value>
struct value_traits;
template <class T>
struct value_traits<T, true>
{
const static bool valid = valid_value<typename std::decay<T>::type>::value
|| std::is_convertible<T, std::string>::value;
using value_type = typename std::
conditional<valid_value<typename std::decay<T>::type>::value,
typename std::decay<T>::type, std::string>::type;
using type = value<value_type>;
};
class array;
class table;
class table_array;
template <class T>
inline std::shared_ptr<typename value_traits<T>::type> make_value(T&& val);
inline std::shared_ptr<array> make_array();
template <class T>
inline std::shared_ptr<T> make_element();
inline std::shared_ptr<table> make_table();
inline std::shared_ptr<table_array> make_table_array();
/**
* A generic base TOML value used for type erasure.
*/
class base : public std::enable_shared_from_this<base>
{
public:
/**
* Determines if the given TOML element is a value.
*/
virtual bool is_value() const
{
return false;
}
/**
* Determines if the given TOML element is a table.
*/
virtual bool is_table() const
{
return false;
}
/**
* Converts the TOML element into a table.
*/
std::shared_ptr<table> as_table()
{
if (is_table())
return std::static_pointer_cast<table>(shared_from_this());
return nullptr;
}
/**
* Determines if the TOML element is an array of "leaf" elements.
*/
virtual bool is_array() const
{
return false;
}
/**
* Converts the TOML element to an array.
*/
std::shared_ptr<array> as_array()
{
if (is_array())
return std::static_pointer_cast<array>(shared_from_this());
return nullptr;
}
/**
* Determines if the given TOML element is an array of tables.
*/
virtual bool is_table_array() const
{
return false;
}
/**
* Converts the TOML element into a table array.
*/
std::shared_ptr<table_array> as_table_array()
{
if (is_table_array())
return std::static_pointer_cast<table_array>(shared_from_this());
return nullptr;
}
/**
* Attempts to coerce the TOML element into a concrete TOML value
* of type T.
*/
template <class T>
std::shared_ptr<value<T>> as();
template <class T>
std::shared_ptr<const value<T>> as() const;
template <class Visitor, class... Args>
void accept(Visitor&& visitor, Args&&... args) const;
protected:
base()
{
// nothing
}
};
/**
* A concrete TOML value representing the "leaves" of the "tree".
*/
template <class T>
class value : public base
{
struct make_shared_enabler
{
// nothing; this is a private key accessible only to friends
};
template <class U>
friend std::shared_ptr<typename value_traits<U>::type>
cpptoml::make_value(U&& val);
public:
static_assert(valid_value<T>::value, "invalid value type");
value(const make_shared_enabler&, const T& val) : value(val)
{
// nothing; note that users cannot actually invoke this function
// because they lack access to the make_shared_enabler.
}
bool is_value() const override
{
return true;
}
/**
* Gets the data associated with this value.
*/
T& get()
{
return data_;
}
/**
* Gets the data associated with this value. Const version.
*/
const T& get() const
{
return data_;
}
private:
T data_;
/**
* Constructs a value from the given data.
*/
value(const T& val) : data_(val)
{
}
value(const value& val) = delete;
value& operator=(const value& val) = delete;
};
template <class T>
std::shared_ptr<typename value_traits<T>::type> make_value(T&& val)
{
using value_type = typename value_traits<T>::type;
using enabler = typename value_type::make_shared_enabler;
return std::make_shared<value_type>(enabler{}, std::forward<T>(val));
}
template <class T>
inline std::shared_ptr<value<T>> base::as()
{
if (auto v = std::dynamic_pointer_cast<value<T>>(shared_from_this()))
return v;
return nullptr;
}
// special case value<double> to allow getting an integer parameter as a
// double value
template <>
inline std::shared_ptr<value<double>> base::as()
{
if (auto v = std::dynamic_pointer_cast<value<double>>(shared_from_this()))
return v;
if (auto v = std::dynamic_pointer_cast<value<int64_t>>(shared_from_this()))
return make_value<double>(static_cast<double>(v->get()));
return nullptr;
}
template <class T>
inline std::shared_ptr<const value<T>> base::as() const
{
if (auto v = std::dynamic_pointer_cast<const value<T>>(shared_from_this()))
return v;
return nullptr;
}
// special case value<double> to allow getting an integer parameter as a
// double value
template <>
inline std::shared_ptr<const value<double>> base::as() const
{
if (auto v
= std::dynamic_pointer_cast<const value<double>>(shared_from_this()))
return v;
if (auto v = as<int64_t>())
{
// the below has to be a non-const value<double> due to a bug in
// libc++: https://llvm.org/bugs/show_bug.cgi?id=18843
return make_value<double>(static_cast<double>(v->get()));
}
return nullptr;
}
/**
* Exception class for array insertion errors.
*/
class array_exception : public std::runtime_error
{
public:
array_exception(const std::string& err) : std::runtime_error{err}
{
}
};
class array : public base
{
public:
friend std::shared_ptr<array> make_array();
virtual bool is_array() const override
{
return true;
}
/**
* arrays can be iterated over
*/
using iterator = std::vector<std::shared_ptr<base>>::iterator;
/**
* arrays can be iterated over. Const version.
*/
using const_iterator = std::vector<std::shared_ptr<base>>::const_iterator;
iterator begin()
{
return values_.begin();
}
const_iterator begin() const
{
return values_.begin();
}
iterator end()
{
return values_.end();
}
const_iterator end() const
{
return values_.end();
}
/**
* Obtains the array (vector) of base values.
*/
std::vector<std::shared_ptr<base>>& get()
{
return values_;
}
/**
* Obtains the array (vector) of base values. Const version.
*/
const std::vector<std::shared_ptr<base>>& get() const
{
return values_;
}
std::shared_ptr<base> at(size_t idx) const
{
return values_.at(idx);
}
/**
* Obtains an array of value<T>s. Note that elements may be
* nullptr if they cannot be converted to a value<T>.
*/
template <class T>
std::vector<std::shared_ptr<value<T>>> array_of() const
{
std::vector<std::shared_ptr<value<T>>> result(values_.size());
std::transform(values_.begin(), values_.end(), result.begin(),
[&](std::shared_ptr<base> v)
{
return v->as<T>();
});
return result;
}
/**
* Obtains an array of arrays. Note that elements may be nullptr
* if they cannot be converted to a array.
*/
std::vector<std::shared_ptr<array>> nested_array() const
{
std::vector<std::shared_ptr<array>> result(values_.size());
std::transform(values_.begin(), values_.end(), result.begin(),
[&](std::shared_ptr<base> v) -> std::shared_ptr<array>
{
if (v->is_array())
return std::static_pointer_cast<array>(v);
return std::shared_ptr<array>{};
});
return result;
}
/**
* Add a value to the end of the array
*/
template <class T>
void push_back(const std::shared_ptr<value<T>>& val)
{
if (values_.empty() || values_[0]->as<T>())
{
values_.push_back(val);
}
else
{
throw array_exception{"Arrays must be homogenous."};
}
}
/**
* Add an array to the end of the array
*/
void push_back(const std::shared_ptr<array>& val)
{
if (values_.empty() || values_[0]->is_array())
{
values_.push_back(val);
}
else
{
throw array_exception{"Arrays must be homogenous."};
}
}
/**
* Convenience function for adding a simple element to the end
* of the array.
*/
template <class T>
void push_back(T&& val, typename value_traits<T>::type* = 0)
{
push_back(make_value(std::forward<T>(val)));
}
/**
* Insert a value into the array
*/
template <class T>
iterator insert(iterator position, const std::shared_ptr<value<T>>& value)
{
if (values_.empty() || values_[0]->as<T>())
{
return values_.insert(position, value);
}
else
{
throw array_exception{"Arrays must be homogenous."};
}
}
/**
* Insert an array into the array
*/
iterator insert(iterator position, const std::shared_ptr<array>& value)
{
if (values_.empty() || values_[0]->is_array())
{
return values_.insert(position, value);
}
else
{
throw array_exception{"Arrays must be homogenous."};
}
}
/**
* Convenience function for inserting a simple element in the array
*/
template <class T>
iterator insert(iterator position, T&& val,
typename value_traits<T>::type* = 0)
{
return insert(position, make_value(std::forward<T>(val)));
}
/**
* Erase an element from the array
*/
iterator erase(iterator position)
{
return values_.erase(position);
}
/**
* Clear the array
*/
void clear()
{
values_.clear();
}
private:
array() = default;
template <class InputIterator>
array(InputIterator begin, InputIterator end)
: values_{begin, end}
{
// nothing
}
array(const array& obj) = delete;
array& operator=(const array& obj) = delete;
std::vector<std::shared_ptr<base>> values_;
};
inline std::shared_ptr<array> make_array()
{
struct make_shared_enabler : public array
{
make_shared_enabler()
{
// nothing
}
};
return std::make_shared<make_shared_enabler>();
}
template <>
inline std::shared_ptr<array> make_element<array>()
{
return make_array();
}
class table;
class table_array : public base
{
friend class table;
friend std::shared_ptr<table_array> make_table_array();
public:
/**
* arrays can be iterated over
*/
using iterator = std::vector<std::shared_ptr<table>>::iterator;
/**
* arrays can be iterated over. Const version.
*/
using const_iterator = std::vector<std::shared_ptr<table>>::const_iterator;
iterator begin()
{
return array_.begin();
}
const_iterator begin() const
{
return array_.begin();
}
iterator end()
{
return array_.end();
}
const_iterator end() const
{
return array_.end();
}
virtual bool is_table_array() const override
{
return true;
}
std::vector<std::shared_ptr<table>>& get()
{
return array_;
}
const std::vector<std::shared_ptr<table>>& get() const
{
return array_;
}
/**
* Add a table to the end of the array
*/
void push_back(const std::shared_ptr<table>& val)
{
array_.push_back(val);
}
/**
* Insert a table into the array
*/
iterator insert(iterator position, const std::shared_ptr<table>& value)
{
return array_.insert(position, value);
}
/**
* Erase an element from the array
*/
iterator erase(iterator position)
{
return array_.erase(position);
}
/**
* Clear the array
*/
void clear()
{
array_.clear();
}
private:
table_array()
{
// nothing
}
table_array(const table_array& obj) = delete;
table_array& operator=(const table_array& rhs) = delete;
std::vector<std::shared_ptr<table>> array_;
};
inline std::shared_ptr<table_array> make_table_array()
{
struct make_shared_enabler : public table_array
{
make_shared_enabler()
{
// nothing
}
};
return std::make_shared<make_shared_enabler>();
}
template <>
inline std::shared_ptr<table_array> make_element<table_array>()
{
return make_table_array();
}
/**
* Represents a TOML keytable.
*/
class table : public base
{
public:
friend class table_array;
friend std::shared_ptr<table> make_table();
/**
* tables can be iterated over.
*/
using iterator = string_to_base_map::iterator;
/**
* tables can be iterated over. Const version.
*/
using const_iterator = string_to_base_map::const_iterator;
iterator begin()
{
return map_.begin();
}
const_iterator begin() const
{
return map_.begin();
}
iterator end()
{
return map_.end();
}
const_iterator end() const
{
return map_.end();
}
bool is_table() const override
{
return true;
}
bool empty() const
{
return map_.empty();
}
/**
* Determines if this key table contains the given key.
*/
bool contains(const std::string& key) const
{
return map_.find(key) != map_.end();
}
/**
* Determines if this key table contains the given key. Will
* resolve "qualified keys". Qualified keys are the full access
* path separated with dots like "grandparent.parent.child".
*/
bool contains_qualified(const std::string& key) const
{
return resolve_qualified(key);
}
/**
* Obtains the base for a given key.
* @throw std::out_of_range if the key does not exist
*/
std::shared_ptr<base> get(const std::string& key) const
{
return map_.at(key);
}
/**
* Obtains the base for a given key. Will resolve "qualified
* keys". Qualified keys are the full access path separated with
* dots like "grandparent.parent.child".
*
* @throw std::out_of_range if the key does not exist
*/
std::shared_ptr<base> get_qualified(const std::string& key) const
{
std::shared_ptr<base> p;
resolve_qualified(key, &p);
return p;
}
/**
* Obtains a table for a given key, if possible.
*/
std::shared_ptr<table> get_table(const std::string& key) const
{
if (contains(key) && get(key)->is_table())
return std::static_pointer_cast<table>(get(key));
return nullptr;
}
/**
* Obtains a table for a given key, if possible. Will resolve
* "qualified keys".
*/
std::shared_ptr<table> get_table_qualified(const std::string& key) const
{
if (contains_qualified(key) && get_qualified(key)->is_table())
return std::static_pointer_cast<table>(get_qualified(key));
return nullptr;
}
/**
* Obtains an array for a given key.
*/
std::shared_ptr<array> get_array(const std::string& key) const
{
if (!contains(key))
return nullptr;
return get(key)->as_array();
}
/**
* Obtains an array for a given key. Will resolve "qualified keys".
*/
std::shared_ptr<array> get_array_qualified(const std::string& key) const
{
if (!contains_qualified(key))
return nullptr;
return get_qualified(key)->as_array();
}
/**
* Obtains a table_array for a given key, if possible.
*/
std::shared_ptr<table_array> get_table_array(const std::string& key) const
{
if (!contains(key))
return nullptr;
return get(key)->as_table_array();
}
/**
* Obtains a table_array for a given key, if possible. Will resolve
* "qualified keys".
*/
std::shared_ptr<table_array>
get_table_array_qualified(const std::string& key) const
{
if (!contains_qualified(key))
return nullptr;
return get_qualified(key)->as_table_array();
}
/**
* Helper function that attempts to get a value corresponding
* to the template parameter from a given key.
*/
template <class T>
option<T> get_as(const std::string& key) const
{
try
{
if (auto v = get(key)->as<T>())
return {v->get()};
else
return {};
}
catch (const std::out_of_range&)
{
return {};
}
}
/**
* Helper function that attempts to get a value corresponding
* to the template parameter from a given key. Will resolve "qualified
* keys".
*/
template <class T>
option<T> get_qualified_as(const std::string& key) const
{
try
{
if (auto v = get_qualified(key)->as<T>())
return {v->get()};
else
return {};
}
catch (const std::out_of_range&)
{
return {};
}
}
/**
* Adds an element to the keytable.
*/
void insert(const std::string& key, const std::shared_ptr<base>& value)
{
map_[key] = value;
}
/**
* Convenience shorthand for adding a simple element to the
* keytable.
*/
template <class T>
void insert(const std::string& key, T&& val,
typename value_traits<T>::type* = 0)
{
insert(key, make_value(std::forward<T>(val)));
}
/**
* Removes an element from the table.
*/
void erase(const std::string& key)
{
map_.erase(key);
}
private:
table()
{
// nothing
}
table(const table& obj) = delete;
table& operator=(const table& rhs) = delete;
std::vector<std::string> split(const std::string& value,
char separator) const
{
std::vector<std::string> result;
std::string::size_type p = 0;
std::string::size_type q;
while ((q = value.find(separator, p)) != std::string::npos)
{
result.emplace_back(value, p, q - p);
p = q + 1;
}
result.emplace_back(value, p);