Repository navigation
Expand file tree
/
Copy pathyaml.hpp
More file actions
1527 lines (1247 loc) · 40.4 KB
/
Copy pathyaml.hpp
File metadata and controls
1527 lines (1247 loc) · 40.4 KB
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
8000
div>
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
/*
* MIT License
*
* Copyright(c) 2019 Jimmie Bergmann
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files(the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions :
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
*/
/*
YAML documentation:
http://yaml.org/spec/1.0/index.html
https://www.codeproject.com/Articles/28720/YAML-Parser-in-C
*/
#ifndef MINI_YAML_HEADER
#define MINI_YAML_HEADER
#include <exception>
#include <memory>
#include <algorithm>
#include <ostream>
#include <sstream>
#include <map>
#include <list>
#include <cstring>
#include <string>
#include <vector>
/**
* @breif Namespace wrapping mini-yaml classes.
*
*/
namespace yaml
{
/**
* @breif Forward declarations.
*
*/
class node;
/**
* @breif Enumeration of exception types.
*
*/
enum class exception_type
{
internal_error, ///< Internal error.
parsing_error, ///< Invalid parsing data.
operation_error ///< User operation error.
};
/**
* @breif Enumeration of node types.
*
*/
enum class node_type
{
map,
null,
scalar,
sequence
};
/**
* @breif Enumeration of node data types.
*
*/
enum class node_data_type
{
boolean,
float32,
float64,
int32,
int64,
null,
string
};
using node_unique_ptr = std::unique_ptr<node>;
using node_map = std::map<std::string, node_unique_ptr>;
using node_list = std::list<node_unique_ptr>;
using documents = std::vector<node>;
/**
* @breif Private mini-yaml API namespace, containing helper classes and functions.
*
*/
namespace priv
{
/**
* @breif Traits struct, to retreive default values of different types.
*
*/
template<typename Type>
struct scalar_default_value
{
};
template<>
struct scalar_default_value<bool>
{
static const bool value;
};
template<>
struct scalar_default_value<float>
{
static const float value;
};
template<>
struct scalar_default_value<double>
{
static const double value;
};
template<>
struct scalar_default_value<int32_t>
{
static const int32_t value;
};
template<>
struct scalar_default_value<int64_t>
{
static const int64_t value;
};
template<>
struct scalar_default_value<std::string>
{
static const std::string value;
};
/**
* @breif Traits struct, to converting string to any data type.
* std::string are left untouched and bool converting checks for "true"/"yes"/"1".
*
*/
template<typename From, typename To>
struct data_converter
{
static To get(const From data);
static To get(const From data, const To);
};
template<>
struct data_converter<std::string, std::string>
{
static const std::string & get(const std::string & data);
static const std::string & get(const std::string & data, const std::string & defaultValue);
};
template<typename To>
struct data_converter<std::string, To>
{
static To get(const std::string & data);
static To get(const std::string & data, const To default_value);
};
template<typename From>
struct data_converter<From, std::string>
{
static std::string get(const From data);
static std::string get(const From data, const std::string & default_value);
};
template<>
struct data_converter<std::string, bool>
{
static bool get(const std::string & data);
static bool get(const std::string & data, const bool default_value);
};
template<>
struct data_converter<bool, std::string>
{
static std::string get(const bool data);
};
/**
* @breif Base class for node implementation.
* Inherited by null, map, scalar and sequence node implementation classes.
*
*/
class node_impl
{
public:
virtual ~node_impl();
virtual const node & at(const std::string & key) const;
virtual void clear();
virtual node_data_type data_type() const;
virtual size_t size() const;
};
class null_node_impl : public node_impl
{
public:
};
class map_node_impl : public node_impl
{
public:
const node & at(const std::string & key) const;
node_map::iterator begin();
node_map::const_iterator begin() const;
node_map::iterator end();
node_map::const_iterator end() const;
void clear();
bool erase(const std::string & key);
node_map::iterator erase(node_map::iterator & it);
node_map::const_iterator erase(node_map::const_iterator & it);
bool exists(const std::string & key) const;
node_map::iterator find(const std::string & key);
node_map::const_iterator find(const std::string & key) const;
node & find_or_insert(const std::string & key);
template<typename T>
node & insert(const std::string & key, const T value);
template<typename T>
std::pair<node_map::iterator, bool> insert(const std::pair<const std::string &, const T> & pair);
size_t size() const;
private:
node_map m_nodes;
};
class scalar_node_impl : public node_impl
{
public:
scalar_node_impl(const node_data_type data_type = node_data_type::null);
scalar_node_impl(const scalar_node_impl & scalar_node);
scalar_node_impl(const bool boolean);
scalar_node_impl(const float float32);
scalar_node_impl(const double float64);
scalar_node_impl(const int32_t int32);
scalar_node_impl(const int64_t int64);
scalar_node_impl(const std::string & string);
scalar_node_impl(const char * string);
~scalar_node_impl();
template<typename T>
T as() const;
template<typename T>
T as(const T & default_value) const;
void clear();
node_data_type data_type() const;
void set(const node_data_type data_type);
template<typename T>
void set(T value);
void set(const std::string &);
private:
union value
{
bool boolean;
float float32;
double float64;
int32_t int32;
int64_t int64;
std::string * string;
};
node_data_type m_data_type; ///< Data type of scalar node.
value m_value; ///< Union of the different data type values.
};
class sequence_node_impl : public node_impl
{
public:
node_list::iterator begin();
node_list::const_iterator begin() const;
node_list::iterator end();
node_list::const_iterator end() const;
void clear();
node_list::iterator erase(node_list::iterator & it);
node_list::const_iterator erase(node_list::const_iterator & it);
template<typename T>
node & push_back(const T value);
template<typename T>
node & push_front(const T value);
size_t size() const;
private:
node_list m_nodes;
};
class iterator_impl
{
public:
virtual ~iterator_impl();
};
class const_iterator_impl
{
public:
virtual ~const_iterator_impl();
};
class map_iterator_impl : public iterator_impl
{
public:
map_iterator_impl(node_map::iterator it);
node_map::iterator it;
};
class map_const_iterator_impl : public const_iterator_impl
{
public:
map_const_iterator_impl(node_map::const_iterator it);
node_map::const_iterator it;
};
class sequence_iterator_impl : public iterator_impl
{
public:
sequence_iterator_impl(node_list::iterator it);
node_list::iterator it;
};
class sequence_const_iterator_impl : public const_iterator_impl
{
public:
sequence_const_iterator_impl(node_list::const_iterator it);
node_list::const_iterator it;
};
/**
* @breif Helper class for writing strings.
*
*/
template<typename Writer>
class string_writer
{
public:
string_writer(Writer & writer);
void write(const std::string & out);
void write(const char * out);
private:
Writer & m_out;
};
template<>
class string_writer<std::string>
{
public:
string_writer(std::string & out);
void write(const std::string & out);
void write(const char * out);
private:
std::string & m_out;
};
// Helper functions.
node_impl * create_impl(const node_type type);
}
/**
* @breif Exception class.
*
*/
class exception : public std::runtime_error
{
public:
/**
* @breif Constructor.
*
* @param message Exception message.
* @param type Type of exception.
*
*/
exception(const std::string & message, const exception_type type);
/**
* @breif Get type of exception.
*
*/
exception_type type() const;
private:
exception_type m_type; ///< Type of exception.
};
/**
* @breif Error exception class.
*
* @see Exception
*
*/
class error : public exception
{
public:
/**
* @breif Constructor.
*
* @param message Error message.
*
*/
error(const std::string & message, const exception_type type);
};
/**
* @breif Internal error exception class.
*
* @see Exception
*
*/
class internal_error : public error
{
public:
/**
* @breif Constructor.
*
* @param message Internal error message.
*
*/
internal_error(const std::string & message);
};
/**
* @breif Parsing error exception class.
*
* @see Exception
*
*/
class parsing_error : public error
{
public:
/**
* @breif Constructor.
*
* @param message Parsing error message.
*
*/
parsing_error(const std::string & message);
};
/**
* @breif Operation error exception class.
*
* @see Exception
*
*/
class operation_error : public error
{
public:
/**
* @breif Constructor.
*
* @param message Exception message.
*
*/
operation_error(const std::string & message);
};
/**
* @breif Node class.
*
*/
class node
{
public:
/**
* @breif Enumeration of iterator types.
*
*/
enum class iterator_type
{
map,
null,
sequence
};
/**
* @breif Iterator class.
*
*/
class iterator
{
public:
/**
* @breif Default constructor.
*
*/
iterator();
/**
* @breif Copy constructor.
*
*/
iterator(const iterator & it);
/**
* @breif Constructing iterator as a map iterator.
*
*/
iterator(node_map::iterator it);
/**
* @breif Constructing iterator as a sequence iterator.
*
*/
iterator(node_list::iterator it);
/**
* @breif Destructor.
*
*/
~iterator();
/**
* @breif Get type of node iterator.
*
*/
iterator_type type() const;
/**
* @breif Get node of iterator.
* First pair item is the key of map value, empty if type is sequence.
*
*/
std::pair<const std::string &, node &> operator *();
/**
* @breif Assignment operator.
*
*/
iterator & operator = (const iterator & it);
/**
* @breif Pre-increment operator.
*
*/
iterator operator ++ ();
/**
* @breif Post-increment operator.
*
*/
iterator operator ++ (int);
/**
* @breif pre-decrement operator.
*
*/
iterator operator -- ();
/**
* @breif Post-decrement operator.
*
*/
iterator operator -- (int);
/**
* @breif Check if iterator is equal to other iterator.
*
*/
bool operator == (const iterator & it) const;
/**
* @breif Check if iterator is not equal to other iterator.
*
*/
bool operator != (const iterator & it) const;
private:
iterator_type m_type; ///< Type of iterator.
priv::iterator_impl * m_impl; ///< Pointer of implementation class.
friend class node;
};
/**
* @breif Constant iterator class.
*
*/
class const_iterator
{
public:
/**
* @breif Default constructor.
*
*/
const_iterator();
/**
* @breif Copy constructor.
*
*/
const_iterator(const const_iterator & it);
/**
* @breif Constructing iterator as a map iterator.
*
*/
const_iterator(node_map::const_iterator it);
/**
* @breif Constructing iterator as a sequence iterator.
*
*/
const_iterator(node_list::const_iterator it);
/**
* @breif Destructor.
*
*/
~const_iterator();
/**
* @breif Get type of node iterator.
*
*/
iterator_type type() const;
/**
* @breif Get node of iterator.
* First pair item is the key of map value, empty if type is sequence.
*
*/
std::pair<const std::string &, const node &> operator *();
/**
* @breif Assignment operator.
*
*/
const_iterator & operator = (const const_iterator & it);
/**
* @breif Pre-increment operator.
*
*/
const_iterator operator ++ ();
/**
* @breif Post-increment operator.
*
*/
10BC8
const_iterator operator ++ (int);
/**
* @breif pre-decrement operator.
*
*/
const_iterator operator -- ();
/**
* @breif Post-decrement operator.
*
*/
const_iterator operator -- (int);
/**
* @breif Check if iterator is equal to other iterator.
*
*/
bool operator == (const const_iterator & it) const;
/**
* @breif Check if iterator is not equal to other iterator.
*
*/
bool operator != (const const_iterator & it) const;
private:
iterator_type m_type; ///< Type of iterator.
priv::const_iterator_impl * m_impl; ///< Pointer of implementation class.
friend class node;
};
static const node null_node;
/**
* @breif Default constructor.
*
*/
node();
/**
* @breif Constructs node of given type.
* Default data type value of given type is set.
*
*/
node(const node_type type);
/**
* @breif Constructs scalar node.
* Default value of given node data type is set.
*
*/
node(const node_data_type data_type);
/**
* @breif Assignment constructor.
* Turns the node into a scalar node and assignes by given value.
*
*/
template<typename T>
node(const T value);
/**
* @breif Destructor.
*
*/
~node();
/**
* @breif Get node as given template type.
*
*/
template<typename T>
T as() const;
/**
* @breif Get node as given template type.
*
* @param default_value Returned if conversion between current data type and T fails.
*
*/
template<typename T>
T as(const T & default_value) const;
/**
* @breif Get child node by key value.
*
* @return Reference to node if found, else clear null node is returned.
*
*/
//node & at(const std::string & key);
const node & at(const std::string & key) const;
/**
* @breif Get start iterator.
*
*/
iterator begin();
const_iterator begin() const;
/**
* @breif Get end iterator.
*
*/
iterator end();
const_iterator end() const;
/**
* @breif Clears content of node.
* Scalar's value is set to default value,
* map and sequence items are cleared, resulting in size() = 0.
*
*/
void clear();
/**
* @breif Get data type of scalar.
*
* @return Data type of scalar, will return node_data_type::null if type() != node_type::scalar.
*
*/
node_data_type data_type() const;
/**
* @breif Erase map item.
* No action node type != map.
*
* @param key Only available if node type is map.
* @param it iterator of map or sequence node.
*
* @return true if key is found and erased, else false.
*
*/
bool erase(const std::string & key);
iterator erase(iterator it);
const_iterator erase(const_iterator it);
/*
* @brief Find node item by key.
*
*/
iterator find(const std::string & key);
const_iterator find(const std::string & key) const;
/**
* @breif Checks if map item exists, by key value.
*
*/
bool exists(const std::string & key) const;
/**
* @breif Inserts or updates map item.
* Converts node to map type if needed.
*
* @tparam T = scalar value / node_type / node_data_type.
*
* @return Pair of iterator and bool. pair::first is an iterator of the newly inserted or already existing node.
* pair::second is a boolean, set to true if a new node was inserted, false if an already existing node were updated.
*
*/
template<typename T>
node & insert(const std::string & key, const T value = node_type::null);
template<typename T>
std::pair<iterator, bool> insert(const std::pair<const std::string &, const T> & pair);
/**
* @breif Pushing new sequence item to back.
* Converts node to sequence type if needed.
*
* @tparam T = scalar value / node_type / node_data_type.
*
* @return Reference of pushed node.
*
*/
template<typename T>
node & push_back(const T value);
/**
* @breif Pushing new sequence item to front.
* Converts node to sequence type if needed.
*
* @tparam T = scalar value / node_type / node_data_type.
*
* @return Reference of pushed node.
*
*/
template<typename T>
node & push_front(const T value);
/**
* @breif Methods for checking node type.
*
*/
bool is_map() const;
bool is_null() const;
bool is_scalar() const;
bool is_sequence() const;
node_type type() const;
/**
* @breif Get item count of map or sequence.
*
* @return Number of sequence or map entries in node. 0 if node_type != map or sequence.
*
*/
size_t size() const;
/**
* @breif Converts node to given type if needed.
* Any existing node data is lost and the node's data type is sett to null.
*
*/
node & operator = (const node_type type);
/**
* @breif Assigns default value of given node data type.
* This is one way to explicitly assign null values.
* Converts node to scalar type if needed.
*
*/
node & operator = (const node_data_type data_type);
/**
* @breif Assigns scalar value.
* Converts node to scalar type if needed.
*
*/
template<typename T>
node & operator = (const T value);
/**
* @breif Get existing or insert new map item.
* Converts node to map type if needed.
*
* @param key Map key. Creates a new node if key is unknown.
*
*/
You can’t perform that action at this time.