1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
|
#include "internal_value.h"
#include "expression_evaluator.h"
#include "generic_adapters.h"
#include "helpers.h"
#include "value_visitors.h"
namespace jinja2
{
std::atomic_uint64_t UserCallable::m_gen{};
bool Value::IsEqual(const Value& rhs) const
{
return this->m_data == rhs.m_data;
}
bool operator==(const Value& lhs, const Value& rhs)
{
return lhs.IsEqual(rhs);
}
bool operator!=(const Value& lhs, const Value& rhs)
{
return !(lhs == rhs);
}
bool operator==(const GenericMap& lhs, const GenericMap& rhs)
{
auto* lhsAccessor = lhs.GetAccessor();
auto* rhsAccessor = rhs.GetAccessor();
return lhsAccessor && rhsAccessor && lhsAccessor->IsEqual(*rhsAccessor);
}
bool operator!=(const GenericMap& lhs, const GenericMap& rhs)
{
return !(lhs == rhs);
}
bool operator==(const UserCallable& lhs, const UserCallable& rhs)
{
// TODO: rework
return lhs.IsEqual(rhs);
}
bool operator!=(const UserCallable& lhs, const UserCallable& rhs)
{
return !(lhs == rhs);
}
bool operator==(const types::ValuePtr<UserCallable>& lhs, const types::ValuePtr<UserCallable>& rhs)
{
if (lhs && rhs)
return *lhs == *rhs;
if ((lhs && !rhs) || (!lhs && rhs))
return false;
return true;
}
bool operator!=(const types::ValuePtr<UserCallable>& lhs, const types::ValuePtr<UserCallable>& rhs)
{
return !(lhs == rhs);
}
bool operator==(const types::ValuePtr<ValuesMap>& lhs, const types::ValuePtr<ValuesMap>& rhs)
{
if (lhs && rhs)
return *lhs == *rhs;
if ((lhs && !rhs) || (!lhs && rhs))
return false;
return true;
}
bool operator!=(const types::ValuePtr<ValuesMap>& lhs, const types::ValuePtr<ValuesMap>& rhs)
{
return !(lhs == rhs);
}
bool operator==(const types::ValuePtr<Value>& lhs, const types::ValuePtr<Value>& rhs)
{
if (lhs && rhs)
return *lhs == *rhs;
if ((lhs && !rhs) || (!lhs && rhs))
return false;
return true;
}
bool operator!=(const types::ValuePtr<Value>& lhs, const types::ValuePtr<Value>& rhs)
{
return !(lhs == rhs);
}
bool operator==(const types::ValuePtr<std::vector<Value>>& lhs, const types::ValuePtr<std::vector<Value>>& rhs)
{
if (lhs && rhs)
return *lhs == *rhs;
if ((lhs && !rhs) || (!lhs && rhs))
return false;
return true;
}
bool operator!=(const types::ValuePtr<std::vector<Value>>& lhs, const types::ValuePtr<std::vector<Value>>& rhs)
{
return !(lhs == rhs);
}
bool InternalValue::IsEqual(const InternalValue &other) const
{
if (m_data != other.m_data)
return false;
return m_parentData == other.m_parentData;
}
InternalValue Value2IntValue(const Value& val);
InternalValue Value2IntValue(Value&& val);
struct SubscriptionVisitor : public visitors::BaseVisitor<>
{
using BaseVisitor<>::operator();
template<typename CharT>
InternalValue operator()(const MapAdapter& values, const std::basic_string<CharT>& fieldName) const
{
auto field = ConvertString<std::string>(fieldName);
if (!values.HasValue(field))
return InternalValue();
return values.GetValueByName(field);
}
template<typename CharT>
InternalValue operator()(const MapAdapter& values, const std::basic_string_view<CharT>& fieldName) const
{
auto field = ConvertString<std::string>(fieldName);
if (!values.HasValue(field))
return InternalValue();
return values.GetValueByName(field);
}
template<typename CharT>
InternalValue operator()(std::basic_string<CharT> value, const std::basic_string<CharT>& /*fieldName*/) const
{
return TargetString(std::move(value));
}
InternalValue operator()(const ListAdapter& values, int64_t index) const
{
if (index < 0 || static_cast<size_t>(index) >= values.GetSize())
return InternalValue();
return values.GetValueByIndex(index);
}
InternalValue operator()(const MapAdapter& /*values*/, int64_t /*index*/) const { return InternalValue(); }
template<typename CharT>
InternalValue operator()(const std::basic_string<CharT>& str, int64_t index) const
{
if (index < 0 || static_cast<size_t>(index) >= str.size())
return InternalValue();
std::basic_string<CharT> resultStr(1, str[static_cast<size_t>(index)]);
return TargetString(std::move(resultStr));
}
template<typename CharT>
InternalValue operator()(const std::basic_string_view<CharT>& str, int64_t index) const
{
// std::cout << "operator() (const std::basic_string<CharT>& str, int64_t index)" << ": index = " << index << std::endl;
if (index < 0 || static_cast<size_t>(index) >= str.size())
return InternalValue();
std::basic_string<CharT> result(1, str[static_cast<size_t>(index)]);
return TargetString(std::move(result));
}
template<typename CharT>
InternalValue operator()(const KeyValuePair& values, const std::basic_string<CharT>& fieldName) const
{
return SubscriptKvPair(values, ConvertString<std::string>(fieldName));
}
template<typename CharT>
InternalValue operator()(const KeyValuePair& values, const std::basic_string_view<CharT>& fieldName) const
{
return SubscriptKvPair(values, ConvertString<std::string>(fieldName));
}
InternalValue SubscriptKvPair(const KeyValuePair& values, const std::string& field) const
{
// std::cout << "operator() (const KeyValuePair& values, const std::string& field)" << ": field = " << field << std::endl;
if (field == "key")
return InternalValue(values.key);
else if (field == "value")
return values.value;
return InternalValue();
}
};
InternalValue Subscript(const InternalValue& val, const InternalValue& subscript, RenderContext* values)
{
static const std::string callOperName = "value()";
auto result = Apply2<SubscriptionVisitor>(val, subscript);
if (!values)
return result;
auto map = GetIf<MapAdapter>(&result);
if (!map || !map->HasValue(callOperName))
return result;
auto callableVal = map->GetValueByName(callOperName);
auto callable = GetIf<Callable>(&callableVal);
if (!callable || callable->GetKind() == Callable::Macro || callable->GetType() == Callable::Type::Statement)
return result;
CallParams callParams;
return callable->GetExpressionCallable()(callParams, *values);
}
InternalValue Subscript(const InternalValue& val, const std::string& subscript, RenderContext* values)
{
return Subscript(val, InternalValue(subscript), values);
}
struct StringGetter : public visitors::BaseVisitor<std::string>
{
using BaseVisitor::operator();
std::string operator()(const std::string& str) const { return str; }
std::string operator()(const std::string_view& str) const { return std::string(str.begin(), str.end()); }
std::string operator()(const std::wstring& str) const { return ConvertString<std::string>(str); }
std::string operator()(const std::wstring_view& str) const { return ConvertString<std::string>(str); }
};
std::string AsString(const InternalValue& val)
{
return Apply<StringGetter>(val);
}
struct ListConverter : public visitors::BaseVisitor<boost::optional<ListAdapter>>
{
using BaseVisitor::operator();
using result_t = boost::optional<ListAdapter>;
bool strictConvertion;
ListConverter(bool strict)
: strictConvertion(strict)
{
}
result_t operator()(const ListAdapter& list) const { return list; }
result_t operator()(const MapAdapter& map) const
{
if (strictConvertion)
return result_t();
InternalValueList list;
for (auto& k : map.GetKeys())
list.push_back(TargetString(k));
return ListAdapter::CreateAdapter(std::move(list));
}
template<typename CharT>
result_t operator() (const std::basic_string<CharT>& str) const
{
return strictConvertion ? result_t() : result_t(ListAdapter::CreateAdapter(str.size(), [str](size_t idx) {
return TargetString(str.substr(idx, 1));}));
}
template<typename CharT>
result_t operator()(const std::basic_string_view<CharT>& str) const
{
return strictConvertion ? result_t() : result_t(ListAdapter::CreateAdapter(str.size(), [str](size_t idx) {
return TargetString(std::basic_string<CharT>(str[idx], 1)); }));
}
};
ListAdapter ConvertToList(const InternalValue& val, bool& isConverted, bool strictConversion)
{
auto result = Apply<ListConverter>(val, strictConversion);
if (!result)
{
isConverted = false;
return ListAdapter();
}
isConverted = true;
return result.get();
}
ListAdapter ConvertToList(const InternalValue& val, InternalValue subscipt, bool& isConverted, bool strictConversion)
{
auto result = Apply<ListConverter>(val, strictConversion);
if (!result)
{
isConverted = false;
return ListAdapter();
}
isConverted = true;
if (IsEmpty(subscipt))
return std::move(result.get());
return result.get().ToSubscriptedList(subscipt, false);
}
template<typename T>
class ByRef
{
public:
explicit ByRef(const T& val)
: m_val(&val)
{
}
const T& Get() const { return *m_val; }
T& Get() { return *const_cast<T*>(m_val); }
bool ShouldExtendLifetime() const { return false; }
bool operator==(const ByRef<T>& other) const
{
if (m_val && other.m_val && m_val != other.m_val)
return false;
if ((m_val && !other.m_val) || (!m_val && other.m_val))
return false;
return true;
}
bool operator!=(const ByRef<T>& other) const
{
return !(*this == other);
}
private:
const T* m_val{};
};
template<typename T>
class ByVal
{
public:
explicit ByVal(T&& val)
: m_val(std::move(val))
{
}
~ByVal() = default;
const T& Get() const { return m_val; }
T& Get() { return m_val; }
bool ShouldExtendLifetime() const { return false; }
bool operator==(const ByVal<T>& other) const
{
return m_val == other.m_val;
}
bool operator!=(const ByVal<T>& other) const
{
return !(*this == other);
}
private:
T m_val;
};
template<typename T>
class BySharedVal
{
public:
explicit BySharedVal(T&& val)
: m_val(std::make_shared<T>(std::move(val)))
{
}
~BySharedVal() = default;
const T& Get() const { return *m_val; }
T& Get() { return *m_val; }
bool ShouldExtendLifetime() const { return true; }
bool operator==(const BySharedVal<T>& other) const
{
return m_val == other.m_val;
}
bool operator!=(const BySharedVal<T>& other) const
{
return !(*this == other);
}
private:
std::shared_ptr<T> m_val;
};
template<template<typename> class Holder>
class GenericListAdapter : public IListAccessor
{
public:
struct Enumerator : public IListAccessorEnumerator
{
ListEnumeratorPtr m_enum;
explicit Enumerator(ListEnumeratorPtr e)
: m_enum(std::move(e))
{
}
// Inherited via IListAccessorEnumerator
void Reset() override
{
if (m_enum)
m_enum->Reset();
}
bool MoveNext() override { return !m_enum ? false : m_enum->MoveNext(); }
InternalValue GetCurrent() const override { return !m_enum ? InternalValue() : Value2IntValue(m_enum->GetCurrent()); }
IListAccessorEnumerator* Clone() const override { return !m_enum ? new Enumerator(MakeEmptyListEnumeratorPtr()) : new Enumerator(m_enum->Clone()); }
IListAccessorEnumerator* Transfer() override { return new Enumerator(std::move(m_enum)); }
bool IsEqual(const IComparable& other) const override
{
auto* val = dynamic_cast<const Enumerator*>(&other);
if (!val)
return false;
if (m_enum && val->m_enum && !m_enum->IsEqual(*val->m_enum))
return false;
if ((m_enum && !val->m_enum) || (!m_enum && val->m_enum))
return false;
return true;
}
};
template<typename U>
GenericListAdapter(U&& values)
: m_values(std::forward<U>(values))
{
}
std::optional<size_t> GetSize() const override { return m_values.Get().GetSize(); }
std::optional<InternalValue> GetItem(int64_t idx) const override
{
const IListItemAccessor* accessor = m_values.Get().GetAccessor();
auto indexer = accessor->GetIndexer();
if (!indexer)
return std::optional<InternalValue>();
auto val = indexer->GetItemByIndex(idx);
return visit(visitors::InputValueConvertor(true, false), std::move(val.data())).get();
}
bool ShouldExtendLifetime() const override { return m_values.ShouldExtendLifetime(); }
ListAccessorEnumeratorPtr CreateListAccessorEnumerator() const override
{
const IListItemAccessor* accessor = m_values.Get().GetAccessor();
if (!accessor)
return ListAccessorEnumeratorPtr(new Enumerator(MakeEmptyListEnumeratorPtr()));
return ListAccessorEnumeratorPtr(new Enumerator(m_values.Get().GetAccessor()->CreateEnumerator()));
}
GenericList CreateGenericList() const override
{
// return m_values.Get();
return GenericList([list = m_values]() -> const IListItemAccessor* { return list.Get().GetAccessor(); });
}
private:
Holder<GenericList> m_values;
};
template<template<typename> class Holder>
class ValuesListAdapter : public IndexedListAccessorImpl<ValuesListAdapter<Holder>>
{
public:
template<typename U>
ValuesListAdapter(U&& values)
: m_values(std::forward<U>(values))
{
}
size_t GetItemsCountImpl() const { return m_values.Get().size(); }
std::optional<InternalValue> GetItem(int64_t idx) const override
{
const auto& val = m_values.Get()[static_cast<size_t>(idx)];
return visit(visitors::InputValueConvertor(false, true), val.data()).get();
}
bool ShouldExtendLifetime() const override { return m_values.ShouldExtendLifetime(); }
GenericList CreateGenericList() const override
{
// return m_values.Get();
return GenericList([list = *this]() -> const IListItemAccessor* { return &list; });
}
private:
Holder<ValuesList> m_values;
};
ListAdapter ListAdapter::CreateAdapter(InternalValueList&& values)
{
class Adapter : public IndexedListAccessorImpl<Adapter>
{
public:
explicit Adapter(InternalValueList&& values)
: m_values(std::move(values))
{
}
size_t GetItemsCountImpl() const { return m_values.size(); }
std::optional<InternalValue> GetItem(int64_t idx) const override { return m_values[static_cast<size_t>(idx)]; }
bool ShouldExtendLifetime() const override { return false; }
GenericList CreateGenericList() const override
{
return GenericList([adapter = *this]() -> const IListItemAccessor* { return &adapter; });
}
private:
InternalValueList m_values;
};
return ListAdapter([accessor = Adapter(std::move(values))]() { return &accessor; });
}
ListAdapter ListAdapter::CreateAdapter(const GenericList& values)
{
return ListAdapter([accessor = GenericListAdapter<ByRef>(values)]() { return &accessor; });
}
ListAdapter ListAdapter::CreateAdapter(const ValuesList& values)
{
return ListAdapter([accessor = ValuesListAdapter<ByRef>(values)]() { return &accessor; });
}
ListAdapter ListAdapter::CreateAdapter(GenericList&& values)
{
return ListAdapter([accessor = GenericListAdapter<BySharedVal>(std::move(values))]() { return &accessor; });
}
ListAdapter ListAdapter::CreateAdapter(ValuesList&& values)
{
return ListAdapter([accessor = ValuesListAdapter<BySharedVal>(std::move(values))]() { return &accessor; });
}
ListAdapter ListAdapter::CreateAdapter(std::function<std::optional<InternalValue>()> fn)
{
using GenFn = std::function<std::optional<InternalValue>()>;
class Adapter : public IListAccessor
{
public:
class Enumerator : public IListAccessorEnumerator
{
public:
explicit Enumerator(const GenFn* fn)
: m_fn(fn)
{
if (!fn)
throw std::runtime_error("List enumerator couldn't be created without element accessor function!");
}
void Reset() override {}
bool MoveNext() override
{
if (m_isFinished)
return false;
auto res = (*m_fn)();
if (!res)
return false;
m_current = *res;
return true;
}
InternalValue GetCurrent() const override { return m_current; }
IListAccessorEnumerator* Clone() const override
{
auto result = new Enumerator(*this);
return result;
}
IListAccessorEnumerator* Transfer() override
{
auto result = new Enumerator(std::move(*this));
return result;
}
bool IsEqual(const IComparable& other) const override
{
auto* val = dynamic_cast<const Enumerator*>(&other);
if (!val)
return false;
if (m_isFinished != val->m_isFinished)
return false;
if (m_current != val->m_current)
return false;
// TODO: compare fn?
if (m_fn != val->m_fn)
return false;
return true;
}
protected:
const GenFn* m_fn{};
InternalValue m_current;
bool m_isFinished = false;
};
explicit Adapter(std::function<std::optional<InternalValue>()>&& fn)
: m_fn(std::move(fn))
{
}
std::optional<size_t> GetSize() const override { return std::optional<size_t>(); }
std::optional<InternalValue> GetItem(int64_t /*idx*/) const override { return std::optional<InternalValue>(); }
bool ShouldExtendLifetime() const override { return false; }
ListAccessorEnumeratorPtr CreateListAccessorEnumerator() const override { return ListAccessorEnumeratorPtr(new Enumerator(&m_fn)); }
GenericList CreateGenericList() const override
{
return GenericList(); // return GenericList([adapter = *this]() -> const ListItemAccessor* {return &adapter; });
}
private:
std::function<std::optional<InternalValue>()> m_fn;
};
return ListAdapter([accessor = Adapter(std::move(fn))]() { return &accessor; });
}
ListAdapter ListAdapter::CreateAdapter(size_t listSize, std::function<InternalValue(size_t idx)> fn)
{
using GenFn = std::function<InternalValue(size_t idx)>;
class Adapter : public IndexedListAccessorImpl<Adapter>
{
public:
explicit Adapter(size_t listSize, GenFn&& fn)
: m_listSize(listSize)
, m_fn(std::move(fn))
{
}
size_t GetItemsCountImpl() const { return m_listSize; }
std::optional<InternalValue> GetItem(int64_t idx) const override { return m_fn(static_cast<size_t>(idx)); }
bool ShouldExtendLifetime() const override { return false; }
GenericList CreateGenericList() const override
{
return GenericList([adapter = *this]() -> const IListItemAccessor* { return &adapter; });
}
private:
size_t m_listSize;
GenFn m_fn;
};
return ListAdapter([accessor = Adapter(listSize, std::move(fn))]() { return &accessor; });
}
template<typename Holder>
auto CreateIndexedSubscribedList(Holder&& holder, const InternalValue& subscript, size_t size)
{
return ListAdapter::CreateAdapter(
size, [h = std::forward<Holder>(holder), subscript](size_t idx) -> InternalValue { return Subscript(h.Get().GetValueByIndex(idx), subscript, nullptr); });
}
template<typename Holder>
auto CreateGenericSubscribedList(Holder&& holder, const InternalValue& subscript)
{
return ListAdapter::CreateAdapter([h = std::forward<Holder>(holder), e = ListAccessorEnumeratorPtr(), isFirst = true, isLast = false, subscript]() mutable {
using ResultType = std::optional<InternalValue>;
if (isFirst)
{
e = h.Get().GetEnumerator();
isLast = !e->MoveNext();
isFirst = false;
}
if (isLast)
return ResultType();
return ResultType(Subscript(e->GetCurrent(), subscript, nullptr));
});
}
ListAdapter ListAdapter::ToSubscriptedList(const InternalValue& subscript, bool asRef) const
{
auto listSize = GetSize();
if (asRef)
{
ByRef<ListAdapter> holder(*this);
return listSize ? CreateIndexedSubscribedList(holder, subscript, *listSize) : CreateGenericSubscribedList(holder, subscript);
}
else
{
ListAdapter tmp(*this);
BySharedVal<ListAdapter> holder(std::move(tmp));
return listSize ? CreateIndexedSubscribedList(std::move(holder), subscript, *listSize) : CreateGenericSubscribedList(std::move(holder), subscript);
}
}
InternalValueList ListAdapter::ToValueList() const
{
InternalValueList result;
std::copy(begin(), end(), std::back_inserter(result));
return result;
}
template<template<typename> class Holder, bool CanModify>
class InternalValueMapAdapter : public MapAccessorImpl<InternalValueMapAdapter<Holder, CanModify>>
{
public:
template<typename U>
InternalValueMapAdapter(U&& values)
: m_values(std::forward<U>(values))
{
}
size_t GetSize() const override { return m_values.Get().size(); }
bool HasValue(const std::string& name) const override { return m_values.Get().count(name) != 0; }
InternalValue GetItem(const std::string& name) const override
{
auto& vals = m_values.Get();
auto p = vals.find(name);
if (p == vals.end())
return InternalValue();
return p->second;
}
std::vector<std::string> GetKeys() const override
{
std::vector<std::string> result;
for (auto& i : m_values.Get())
result.push_back(i.first);
return result;
}
bool SetValue(std::string name, const InternalValue& val) override
{
if (CanModify)
{
m_values.Get()[name] = val;
return true;
}
return false;
}
bool ShouldExtendLifetime() const override { return m_values.ShouldExtendLifetime(); }
GenericMap CreateGenericMap() const override
{
return GenericMap([accessor = *this]() -> const IMapItemAccessor* { return &accessor; });
}
bool IsEqual(const IComparable& other) const override
{
auto* val = dynamic_cast<const InternalValueMapAdapter*>(&other);
if (!val)
return false;
return m_values == val->m_values;
}
private:
Holder<InternalValueMap> m_values;
};
InternalValue Value2IntValue(const Value& val)
{
auto result = std::visit(visitors::InputValueConvertor(false, true), val.data());
if (result)
return result.get();
return InternalValue(ValueRef(val));
}
InternalValue Value2IntValue(Value&& val)
{
auto result = std::visit(visitors::InputValueConvertor(true, false), val.data());
if (result)
return result.get();
return InternalValue(ValueRef(val));
}
template<template<typename> class Holder>
class GenericMapAdapter : public MapAccessorImpl<GenericMapAdapter<Holder>>
{
public:
template<typename U>
GenericMapAdapter(U&& values)
: m_values(std::forward<U>(values))
{
}
size_t GetSize() const override { return m_values.Get().GetSize(); }
bool HasValue(const std::string& name) const override { return m_values.Get().HasValue(name); }
InternalValue GetItem(const std::string& name) const override
{
auto val = m_values.Get().GetValueByName(name);
if (val.isEmpty())
return InternalValue();
return Value2IntValue(std::move(val));
}
std::vector<std::string> GetKeys() const override { return m_values.Get().GetKeys(); }
bool ShouldExtendLifetime() const override { return m_values.ShouldExtendLifetime(); }
GenericMap CreateGenericMap() const override
{
return GenericMap([accessor = *this]() -> const IMapItemAccessor* { return accessor.m_values.Get().GetAccessor(); });
}
bool IsEqual(const IComparable& other) const override
{
auto* val = dynamic_cast<const GenericMapAdapter*>(&other);
if (!val)
return false;
return m_values == val->m_values;
}
private:
Holder<GenericMap> m_values;
};
template<template<typename> class Holder>
class ValuesMapAdapter : public MapAccessorImpl<ValuesMapAdapter<Holder>>
{
public:
template<typename U>
ValuesMapAdapter(U&& values)
: m_values(std::forward<U>(values))
{
}
size_t GetSize() const override { return m_values.Get().size(); }
bool HasValue(const std::string& name) const override { return m_values.Get().count(name) != 0; }
InternalValue GetItem(const std::string& name) const override
{
auto& vals = m_values.Get();
auto p = vals.find(name);
if (p == vals.end())
return InternalValue();
return Value2IntValue(p->second);
}
std::vector<std::string> GetKeys() const override
{
std::vector<std::string> result;
for (auto& i : m_values.Get())
result.push_back(i.first);
return result;
}
bool ShouldExtendLifetime() const override { return m_values.ShouldExtendLifetime(); }
GenericMap CreateGenericMap() const override
{
return GenericMap([accessor = *this]() -> const IMapItemAccessor* { return &accessor; });
}
bool IsEqual(const IComparable& other) const override
{
auto* val = dynamic_cast<const ValuesMapAdapter*>(&other);
if (!val)
return false;
return m_values == val->m_values;
}
private:
Holder<ValuesMap> m_values;
};
MapAdapter CreateMapAdapter(InternalValueMap&& values)
{
return MapAdapter([accessor = InternalValueMapAdapter<ByVal, true>(std::move(values))]() mutable { return &accessor; });
}
MapAdapter CreateMapAdapter(const InternalValueMap* values)
{
return MapAdapter([accessor = InternalValueMapAdapter<ByRef, false>(*values)]() mutable { return &accessor; });
}
MapAdapter CreateMapAdapter(const GenericMap& values)
{
return MapAdapter([accessor = GenericMapAdapter<ByRef>(values)]() mutable { return &accessor; });
}
MapAdapter CreateMapAdapter(GenericMap&& values)
{
return MapAdapter([accessor = GenericMapAdapter<BySharedVal>(std::move(values))]() mutable { return &accessor; });
}
MapAdapter CreateMapAdapter(const ValuesMap& values)
{
return MapAdapter([accessor = ValuesMapAdapter<ByRef>(values)]() mutable { return &accessor; });
}
MapAdapter CreateMapAdapter(ValuesMap&& values)
{
return MapAdapter([accessor = ValuesMapAdapter<BySharedVal>(std::move(values))]() mutable { return &accessor; });
}
struct OutputValueConvertor
{
using result_t = Value;
result_t operator()(const EmptyValue&) const { return result_t(); }
result_t operator()(const MapAdapter& adapter) const { return result_t(adapter.CreateGenericMap()); }
result_t operator()(const ListAdapter& adapter) const { return result_t(adapter.CreateGenericList()); }
result_t operator()(const ValueRef& ref) const { return ref.get(); }
result_t operator()(const TargetString& str) const
{
switch (str.index())
{
case 0:
return std::get<std::string>(str);
default:
return std::get<std::wstring>(str);
}
}
result_t operator()(const TargetStringView& str) const
{
switch (str.index())
{
case 0:
return std::get<std::string_view>(str);
default:
return std::get<std::wstring_view>(str);
}
}
result_t operator()(const KeyValuePair& pair) const { return ValuesMap{ { "key", Value(pair.key) }, { "value", IntValue2Value(pair.value) } }; }
result_t operator()(const Callable&) const { return result_t(); }
result_t operator()(const UserCallable&) const { return result_t(); }
result_t operator()(const std::shared_ptr<IRendererBase>&) const { return result_t(); }
template<typename T>
result_t operator()(const RecWrapper<T>& val) const
{
return this->operator()(const_cast<const T&>(*val));
}
template<typename T>
result_t operator()(RecWrapper<T>& val) const
{
return this->operator()(*val);
}
template<typename T>
result_t operator()(T&& val) const
{
return result_t(std::forward<T>(val));
}
bool m_byValue;
};
Value OptIntValue2Value(std::optional<InternalValue> val)
{
if (val)
return Apply<OutputValueConvertor>(val.value());
return Value();
}
Value IntValue2Value(const InternalValue& val)
{
return Apply<OutputValueConvertor>(val);
}
class ContextMapper : public IMapItemAccessor
{
public:
explicit ContextMapper(RenderContext* context)
: m_context(context)
{
}
size_t GetSize() const override { return std::numeric_limits<size_t>::max(); }
bool HasValue(const std::string& name) const override
{
bool found = false;
m_context->FindValue(name, found);
return found;
}
Value GetValueByName(const std::string& name) const override
{
bool found = false;
auto p = m_context->FindValue(name, found);
return found ? IntValue2Value(p->second) : Value();
}
std::vector<std::string> GetKeys() const override { return std::vector<std::string>(); }
bool IsEqual(const IComparable& other) const override
{
auto* val = dynamic_cast<const ContextMapper*>(&other);
if (!val)
return false;
if (m_context && val->m_context && !m_context->IsEqual(*val->m_context))
{
return false;
}
if ((m_context && !val->m_context) || (!m_context && val->m_context))
return false;
return true;
}
private:
RenderContext* m_context;
};
UserCallableParams PrepareUserCallableParams(const CallParams& params, RenderContext& context, const std::vector<ArgumentInfo>& argsInfo)
{
UserCallableParams result;
ParsedArguments args = helpers::ParseCallParams(argsInfo, params, result.paramsParsed);
if (!result.paramsParsed)
return result;
for (auto& argInfo : argsInfo)
{
if (argInfo.name.size() > 1 && argInfo.name[0] == '*')
continue;
auto p = args.args.find(argInfo.name);
if (p == args.args.end())
{
result.args[argInfo.name] = IntValue2Value(argInfo.defaultVal);
continue;
}
const auto& v = p->second;
result.args[argInfo.name] = IntValue2Value(v);
}
ValuesMap extraKwArgs;
for (auto& p : args.extraKwArgs)
extraKwArgs[p.first] = IntValue2Value(p.second);
result.extraKwArgs = Value(std::move(extraKwArgs));
ValuesList extraPosArgs;
for (auto& p : args.extraPosArgs)
extraPosArgs.push_back(IntValue2Value(p));
result.extraPosArgs = Value(std::move(extraPosArgs));
result.context = GenericMap([accessor = ContextMapper(&context)]() -> const IMapItemAccessor* { return &accessor; });
return result;
}
namespace visitors
{
InputValueConvertor::result_t InputValueConvertor::ConvertUserCallable(const UserCallable& val)
{
std::vector<ArgumentInfo> args;
for (auto& pi : val.argsInfo)
{
args.emplace_back(pi.paramName, pi.isMandatory, Value2IntValue(pi.defValue));
}
return InternalValue(Callable(Callable::UserCallable, [val, argsInfo = std::move(args)](const CallParams& params, RenderContext& context) -> InternalValue {
auto ucParams = PrepareUserCallableParams(params, context, argsInfo);
return Value2IntValue(val.callable(ucParams));
}));
}
} // namespace visitors
} // namespace jinja2
|