-
Notifications
You must be signed in to change notification settings - Fork 0
/
bomba_core.hpp
1235 lines (1066 loc) · 43.6 KB
/
bomba_core.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#ifndef BOMBA_CORE
#define BOMBA_CORE
#include <optional>
#include <array>
#include <string_view>
#include <span>
#include <tuple>
#include <cstring>
#ifndef BOMBA_ALTERNATIVE_ERROR_HANDLING
#include <stdexcept>
#endif
namespace Bomba {
#ifndef BOMBA_ALTERNATIVE_ERROR_HANDLING
struct ParseError : std::runtime_error {
using std::runtime_error::runtime_error;
};
void parseError(std::string_view problem) {
throw ParseError(std::string(problem));
}
struct MethodNotFoundError : std::runtime_error {
using std::runtime_error::runtime_error;
};
void methodNotFoundError(std::string_view problem) {
throw MethodNotFoundError(std::string(problem));
}
struct RemoteError : std::runtime_error {
using std::runtime_error::runtime_error;
};
void remoteError(std::string_view problem) {
throw RemoteError(std::string(problem));
}
void logicError(std::string_view problem) {
throw std::logic_error(std::string(problem));
}
#endif
template <typename T = void()>
struct Callback {
// This should never be instantiated
static_assert(std::is_same_v<T, T>, "Invalid callback signature");
};
template <typename Functor, typename Returned, typename... Args>
concept FunctorForCallbackConstruction = requires(Functor functor, const Args&... args) {
{ functor(args...) } -> std::same_as<Returned>;
};
template <typename Returned, typename... Args>
class Callback<Returned(Args...)> {
void* closure = nullptr;
Returned (*typeErased)(void*, const Args&...) = [] (void*, const Args&...) {};
public:
Returned operator() (const Args&... args) const {
return typeErased(closure, args...);
}
Callback() = default;
template <FunctorForCallbackConstruction<Returned, Args...> T>
Callback(const T& lambda)
: closure(reinterpret_cast<void*>(const_cast<T*>(&lambda)))
, typeErased([] (void* closure, const Args&... args) {
return reinterpret_cast<T*>(closure)->operator()(args...);
}) {}
};
template <typename Resource, typename Destruction>
auto makeRaiiContainer(Resource&& resource, Destruction&& destruction) {
class RaiiContainer {
Resource _resource;
Destruction _destruction;
public:
RaiiContainer(Resource&& resource, Destruction&& destruction)
: _resource(std::move(resource)), _destruction(std::move(destruction)) {}
~RaiiContainer() {
_destruction();
}
Resource* operator->() {
return _resource;
}
Resource& operator*() {
return *_resource;
}
};
return RaiiContainer(std::move(resource), std::move(destruction));
}
struct Float16Placeholder {
uint16_t data;
Float16Placeholder& operator=(float value) {
uint32_t asInt = reinterpret_cast<uint32_t&>(value);
data = ((asInt >> 16) & 0x8000) | ((((asInt & 0x7f800000) - 0x38000000) >> 13 ) & 0x7c00) | ((asInt >> 13) & 0x03ff);
return *this;
}
operator float() const {
return ((data & 0x8000) << 16) | (((data & 0x7c00) + 0x1C000) << 13) | ((data & 0x03ff) << 13);
}
};
namespace SerialisationFlags {
// Flags for altering the behaviour of ISerialisedOutput and ISerialisedInput interfaces
enum Flags {
NONE = 0, // No flags, default value
OBJECT_LAYOUT_KNOWN = 0x1, // Declares that the data is a known type and doesn't need names of elements
MANDATORY = 0x2, // Fail instead of returning default values even if the format supports it (not implemented)
OMIT_FALSE = 0x4, // Skip the entry if it's bool and it's false
EMPTY_IS_NULL = 0x8, // Skip the entry if it's null
// Declare the specific numeric type for formats where it matters (mutually exclusive)
INT_8 = 0x100,
UINT_8 = 0x110,
INT_16 = 0x120,
UINT_16 = 0x130,
INT_32 = 0x140,
UINT_32 = 0x150,
INT_64 = 0x160,
UINT_64 = 0x170,
FLOAT_16 = 0x180,
FLOAT_32 = 0x190,
FLOAT_64 = 0x1a0,
DETERMINED_NUMERIC_TYPE = 0x1f0, // Has all the bits used by numeric types at 1
};
template <typename T>
Flags typeToFlags(T) {
if constexpr(std::is_same_v<T, int8_t>) return INT_8;
else if constexpr(std::is_same_v<T, uint8_t>) return UINT_8;
else if constexpr(std::is_same_v<T, int16_t>) return INT_16;
else if constexpr(std::is_same_v<T, uint16_t>) return UINT_16;
else if constexpr(std::is_same_v<T, int32_t>) return INT_32;
else if constexpr(std::is_same_v<T, uint32_t>) return UINT_32;
else if constexpr(std::is_same_v<T, int64_t>) return INT_64;
else if constexpr(std::is_same_v<T, uint64_t>) return UINT_64;
else if constexpr(std::is_same_v<T, Float16Placeholder>) return FLOAT_16;
else if constexpr(std::is_same_v<T, float>) return FLOAT_32;
else if constexpr(std::is_same_v<T, double>) return FLOAT_64;
}
template <typename Functor>
auto typeWithFlags(Flags flags, const Functor& callback) {
Flags cleaned = Flags(flags & DETERMINED_NUMERIC_TYPE);
if (!cleaned)
cleaned = INT_32;
if (cleaned == INT_8) return callback(int8_t());
else if (cleaned == UINT_8) return callback(uint8_t());
else if (cleaned == INT_16) return callback(int16_t());
else if (cleaned == UINT_16) return callback(uint16_t());
else if (cleaned == INT_32) return callback(int32_t());
else if (cleaned == UINT_32) return callback(uint32_t());
else if (cleaned == INT_64) return callback(int64_t());
else if (cleaned == UINT_64) return callback(uint64_t());
else if (cleaned == FLOAT_16) return callback(Float16Placeholder());
else if (cleaned == FLOAT_32) return callback(float());
else if (cleaned == FLOAT_64) return callback(double());
else throw std::logic_error("Weird numeric type");
}
};
template <typename T>
struct TypedSerialiser {
static_assert(!std::is_same_v<T, T>, "No specialisation for serialising this type");
};
struct IStructuredOutput {
// Used by code that writes data or messages, implement it to create an output format
using Flags = SerialisationFlags::Flags;
// Should write an integer to the output
virtual void writeInt(Flags flags, int64_t value) = 0;
// Should write a floating-point number to the output
virtual void writeFloat(Flags flags, double value) = 0;
// Should write a string to the output
virtual void writeString(Flags flags, std::string_view value) = 0;
// Should write a boolean variable to the output
virtual void writeBool(Flags flags, bool value) = 0;
// Should write a null value to the output
virtual void writeNull(Flags flags) = 0;
// Array is written as startWritingArray, [introduceObjectElement, writeInt (or any other type)], endWritingArray
constexpr static int UNKNOWN_SIZE = -1;
// Called just before writing an array, if size is not known in advance, UNKNOWN_SIZE is used as size
virtual void startWritingArray(Flags flags, int size) = 0;
// Called before writing the value of any array element
virtual void introduceArrayElement(Flags flags, int index) = 0;
// Called after writing the last value in the array
virtual void endWritingArray(Flags flags) = 0;
// Object is written as startWritingObject, [introduceObjectMember, writeInt (or any other type], endWritingObject
// Called just before writing an object, if number of elements is not known in advance, UNKNOWN_SIZE is used as size
virtual void startWritingObject(Flags flags, int size) = 0;
// Called before writing the value of any object element, name of element is needed (otherwise it's an array)
virtual void introduceObjectMember(Flags flags, std::string_view name, int index) = 0;
// Called after writing the last value in the object
virtual void endWritingObject(Flags flags) = 0;
// Should write an optional value, empty if it's absent and calling the callback to write the value if present
virtual void writeOptional(Flags flags, bool present, Callback<> writeValue) = 0;
virtual ~IStructuredOutput() = default;
// Convenience classes and methods for using this interface in a less error prone way
class ArrayFiller {
int _index = 0;
IStructuredOutput& _output;
static constexpr SerialisationFlags::Flags NoFlags = SerialisationFlags::NONE;
ArrayFiller(IStructuredOutput& output, int size) : _output(output) {
output.startWritingArray(NoFlags, size);
}
friend struct IStructuredOutput;
public:
~ArrayFiller() {
_output.endWritingArray(NoFlags);
}
void writeInt(int64_t value) { introduceMember().writeInt(NoFlags, value); }
void writeFloat(double value) { introduceMember().writeFloat(NoFlags, value); }
void writeString(std::string_view value) { introduceMember().writeString(NoFlags, value); }
void writeBool(bool value) { introduceMember().writeBool(NoFlags, value); }
void writeNull() { introduceMember().writeNull(NoFlags); }
ArrayFiller writeArray(int size = UNKNOWN_SIZE) {
return ArrayFiller(introduceMember(), size);
}
auto writeObject(int size = UNKNOWN_SIZE);
IStructuredOutput& underlyingOutput() {
return _output;
}
IStructuredOutput& introduceMember() {
_output.introduceArrayElement(NoFlags, _index);
_index++;
return _output;
}
};
ArrayFiller writeArray(int size = UNKNOWN_SIZE) {
return ArrayFiller(*this, size);
}
class ObjectFiller {
int _index = 0;
IStructuredOutput& _output;
static constexpr SerialisationFlags::Flags NoFlags = SerialisationFlags::NONE;
ObjectFiller(IStructuredOutput& output, int size) : _output(output) {
output.startWritingObject(NoFlags, size);
}
friend struct IStructuredOutput;
public:
~ObjectFiller() {
_output.endWritingObject(NoFlags);
}
void writeInt(std::string_view name, int64_t value) { introduceMember(name).writeInt(NoFlags, value); }
void writeFloat(std::string_view name, double value) { introduceMember(name).writeFloat(NoFlags, value); }
void writeString(std::string_view name, std::string_view value) { introduceMember(name).writeString(NoFlags, value); }
void writeBool(std::string_view name, bool value) { introduceMember(name).writeBool(NoFlags, value); }
void writeNull(std::string_view name) { introduceMember(name).writeNull(NoFlags); }
ArrayFiller writeArray(std::string_view name, int size = UNKNOWN_SIZE) {
return ArrayFiller(introduceMember(name), size);
}
ObjectFiller writeObject(std::string_view name, int size = UNKNOWN_SIZE) {
return ObjectFiller(introduceMember(name), size);
}
IStructuredOutput& underlyingOutput() {
return _output;
}
IStructuredOutput& introduceMember(std::string_view name) {
_output.introduceObjectMember(NoFlags, name, _index);
_index++;
return _output;
}
};
ObjectFiller writeObject(int size = UNKNOWN_SIZE) {
return ObjectFiller(*this, size);
}
};
auto IStructuredOutput::ArrayFiller::writeObject(int size) {
return ObjectFiller(introduceMember(), size);
}
struct NullStructredOutput : IStructuredOutput {
void writeInt(Flags, int64_t) final override {}
void writeFloat(Flags, double) final override {}
void writeString(Flags, std::string_view) final override {}
void writeBool(Flags, bool) final override {}
void writeNull(Flags) final override {}
void startWritingArray(Flags, int) final override {}
void introduceArrayElement(Flags, int) final override {}
void endWritingArray(Flags) final override {}
void startWritingObject(Flags, int) final override {}
void introduceObjectMember(Flags, std::string_view, int) final override {}
void endWritingObject(Flags) final override {}
void writeOptional(Flags, bool, Callback<>) final override {}
};
struct IStructuredInput {
// Used by code that reads data or messages, implement it to create an input format
using Flags = SerialisationFlags::Flags;
enum MemberType {
TYPE_INTEGER,
TYPE_FLOAT,
TYPE_STRING,
TYPE_BOOLEAN,
TYPE_NULL,
TYPE_ARRAY,
TYPE_OBJECT,
TYPE_INVALID, // Other values are not guaranteed to be valid if the data is invalid
};
bool good = true;
// Should return the type of the following element
virtual MemberType identifyType(Flags flags) = 0;
// Should parse an integer and return it (exact type may be supplied in flags)
virtual int64_t readInt(Flags flags) = 0;
// Should parse a floating point number and return it (exact type may be supplied in flags)
virtual double readFloat(Flags flags) = 0;
// Should parse a string and return a view of it (size type may be supplied in flags)
virtual std::string_view readString(Flags flags) = 0;
// Should parse a boolean and return it
virtual bool readBool(Flags flags) = 0;
// Should read a null (only to get the reading position behind it)
virtual void readNull(Flags flags) = 0;
// Should read an array, will be called as startReadingArray, [nextArrayElement, readInt (or something else)], endReadingArray
// Should start reading an array
virtual void startReadingArray(Flags flags) = 0;
// Should read the introduction of the next array element, returns if it's the last element
virtual bool nextArrayElement(Flags flags) = 0;
// Should end reading an array
virtual void endReadingArray(Flags flags) = 0;
// TODO: Refactor to more functional style
// Should read an object, calling the supplied callback on each element, with name (if available) and index
// Should end on the last element and when the callback returns 0
virtual void readObject(Flags flags, Callback<bool(std::optional<std::string_view> memberName, int index)> onEach) = 0;
// Skips the next value
virtual void skipObjectElement(Flags flags) = 0;
// Should call the functor on the value if it's present and return if it was present
virtual bool readOptional(Flags flags, Callback<> readValue) = 0;
struct Location {
constexpr static int UNINITIALISED = -1;
int loc = UNINITIALISED;
operator bool() {
return (loc != UNINITIALISED);
}
};
// Should return the current position to be returned there later
virtual Location storePosition(Flags flags) = 0;
// Should come back to a previously stored position
virtual void restorePosition(Flags flags, Location location) = 0;
};
template<int Size>
struct StringLiteral {
constexpr StringLiteral(const char (&str)[Size]) {
std::copy_n(str, Size, value);
}
constexpr int size() const {
return Size - 1;
}
constexpr const char* c_str() const {
return value;
}
constexpr char& operator[](int index) {
return value[index];
}
constexpr char operator[](int index) const {
return value[index];
}
constexpr bool operator==(const char* other) const {
return compare(other);
}
template <int Size2>
constexpr bool operator==(const StringLiteral<Size2>& other) const {
if constexpr(Size != Size2)
return false;
else
return compare(other.c_str());
}
constexpr operator std::string_view() const {
return std::string_view(c_str());
}
char value[Size];
private:
template <int depth = 0>
constexpr bool compare(const char* other) const {
if constexpr(depth == Size)
return true;
else {
if (value[depth] != *other)
return false;
return compare<depth + 1>(other + 1);
}
}
};
class GeneralisedBuffer {
using BufferType = std::span<char>;
BufferType _buffer;
int _size = 0;
public:
GeneralisedBuffer(const BufferType& buffer) : _buffer(buffer) {}
GeneralisedBuffer& operator+=(char added) {
*_buffer.begin() = added;
_buffer = BufferType(_buffer.begin() + 1, _buffer.end());
_size++;
if (_buffer.size() == 0) {
bufferFull();
}
return *this;
}
GeneralisedBuffer& operator+=(std::span<const char> added) {
int position = 0;
while (position < int(added.size())) {
int toCopy = std::min<int>(added.size() - position, remainingSpace());
memcpy(_buffer.data(), added.data() + position, toCopy);
_buffer = BufferType(_buffer.begin() + toCopy, _buffer.end());
_size += toCopy;
if (_buffer.size() == 0) [[likely]] {
if (!bufferFull()) return *this;
}
position += toCopy;
}
return *this;
}
GeneralisedBuffer& operator+=(std::string_view added) {
return operator+=(std::span<const char>(added.begin(), added.end()));
}
GeneralisedBuffer& operator+=(const char* added) {
return operator+=(std::span<const char>(added, strlen(added)));
}
int size() {
return _size;
}
protected:
virtual bool bufferFull() = 0;
void moveBuffer(const BufferType& newBuffer) {
_buffer = newBuffer;
}
int remainingSpace() const {
return _buffer.size();
}
};
template <int StaticSize = 1024>
class StreamingBuffer : public GeneralisedBuffer {
protected:
std::array<char, StaticSize> _basic;
int _sizeAtLastFlush = 0;
bool bufferFull() override {
flush();
_sizeAtLastFlush = size();
moveBuffer({_basic.data(), StaticSize});
return true;
}
virtual void flush() = 0;
public:
StreamingBuffer() : GeneralisedBuffer({reinterpret_cast<char*>(&_basic), sizeof(_basic)}) { }
};
template <int StaticSize>
struct NonOwningStreamingBuffer : StreamingBuffer<StaticSize> {
Callback<void(std::span<const char>)> writer;
void flush() override {
if (this->size() > this->_sizeAtLastFlush) {
writer({this->_basic.data(), size_t(this->size() - this->_sizeAtLastFlush)});
this->_sizeAtLastFlush = this->size();
}
}
NonOwningStreamingBuffer(Callback<void(std::span<const char>)> writer) : writer(writer) {}
~NonOwningStreamingBuffer() {
flush();
}
};
template <int StaticSize = 1024>
class NonExpandingBuffer : public GeneralisedBuffer {
std::array<char, StaticSize> _basic;
bool bufferFull() override {
return false;
}
public:
NonExpandingBuffer() : GeneralisedBuffer({reinterpret_cast<char*>(&_basic), sizeof(_basic)}) {}
operator std::span<char>() {
return std::span<char>(_basic.data(), _basic.size() - remainingSpace());
}
operator std::string_view() const {
return {_basic.data(), _basic.size() - remainingSpace()};
}
void clear() {
moveBuffer({_basic.data(), _basic.size()});
}
};
template <typename V>
concept CharVectorType = requires(V v) {
{ v.data() } -> std::same_as<char*>;
{ v.size() } -> std::integral;
v.resize(0);
{ std::span<char>{ v.begin(), v.end() - 1 } };
};
template <int StaticSize = 1024, CharVectorType Vector = std::string>
class ExpandingBuffer : public GeneralisedBuffer {
std::array<char, StaticSize> _basic;
Vector _extended;
bool bufferFull() override {
if (_extended.empty()) {
_extended.resize(3 * StaticSize);
memcpy(_extended.data(), _basic.data(), StaticSize);
moveBuffer({&_extended[StaticSize], 2 * StaticSize});
} else {
size_t oldSize = _extended.size();
_extended.resize(2 * oldSize);
moveBuffer({&_extended[oldSize], oldSize});
}
return true;
}
public:
ExpandingBuffer() : GeneralisedBuffer({reinterpret_cast<char*>(&_basic), sizeof(_basic)}) {}
operator std::span<char>() {
if (_extended.empty()) {
return {_basic.begin(), _basic.end() - remainingSpace()};
} else {
return std::span<char>{_extended.begin(), _extended.end() - remainingSpace()};
}
}
operator std::string_view() const {
if (_extended.empty()) {
return {_basic.begin(), _basic.end() - remainingSpace()};
} else {
return {_extended.begin(), _extended.end() - remainingSpace()};
}
}
void clear() {
_extended.resize(0);
moveBuffer({_basic.data(), _basic.size()});
}
};
template <typename T>
concept AssembledString = requires(T str) {
str += 'a';
str += std::string_view("a");
};
template <typename T>
concept BetterAssembledString = std::is_constructible_v<T>
&& std::is_same_v<T&, decltype(std::declval<T>() += 'a')>
&& std::is_same_v<T&, decltype(std::declval<T>() += "a")>
&& std::is_same_v<T&, decltype(std::declval<T>() += std::string_view("a"))>
&& std::is_same_v<T&, decltype(std::declval<T>() += std::declval<T>())>
&& std::is_convertible_v<T, std::string_view>
&& std::is_void_v<decltype(std::declval<T>().clear())>;
template <typename T>
concept DataFormat = std::is_base_of_v<IStructuredOutput, typename T::Output>
&& std::is_base_of_v<IStructuredInput, typename T::Input>;
struct IPropertyDescriptionFiller {
// Implementing this interface adds a new format for describing structures of objects
// Should introduce a new member with the given name and description and call the functor when it's ready
virtual void addMember(std::string_view name, std::string_view description, Callback<> writer) = 0;
// Should add an integer type to the description
virtual void addInteger() = 0;
// Should add a floating point type to the description
virtual void addFloat() = 0;
// Should add a boolean type to the description
virtual void addBoolean() = 0;
// Should add a string type to the description
virtual void addString() = 0;
// Should add an optional type to the description and use the functor to fill it
virtual void addOptional(Callback<void(IPropertyDescriptionFiller&)> filler) = 0;
// Should add an array type to the description and use the functor to fill it
virtual void addArray(Callback<void(IPropertyDescriptionFiller&)> filler) = 0;
// Should add an already defined object type to the description with the given name and use the functor to fill it
virtual void addSubobject(std::optional<std::string_view> typeName,
Callback<void(IPropertyDescriptionFiller&)> filler) = 0;
};
struct ISerialisableDescriptionFiller {
// Implementing this interface adds a new format for describing nested structures of objects
// Should add another type, usually nested type, using the provided functor
virtual void addMoreTypes(Callback<void(ISerialisableDescriptionFiller&)> otherFiller) = 0;
// Should start describing the current type, with the given name and using the functor in argument
virtual void fillMembers(std::string_view name, Callback<void(IPropertyDescriptionFiller&)> filler) = 0;
};
struct ISerialisable {
// Represents a class that can be serialised and deserialised, usually implemented by Serialisable in bomba_object.hpp
template <DataFormat F>
std::string serialise() {
std::string output;
typename F::Output format(output);
serialiseInternal(format);
return output;
}
template <DataFormat F, typename FromType>
void serialise(FromType& output) {
typename F::Output format(output);
serialiseInternal(format);
return output;
}
template <DataFormat F, typename FromType>
bool deserialise(const FromType& from) {
typename F::Input format(from);
return deserialiseInternal(format);
}
protected:
// Should write the structure's contents using the output format object
virtual void serialiseInternal(IStructuredOutput& format,
SerialisationFlags::Flags flags = SerialisationFlags::NONE) const = 0;
// Should update the structure's contents using the input format object
virtual bool deserialiseInternal(IStructuredInput& format,
SerialisationFlags::Flags flags = SerialisationFlags::NONE) = 0;
template <typename> friend struct TypedSerialiser;
};
struct IDescribableSerialisable : ISerialisable {
virtual void describe([[maybe_unused]] IPropertyDescriptionFiller& filler) const {
// Does nothing if not supported
}
virtual std::string_view getTypeName() const {
return "Unnamed";
}
virtual void listTypes([[maybe_unused]] ISerialisableDescriptionFiller& filler) const {
// Not supported if not overloaded, does nothing
}
template <typename> friend struct TypedSerialiser;
};
struct IMethodDescriptionFiller {
// Implementing this allows adding parameters to autogenerated remote method description for a custom description format
// Should add a parameter to the description
virtual void addParameter(std::string_view name, std::string_view type, std::string_view description, bool mandatory) = 0;
};
struct IRemoteCallableDescriptionFiller {
// Implementing this allows adding methods and internally used objects to autogenerated method descruption for a new format
// Should add a new method with the given name and description, using the provided interface to describe arguments and return values
virtual void addMethod(std::string_view name, std::string_view description,
Callback<void(IPropertyDescriptionFiller&)> paramFiller, Callback<void(IPropertyDescriptionFiller&)> returnFiller) = 0;
// Should add a new internally used object with the given name, using the interface provided by the functor
virtual void addSubobject(std::string_view name, Callback<void(IRemoteCallableDescriptionFiller&)> nestedFiller) = 0;
};
struct IWriteStarter {
// An implementation of this interface should be provided by a class that can deal with both messages whose size is known
// before writing and whose is determined by the write. The purpose is to allow both sending responses whose size is determined
// by serialisation and downloading large files that are usually streamed, while the message needs to start with size.
// Should prepare a buffer whose size will grow or is large enough and call the callback on it
virtual void writeUnknownSize(std::string_view resourceType, Callback<void(GeneralisedBuffer&)> filler) = 0;
// Should prepare a buffer for a message whose size is known in advance and call the callback on it
virtual void writeKnownSize(std::string_view resourceType, int64_t size, Callback<void(GeneralisedBuffer&)> filler) = 0;
};
class IRemoteCallable;
struct UserId {
int id;
};
struct RequestToken {
uint32_t id = 0; // We want it to overflow in a defined way
bool operator==(RequestToken other) const {
return id == other.id;
}
bool operator!=(RequestToken other) const {
return id != other.id;
}
};
struct IRpcResponder {
// Implementing this interface creates clients for a message type
// Should serialise a message from a given user from a given method, supplying the output format and identifier to the callback
virtual RequestToken send(UserId user, const IRemoteCallable* method,
Callback<void(IStructuredOutput&, RequestToken)> request) = 0;
// Should look for a response with a given identifier, construct a parser for it and call the callback on the parser
virtual bool getResponse(RequestToken token, Callback<void(IStructuredInput&)> reader) = 0;
// Should decide if a response with the given identifier is already available and can be accessed without waiting
virtual bool hasResponse(RequestToken) {
return true; // If not sufficiently async, it's sort of always available and getting it causes it to wait
}
// TODO: Add a way to discard a response and make the polling code call it when destroyed, or forgotten responses will pile up
};
class IRemoteCallable {
// Implementing the virtual methods in this class will create a custom callable object
// Doing so is not very practical without something like RPC lambda in bomba_rpc_object.hpp
IRemoteCallable* _parent = nullptr;
IRpcResponder* _responder = nullptr;
protected:
void setSelfAsParent(IRemoteCallable* reparented) {
reparented->_parent = this;
reparented->_responder = _responder;
}
void unsetParent(IRemoteCallable* reparented) {
reparented->_parent = nullptr;
reparented->_responder = nullptr;
}
IRpcResponder* getResponder() const {
if (!_responder && _parent) [[unlikely]]
const_cast<IRpcResponder*&>(_responder) = _parent->getResponder(); // Lazy loading
if (!_responder) [[unlikely]]
logicError("Calling a remote procedure while not being a client");
return _responder;
}
public:
IRemoteCallable(IRemoteCallable* parent = nullptr, IRpcResponder* responder = nullptr)
: _parent(parent), _responder(responder) {}
IRemoteCallable* parent() const {
return _parent;
}
void setResponder(IRpcResponder& responder) {
_responder = &responder;
}
// Should call the function with a prepared input of arguments assumed to be object members (or null), prepared output,
// a functor that starts writing the result, a functor that writes an error message and a user identifier (or nullopt)
virtual bool call([[maybe_unused]] IStructuredInput* arguments, [[maybe_unused]] IStructuredOutput& result,
[[maybe_unused]] Callback<> introduceResult, [[maybe_unused]] Callback<void(std::string_view)> introduceError,
[[maybe_unused]] std::optional<UserId> user = std::nullopt) const {
return false;
}
// Should return the pointer to a child callable with a certain name (recursive access is handled elsewhere)
virtual const IRemoteCallable* getChild([[maybe_unused]] std::string_view name) const {
return nullptr;
}
// Should return the pointer to a child callable with a certain index (recursive access is handled elsewhere)
virtual const IRemoteCallable* getChild([[maybe_unused]] int index) const {
return nullptr;
}
// Returns the name and index of a child with a certain address (to allow a callable to identify itself)
constexpr static int NO_SUCH_STRUCTURE = -1;
virtual std::pair<std::string_view, int> childName([[maybe_unused]] const IRemoteCallable* child) const {
logicError("No such structure");
return {"", NO_SUCH_STRUCTURE};
}
// Uses the supplied interface to describe all composed types used by this function
virtual void listTypes([[maybe_unused]] ISerialisableDescriptionFiller& filler) const {
// Not supported if not overloaded, does nothing
}
// Use the supplied interface to describe the function, its arguments and its return type
virtual void generateDescription([[maybe_unused]] IRemoteCallableDescriptionFiller& filler) const {
// Not supported if not overloaded, does nothing
}
virtual ~IRemoteCallable() = default;
};
template <StringLiteral Separator, BetterAssembledString StringType = std::string>
struct PathWithSeparator {
static const IRemoteCallable* findCallable(std::string_view path, const IRemoteCallable* root) {
auto start = path.begin();
const IRemoteCallable* current = root;
while (start < path.end()) {
auto end = start;
while (end != path.end()) {
for (int i = 0; (end + i) != path.end() && *(end + i) == Separator[i]; i++) {
if (i + 1 == Separator.size()) {
end += i;
goto foundSeparator;
}
}
end++;
}
foundSeparator:;
current = current->getChild(std::string_view(start, end));
if (!current)
return nullptr;
end++;
start = end;
}
return current;
}
static StringType constructPath(const IRemoteCallable* callable) {
if (!callable->parent())
return StringType();
StringType path;
prependPath(callable->parent(), path);
path += callable->parent()->childName(callable).first;
return path;
}
private:
static void prependPath(const IRemoteCallable* callable, StringType& pathSoFar) {
if (!callable->parent())
return;
prependPath(callable->parent(), pathSoFar);
pathSoFar += callable->parent()->childName(callable).first;
pathSoFar += Separator.c_str();
}
};
enum class ServerReaction {
OK,
READ_ON,
WRONG_REPLY,
DISCONNECT,
};
struct ITcpClient {
// Interface for networking clients that communicate using streams, should be able to store identified responses
// that were not accessed yet
// Should write the buffer in argument into the stream
virtual void writeRequest(std::span<char> written) = 0;
// Should look for a response with the identifier in the stream or an already identified response, using the supplied functor
// that parses the stream, taking chunks of data and a flag whether it's already identified, returning information whether it's
// the right message, or it's a wrong one, or incomplete, or if the stream is corrupted, plus the parsed message's identifier
// (if identified) and the size of the message it read (if it could read a message). The functor shall parse zero or one message.
virtual void getResponse(RequestToken token, Callback<std::tuple<ServerReaction, RequestToken, int64_t>
(std::span<char> input, bool identified)> reader) = 0;
// Should look for a response with the identifier in the stream, similarly to getResponse(), but without processing
// the actual response. Intended for periodically polling if a response has arrived. Must be fast if called token.
virtual void tryToGetResponse(RequestToken token, Callback<std::tuple<ServerReaction, RequestToken, int64_t>
(std::span<char> input, bool identified)> reader) {
getResponse(token, reader);
}
};
struct ITcpResponder {
// Interface for classes that can respond to streams of requests.
// Should take a chunk of data and a functor that sends chunks of data and return whether the data is okay or corrupted
// and the number of bytes read (the functor can be called as many times as needed)
virtual std::pair<ServerReaction, int64_t> respond(
std::span<char> input, Callback<void(std::span<const char>)> writer) = 0;
};
// Matching types to the interface
template <std::integral Integer>
struct TypedSerialiser<Integer> {
static void serialiseMember(IStructuredOutput& out, Integer value, SerialisationFlags::Flags flags) {
if (!(flags & SerialisationFlags::DETERMINED_NUMERIC_TYPE))
flags = decltype(flags)(flags | SerialisationFlags::typeToFlags(value));
out.writeInt(flags, value);
}
static void deserialiseMember(IStructuredInput& in, Integer& value, SerialisationFlags::Flags flags) {
if (!(flags & SerialisationFlags::DETERMINED_NUMERIC_TYPE))
flags = decltype(flags)(flags | SerialisationFlags::typeToFlags(value));
value = in.readInt(flags);
}
static void describeType(IPropertyDescriptionFiller& filler) {
filler.addInteger();
}
static void listTypes(ISerialisableDescriptionFiller&) {};
};
template <std::floating_point Float>
struct TypedSerialiser<Float> {
static void serialiseMember(IStructuredOutput& out, Float value, SerialisationFlags::Flags flags) {
if (!(flags & SerialisationFlags::DETERMINED_NUMERIC_TYPE))
flags = decltype(flags)(flags | SerialisationFlags::typeToFlags(value));
out.writeFloat(flags, value);
}
static void deserialiseMember(IStructuredInput& in, Float& value, SerialisationFlags::Flags flags) {
if (!(flags & SerialisationFlags::DETERMINED_NUMERIC_TYPE))
flags = decltype(flags)(flags | SerialisationFlags::typeToFlags(value));
value = in.readFloat(flags);
}
static void describeType(IPropertyDescriptionFiller& filler) {
filler.addFloat();
}
static void listTypes(ISerialisableDescriptionFiller&) {};
};
template <>
struct TypedSerialiser<bool> {
static void serialiseMember(IStructuredOutput& out, bool value, SerialisationFlags::Flags flags) {
out.writeBool(flags, value);
}
static void deserialiseMember(IStructuredInput& in, bool& value, SerialisationFlags::Flags flags) {
value = in.readBool(flags);
}
static void describeType(IPropertyDescriptionFiller& filler) {
filler.addBoolean();
}
static void listTypes(ISerialisableDescriptionFiller&) {};
};
template <std::derived_from<ISerialisable> Serialisable>
struct TypedSerialiser<Serialisable> {
static void serialiseMember(IStructuredOutput& out, const Serialisable& value, SerialisationFlags::Flags flags) {
static_cast<const ISerialisable&>(value).serialiseInternal(out, flags);
}
static void deserialiseMember(IStructuredInput& in, Serialisable& value, SerialisationFlags::Flags flags) {
static_cast<ISerialisable&>(value).deserialiseInternal(in, flags);
}
static void describeType(IPropertyDescriptionFiller& filler) {
Serialisable value;
filler.addSubobject(value.getTypeName(), [&value] (IPropertyDescriptionFiller& subFiller) {
value.describe(subFiller);
});
}
static void listTypes(ISerialisableDescriptionFiller& filler) {
Serialisable instance;
instance.listTypes(filler);
filler.fillMembers(instance.getTypeName(), [&instance] (IPropertyDescriptionFiller& propertyFiller) {
instance.describe(propertyFiller);
});
};
};
template <typename T>
concept MemberString = requires(T value, std::string_view view) {
std::string_view(const_cast<const T&>(value));
value = view;
};
template <MemberString StringType>
struct TypedSerialiser<StringType> {
static void serialiseMember(IStructuredOutput& out, const StringType& value, SerialisationFlags::Flags flags) {
out.writeString(flags, value);
}
static void deserialiseMember(IStructuredInput& in, StringType& value, SerialisationFlags::Flags flags) {
value = in.readString(flags);
}
static void describeType(IPropertyDescriptionFiller& filler) {
filler.addString();
}
static void listTypes(ISerialisableDescriptionFiller&) {};
};
template <typename T>
concept WithSerialiserFunctions = requires(T value, IStructuredInput& in, IStructuredOutput& out) {
TypedSerialiser<std::decay_t<T>>::serialiseMember(out, value, SerialisationFlags::NONE);
TypedSerialiser<std::decay_t<T>>::deserialiseMember(in, value, SerialisationFlags::NONE);