-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathjsonipc.hh
More file actions
1474 lines (1413 loc) · 50.1 KB
/
jsonipc.hh
File metadata and controls
1474 lines (1413 loc) · 50.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
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
// This Source Code Form is licensed MPL-2.0: http://mozilla.org/MPL/2.0
#ifndef __JSONIPC_JSONIPC_HH__
#define __JSONIPC_JSONIPC_HH__
#include <rapidjson/document.h>
#include <rapidjson/stringbuffer.h>
#include <rapidjson/writer.h>
#include <stdarg.h>
#include <cxxabi.h> // abi::__cxa_demangle
#include <algorithm>
#include <functional>
#include <typeindex>
#include <memory>
#include <vector>
#include <unordered_map>
#include <map>
#include <set>
// Much of the API and some implementation ideas are influenced by https://github.com/pmed/v8pp/ and https://www.jsonrpc.org/.
#define JSONIPC_ISLIKELY(expr) __builtin_expect (bool (expr), 1)
#define JSONIPC_ASSERT_RETURN(expr,...) do { if (JSONIPC_ISLIKELY (expr)) break; fprintf (stderr, "%s:%d: assertion failed: %s\n", __FILE__, __LINE__, #expr); return __VA_ARGS__; } while (0)
namespace Jsonipc {
// == Json types ==
using JsonValue = rapidjson::GenericValue<rapidjson::UTF8<char>, rapidjson::MemoryPoolAllocator<rapidjson::CrtAllocator> >;
using JsonAllocator = rapidjson::MemoryPoolAllocator<rapidjson::CrtAllocator>;
// == C++ Utilities ==
/// Construct a std::string with printf-like syntax, ignoring locale settings.
static std::string
string_format (const char *format, ...)
{
va_list vargs;
va_start (vargs, format);
static locale_t posix_c_locale = newlocale (LC_ALL_MASK, "C", NULL);
locale_t saved_locale = uselocale (posix_c_locale);
constexpr const size_t maxlen = 8192;
char buffer[maxlen + 1 + 1] = { 0, };
vsnprintf (buffer, maxlen, format, vargs);
buffer[maxlen] = 0;
uselocale (saved_locale);
va_end (vargs);
return std::string (buffer);
}
/// REQUIRES<value> - Simplified version of std::enable_if<cond,bool>::type to use SFINAE in function templates.
template<bool value> using REQUIRES = typename ::std::enable_if<value, bool>::type;
/// REQUIRESv<value> - Simplified version of std::enable_if<cond,void>::type to use SFINAE in struct templates.
template<bool value> using REQUIRESv = typename ::std::enable_if<value, void>::type;
/// Template class to identify std::shared_ptr<> classes
template<typename> struct IsSharedPtr : std::false_type {};
template<typename T> struct IsSharedPtr<std::shared_ptr<T>> : std::true_type {};
/// Test string equality at compile time.
static inline constexpr bool
constexpr_equals (const char *a, const char *b, size_t n)
{
return n == 0 || (a[0] == b[0] && (a[0] == 0 || constexpr_equals (a + 1, b + 1, n - 1)));
}
/** Demangle a std::typeinfo.name() string into a proper C++ type name.
* This function uses abi::__cxa_demangle() from <cxxabi.h> to demangle C++ type names,
* which works for g++, libstdc++, clang++, libc++.
*/
static inline std::string
string_demangle_cxx (const char *mangled_identifier)
{
int status = 0;
char *malloced_result = abi::__cxa_demangle (mangled_identifier, NULL, NULL, &status);
std::string result = malloced_result && !status ? malloced_result : mangled_identifier;
if (malloced_result)
free (malloced_result);
return result;
}
/// Provide demangled stringified name for a type `T`.
template<class T> static inline std::string
rtti_typename()
{
return string_demangle_cxx (typeid (T).name());
}
/// Provide demangled stringified name for the runtime type of object `o`.
template<class T> static inline std::string
rtti_typename (T &o)
{
return string_demangle_cxx (typeid (o).name());
}
/// Has___typename__<T> - Check if @a T provides a @a __typename__() method.
template<class, class = void> struct Has___typename__ : std::false_type {};
template<typename T> struct Has___typename__<T, std::void_t< decltype (std::declval<const T&>().__typename__()) > > : std::true_type {};
/// Provide the __typename__() of @a object, or its rtti_typename().
template<typename T, REQUIRES< Has___typename__<T>::value > = true> static inline std::string
get___typename__ (const T &o)
{
return o.__typename__();
}
template<typename T, REQUIRES< !Has___typename__<T>::value > = true> static inline std::string
get___typename__ (const T &o)
{
return rtti_typename (o);
}
// == Forward Decls ==
class InstanceMap;
template<typename> struct Class;
// == Scope ==
/// Keep track of temporary instances during IpcDispatcher::dispatch_message().
class Scope {
std::vector<std::shared_ptr<void>> locals_;
InstanceMap &instance_map_;
static std::vector<Scope*>&
stack()
{
static thread_local std::vector<Scope*> stack_;
return stack_;
}
static Scope*
head()
{
auto &stack_ = stack();
return stack_.empty() ? nullptr : stack_.back();
}
public:
template<typename T> static std::shared_ptr<T>
make_shared ()
{
Scope *scope = head();
if (!scope)
throw std::logic_error ("Jsonipc::Scope::make_shared(): invalid Scope: nullptr");
std::shared_ptr<T> sptr;
if (scope)
{
sptr = std::make_shared<T>();
scope->locals_.push_back (sptr);
}
return sptr;
}
static InstanceMap*
instance_map ()
{
Scope *scope = head();
if (!scope)
throw std::logic_error ("Jsonipc::Scope::instance_map(): invalid Scope: nullptr");
return scope ? &scope->instance_map_ : nullptr;
}
Scope (InstanceMap &instance_map) :
instance_map_ (instance_map)
{
auto &stack_ = stack();
stack_.push_back (this);
}
~Scope()
{
auto &stack_ = stack();
stack_.erase (std::remove (stack_.begin(), stack_.end(), this), stack_.end());
}
};
// == Convert ==
/// Template class providing C++ <-> JsonValue conversions for various types
template<typename T, typename Enable = void>
struct Convert;
// int types
template<typename T>
struct Convert<T, REQUIRESv< std::is_integral<T>::value > > {
static T
from_json (const JsonValue &value, T fallback = T())
{
if (value.IsBool()) return value.GetBool();
else if (value.IsInt()) return value.GetInt();
else if (value.IsUint()) return value.GetUint();
else if (value.IsInt64()) return value.GetInt64();
else if (value.IsUint64()) return value.GetUint64();
else if (value.IsDouble()) return value.GetDouble();
else return fallback; // !IsNumber()
}
static JsonValue
to_json (T i, JsonAllocator &allocator)
{
return JsonValue (i);
}
};
// bool type
template<>
struct Convert<bool> {
static bool
from_json (const JsonValue &value, bool fallback = bool())
{
return Convert<uint64_t>::from_json (value, fallback);
}
static JsonValue
to_json (bool b, JsonAllocator &allocator)
{
return JsonValue (b);
}
};
// floating point types
template<typename T>
struct Convert<T, REQUIRESv< std::is_floating_point<T>::value >> {
static T
from_json (const JsonValue &value, T fallback = T())
{
if (value.IsBool()) return value.GetBool();
else if (value.IsInt()) return value.GetInt();
else if (value.IsUint()) return value.GetUint();
else if (value.IsInt64()) return value.GetInt64();
else if (value.IsUint64()) return value.GetUint64();
else if (value.IsDouble()) return value.GetDouble();
else return fallback; // !IsNumber()
}
static JsonValue
to_json (T f, JsonAllocator &allocator)
{
return JsonValue (f);
}
};
// const char* type
template<>
struct Convert<const char*> {
static const char*
from_json (const JsonValue &value, const char *fallback = "")
{
return value.IsString() ? value.GetString() : fallback;
}
static JsonValue
to_json (const char *str, size_t l, JsonAllocator &allocator)
{
return str ? JsonValue (str, l, allocator) : JsonValue();
}
static JsonValue
to_json (const char *str, JsonAllocator &allocator)
{
return str ? JsonValue (str, strlen (str), allocator) : JsonValue();
}
};
// std::string
template<>
struct Convert<std::string> {
static std::string
from_json (const JsonValue &value, const std::string &fallback = std::string())
{
return value.IsString() ? std::string (value.GetString(), value.GetStringLength()) : fallback;
}
static JsonValue
to_json (const std::string &s, JsonAllocator &allocator)
{
return JsonValue (s.data(), s.size(), allocator);
}
};
/// DerivesVector<T> - Check if @a T derives from std::vector<>.
template<class T, typename = void> struct DerivesVector : std::false_type {};
// Use void_t to prevent errors for T without vector's typedefs
template<class T> struct DerivesVector<T, std::void_t< typename T::value_type, typename T::allocator_type >> :
std::is_base_of< std::vector<typename T::value_type, typename T::allocator_type>, T > {};
// std::vector
template<typename T>
struct Convert<T, REQUIRESv< DerivesVector<T>::value >> {
static T
from_json (const JsonValue &jarray)
{
T vec;
if (jarray.IsArray())
{
vec.reserve (jarray.Size());
for (size_t i = 0; i < jarray.Size(); i++)
vec.emplace_back (Convert<typename T::value_type>::from_json (jarray[i]));
}
return vec;
}
static JsonValue
to_json (const T &vec, JsonAllocator &allocator)
{
JsonValue jarray (rapidjson::kArrayType);
jarray.Reserve (vec.size(), allocator);
for (size_t i = 0; i < vec.size(); ++i)
jarray.PushBack (Convert<typename T::value_type>::to_json (vec[i], allocator).Move(), allocator);
return jarray;
}
};
// const reference types
template<typename T>
struct Convert<T&> : Convert<T> {};
// reference types
template<typename T>
struct Convert<T const&> : Convert<T> {};
/// Convert JsonValue to C++ value
template<typename T> static inline auto
from_json (const JsonValue &value)
-> decltype (Convert<T>::from_json (value))
{
return Convert<T>::from_json (value);
}
/// Convert JsonValue to C++ value with fallback for failed conversions
template<typename T> static inline auto
from_json (const JsonValue &value, const T &fallback)
-> decltype (Convert<T>::from_json (value, fallback))
{
return Convert<T>::from_json (value, fallback);
}
/// Convert C++ value to JsonValue
template<typename T> static inline JsonValue
to_json (const T &value, JsonAllocator &allocator)
{
return Convert<T>::to_json (value, allocator);
}
/// Convert C++ value to JsonValue
template<> inline JsonValue
to_json<const char*> (const char *const &value, JsonAllocator &allocator)
{
return Convert<const char*>::to_json (value, allocator);
}
/// Convert C++ char array to JsonValue
template<size_t N> static inline auto
to_json (const char (&c)[N], JsonAllocator &allocator)
{
return Convert<const char*>::to_json (c, N - 1, allocator);
}
template<size_t N> static inline auto
to_json (const char (&c)[N], size_t l, JsonAllocator &allocator)
{
return Convert<const char*>::to_json (c, l, allocator);
}
// == CallbackInfo ==
struct CallbackInfo;
using Closure = std::function<std::string* (CallbackInfo&)>;
/// Context for calling C++ functions from Json
struct CallbackInfo final {
CallbackInfo (const JsonValue &args, JsonAllocator *allocator = nullptr) :
args_ (args), doc_ (allocator)
{}
const JsonValue& ntharg (size_t index) const { static JsonValue j0; return index < args_.Size() ? args_[index] : j0; }
size_t n_args () const { return args_.Size(); }
Closure* find_closure (const char *methodname);
JsonAllocator& allocator () { return doc_.GetAllocator(); }
void set_result (JsonValue &result) { result_ = result; have_result_ = true; } // move-semantic!
JsonValue& get_result () { return result_; }
bool have_result () const { return have_result_; }
rapidjson::Document& document () { return doc_; }
static constexpr const char *method_not_found = "Method not found"; // -32601
static constexpr const char *invalid_params = "Invalid params"; // -32602
static constexpr const char *internal_error = "Internal error"; // -32603
static constexpr const char *application_error = "Application error"; // -32500
private:
size_t
thisid () const
{
const JsonValue &value = ntharg (0);
if (value.IsObject())
{
auto it = value.FindMember ("$id");
if (it != value.MemberEnd())
return Convert<size_t>::from_json (it->value);
}
return 0;
}
const JsonValue &args_;
JsonValue result_;
bool have_result_ = false;
rapidjson::Document doc_;
};
// == FunctionTraits ==
/// Template class providing return type and argument type information for functions
template<typename F> struct FunctionTraits;
template<typename R, typename ...Args> struct FunctionTraits<R (Args...)> {
using ReturnType = R;
using Arguments = std::tuple<Args...>;
};
// Member function pointer
template<typename C, typename R, typename ...Args>
struct FunctionTraits<R (C::*) (Args...)> : FunctionTraits<R (C&, Args...)> {
// HAS_THIS = true
};
// Const member function pointer
template<typename C, typename R, typename ...Args>
struct FunctionTraits<R (C::*) (Args...) const> : FunctionTraits<R (const C&, Args...)> {
// HAS_THIS = true
};
// Reference/const callables
template<typename F>
struct FunctionTraits<F&> : FunctionTraits<F> {};
template<typename F>
struct FunctionTraits<const F> : FunctionTraits<F> {};
// Member object pointer
template<typename C, typename R>
struct FunctionTraits<R (C::*)> : FunctionTraits<R (C&)> {
template<typename D = C> using PointerType = R (D::*);
};
// == CallTraits ==
/// Template class providing conversion helpers for JsonValue to indexed C++ function argument
template<typename F>
struct CallTraits {
using FuncType = typename std::decay<F>::type;
using Arguments = typename FunctionTraits<FuncType>::Arguments;
static constexpr const bool HAS_THIS = std::is_member_function_pointer<FuncType>::value;
static constexpr const size_t N_ARGS = std::tuple_size<Arguments>::value - HAS_THIS;
// Argument type via INDEX
template<size_t INDEX, bool> struct TupleElement { using Type = typename std::tuple_element<INDEX, Arguments>::type; };
template<size_t INDEX> struct TupleElement<INDEX, false> { using Type = void; }; // void iff INDEX out of range
template<size_t INDEX> using ArgType = typename TupleElement<HAS_THIS + INDEX, (INDEX < N_ARGS)>::Type;
template<size_t INDEX> using ConvertType = decltype (Convert<ArgType<INDEX>>::from_json (std::declval<JsonValue>()));
template<size_t INDEX> static ConvertType<INDEX>
arg_from_json (const CallbackInfo &args)
{
return Convert<ArgType<INDEX>>::from_json (args.ntharg (HAS_THIS + INDEX));
}
template<typename T, size_t ...INDICES> static typename FunctionTraits<F>::ReturnType
call_unpacked (T &obj, const F &func, const CallbackInfo &args, std::index_sequence<INDICES...>)
{
return (obj.*func) (arg_from_json<INDICES> (args)...);
}
};
// == call_from_json ==
template<typename T, typename F> static inline typename FunctionTraits<F>::ReturnType
call_from_json (T &obj, const F &func, const CallbackInfo &args)
{
using CallTraits = CallTraits<F>;
return CallTraits::call_unpacked (obj, func, args, std::make_index_sequence<CallTraits::N_ARGS>());
}
// == InstanceMap ==
class InstanceMap {
struct TypeidKey {
const std::type_index tindex; void *ptr;
bool
operator< (const TypeidKey &other) const noexcept
{
return tindex < other.tindex || (tindex == other.tindex && ptr < other.ptr);
}
};
public:
class Wrapper {
virtual TypeidKey typeid_key () = 0;
virtual ~Wrapper () {}
friend class InstanceMap;
public:
virtual Closure* lookup_closure (const char *method) = 0;
virtual void try_upcast (const std::string &baseclass, void *sptrB) = 0;
};
private:
template<typename T>
class InstanceWrapper : public Wrapper {
std::shared_ptr<T> sptr_;
virtual
~InstanceWrapper ()
{
// printf ("InstanceMap::Wrapper: %s: deleting %s wrapper: %p\n", __func__, rtti_typename<T>().c_str(), sptr_.get());
}
public:
explicit InstanceWrapper (const std::shared_ptr<T> &sptr) : sptr_ (sptr) {}
Closure* lookup_closure (const char *method) override { return Class<T>::lookup_closure (method); }
TypeidKey typeid_key () override { return create_typeid_key (sptr_); }
void try_upcast (const std::string &baseclass, void *sptrB) override
{ Class<T>::try_upcast (sptr_, baseclass, sptrB); }
static TypeidKey
create_typeid_key (const std::shared_ptr<T> &sptr)
{
return { typeid (T), sptr.get() };
}
};
using WrapperMap = std::unordered_map<size_t, Wrapper*>;
using TypeidMap = std::map<TypeidKey, size_t>;
WrapperMap wmap_;
TypeidMap typeid_map_;
static size_t next_counter() { static size_t counter_ = 0; return ++counter_; }
public:
~InstanceMap()
{
WrapperMap old;
std::swap (old, wmap_);
typeid_map_.clear();
for (auto &pair : old)
{
Wrapper *wrapper = pair.second;
delete wrapper;
}
JSONIPC_ASSERT_RETURN (wmap_.size() == 0); // deleters shouldn't re-add
JSONIPC_ASSERT_RETURN (typeid_map_.size() == 0); // deleters shouldn't re-add
}
template<typename T> static size_t
make_wrapper_id (const std::shared_ptr<T> &sptr)
{
JSONIPC_ASSERT_RETURN (sptr.get() != nullptr, 0);
const TypeidKey tkey = InstanceWrapper<T>::create_typeid_key (sptr);
InstanceMap *imap = Scope::instance_map();
auto it = imap->typeid_map_.find (tkey);
if (it != imap->typeid_map_.end())
return it->second;
const size_t id = next_counter();
imap->wmap_[id] = new InstanceWrapper<T> (sptr);
imap->typeid_map_[tkey] = id;
return id;
/* A note about TypeidKey:
* Two tuples (TypeX,ptr0x123) and (TypeY,ptr0x123) holding the same pointer address can
* occur if the RTII lookup to determine the actual Wrapper class fails, e.g. when
* Class<MostDerived> is unregisterd. In this case, ptr0x123 can be wrapped multiple
* times through different base classes.
*/
}
static bool
forget_id (size_t thisid)
{
InstanceMap *imap = Scope::instance_map();
auto &wmap_ = imap->wmap_;
auto &typeid_map_ = imap->typeid_map_;
const auto w = wmap_.find (thisid);
if (w != wmap_.end())
{
Wrapper *wrapper = w->second;
wmap_.erase (w);
const auto t = typeid_map_.find (wrapper->typeid_key());
if (t != typeid_map_.end())
typeid_map_.erase (t);
delete wrapper;
return true;
}
return false;
}
static Wrapper*
lookup_wrapper (size_t thisid)
{
InstanceMap *imap = Scope::instance_map();
if (imap)
{
auto &wmap_ = imap->wmap_;
auto it = wmap_.find (thisid);
if (it != wmap_.end())
return it->second;
}
return nullptr;
}
};
inline Closure*
CallbackInfo::find_closure (const char *methodname)
{
InstanceMap::Wrapper *wrapper = InstanceMap::lookup_wrapper (thisid());
return wrapper ? wrapper->lookup_closure (methodname) : NULL;
}
// == ClassPrinter ==
struct ClassPrinter {
static bool recording () { return recording_(); }
static void recording (bool toggle) { recording_ (toggle); }
enum Tag { ENUMS = 1, CLASSES, SERIALIZABLE };
ClassPrinter (Tag tag) :
tag_ (tag)
{
auto &printers = printers_();
printers.push_back (this);
}
static std::string
to_string()
{
std::string all;
for (auto &p : printers_())
{
p->done();
all += p->out_;
}
return all;
}
/// Yield the Javascript identifier name by substituting ':+' with '.'
static std::string
normalize_typename (const std::string &string)
{
std::string normalized;
auto is_identifier_char = [] (int ch) {
return ( (ch >= 'A' && ch <= 'Z') ||
(ch >= 'a' && ch <= 'z') ||
(ch >= '0' && ch <= '9') ||
ch == '_' || ch == '$' );
};
for (size_t i = 0; i < string.size() && string[i]; ++i)
if (is_identifier_char (string[i]))
normalized += string[i];
else if (normalized.size() && normalized[normalized.size() - 1] != '.')
normalized += '.';
return normalized;
}
void
done()
{
advance_state (0); // force close
}
void
print (const std::string &classname, const std::string &what, const std::string &name, int64_t count)
{
if (what == "new")
{
JSONIPC_ASSERT_RETURN (classname_.empty() && !classname.empty());
classname_ = classname;
jsclass_ = canonify (classname);
jsalias_ = name.empty() ? "" : canonify (name);
out_ += "\nexport class " + jsclass_;
if (jsclass_ != classname_)
out_ += " // " + classname_;
out_ += "\n";
state_++; // start class
}
if (what == "inherit")
{
advance_state (1); // start class
if (jsprinter_extends)
out_ += " /* extends " + name + " */\n";
else
out_ += " extends " + canonify (name) + "\n";
jsprinter_extends = true;
}
if (what == "method")
{
advance_state (2); // force class open
std::string args;
for (int i = 0; i < count; i++)
args += (i ? ", " : "") + string_format ("a%d", i + 1);
out_ += string_format (" %s (%s) { return $jsonipc.send ('%s', [this%s%s]); }\n",
name.c_str(), args.c_str(), name.c_str(), args.empty() ? "" : ", ", args.c_str());
}
if (what == "get")
{
advance_state (2); // force class open
out_ += string_format (" async %s (v) { return await (arguments.length > 0 ? $jsonipc.send ('set/%s', [this, await v]) : $jsonipc.send ('get/%s', [this])); }\n",
name.c_str(), name.c_str(), name.c_str());
#if 0 // async property get
out_ += string_format (" get %s () { return $jsonipc.send ('get/%s', [this]); }\n",
name.c_str(), name.c_str());
#endif
}
if (what == "set")
{
advance_state (2); // force class open
#if 0 // async property set
out_ += string_format (" set %s (v) { (async () => $jsonipc.send ('set/%s', [this, await v])) (); }\n",
name.c_str(), name.c_str());
#endif
}
if (what == "attribute")
serializable_attributes.push_back (name);
if (what == "enumvalue")
{
done(); // enum values are defined *after* the class
std::string jsname = normalize_typename (classname_ + "." + name);
out_ += string_format ("%s.%s = \"%s\"; // %d\n", jsclass_.c_str(), name.c_str(), jsname.c_str(), count);
}
if (what == "done")
advance_state (0); // force close
}
private:
/// Enforce a canonical charset for a string.
static std::string
canonify (const std::string &string)
{
const char *const valids = "abcdefghijklmnopqrstuvwxyz" "0123456789" "ABCDEFGHIJKLMNOPQRSTUVWXYZ" "_$";
const char *const substitute = "_";
const size_t l = string.size();
const char *p = string.c_str();
for (size_t i = 0; i < l; i++)
if (!strchr (valids, p[i]))
{
// rewrite_string:
std::string d = string.substr (0, i);
bool collapse = true; // collapse substitutions
d += substitute;
for (++i; i < l; i++)
if (strchr (valids, p[i]))
{
d += p[i];
collapse = false;
}
else if (!collapse)
{
d += substitute;
collapse = true;
}
return d;
}
// else, pass through
return string;
}
void
advance_state (int next)
{
if (state_ == next)
return;
if (next == 0)
next = 999;
if (state_ == 0 && state_ < next) // no class yet
{
JSONIPC_ASSERT_RETURN (!"mixed Class<> setters");
state_++; // class started
}
if (state_ == 1 && state_ < next) // class started
{
if (tag_ == SERIALIZABLE)
{
out_ += "{\n";
}
else if (tag_ == CLASSES)
{
out_ += "{\n constructor ($id) { ";
if (jsprinter_extends)
out_ += "super ($id); ";
else
out_ += "Object.defineProperty (this, '$id', { value: $id }); ";
out_ += "if (new.target === " + jsclass_ + ") Object.freeze (this); ";
out_ += "}\n";
}
else if (tag_ == ENUMS)
out_ += "{";
state_++; // class open
}
if (state_ == 2 && state_ < next) // class open
{
if (tag_ == SERIALIZABLE) // close serializable_
{
out_ += " constructor (";
uint n = 0;
for (auto &prop : serializable_attributes)
out_ += (n++ ? ", " : "") + prop;
out_ += ") {\n";
if (jsprinter_extends)
out_ += " super ();\n";
for (auto &prop : serializable_attributes) // attributes are defined *inside* the constructor
out_ += string_format (" this.%s = %s;\n", prop.c_str(), prop.c_str());
out_ += " }\n";
}
out_ += "}\n";
if (tag_ == CLASSES || // support '$class' lookups
tag_ == SERIALIZABLE)
out_ += "$jsonipc.classes['" + classname_ + "'] = " + jsclass_ + ";\n";
if (!jsalias_.empty())
out_ += "export const " + jsalias_ + " = " + jsclass_ + ";\n";
state_++; // class closed
}
if (state_ < next)
state_ = 0; // close state
}
static std::vector<ClassPrinter*>& printers_() { static std::vector<ClassPrinter*> printers; return printers; }
std::vector<std::string> serializable_attributes;
std::string classname_, jsclass_, jsalias_, out_;
int state_ = 0;
Tag tag_ = Tag (0);
bool jsprinter_extends = false;
static bool recording_ (int v = -1) { static bool r = false; if (v >= 0) r = v; return r; }
};
// == TypeInfo ==
class TypeInfo {
ClassPrinter *printer_ = NULL;
protected:
virtual ~TypeInfo() {}
virtual ClassPrinter* create_printer () = 0;
void
print (const std::string &classname, const std::string &what, const std::string &name, int64_t count = 0)
{
if (!ClassPrinter::recording())
return;
if (!printer_)
printer_ = create_printer();
printer_->print (classname, what, name, count);
}
};
// == Enum ==
template<typename T>
struct Enum : TypeInfo {
static_assert (std::is_enum<T>::value, "");
virtual ClassPrinter* create_printer () override { return new ClassPrinter (ClassPrinter::ENUMS); }
using UnderlyingType = typename std::underlying_type<T>::type;
Enum (const std::string &altname = "")
{
const std::string class_name = rtti_typename<T>();
print (class_name, "new", altname);
}
Enum&
set (T v, const char *valuename)
{
const std::string class_name = typename_of<T>();
auto &entries_ = entries();
Entry e { ClassPrinter::normalize_typename (class_name + "." + valuename), v };
entries_.push_back (e);
print (class_name, "enumvalue", valuename, UnderlyingType (v));
return *this;
}
static bool
has_names ()
{
return !entries().empty();
}
static const std::string&
get_name (T v)
{
auto &entries_ = entries();
for (const auto &e : entries_)
if (v == e.value)
return e.name;
static std::string empty;
return empty;
}
static T
get_value (const std::string &name, T fallback)
{
auto c_isalnum = [] (char c) {
return (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') || (c >= '0' && c <= '9');
};
auto &entries_ = entries();
for (const auto &e : entries_)
if (name == e.name || // exact match, or
(name.size() < e.name.size() && // name starts at e.name word boundary
!c_isalnum (e.name[e.name.size() - name.size() - 1]) && // and matches the tail of e.name
e.name.compare (e.name.size() - name.size(), name.size(), name) == 0))
return e.value;
return fallback;
}
private:
struct Entry { const std::string name; T value; };
static std::vector<Entry>& entries() { static std::vector<Entry> entries_; return entries_; }
template<typename U> static std::string
typename_of()
{
using Type = typename std::decay<U>::type;
return rtti_typename<Type>();
}
};
// enum types
template<typename T>
struct Convert<T, REQUIRESv< std::is_enum<T>::value > > {
using UnderlyingType = typename std::underlying_type<T>::type;
static T
from_json (const JsonValue &value, T fallback = T())
{
if (value.IsString())
{
using EnumType = Enum<T>;
const std::string string = Convert<std::string>::from_json (value);
return EnumType::get_value (string, fallback);
}
return T (Convert<UnderlyingType>::from_json (value, UnderlyingType (fallback)));
}
static JsonValue
to_json (T evalue, JsonAllocator &allocator)
{
using EnumType = Enum<T>;
if (EnumType::has_names())
{
const std::string &name = EnumType::get_name (evalue);
if (!name.empty())
return Convert<std::string>::to_json (name, allocator);
}
return Convert<UnderlyingType>::to_json (UnderlyingType (evalue), allocator);
}
};
// == Serializable ==
/// Jsonipc wrapper type for objects that support field-wise serialization to/from JSON.
template<typename T>
struct Serializable : TypeInfo {
virtual ClassPrinter* create_printer () override { return new ClassPrinter (ClassPrinter::SERIALIZABLE); }
/// Allow object handles to be streamed to/from Javascript, needs a Scope for temporaries.
Serializable (const std::string &altname = "")
{
const std::string class_name = rtti_typename<T>();
print (class_name, "new", altname);
make_serializable<T>();
}
/// Add a member object pointer
template<typename A, REQUIRES< std::is_member_object_pointer<A>::value > = true> Serializable&
set (const char *name, A attribute)
{
using SetterAttributeType = typename FunctionTraits<A>::ReturnType;
Accessors accessors;
accessors.setter = [attribute] (T &obj, const JsonValue &value) -> void { obj.*attribute = from_json<SetterAttributeType> (value); };
accessors.getter = [attribute] (const T &obj, JsonAllocator &a) -> JsonValue { return to_json (obj.*attribute, a); };
AccessorMap &amap = accessormap();
auto it = amap.find (name);
if (it != amap.end())
throw std::runtime_error ("duplicate attribute registration: " + std::string (name));
amap.insert (std::make_pair<std::string, Accessors> (name, std::move (accessors)));
const std::string class_name = rtti_typename<T>();
print (class_name, "attribute", name, 0);
return *this;
}
static bool is_serializable () { return serialize_from_json_() && serialize_to_json_(); }
static JsonValue serialize_to_json (const T &o, JsonAllocator &a) { return serialize_to_json_() (o, a); }
static std::shared_ptr<T> serialize_from_json (const JsonValue &value) { return serialize_from_json_() (value); }
private:
struct Accessors {
std::function<void (T&,const JsonValue&)> setter;
std::function<JsonValue (const T&, JsonAllocator&)> getter;
};
using AccessorMap = std::map<std::string, Accessors>;
static AccessorMap& accessormap() { static AccessorMap amap; return amap; }
template<typename U> static void
make_serializable()
{
// implement serialize_from_json by calling all setters
SerializeFromJson sfj = [] (const JsonValue &value) -> std::shared_ptr<T> {
std::shared_ptr<T> obj = Scope::make_shared<T>();
if (!obj)
return obj;
AccessorMap &amap = accessormap();
for (const auto &field : value.GetObject())
{
const std::string field_name = field.name.GetString();
auto it = amap.find (field_name);
if (it == amap.end())
continue;
Accessors &accessors = it->second;
accessors.setter (*obj, field.value);
}
return obj;
};
serialize_from_json_() = sfj;
// implement serialize_to_json by calling all getters
SerializeToJson stj = [] (const T &object, JsonAllocator &allocator) -> JsonValue {
JsonValue jobject (rapidjson::kObjectType); // serialized result
jobject.AddMember ("__typename__", JsonValue (get___typename__ (object).c_str(), allocator), allocator);
AccessorMap &amap = accessormap();
for (auto &it : amap)
{
const std::string field_name = it.first;
Accessors &accessors = it.second;
JsonValue result = accessors.getter (object, allocator);
jobject.AddMember (JsonValue (field_name.c_str(), allocator), result, allocator);
}
return jobject;
};
serialize_to_json_() = stj;
}
using SerializeFromJson = std::function<std::shared_ptr<T> (const JsonValue&)>;
using SerializeToJson = std::function<JsonValue (const T&, JsonAllocator&)>;
static SerializeFromJson& serialize_from_json_ () { static SerializeFromJson impl; return impl; }
static SerializeToJson& serialize_to_json_ () { static SerializeToJson impl; return impl; }
};
// == Helper for known derived classes by RTTI typename ==
using WrapObjectFromBase = size_t (const std::string&, void*);
// This *MUST* use `extern inline` for the ODR to apply to its `static` variable
extern inline WrapObjectFromBase*
can_wrap_object_from_base (const std::string &rttiname, WrapObjectFromBase *handler = nullptr)
{
static std::map<std::string, WrapObjectFromBase*> downcastwrappers;
if (handler)
{
downcastwrappers[rttiname] = handler;
return handler;
}
auto it = downcastwrappers.find (rttiname);
return it != downcastwrappers.end() ? it->second : nullptr;
}
// == Class ==
template<typename T>
struct Class : TypeInfo {
virtual ClassPrinter* create_printer () override { return new ClassPrinter (ClassPrinter::CLASSES); }
Class (const std::string &altname = "")
{
const std::string class_name = classname();
print (class_name, "new", altname);
}
// Inherit base class `B`
template<typename B> Class&
inherit()
{
add_base<B>();
print (classname(), "inherit", rtti_typename<B>(), 0);