-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathRESTProcess_base.h
1679 lines (1482 loc) · 54.7 KB
/
RESTProcess_base.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
/*
@copyright Russell Standish 2019
@author Russell Standish
This file is part of Classdesc
Open source licensed under the MIT license. See LICENSE for details.
*/
#ifndef CLASSDESC_RESTPROCESS_BASE_H
#define CLASSDESC_RESTPROCESS_BASE_H
/// A classdesc descriptor to generate virtual xrap processing calls
#include "function.h"
#include "multiArray.h"
#include "object.h"
#include "polyRESTProcessBase.h"
#include "signature.h"
#include <map>
#include <stdexcept>
#include <string>
#include <vector>
#include <assert.h>
#ifndef REST_PROCESS_BUFFER
#include "json_pack_base.h"
#define REST_PROCESS_BUFFER json_pack_t
#endif
namespace classdesc
{
class RESTProcessBase;
using RPPtr=std::shared_ptr<RESTProcessBase>;
struct RESTProcess_t;
template <class T>
typename enable_if<Not<functional::is_nonmember_function_ptr<T>>, void>::T
RESTProcess(RESTProcess_t&, const std::string&, T&);
/// interface for the REST processor
class RESTProcessBase
{
protected:
/// implementation of upcasting to a classdesc::object
template <class T> typename enable_if<is_base_of<object,T>, const object*>::T
getClassdescObjectImpl(T& obj) {return &obj;}
template <class T> typename enable_if<Not<is_base_of<object,T>>, const object*>::T
getClassdescObjectImpl(T& obj) {return nullptr;}
public:
virtual ~RESTProcessBase() {}
/// perform the REST operation, with \a remainder being the query string and \a arguments as body text
/// result of operation is returned as an object, and can be serialised into REST_PROCESS_BUFFER using asBuffer
virtual RPPtr process(const string& remainder, const REST_PROCESS_BUFFER& arguments)=0;
virtual REST_PROCESS_BUFFER asBuffer() const=0;
/// return signature(s) of the operations
virtual std::vector<Signature> signature() const=0;
/// return list of subcommands to this
virtual RESTProcess_t list() const=0;
/// return type name of this
virtual std::string type() const=0;
/// populate \a map from the object wrapped by this, if any
virtual void populate(RESTProcess_t& map) const {}
/// return signature for a function type F
template <class F> Signature functionSignature() const;
/// returns a pointer to the underlying object if it is one of type T, otherwise null
template <class T> T* getObject();
/// @{ returns a classdesc object is referring to an object derived from classdesc::object
virtual object* getClassdescObject() {return nullptr;}
virtual const object* getConstClassdescObject() {return nullptr;}
/// @}
/// true if this is an object, not a function
virtual bool isObject() const {return false;}
/// true if this is a const object, a const member function or static/free function
virtual bool isConst() const {return false;}
/// arity if this is a function, 0 otherwise
virtual unsigned arity() const {return 0;}
/// size if this is a container, 0 otherwise
virtual size_t size() const {return 0; }
/// get element by position for sequences, by key for associative containers
virtual RPPtr getElem(const REST_PROCESS_BUFFER&);
/// sets element indexed/keyed by \a index to \a value
/// @return updated element
virtual RPPtr setElem(const REST_PROCESS_BUFFER& index, const REST_PROCESS_BUFFER& value)
{return getElem(index);}
/// append to end of a sequence, or inserts key into an associative container
virtual void insert(const REST_PROCESS_BUFFER& value) {}
/// erase an element - by position for sequences, by key for associative containers
virtual void erase(const REST_PROCESS_BUFFER& index) {}
/// returns true if an associative container contains \a key
virtual bool contains(const REST_PROCESS_BUFFER& key) const {return false;}
/// returns a list of keys if this is an associative container, otherwise void
virtual RPPtr keys() const;
};
/// @{ create an appropriate RESTProcess object referring to the argument.
template <class T>
typename enable_if<
And<
Not<is_container<T>>,
Not<is_smart_ptr<T>>
>, RPPtr>::T makeRESTProcessRef(T& obj);
template <class T>
typename enable_if<
And<
is_sequence<T>,
Not<is_base_of<MultiArrayBase,T>>
>, RPPtr>::T
makeRESTProcessRef(T& obj);
template <class T>
typename enable_if<is_base_of<MultiArrayBase,T>, RPPtr>::T
makeRESTProcessRef(T& obj);
template <class T>
typename enable_if<is_smart_ptr<T>, RPPtr>::T
makeRESTProcessRef(T& obj);
/// @}
/// marker for containers and pointers that wrap
class RESTProcessWrapperBase: public RESTProcessBase {};
// used to mark function types that can be overloaded
class RESTProcessFunctionBase: public RESTProcessBase
{
public:
// match score if argument match impossible
static const unsigned maxMatchScore=1000000;
virtual ~RESTProcessFunctionBase() {}
/// returns how good the match is with arguments, less is best
virtual unsigned matchScore(const REST_PROCESS_BUFFER& arguments) const=0;
REST_PROCESS_BUFFER asBuffer() const override {return {};}
};
struct RESTProcessOverloadedFunction: public RESTProcessBase
{
std::vector<std::shared_ptr<RESTProcessFunctionBase>> overloadedFunctions;
RPPtr process(const string& remainder, const REST_PROCESS_BUFFER& arguments) override;
REST_PROCESS_BUFFER asBuffer() const override {return {};}
std::vector<Signature> signature() const override {
std::vector<Signature> r;
for (auto& i: overloadedFunctions) {
auto s=i->signature();
r.insert(r.end(),s.begin(),s.end());
}
return r;
}
RESTProcess_t list() const override;
std::string type() const override {return "overloaded function";}
};
inline void convert(char& y, const string& x)
{
if (x.size()!=1)
throw std::runtime_error("can only assign a single character string to a character variable");
y=x[0];
}
inline void convert(const char* y, const string& x)
{
y=x.c_str();
}
template <class X, class Y>
typename enable_if<And<is_assignable<Y,X>,Not<is_const<Y>>>, void>::T
convert(Y& y, const X& x)
{y=x;}
template <class X, class Y>
typename enable_if<And<Not<is_assignable<Y,X>>, is_convertible<X,Y>, std::is_move_assignable<Y>, Not<is_const<Y>>>, void>::T
convert(Y& y, const X& x)
{y=std::move(Y(x));}
template <class X, class Y>
typename enable_if<And<
Not<is_assignable<Y,X>>,
Not<And<is_convertible<X,Y>, std::is_move_assignable<Y>>>,
Not<is_const<Y>>,
Not<is_enum<Y>>
>, void>::T
convert(Y& y, const X& x)
{throw std::runtime_error(typeName<X>()+" cannot be converted to "+typeName<Y>());}
template <class X, class Y>
void convert(const Y&, const X&)
{throw std::runtime_error("attempt to alter a const variable");}
template <class X>
typename enable_if<
And<
Not<is_sequence<X>>,
Not<is_associative_container<X>>,
Not<is_const<X>>,
Not<is_enum<X>>
>, void>::T
convert(X& x, const REST_PROCESS_BUFFER& j)
{
switch (j.type())
{
case RESTProcessType::object:
j>>x;
break;
case RESTProcessType::array:
{
auto arr=j.array();
if (arr.size()>0)
REST_PROCESS_BUFFER(arr[0])>>x;
}
break;
case RESTProcessType::string:
{
string tmp;
j>>tmp;
convert(x,tmp);
}
break;
case RESTProcessType::boolean:
{
bool tmp{};
j>>tmp;
convert(x,tmp);
}
break;
case RESTProcessType::int_number:
{
int64_t tmp{};
j>>tmp;
convert(x,tmp);
}
break;
case RESTProcessType::float_number:
{
double tmp{};
j>>tmp;
convert(x,tmp);
}
break;
case RESTProcessType::null:
break;
}
}
template <class X>
typename enable_if<
And<
Not<is_base_of<MultiArrayBase,X>>,
is_sequence<X>,
Not<is_const<X>>
>, void>::T
convert(X& x, const REST_PROCESS_BUFFER& j)
{
if (j.type()==RESTProcessType::array)
{
auto arr=j.array();
resize(x, arr.size());
auto xi=x.begin();
for (auto& ai: arr)
{
if (xi==x.end()) break;
REST_PROCESS_BUFFER(ai) >> *xi++;
}
}
}
template <class T,int R>
void convert(MultiArray<T,R>& x, const REST_PROCESS_BUFFER& j)
{
if (j.type()==RESTProcessType::array)
{
auto arr=j.array();
auto xi=x.begin();
for (auto& ai: arr)
{
if (xi==x.end()) break;
auto target=*xi++;
convert(target, REST_PROCESS_BUFFER(ai));
}
}
}
template <class T>
void convert(MultiArray<T,1>& x, const REST_PROCESS_BUFFER& j)
{
if (j.type()==RESTProcessType::array)
{
auto arr=j.array();
auto xi=x.begin();
for (auto& ai: arr)
{
if (xi==x.end()) break;
REST_PROCESS_BUFFER(ai) >> *xi++;
}
}
}
template <class X>
typename enable_if<And<is_associative_container<X>,Not<is_const<X>>>, void>::T
convert(X& x, const REST_PROCESS_BUFFER& j)
{
switch (j.type())
{
case RESTProcessType::array:
{
auto arr=j.array();
x.clear();
for (auto& ai: arr)
{
typename X::value_type v;
REST_PROCESS_BUFFER(ai) >> v;
x.insert(v);
}
}
break;
case RESTProcessType::object: // special case for StringKeys
j>>x;
break;
default: break;
}
}
template <class X>
void convert(std::shared_ptr<X>& x, const REST_PROCESS_BUFFER& j)
{
if (x) convert(*x,j);
}
template <class X>
void convert(std::weak_ptr<X>& x, const REST_PROCESS_BUFFER& j)
{
if (auto s=x.lock()) convert(*s,j);
}
template <class E>
typename enable_if<And<is_enum<E>,Not<is_const<E>>>, void>::T
convert(E& x, const REST_PROCESS_BUFFER& j)
{
string tmp; j>>tmp;
x=enum_keys<E>()(tmp);
}
template <class X>
void convert(const X* x, const REST_PROCESS_BUFFER& j)
{}
template <class F, int N> struct DefineFunctionArgTypes;
/// REST processor registry
struct RESTProcess_t: public std::map<std::string, RPPtr >
{
RESTProcess_t()=default;
/// populate this map with the members of \a obj. T must be a
/// classdescified type, otherwise this is equivalent to a default
/// contructor
template <class T> RESTProcess_t(T& obj);
/// Construct from an object that RPPPtr wraps
RESTProcess_t(const RPPtr& p) {p->populate(*this);}
/// ownership of \a rp is passed
void add(string d, RESTProcessBase* rp)
{
// for objects, ensure any previous entries of this key are deleted
erase(d);
emplace(d,rp);
}
/// ownership of \a rp is passed
void add(string d, RESTProcessFunctionBase* rp)
{
auto i=find(d);
if (i==end())
emplace(d,rp);
else if (auto functions=dynamic_cast<RESTProcessOverloadedFunction*>(i->second.get()))
{
// replace if signature the same
for (auto& f: functions->overloadedFunctions)
if (f->signature()==rp->signature())
{
i->second.reset(rp);
return;
}
functions->overloadedFunctions.emplace_back(rp);
}
else if (auto firstFunction=dynamic_pointer_cast<RESTProcessFunctionBase>(i->second))
{
// replace if signature the same
if (firstFunction->signature()==rp->signature())
{
i->second.reset(rp);
return;
}
auto functs=std::make_shared<RESTProcessOverloadedFunction>();
if (firstFunction) functs->overloadedFunctions.push_back(std::move(firstFunction));
functs->overloadedFunctions.emplace_back(rp);
i->second=functs;
}
else // overloading something that is not a function
i->second.reset(rp);
}
inline RPPtr process(const std::string& query, const REST_PROCESS_BUFFER& jin);
/// define all arguments of \a F
template <class F> void defineFunctionArgTypes()
{
DefineFunctionArgTypes<F,functional::Arity<F>::value>::define(*this);
}
/// adds a factory function for creating objects of type \a
/// T.
/// @tparam T type of objects this factory will create
/// @tparam Args are the types of arguments to be passed to T's constructor
/// @param typeName alias for the factory - usually the unqualified type name
/// @param callback an optional callback that is run whenever a new object is created
/// The factory gets called with a string argument giving the new
/// objects name in the registry, followed by the arguments for
/// T's constructor, if any
template <class T, class... Args> void addFactory
(const std::string& typeName,const std::function<void(const std::string& objName)>& callback=nullptr);
};
template <class T>
RPPtr mapAndProcess(const string& query, const REST_PROCESS_BUFFER& arguments, T& a);
template <class T>
inline typename enable_if<is_default_constructible<T>,RPPtr>::T
mapAndProcessDummy(const string& query, const REST_PROCESS_BUFFER& arguments)
{
T dummy{};
return mapAndProcess(query,arguments,dummy);
}
template <class T>
inline typename enable_if<Not<is_default_constructible<T>>,RPPtr>::T
mapAndProcessDummy(const string& query, const REST_PROCESS_BUFFER& arguments)
{
throw std::runtime_error(typeName<T>()+" is not default constructible, but requested element doesn't exist");
}
template <class T> struct RESTProcessObject;
template <class T> inline
typename enable_if<is_copy_constructible<T>,RPPtr>::T
toObjectValueImpl(const RESTProcessObject<T>&);
template <class T> inline
typename enable_if<Not<is_copy_constructible<T>>,RPPtr>::T
toObjectValueImpl(const RESTProcessObject<T>&) {return nullptr;}
/// handle setting and getting of objects
template <class T> struct RESTProcessObject: public RESTProcessBase
{
T& obj;
RESTProcessObject(T& obj): obj(obj) {}
std::shared_ptr<classdesc::RESTProcessBase> process(const string& remainder, const REST_PROCESS_BUFFER& arguments) override
{
if (remainder.empty())
{
switch (arguments.type())
{
case RESTProcessType::null: break;
case RESTProcessType::array:
{
auto& arr=arguments.array();
if (!arr.empty()) convert(obj, REST_PROCESS_BUFFER(arr[0]));
break;
}
default:
convert(obj, arguments);
break;
}
return std::make_shared<RESTProcessObject>(obj);
}
return mapAndProcess(remainder, arguments, obj);
}
REST_PROCESS_BUFFER asBuffer() const override {REST_PROCESS_BUFFER r; return r<<obj;}
std::vector<Signature> signature() const override;
RESTProcess_t list() const override;
std::string type() const override;
void populate(RESTProcess_t& map) const override {RESTProcess(map,"",obj);}
object* getClassdescObject() override {
if (is_const<T>::value) return nullptr;
return const_cast<object*>(getClassdescObjectImpl(obj));
}
bool isObject() const override {return true;}
const object* getConstClassdescObject() override {return getClassdescObjectImpl(obj);}
bool isConst() const override {return std::is_const<T>::value;}
};
/// same as \a RESTProcessObject, but internally stores the object. T must be copy constructible or moveable
template <class T> struct RESTProcessValueObject: public RESTProcessObject<T>
{
T actual;
public:
template <class... Args>
RESTProcessValueObject(Args&&... args): RESTProcessObject<T>(actual), actual(std::forward<Args>(args)...) {}
};
template <class T> inline
typename enable_if<is_copy_constructible<T>,RPPtr>::T
toObjectValueImpl(const RESTProcessObject<T>& x)
{return std::make_shared<RESTProcessValueObject<T>>(x.obj);}
template <class T>
RPPtr makeRESTProcessValueObject(T&& obj)
{return std::make_shared<RESTProcessValueObject<typename std::remove_reference<T>::type>>(std::forward<T>(obj));}
// specialization for string and string vector to allow
inline RPPtr makeRESTProcessValueObject(const char* s)
{return std::make_shared<RESTProcessValueObject<std::string>>(s);}
inline RPPtr makeRESTProcessValueObject(const std::initializer_list<std::string>& init)
{return std::make_shared<RESTProcessValueObject<std::vector<std::string>>>(init);}
/// class that represents the void, or null object
struct RESTProcessVoid: public RESTProcessBase
{
std::shared_ptr<RESTProcessBase> process(const string&, const REST_PROCESS_BUFFER&) override
{return std::make_shared<RESTProcessVoid>();}
REST_PROCESS_BUFFER asBuffer() const override {return {};}
std::vector<Signature> signature() const override {return {};}
RESTProcess_t list() const override {return {};}
std::string type() const override {return "void";}
bool isConst() const override {return true;}
};
inline RPPtr RESTProcessBase::getElem(const REST_PROCESS_BUFFER&)
{return std::make_shared<RESTProcessVoid>();}
inline RPPtr RESTProcessBase::keys() const
{return std::make_shared<RESTProcessVoid>();}
template <class T> T* RESTProcessBase::getObject()
{
if (auto p=dynamic_cast<RESTProcessObject<T>*>(this))
return &p->obj;
else
return nullptr;
}
template <class T>
struct is_classdescGenerated:
public And<
is_class<T>,
Not<is_container<T>>,
Not<is_smart_ptr<T>>,
Not<is_string<T>>,
Not<is_excluded<T>>
> {};
template <class T>
typename enable_if<is_excluded<T>,void>::T
RESTProcessp(RESTProcess_t& repo, const string& d, T& a)
{}
template <class T>
typename enable_if<Or<is_fundamental<T>,is_string<T>>, void>::T
RESTProcessp(RESTProcess_t& repo, const string& d, T& a)
{repo.emplace(d, makeRESTProcessRef(a));}
template <class T>
void RESTProcessp(RESTProcess_t& repo, const string& d, T* a)
{/* pointers ignored */}
template <class T>
void RESTProcess(RESTProcess_t& repo, const string& d, is_const_static, T& a)
{RESTProcess(repo,d,a);}
template <class T>
void RESTProcess(RESTProcess_t& r, const string& d, is_const_static, T* a)
{RESTProcess(r,d,*a);}
inline bool startsWith(const std::string& x, const std::string& prefix)
{return x.size()>=prefix.size() && equal(prefix.begin(), prefix.end(), x.begin());}
template <class U>
typename enable_if<is_default_constructible<U>, U&>::T
dummyRef() {
static U dummy{};
return dummy;
}
template <class U>
typename enable_if<Not<is_default_constructible<U>>, U&>::T
dummyRef() {
throw std::runtime_error(typeName<U>()+" is not default constructible, but requested element doesn't exist");
}
// sequences
template <class T> class RESTProcessSequence: public RESTProcessWrapperBase
{
T& obj;
template <class U>
struct Insertable: public
And<
And<
has_member_push_back<T,void (T::*)(const typename T::value_type&)>,
is_default_constructible<typename T::value_type>>,
Not<is_const<U>>> {};
template <class U>
typename enable_if<Insertable<U>, void>::T
insert(U& o, const REST_PROCESS_BUFFER& j) {
typename U::value_type v;
convert(v,j);
push_back(o,v);
}
template <class U>
typename enable_if<Not<Insertable<U> >, void>::T insert(U&, const REST_PROCESS_BUFFER&)
{
throw std::runtime_error("cannot insert into this sequence");
}
template <class U>
struct Erasable: public
And<
Or<
has_member_erase<T,typename T::iterator (T::*)(typename T::iterator)>,
has_member_erase<T,typename T::iterator (T::*)(typename T::const_iterator)>
>,
Not<is_const<U>>> {};
template <class U>
typename enable_if<Erasable<U>,void>::T
erase(U& seq, const REST_PROCESS_BUFFER& j)
{
size_t idx{}; convert(idx,j);
if (idx<seq.size())
{
auto i=seq.begin();
std::advance(i, idx);
seq.erase(i);
}
}
template <class U>
typename enable_if<Not<Erasable<U>>,void>::T
erase(U& seq, const REST_PROCESS_BUFFER& j)
{
throw std::runtime_error("cannot erase from this sequence");
}
// common element extraction code - assumes index is in bounds
typename transfer_const<T, typename T::value_type>::type& elemCommon(size_t idx) {
auto i=obj.begin();
std::advance(i, idx);
return *i;
}
typename transfer_const<T, typename T::value_type>::type& elemNoThrow(size_t idx) {
if (idx<obj.size()) return elemCommon(idx);
return dummyRef<typename T::value_type>();
}
void pushBack(const typename T::value_type& v) {
push_back(obj, v);
}
template <class U>
typename enable_if<has_member_erase<U,typename U::iterator(U::*)(typename U::const_iterator)>,void>::T
erase(U& o,typename U::iterator i) {o.erase(i);}
template <class U>
typename enable_if<Not<is_same<typename U::iterator,typename U::const_iterator>>,void>::T
erase(U& o,typename U::const_iterator i) {}
template <class U>
typename enable_if<Not<has_member_erase<U,typename U::iterator(U::*)(typename U::const_iterator)>>,void>::T
erase(U& o,typename U::iterator i) {}
void eraseElem(size_t idx) {
if (idx>=size()) return;
auto i=obj.begin();
std::advance(i, idx);
erase(obj,i);
}
protected:
typename transfer_const<T, typename T::value_type>::type& elem(size_t idx) {
if (idx<obj.size()) return elemCommon(idx);
throw std::runtime_error("idx out of bounds");
}
public:
RESTProcessSequence(T& obj): obj(obj) {}
RPPtr process(const string& remainder, const REST_PROCESS_BUFFER& arguments) override;
std::vector<Signature> signature() const override;
RESTProcess_t list() const override;
std::string type() const override {return typeName<T>();}
REST_PROCESS_BUFFER asBuffer() const override {REST_PROCESS_BUFFER r; return r<<obj;}
bool isObject() const override {return true;}
RPPtr getElem(const REST_PROCESS_BUFFER& index) override {
size_t idx; index>>idx;
return makeRESTProcessRef(elem(idx));
}
RPPtr setElem(const REST_PROCESS_BUFFER& index, const REST_PROCESS_BUFFER& value) override {
size_t idx; index>>idx;
auto& v=elem(idx);
value>>v;
return makeRESTProcessRef(v);
}
size_t size() const override {return obj.size();}
void insert(const REST_PROCESS_BUFFER& value) override {insert(obj,value);}
void erase(const REST_PROCESS_BUFFER& index) override {
size_t idx; index>>idx;
eraseElem(idx);
}
};
template <class T> struct RESTProcessValueSequence: public RESTProcessSequence<T>
{
T actual;
public:
template <class... Args>
RESTProcessValueSequence(Args&&... args): RESTProcessSequence<T>(actual), actual(std::forward<Args>(args)...) {}
};
template <class T, int R> RPPtr copyMultiArrayIterator(MultiArray<T,R>& x);
template <class T, int R> RPPtr copyMultiArrayIterator(const MultiArray<T,R>& x);
template <class T> RPPtr copyMultiArrayIterator(T& x)
{return std::make_shared<RESTProcessObject<T>>(x);}
template <class T> struct RESTProcessMultiArray: public RESTProcessBase
{
T actual;
public:
template <class... Args>
RESTProcessMultiArray(Args&&... args): actual(std::forward<Args>(args)...) {}
RPPtr process(const string& remainder, const REST_PROCESS_BUFFER& arguments) override;
REST_PROCESS_BUFFER asBuffer() const override {
REST_PROCESS_BUFFER r;
return r<<actual;
}
std::vector<Signature> signature() const override {return {};}
RESTProcess_t list() const override {return {};}
std::string type() const override {return "typeName<T>()";}
bool isObject() const override {return true;}
RPPtr getElem(const REST_PROCESS_BUFFER& index) override {
size_t idx; index>>idx;
if (idx<actual.size())
return copyMultiArrayIterator(actual[idx]);
return std::make_shared<RESTProcessVoid>();
}
RPPtr setElem(const REST_PROCESS_BUFFER& index, const REST_PROCESS_BUFFER& value) override {
size_t idx; index>>idx;
if (idx<actual.size())
{
auto elem=actual[idx];
convert(elem,value);
return copyMultiArrayIterator(elem);
}
return std::make_shared<RESTProcessVoid>();
}
size_t size() const override {return actual.size();}
};
template <class T, int R> RPPtr copyMultiArrayIterator(MultiArray<T,R>& x)
{return std::make_shared<RESTProcessMultiArray<MultiArray<T,R>>>(x);}
template <class T, int R> RPPtr copyMultiArrayIterator(const MultiArray<T,R>& x)
{return std::make_shared<RESTProcessMultiArray<MultiArray<T,R>>>(x);}
template <class T> struct RESTProcessMultiArray<classdesc::MultiArray<T,1>>:
public RESTProcessSequence<classdesc::MultiArray<T,1>>
{
MultiArray<T,1> actual;
public:
template <class... Args>
RESTProcessMultiArray(Args&&... args): RESTProcessSequence<classdesc::MultiArray<T,1>>(actual), actual(std::forward<Args>(args)...) {}
RPPtr getElem(const REST_PROCESS_BUFFER& index) {
size_t idx; index>>idx;
return std::make_shared<RESTProcessObject<T>>(this->elem(idx));
}
RPPtr setElem(const REST_PROCESS_BUFFER& index, const REST_PROCESS_BUFFER& value) {
size_t idx; index>>idx;
auto& v=this->elem(idx);
value>>v;
return std::make_shared<RESTProcessObject<T>>(v);
}
size_t size() const override {return actual.size();}
};
template <class T>
typename enable_if<
And<
is_sequence<T>,
Not<is_base_of<MultiArrayBase,T>>
>, void>::T
RESTProcessp(RESTProcess_t& repo, const string& d, T& a)
{repo.add(d, new RESTProcessSequence<T>(a));}
template <class T>
typename enable_if<is_base_of<MultiArrayBase,T>, void>::T
RESTProcessp(RESTProcess_t& repo, const string& d, T& a)
{repo.add(d, new RESTProcessValueSequence<T>(a));}
/// used for removing const attributes of an associative container's value_type
template <class T>
struct MutableValueType
{
typedef typename std::remove_const<T>::type type;
};
template <class K, class V>
struct MutableValueType<std::pair<const K, V> >
{
typedef std::pair<K, V> type;
};
/// insert element into an associative container
template <class T>
void RPAC_insert(T& obj, const REST_PROCESS_BUFFER& arguments)
{
typename MutableValueType<typename T::value_type>::type v;
convert(v,arguments);
if (!obj.insert(v).second)
throw std::runtime_error("key already exists, not inserted");
}
/// insert element into map
template <class T>
void RPAC_insert(const T&, const REST_PROCESS_BUFFER& argument)
{
throw std::runtime_error("cannot insert data into a constant container");
}
template <class T>
void RPAC_erase(T& obj, const REST_PROCESS_BUFFER& arguments)
{
typename T::key_type k;
convert(k,arguments);
obj.erase(k);
}
/// insert element into map
template <class T>
void RPAC_erase(const T&, const REST_PROCESS_BUFFER& argument)
{
throw std::runtime_error("cannot erase data from a constant container");
}
template <class U>
inline typename enable_if<is_fundamental<U>, void>::T
assignRawStringToKey(U& key, const std::string& x)
{
std::istringstream is(x); is>>key;
}
template <class U>
inline typename enable_if<Not<is_fundamental<U>>, void>::T
assignRawStringToKey(U& key, const std::string& x)
{
throw std::runtime_error("key "+x+" needs to be JSON encoded");
}
template <>
inline void assignRawStringToKey(std::string& key, const std::string& x)
{
key=x;
}
/// assign \a x if T is a map
template <class T,class K>
typename enable_if<
And<
Not<is_const<T>>,
is_pair<typename T::value_type>
>, void>::T
assignElem(T& obj, const K& k, const REST_PROCESS_BUFFER& x)
{
auto iter=obj.emplace(k, typename T::mapped_type()).first;
convert(iter->second,x);
}
/// assign \a x if T is a set
template <class T,class K>
typename enable_if<
And<
Not<is_const<T>>,
Not<is_pair<typename T::value_type>>
>, void>::T
assignElem(T& obj, const K& k,const REST_PROCESS_BUFFER& x)
{
bool v; x>>v;
if (v)
obj.insert(k);
else
obj.erase(k);
}
template <class T,class K>
typename enable_if<is_const<T>, void>::T
assignElem(T& obj, const K& k,const REST_PROCESS_BUFFER& x) {}
template <class T, class Enable=void> struct MappedType;
template <class T> struct MappedType // for maps
<T, typename enable_if<is_pair<typename T::value_type>, void>::T>
{
using type=typename transfer_const<
T,
typename T::value_type::second_type,
Or<is_const<T>, is_const<typename T::value_type>,
is_const<typename T::value_type::second_type>>::value
>::type;
};
template <class T> struct MappedType // for sets
<T, typename enable_if<Not<is_pair<typename T::value_type>>, void>::T>
{using type=const typename T::value_type;};
template <class T>
typename enable_if<is_pair<typename T::value_type>, typename T::value_type>::T
makeElement(const typename T::key_type& k) {return {k,{}};}
template <class T>
typename enable_if<Not<is_pair<typename T::value_type>>, typename T::value_type>::T
makeElement(const typename T::key_type& k) {return k;}
template <class T> class RESTProcessAssociativeContainer: public RESTProcessWrapperBase
{
T& obj;
/// get element if a map
template <class I>
typename enable_if<
is_pair<typename std::iterator_traits<I>::value_type>,
// in C++20, std::iterator_traits<I>::value_type has any const
// attribute stripped off, hence this convoluted mouthful
typename transfer_const<
typename std::remove_reference<
typename std::iterator_traits<I>::reference
>::type,
typename std::iterator_traits<I>::value_type::second_type
>::type&
>::T
elem_of(const I& i) const {return i->second;}
/// get element if a set
template <class I>
typename enable_if<Not<is_pair<typename std::iterator_traits<I>::value_type>>,
const typename std::iterator_traits<I>::value_type&>::T
elem_of(const I& i) const {return *i;}
template <class U>
typename enable_if<Not<is_const<U>>, typename MappedType<U>::type&>::T elemImpl(const typename U::key_type& k)
{return elem_of(obj.emplace(makeElement<T>(k)).first);}
template <class U>
typename enable_if<is_const<U>, typename MappedType<U>::type&>::T elemImpl(const typename U::key_type& k)
{
auto i=obj.find(k);
if (i==obj.end()) return dummyRef<typename MappedType<U>::type>();
return elem_of(i);
}
typename MappedType<T>::type& elem(const typename T::key_type& k)
{return elemImpl<T>(k);}
public:
RESTProcessAssociativeContainer(T& obj): obj(obj) {}
RPPtr process(const string& remainder, const REST_PROCESS_BUFFER& arguments) override;
std::vector<Signature> signature() const override;
RESTProcess_t list() const override;
std::string type() const override {return typeName<T>();}
REST_PROCESS_BUFFER asBuffer() const override {REST_PROCESS_BUFFER r; return r<<obj;}
bool isObject() const override {return true;}
RPPtr getElem(const REST_PROCESS_BUFFER& index) override {
typename T::key_type idx; index>>idx;
return makeRESTProcessRef(elem(idx));
}
RPPtr setElem(const REST_PROCESS_BUFFER& index, const REST_PROCESS_BUFFER& value) override {
typename T::key_type idx; index>>idx;
auto& v=elem(idx);
value>>v;
return makeRESTProcessRef(v);
}
size_t size() const override {return obj.size();}
void insert(const REST_PROCESS_BUFFER& value) override {RPAC_insert(obj,value);}
void erase(const REST_PROCESS_BUFFER& index) override {RPAC_erase(obj,index);}
bool contains(const REST_PROCESS_BUFFER& index) const override {
typename T::key_type k; convert(k,index);
return obj.count(k);
}
RPPtr keys() const override {
std::vector<typename T::key_type> k;
for (auto& i: obj)
k.emplace_back(keyOf(i));
return makeRESTProcessValueObject(std::move(k));
}
};
template <class T> struct RESTProcessValueAssociativeContainer: public RESTProcessAssociativeContainer<T>
{
T actual;
public: