-
Notifications
You must be signed in to change notification settings - Fork 10
/
rabbit.hpp
1478 lines (1212 loc) · 49.8 KB
/
rabbit.hpp
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
// The MIT License (MIT)
//
// Copyright (c) 2013-2014 mashiro
//
// 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.
#ifndef RABBIT_HPP_INCLUDED
#define RABBIT_HPP_INCLUDED
#ifdef __clang__
#pragma clang diagnostic ignored "-Wtautological-constant-out-of-range-compare"
#endif
#ifndef RABBIT_NAMESPACE
#define RABBIT_NAMESPACE rabbit
#endif
#include <string>
#include <stdexcept>
#include <iterator>
#include <algorithm>
#include <sstream>
#include <iosfwd>
#include <rapidjson/document.h>
#include <rapidjson/writer.h>
#include <rapidjson/stringbuffer.h>
#include <rapidjson/error/en.h>
namespace RABBIT_NAMESPACE {
#define RABBIT_TAG_DEF(name, id) \
struct name \
{ \
static const rapidjson::Type native_value = id; \
static const int value = id; \
}; \
/**/
RABBIT_TAG_DEF(null_tag, rapidjson::kNullType)
RABBIT_TAG_DEF(false_tag, rapidjson::kFalseType)
RABBIT_TAG_DEF(true_tag, rapidjson::kTrueType)
RABBIT_TAG_DEF(object_tag, rapidjson::kObjectType)
RABBIT_TAG_DEF(array_tag, rapidjson::kArrayType)
RABBIT_TAG_DEF(string_tag, rapidjson::kStringType)
RABBIT_TAG_DEF(number_tag, rapidjson::kNumberType)
#undef RABBIT_TAG_DEF
class type_mismatch : public std::runtime_error
{
public:
type_mismatch(const std::string& msg)
: std::runtime_error(msg)
{}
};
typedef rapidjson::ParseErrorCode parse_error_code;
class parse_error : public std::runtime_error
{
private:
parse_error_code code_;
public:
parse_error(parse_error_code code)
: std::runtime_error(rapidjson::GetParseError_En(code))
, code_(code)
{}
parse_error_code code() const { return code_; }
};
// fwd
template <typename Traits> class basic_value_ref;
template <typename Traits, typename DefaultTag> class basic_value;
template <typename Traits> class basic_object;
template <typename Traits> class basic_array;
namespace details {
template <bool C, typename T = void>
struct enable_if_c
{
typedef T type;
};
template <typename T>
struct enable_if_c<false, T>
{};
template <typename Cond, typename T = void>
struct enable_if : enable_if_c<Cond::value, T>
{};
template <typename Cond, typename T = void>
struct disable_if : enable_if_c<!Cond::value, T>
{};
template <bool C>
struct bool_
{
static const bool value = C;
};
typedef bool_<true> true_;
typedef bool_<false> false_;
template <typename T> struct remove_reference { typedef T type; };
template <typename T> struct remove_reference<T&> { typedef T type; };
template <typename T> struct remove_const { typedef T type; };
template <typename T> struct remove_const<const T> { typedef T type; };
template <typename Char, typename Traits = std::char_traits<Char> >
class basic_string_ref
{
public:
typedef Char value_type;
typedef std::size_t size_type;
private:
const value_type* data_;
size_type length_;
public:
basic_string_ref()
: data_(0)
, length_(0)
{}
basic_string_ref(const basic_string_ref& other)
: data_(other.data_)
, length_(other.length_)
{}
basic_string_ref(const value_type* str)
: data_(str)
, length_(Traits::length(str))
{}
basic_string_ref(const value_type* str, size_type length)
: data_(str)
, length_(length)
{}
template <typename Allocator>
basic_string_ref(const std::basic_string<value_type, Allocator>& other)
: data_(other.data())
, length_(other.length())
{}
size_type size() const { return length_; }
size_type length() const { return length_; }
size_type max_size() const { return length_; }
bool empty() const { return length_ == 0; }
const value_type* data() const { return data_; }
};
// type traits
template <typename T> struct is_tag : false_ {};
template <> struct is_tag<null_tag> : true_ {};
template <> struct is_tag<false_tag> : true_ {};
template <> struct is_tag<true_tag> : true_ {};
template <> struct is_tag<object_tag> : true_ {};
template <> struct is_tag<array_tag> : true_ {};
template <> struct is_tag<string_tag> : true_ {};
template <> struct is_tag<number_tag> : true_ {};
template <typename T> struct is_null : false_ {};
template <> struct is_null<null_tag> : true_ {};
template <typename T> struct is_false : false_ {};
template <> struct is_false<false_tag> : true_ {};
template <typename T> struct is_true : false_ {};
template <> struct is_true<true_tag> : true_ {};
template <typename T> struct is_object : false_ {};
template <> struct is_object<object_tag> : true_ {};
template <typename Traits> struct is_object< basic_object<Traits> > : true_ {};
template <typename T> struct is_array : false_ {};
template <> struct is_array<array_tag> : true_ {};
template <typename Traits> struct is_array< basic_array<Traits> > : true_ {};
template <typename T> struct is_string : false_ {};
template <> struct is_string<string_tag> : true_ {};
template <typename Char> struct is_string< std::basic_string<Char> > : true_ {};
template <typename Char, typename Traits> struct is_string< basic_string_ref<Char, Traits> > : true_ {};
template <typename T> struct is_cstr_ptr : false_ {};
template <> struct is_cstr_ptr< char * > : true_ {};
template <> struct is_cstr_ptr< const char * > : true_ {};
template <> struct is_cstr_ptr< wchar_t * > : true_ {};
template <> struct is_cstr_ptr< const wchar_t * > : true_ {};
template <typename T> struct is_number : false_ {};
template <> struct is_number<number_tag> : true_ {};
template <typename T> struct is_bool : false_ {};
template <> struct is_bool<bool> : true_ {};
template <typename T> struct is_int : false_ {};
template <> struct is_int<int> : true_ {};
template <typename T> struct is_uint : false_ {};
template <> struct is_uint<unsigned> : true_ {};
template <typename T> struct is_int64 : false_ {};
template <> struct is_int64<int64_t> : true_ {};
template <typename T> struct is_uint64 : false_ {};
template <> struct is_uint64<uint64_t> : true_ {};
template <typename T> struct is_double : false_ {};
template <> struct is_double<double> : true_ {};
template <typename T> struct is_value_ref : false_ {};
template <typename Traits> struct is_value_ref< basic_value_ref<Traits> > : true_ {};
template <typename Traits, typename DefaultTag> struct is_value_ref< basic_value<Traits, DefaultTag> > : true_ {};
template <typename Traits> struct is_value_ref< basic_object<Traits> > : true_ {};
template <typename Traits> struct is_value_ref< basic_array<Traits> > : true_ {};
// type name
template <typename T> const char* type_name(typename enable_if< is_null<T> >::type* = 0) { return "null"; }
template <typename T> const char* type_name(typename enable_if< is_false<T> >::type* = 0) { return "false"; }
template <typename T> const char* type_name(typename enable_if< is_true<T> >::type* = 0) { return "true"; }
template <typename T> const char* type_name(typename enable_if< is_object<T> >::type* = 0) { return "object"; }
template <typename T> const char* type_name(typename enable_if< is_array<T> >::type* = 0) { return "array"; }
template <typename T> const char* type_name(typename enable_if< is_string<T> >::type* = 0) { return "string"; }
template <typename T> const char* type_name(typename enable_if< is_number<T> >::type* = 0) { return "number"; }
template <typename T> const char* type_name(typename enable_if< is_bool<T> >::type* = 0) { return "bool"; }
template <typename T> const char* type_name(typename enable_if< is_int<T> >::type* = 0) { return "int"; }
template <typename T> const char* type_name(typename enable_if< is_uint<T> >::type* = 0) { return "uint"; }
template <typename T> const char* type_name(typename enable_if< is_int64<T> >::type* = 0) { return "int64"; }
template <typename T> const char* type_name(typename enable_if< is_uint64<T> >::type* = 0) { return "uint64"; }
template <typename T> const char* type_name(typename enable_if< is_double<T> >::type* = 0) { return "double"; }
template <typename T> const char* type_name(typename enable_if< is_value_ref<T> >::type* = 0) { return "value_ref"; }
template <typename PseudoReference>
struct operator_arrow_proxy
{
mutable typename remove_const<PseudoReference>::type value_;
operator_arrow_proxy(const PseudoReference& value) : value_(value) {}
PseudoReference* operator->() const { return &value_; }
};
template <typename T>
struct operator_arrow_proxy<T&>
{
T& value_;
operator_arrow_proxy(T& value) : value_(value) {}
T* operator->() const { return &value_; }
};
template <typename Function, typename Iterator>
class transform_iterator
{
typedef std::iterator_traits<Iterator> traits_type;
public:
typedef transform_iterator this_type;
typedef Function function_type;
typedef Iterator iterator_type;
typedef typename traits_type::iterator_category iterator_category;
typedef typename traits_type::difference_type difference_type;
typedef typename Function::result_type result_type;
typedef typename remove_reference<result_type>::type value_type;
typedef operator_arrow_proxy<result_type> operator_arrow_proxy_type;
typedef operator_arrow_proxy_type pointer;
typedef result_type reference;
private:
iterator_type it_;
function_type func_;
public:
transform_iterator()
: it_()
, func_()
{}
explicit transform_iterator(const iterator_type& it)
: it_(it)
, func_()
{}
transform_iterator(const iterator_type& it, const function_type& func)
: it_(it)
, func_(func)
{}
template <typename OtherFunction, typename OtherIterator>
transform_iterator(const transform_iterator<OtherFunction, OtherIterator>& other)
: it_(other.base())
, func_(other.functor())
{}
iterator_type& base() { return it_; }
const iterator_type& base() const { return it_; }
function_type& functor() { return func_; }
const function_type& functor() const { return func_; }
result_type dereference() const { return func_(*it_); }
result_type operator*() const { return dereference(); }
operator_arrow_proxy_type operator->() const { return operator_arrow_proxy_type(dereference()); }
this_type& operator++() { ++it_; return *this; }
this_type operator++(int) { return this_type(it_++, func_); }
this_type& operator--() { --it_; return *this; }
this_type operator--(int) { return this_type(it_--, func_); }
this_type operator+(difference_type n) const { return this_type(it_ + n, func_); }
this_type& operator+=(difference_type n) { it_ += n; return *this; }
this_type operator-(difference_type n) const { return this_type(it_ - n, func_); }
this_type& operator-=(difference_type n) { it_ -= n; return *this; }
result_type operator[](difference_type n) const { return func_(it_[n]); }
template <typename OtherFunction, typename OtherIterator>
bool operator==(const transform_iterator<OtherFunction, OtherIterator>& other) const { return base() == other.base(); }
template <typename OtherFunction, typename OtherIterator>
bool operator!=(const transform_iterator<OtherFunction, OtherIterator>& other) const { return base() != other.base(); }
template <typename OtherFunction, typename OtherIterator>
bool operator<(const transform_iterator<OtherFunction, OtherIterator>& other) const { return base() < other.base(); }
template <typename OtherFunction, typename OtherIterator>
bool operator>(const transform_iterator<OtherFunction, OtherIterator>& other) const { return base() > other.base(); }
template <typename OtherFunction, typename OtherIterator>
bool operator<=(const transform_iterator<OtherFunction, OtherIterator>& other) const { return base() <= other.base(); }
template <typename OtherFunction, typename OtherIterator>
bool operator>=(const transform_iterator<OtherFunction, OtherIterator>& other) const { return base() >= other.base(); }
};
template <typename Function, typename Iterator>
transform_iterator<Function, Iterator> make_transform_iterator(Iterator it, Function func = Function())
{
return transform_iterator<Function, Iterator>(it, func);
}
template <typename Member, typename ValueRef>
class member_wrapper
{
public:
typedef Member wrapped_type;
typedef ValueRef value_ref_type;
typedef typename ValueRef::string_type string_type;
typedef typename ValueRef::allocator_type allocator_type;
class proxy
{
wrapped_type& member_;
allocator_type* alloc_;
public:
proxy(wrapped_type& member, allocator_type* alloc)
: member_(member)
, alloc_(alloc)
{}
string_type name() const { return value_ref_type(&(member_.name), alloc_).as_string(); }
value_ref_type value() const { return value_ref_type(&(member_.value), alloc_); }
};
private:
allocator_type* alloc_;
public:
member_wrapper(allocator_type* alloc)
: alloc_(alloc)
{}
template <typename OtherMember, typename OtherValueRef>
member_wrapper(const member_wrapper<OtherMember, OtherValueRef>& other)
: alloc_(other.get_allocator_pointer())
{}
typedef proxy result_type;
result_type operator()(wrapped_type& member) const
{
return result_type(member, alloc_);
}
allocator_type* get_allocator_pointer() const { return alloc_; }
};
template <typename Value, typename ValueRef>
class value_wrapper
{
public:
typedef Value wrapped_type;
typedef ValueRef value_ref_type;
typedef typename ValueRef::string_type string_type;
typedef typename ValueRef::allocator_type allocator_type;
private:
allocator_type* alloc_;
public:
value_wrapper(allocator_type* alloc)
: alloc_(alloc)
{}
template <typename OtherValue, typename OtherValueRef>
value_wrapper(const value_wrapper<OtherValue, OtherValueRef>& other)
: alloc_(other.get_allocator_pointer())
{}
typedef value_ref_type result_type;
result_type operator()(wrapped_type& value) const
{
return result_type(&value, alloc_);
}
allocator_type* get_allocator_pointer() const { return alloc_; }
};
template <typename Encoding> struct value_ref_traits;
template <typename Encoding> struct const_value_ref_traits;
template <typename Encoding>
struct value_ref_traits
{
typedef Encoding encoding_type;
typedef rapidjson::Type native_type;
typedef rapidjson::GenericDocument<Encoding> native_document_type;
typedef rapidjson::GenericValue<Encoding> native_value_type;
typedef typename native_document_type::AllocatorType native_allocator_type;
typedef const_value_ref_traits<Encoding> const_traits;
template <typename ValueRef, typename Tag>
static void set(ValueRef& ref, Tag tag = Tag())
{
ref.set(tag);
}
};
template <typename Encoding>
struct const_value_ref_traits
{
typedef Encoding encoding_type;
typedef const rapidjson::Type native_type;
typedef const rapidjson::GenericDocument<Encoding> native_document_type;
typedef const rapidjson::GenericValue<Encoding> native_value_type;
typedef const typename native_document_type::AllocatorType native_allocator_type;
typedef const_value_ref_traits<Encoding> const_traits;
template <typename ValueRef, typename Tag>
static void set(const ValueRef& ref, Tag tag = Tag())
{}
};
template <typename T>
class scoped_ptr
{
private:
T* p_;
private:
scoped_ptr(const scoped_ptr& other);
scoped_ptr& operator=(const scoped_ptr& other);
public:
explicit scoped_ptr(T* p = 0)
: p_(p)
{}
~scoped_ptr()
{
delete p_;
}
T* operator->() { return p_; }
const T* operator->() const { return p_; }
T& operator*() { return *p_; }
const T& operator*() const { return *p_; }
T* get() { return p_; }
const T* get() const { return p_; }
void swap(scoped_ptr& other) throw()
{
std::swap(p_, other.p_);
}
};
} // details
template <typename Traits>
class basic_value_ref
{
public:
typedef Traits traits;
typedef typename Traits::const_traits const_traits;
typedef typename traits::encoding_type encoding_type;
typedef typename traits::native_type native_type;
typedef typename traits::native_document_type native_document_type;
typedef typename traits::native_value_type native_value_type;
typedef typename traits::native_allocator_type native_allocator_type;
typedef basic_value_ref<traits> value_ref_type;
typedef const basic_value_ref<const_traits> const_value_ref_type;
typedef typename encoding_type::Ch char_type;
typedef std::basic_string<char_type> string_type;
typedef details::basic_string_ref<char_type> string_ref_type;
typedef native_allocator_type allocator_type;
private:
typedef details::member_wrapper< typename native_value_type::Member, value_ref_type> member_wrapper_type;
typedef details::member_wrapper<const typename native_value_type::Member, const_value_ref_type> const_member_wrapper_type;
typedef details::value_wrapper< native_value_type, value_ref_type> value_wrapper_type;
typedef details::value_wrapper<const native_value_type, const_value_ref_type> const_value_wrapper_type;
public:
typedef details::transform_iterator< member_wrapper_type, typename native_value_type::MemberIterator> member_iterator;
typedef details::transform_iterator<const_member_wrapper_type, typename native_value_type::ConstMemberIterator> const_member_iterator;
typedef details::transform_iterator< value_wrapper_type, typename native_value_type::ValueIterator> value_iterator;
typedef details::transform_iterator<const_value_wrapper_type, typename native_value_type::ConstValueIterator> const_value_iterator;
private:
native_value_type* value_;
allocator_type* alloc_;
public:
basic_value_ref(native_value_type* value = 0, allocator_type* alloc = 0)
: value_(value)
, alloc_(alloc)
{}
template <typename OtherTraits>
basic_value_ref(const basic_value_ref<OtherTraits>& other)
: value_(other.get_native_value_pointer())
, alloc_(other.get_allocator_pointer())
{}
native_value_type* get_native_value_pointer() const { return value_; }
allocator_type* get_allocator_pointer() const { return alloc_; }
allocator_type& get_allocator() const { return *alloc_; }
void set(null_tag) { value_->SetNull(); }
void set(object_tag) { value_->SetObject(); }
void set(array_tag) { value_->SetArray(); }
void set(bool value) { value_->SetBool(value); }
void set(int value) { value_->SetInt(value); }
void set(unsigned value) { value_->SetUint(value); }
void set(int64_t value) { value_->SetInt64(value); }
void set(uint64_t value) { value_->SetUint64(value); }
void set(double value) { value_->SetDouble(value); }
void set(const char_type* value) { value_->SetString(value, *alloc_); }
void set(const string_type& value) { value_->SetString(value.data(), value.length(), *alloc_); }
template <typename T>
void set(const T& value, typename details::enable_if< details::is_value_ref<T> >::type* = 0)
{
if (value.is_null()) set(null_tag());
else if (value.is_bool()) set(value.as_bool());
else if (value.is_int()) set(value.as_int());
else if (value.is_uint()) set(value.as_uint());
else if (value.is_int64()) set(value.as_int64());
else if (value.is_uint64()) set(value.as_uint64());
else if (value.is_double()) set(value.as_double());
else if (value.is_string()) set(value.as_string());
else if (value.is_array()) throw std::runtime_error("can not assign array directly. please use insert");
else if (value.is_object()) throw std::runtime_error("can not assign object directly. please use insert");
}
template <typename OtherTraits>
void deep_copy(const basic_value_ref<OtherTraits>& other)
{
value_->CopyFrom(*other.get_native_value_pointer(), *alloc_);
}
template <typename T>
value_ref_type& operator=(const T& value)
{
set(value);
return *this;
}
template <typename OtherTraits>
bool operator==(const basic_value_ref<OtherTraits>& other) const
{
if (is_null() && other.is_null()) return true;
if (is_bool() && other.is_bool() && as_bool() == other.as_bool()) return true;
if (is_int() && other.is_int() && as_int() == other.as_int()) return true;
if (is_uint() && other.is_uint() && as_uint() == other.as_uint()) return true;
if (is_int64() && other.is_int64() && as_int64() == other.as_int64()) return true;
if (is_uint64() && other.is_uint64() && as_uint64() == other.as_uint64()) return true;
if (is_double() && other.is_double() && as_double() == other.as_double()) return true;
if (is_string() && other.is_string() && as_string() == other.as_string()) return true;
return false;
}
template <typename OtherTraits>
bool operator!=(const basic_value_ref<OtherTraits>& other) const
{
return !(*this == other);
}
int which() const
{
return static_cast<int>(value_->GetType());
}
#define RABBIT_IS_DEF(name, base_name) \
template <typename T> \
bool is(typename details::enable_if< details::is_##name<T> >::type* = 0) const \
{ \
return value_->Is##base_name(); \
} \
bool is_##name() const \
{ \
return value_->Is##base_name(); \
} \
/**/
RABBIT_IS_DEF(null, Null)
RABBIT_IS_DEF(false, False)
RABBIT_IS_DEF(true, True)
RABBIT_IS_DEF(object, Object)
RABBIT_IS_DEF(array, Array)
RABBIT_IS_DEF(number, Number)
RABBIT_IS_DEF(bool, Bool)
RABBIT_IS_DEF(int, Int)
RABBIT_IS_DEF(uint, Uint)
RABBIT_IS_DEF(int64, Int64)
RABBIT_IS_DEF(uint64, Uint64)
RABBIT_IS_DEF(double, Double)
RABBIT_IS_DEF(string, String)
#undef RABBIT_IS_DEF
#define RABBIT_AS_DEF(result_type, name, base_name) \
template <typename T> \
T as(typename details::enable_if< details::is_##name<T> >::type* = 0) const \
{ \
type_check<T>(); \
return value_->Get##base_name(); \
} \
result_type as_##name() const \
{ \
type_check<result_type>(); \
return value_->Get##base_name(); \
} \
/**/
RABBIT_AS_DEF(bool, bool, Bool)
RABBIT_AS_DEF(int, int, Int)
RABBIT_AS_DEF(unsigned, uint, Uint)
RABBIT_AS_DEF(int64_t, int64, Int64)
RABBIT_AS_DEF(uint64_t, uint64, Uint64)
RABBIT_AS_DEF(string_type, string, String)
#undef RABBIT_AS_DEF
double as_double() const
{
if(!is_number()){
std::stringstream ss;
ss << "value is not ";
ss << details::type_name<double>();
ss << " (which is " << which() << ")";
throw type_mismatch(ss.str());
}
return value_->GetDouble();
}
template <typename T>
T as(typename details::enable_if< details::is_double<T> >::type* = 0) const
{
return as_double();
}
private:
struct as_t
{
const value_ref_type& ref_;
as_t(const value_ref_type& ref) : ref_(ref) {}
template <typename Result>
operator Result() const { return ref_.as<Result>(); }
};
public:
as_t as() const { return as_t(*this); }
bool has(const string_ref_type& name) const
{
type_check<object_tag>();
return value_->HasMember(name.data());
}
template <typename T>
void insert(const string_ref_type& name, const T& value, const bool copy_name_string = true, typename details::disable_if< details::is_value_ref<T> >::type* = 0, typename details::enable_if< details::is_string<T> >::type * = 0)
{
type_check<object_tag>();
native_value_type v(value.data(), value.length(), *alloc_);
if(copy_name_string){
native_value_type copied_name(name.data(), name.length(), *alloc_);
value_->AddMember(copied_name, v, *alloc_);
}else{
value_->AddMember(rapidjson::StringRef(name.data(), name.length()), v, *alloc_);
}
}
template <typename T>
void insert(const string_ref_type& name, const T& value, const bool copy_name_string = true, typename details::disable_if< details::is_value_ref<T> >::type* = 0, typename details::enable_if< details::is_cstr_ptr<T> >::type * = 0)
{
type_check<object_tag>();
native_value_type v(value, *alloc_);
if(copy_name_string){
native_value_type copied_name(name.data(), name.length(), *alloc_);
value_->AddMember(copied_name, v, *alloc_);
}else{
value_->AddMember(rapidjson::StringRef(name.data(), name.length()), v, *alloc_);
}
}
template <typename T>
void insert(const string_ref_type& name, const T& value, const bool copy_name_string = true, typename details::disable_if< details::is_value_ref<T> >::type* = 0, typename details::disable_if< details::is_string<T> >::type * = 0, typename details::disable_if< details::is_cstr_ptr<T> >::type * = 0)
{
type_check<object_tag>();
native_value_type v(value);
if(copy_name_string){
native_value_type copied_name(name.data(), name.length(), *alloc_);
value_->AddMember(copied_name, v, *alloc_);
}else{
value_->AddMember(rapidjson::StringRef(name.data(), name.length()), v, *alloc_);
}
}
template <typename T>
void insert(const string_ref_type& name, const T& value, const bool copy_name_string = true, typename details::enable_if< details::is_value_ref<T> >::type* = 0)
{
type_check<object_tag>();
if(copy_name_string){
native_value_type copied_name(name.data(), name.length(), *alloc_);
value_->AddMember(copied_name, *value.get_native_value_pointer(), *alloc_);
}else{
value_->AddMember(rapidjson::StringRef(name.data(), name.length()), *value.get_native_value_pointer(), *alloc_);
}
}
bool erase(const string_ref_type& name)
{
type_check<object_tag>();
return value_->RemoveMember(name.data());
}
void reserve(const size_t reserve_size){
type_check<array_tag>();
value_->Reserve(reserve_size, *alloc_);
}
const_member_iterator erase(const const_member_iterator& itr){
type_check<object_tag>();
return details::make_transform_iterator(value_->EraseMember(itr.base()), const_member_wrapper_type(alloc_));
}
member_iterator erase(const member_iterator& itr){
type_check<object_tag>();
return details::make_transform_iterator(value_->EraseMember(itr.base()), member_wrapper_type(alloc_));
}
const_value_iterator erase(const const_value_iterator& itr){
type_check<array_tag>();
return details::make_transform_iterator(value_->Erase(itr.base()), const_value_wrapper_type(alloc_));
}
const_value_iterator erase(const const_value_iterator& beginItr, const const_value_iterator& endItr){
type_check<array_tag>();
return details::make_transform_iterator(value_->Erase(beginItr.base(), endItr.base()), const_value_wrapper_type(alloc_));
}
value_iterator erase(const value_iterator& itr){
type_check<array_tag>();
return details::make_transform_iterator(value_->Erase(itr.base()), value_wrapper_type(alloc_));
}
value_iterator erase(const value_iterator& beginItr, const value_iterator& endItr){
type_check<array_tag>();
return details::make_transform_iterator(value_->Erase(beginItr.base(), endItr.base()), value_wrapper_type(alloc_));
}
value_ref_type at(const string_ref_type& name)
{
type_check<object_tag>();
if (!has(name))
{
native_value_type null;
native_value_type copied_name(name.data(), name.length(), *alloc_);
value_->AddMember(copied_name, null, *alloc_);
}
return value_ref_type(&((*value_)[name.data()]), alloc_);
}
const_value_ref_type at(const string_ref_type& name) const
{
type_check<object_tag>();
if (!has(name))
throw std::out_of_range("'" + string_type(name.data(), name.size()) + "' not found");
return const_value_ref_type(&((*value_)[name.data()]), alloc_);
}
value_ref_type operator[](const string_ref_type& name) { return at(name); }
const_value_ref_type operator[](const string_ref_type& name) const { return at(name); }
member_iterator member_begin()
{
type_check<object_tag>();
return details::make_transform_iterator(value_->MemberBegin(), member_wrapper_type(alloc_));
}
member_iterator member_end()
{
type_check<object_tag>();
return details::make_transform_iterator(value_->MemberEnd(), member_wrapper_type(alloc_));
}
const_member_iterator member_begin() const
{
type_check<object_tag>();
return details::make_transform_iterator(value_->MemberBegin(), const_member_wrapper_type(alloc_));
}
const_member_iterator member_end() const
{
type_check<object_tag>();
return details::make_transform_iterator(value_->MemberEnd(), const_member_wrapper_type(alloc_));
}
const_member_iterator member_cbegin() const { return member_begin(); }
const_member_iterator member_cend() const { return member_end(); }
std::size_t size() const
{
if (is_object()) {
return value_->MemberCount();
} else if (is_array()) {
return value_->Size();
}
throw type_mismatch("cannot take size of non-object/array");
}
std::size_t capacity() const
{
type_check<array_tag>();
return value_->Capacity();
}
bool empty() const
{
type_check<array_tag>();
return value_->Empty();
}
value_ref_type at(std::size_t index)
{
type_check<array_tag>();
range_check(index);
return value_ref_type(&((*value_)[index]), alloc_);
}
const_value_ref_type at(std::size_t index) const
{
type_check<array_tag>();
range_check(index);
return const_value_ref_type(&((*value_)[index]), alloc_);
}
value_ref_type operator[](std::size_t index) { return at(index); }
const_value_ref_type operator[](std::size_t index) const { return at(index); }
template <typename T>
void push_back(const T& value, typename details::disable_if< details::is_value_ref<T> >::type* = 0, typename details::enable_if< details::is_string<T> >::type * = 0)
{
type_check<array_tag>();
native_value_type v(value.data(), value.length(), *alloc_);
value_->PushBack(v, *alloc_);
}
template <typename T>
void push_back(const T& value, typename details::disable_if< details::is_value_ref<T> >::type* = 0, typename details::enable_if< details::is_cstr_ptr<T> >::type * = 0)
{
type_check<array_tag>();
native_value_type v(value, *alloc_);
value_->PushBack(v, *alloc_);
}
template <typename T>
void push_back(const T& value, typename details::disable_if< details::is_value_ref<T> >::type* = 0, typename details::disable_if< details::is_string<T> >::type * = 0, typename details::disable_if< details::is_cstr_ptr<T> >::type * = 0)
{
type_check<array_tag>();
native_value_type v(value);
value_->PushBack(v, *alloc_);
}
template <typename T>
void push_back(const T& value, typename details::enable_if< details::is_value_ref<T> >::type* = 0)
{
type_check<array_tag>();
value_->PushBack(*value.get_native_value_pointer(), *alloc_);
}
void pop_back()
{
type_check<array_tag>();
value_->PopBack();
}
value_iterator value_begin()
{
type_check<array_tag>();
return details::make_transform_iterator(value_->Begin(), value_wrapper_type(alloc_));
}
value_iterator value_end()
{
type_check<array_tag>();
return details::make_transform_iterator(value_->End(), value_wrapper_type(alloc_));
}
const_value_iterator value_begin() const
{
type_check<array_tag>();
return details::make_transform_iterator(value_->Begin(), const_value_wrapper_type(alloc_));
}
const_value_iterator value_end() const
{
type_check<array_tag>();
return details::make_transform_iterator(value_->End(), const_value_wrapper_type(alloc_));
}
const_value_iterator value_cbegin() const { return value_begin(); }
const_value_iterator value_cend() const { return value_end(); }
void swap(value_ref_type& other) throw()
{
std::swap(value_, other.value_);
std::swap(alloc_, other.alloc_);
}
string_type str() const
{
switch (which())
{
case null_tag::value:
return "null";
case false_tag::value:
return "false";
case true_tag::value:
return "true";
case string_tag::value:
return as_string();
case number_tag::value:
{
std::basic_stringstream<char_type> ss;