-
Notifications
You must be signed in to change notification settings - Fork 5
/
prop.h
984 lines (852 loc) · 25.4 KB
/
prop.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
/**
* @file prop.h
* @brief Type-agnotic property system
*
* This file provides a type-agnostic property system.
* It is used to provide a generic interface for convenient property access for objects,
* entities and entire systems.
*/
#ifndef PROP_H_
#define PROP_H_
#include <any>
#include <variant>
#include "cube.h"
namespace prop
{
/**
* @brief Storage for a property value
*
* This type is used to store a property value.
* Supported types are:
* int, float, bvec, ivec, vec, std::string
*/
typedef std::variant<int, float, bvec, ivec, vec, std::string> PropertyValue;
/**
* @brief Callback type for property changes
*
* @param argument - The argument passed to the callback.
*/
typedef std::function<void(std::any argument)> OnChangeCallback;
/**
* @brief Type of a property
*
* This enum is used to determine the type of a property.
* Mandates what sort of type is stored inside the PropertyValue.
*/
enum class PropertyType
{
NoType = -1,
Int,
Float,
Color,
IntVec,
FloatVec,
String,
Count
};
/**
* @brief Meta information about a property
*
* This class stores definition information for a property:
* - name (string)
* - type (PropertyType)
* - min, default and max values (PropertyValue)
*
* All properties should be statically bound to their PropertyMeta counterparts.
*/
class PropertyMeta
{
private:
std::string name;
PropertyType type;
PropertyValue min, def, max;
OnChangeCallback on_change;
public:
PropertyType get_type() const;
PropertyValue get_min() const;
PropertyValue get_def() const;
PropertyValue get_max() const;
std::string get_name() const;
void changed(std::any argument) const;
template<class T> PropertyMeta(std::string _name, PropertyType _type, T _min, T _def, T _max,
OnChangeCallback _on_change = nullptr) :
name(_name), type(_type), min(_min), def(_def), max(_max), on_change(_on_change) {}
template<class T> PropertyMeta(std::string _name, PropertyType _type, T _def,
OnChangeCallback _on_change = nullptr) :
name(_name), type(_type), def(_def), on_change(_on_change) {}
PropertyMeta(const PropertyMeta&) = delete;
};
/**
* @brief A property
*
* @tparam PropertyMetaT - The type of the meta class, defaults to PropertyMeta
*
* This class is an instance of a property. It stores a value and a reference to its meta class.
*/
template<typename PropertyMetaT = PropertyMeta>
class Property
{
private:
PropertyValue value;
const PropertyMetaT& meta;
bool set_check_type(PropertyType type) const;
void set_clamped(PropertyValue value);
public:
const PropertyValue& get_value() const;
int get_int() const;
float get_float() const;
const bvec& get_color() const;
const ivec& get_ivec() const;
const vec& get_fvec() const;
const std::string& get_string() const;
size_t get_size() const;
std::string get_name() const;
PropertyType get_type() const;
std::string to_string() const;
void copy(const Property& prop);
void set(PropertyValue value, std::any on_change_argument = std::any());
void set_no_cb(PropertyValue value);
void reset();
void cmd_result() const;
void cmd_result_min() const;
void cmd_result_def() const;
void cmd_result_max() const;
virtual void pack(std::vector<uint8_t>& buf) const;
virtual size_t unpack(const uint8_t* buf, size_t len);
Property(const PropertyMetaT& _meta);
virtual ~Property() = default;
};
////////////////////
// Implementation //
////////////////////
/**
* @brief Get the integer value of the property
*
* @tparam PropertyMetaT - The type of the meta class.
* @return int
*
* Caution: This function does not check the type of the property. A former check is assumed.
*/
template<typename PropertyMetaT>
int Property<PropertyMetaT>::get_int() const
{
return std::get<int>(value);
}
/**
* @brief Get the float value of the property
*
* @tparam PropertyMetaT - The type of the meta class.
* @return float
*
* Caution: This function does not check the type of the property. A former check is assumed.
*/
template<typename PropertyMetaT>
float Property<PropertyMetaT>::get_float() const
{
return std::get<float>(value);
}
/**
* @brief Get the color value of the property
*
* @tparam PropertyMetaT - The type of the meta class.
* @return bvec
*
* Caution: This function does not check the type of the property. A former check is assumed.
*/
template<typename PropertyMetaT>
const bvec& Property<PropertyMetaT>::get_color() const
{
return std::get<bvec>(value);
}
/**
* @brief Get the integer vector value of the property
*
* @tparam PropertyMetaT - The type of the meta class.
* @return ivec
*
* Caution: This function does not check the type of the property. A former check is assumed.
*/
template<typename PropertyMetaT>
const ivec& Property<PropertyMetaT>::get_ivec() const
{
return std::get<ivec>(value);
}
/**
* @brief Get the float vector value of the property
*
* @tparam PropertyMetaT - The type of the meta class.
* @return vec
*
* Caution: This function does not check the type of the property. A former check is assumed.
*/
template<typename PropertyMetaT>
const vec& Property<PropertyMetaT>::get_fvec() const
{
return std::get<vec>(value);
}
/**
* @brief Get the string value of the property
*
* @tparam PropertyMetaT - The type of the meta class.
* @return std::string
*
* Caution: This function does not check the type of the property. A former check is assumed.
*/
template<typename PropertyMetaT>
const std::string& Property<PropertyMetaT>::get_string() const
{
return std::get<std::string>(value);
}
/**
* @brief Get the size of the property
*
* @tparam PropertyMetaT - The type of the meta class.
* @return size_t
*
* Returns the size of the stored value in bytes.
*/
template<typename PropertyMetaT>
size_t Property<PropertyMetaT>::get_size() const
{
switch(get_type())
{
case PropertyType::Int:
return sizeof(int);
case PropertyType::Float:
return sizeof(float);
case PropertyType::Color:
return sizeof(bvec);
case PropertyType::IntVec:
return sizeof(ivec);
case PropertyType::FloatVec:
return sizeof(vec);
case PropertyType::String:
return get_string().size();
default:
return 0;
}
}
template<typename PropertyMetaT>
std::string Property<PropertyMetaT>::to_string() const
{
switch(get_type())
{
case PropertyType::Int:
return std::to_string(get_int());
case PropertyType::Float:
return std::to_string(get_float());
case PropertyType::Color:
return std::to_string(get_color().r) + " " + std::to_string(get_color().g) + " " + std::to_string(get_color().b);
case PropertyType::IntVec:
return std::to_string(get_ivec().x) + " " + std::to_string(get_ivec().y) + " " + std::to_string(get_ivec().z);
case PropertyType::FloatVec:
return std::to_string(get_fvec().x) + " " + std::to_string(get_fvec().y) + " " + std::to_string(get_fvec().z);
case PropertyType::String:
return get_string();
default:
return "";
}
}
template<typename PropertyMetaT>
bool Property<PropertyMetaT>::set_check_type(PropertyType type) const
{
// typename used in debug printout below
//static constexpr std::string_view typenames[] =
//{
// "int", "float", "color", "int vector", "float vector", "string"
//};
if(get_type() != type)
{
// conoutf("Error: %s, incorrect property assignment type: got %s, expected %s",
// meta.get_name().c_str(), std::string(typenames[static_cast<size_t>(type)]).c_str(),
// std::string(typenames[static_cast<size_t>(get_type())]).c_str());
return false;
}
return true;
}
/**
* @brief Get the name of the property
*
* @tparam PropertyMetaT - The type of the meta class.
* @return std::string
*
* Returns the name of the property, as per the definition in the meta class.
*/
template<typename PropertyMetaT>
std::string Property<PropertyMetaT>::get_name() const
{
return meta.get_name();
}
/**
* @brief Get the type of the property
*
* @tparam PropertyMetaT - The type of the meta class.
* @return std::string
*
* Returns the type of the property, as per the definition in the meta class.
*/
template<typename PropertyMetaT>
PropertyType Property<PropertyMetaT>::get_type() const
{
return meta.get_type();
}
/**
* @brief Copy the value of another property, will not invoke callback
*
* @tparam PropertyMetaT - The type of the meta class.
* @param prop - The property to copy the value from.
* @param on_change_arg - Optional argument for on_change callback.
*
* Note: type check is performed automatically, value of an incompatible type is ignored and reported.
* Value is clamped to the range defined in the meta class, if applicable.
*/
template<typename PropertyMetaT>
void Property<PropertyMetaT>::copy(const Property& prop)
{
set_no_cb(prop.value);
}
template<typename PropertyMetaT>
void Property<PropertyMetaT>::set_clamped(PropertyValue value)
{
switch(get_type())
{
case PropertyType::Int:
{
int v = std::get<int>(value);
this->value = std::clamp(v, std::get<int>(meta.get_min()), std::get<int>(meta.get_max()));
break;
}
case PropertyType::Float:
{
float v = std::get<float>(value);
this->value = std::clamp(v, std::get<float>(meta.get_min()), std::get<float>(meta.get_max()));
break;
}
case PropertyType::IntVec:
{
ivec v = std::get<ivec>(value);
this->value = ivec(v).min(std::get<ivec>(meta.get_max()))
.max(std::get<ivec>(meta.get_min()));
break;
}
case PropertyType::FloatVec:
{
vec v = std::get<vec>(value);
this->value = vec(v).min(std::get<vec>(meta.get_max()))
.max(std::get<vec>(meta.get_min()));
break;
}
default:
this->value = value;
break;
}
}
/**
* @brief Set the value of the property
*
* @tparam PropertyMetaT - The type of the meta class.
* @param value - The new value of the property.
* @param on_change_arg - Optional argument for on_change callback.
*
* Note: type check is performed automatically, value of an incompatible type is ignored and reported.
* Value is clamped to the range defined in the meta class, if applicable.
*/
template<typename PropertyMetaT>
void Property<PropertyMetaT>::set(PropertyValue value, std::any on_change_arg)
{
if(set_check_type(static_cast<PropertyType>(value.index())))
{
set_clamped(value);
meta.changed(on_change_arg);
}
}
/**
* @brief Set the value of the property without invoking callback
*
* @tparam PropertyMetaT - The type of the meta class.
* @param value - The new value of the property.
*
* Note: type check is performed automatically, value of an incompatible type is ignored and reported.
* Value is clamped to the range defined in the meta class, if applicable.
*/
template<typename PropertyMetaT>
void Property<PropertyMetaT>::set_no_cb(PropertyValue value)
{
if(set_check_type(static_cast<PropertyType>(value.index())))
{
set_clamped(value);
}
}
/**
* @brief Reset the value of the property to the default value
*
* @tparam PropertyMetaT - The type of the meta class.
*/
template<typename PropertyMetaT>
void Property<PropertyMetaT>::reset()
{
this->value = meta.get_def();
}
static void _prop_cmd_result_value(PropertyValue value, PropertyType type)
{
switch(type)
{
case PropertyType::Int:
intret(std::get<int>(value));
break;
case PropertyType::Float:
floatret(std::get<float>(value));
break;
case PropertyType::Color:
intret(std::get<bvec>(value).tohexcolor());
break;
case PropertyType::IntVec:
{
const ivec& v = std::get<ivec>(value);
result(tempformatstring("%d %d %d", v.x, v.y, v.z));
break;
}
case PropertyType::FloatVec:
{
const vec& v = std::get<vec>(value);
result(tempformatstring("%f %f %f", v.x, v.y, v.z));
break;
}
case PropertyType::String:
result(std::get<std::string>(value).c_str());
break;
default: break;
}
}
/**
* @brief Results the value of the property
*
* @tparam PropertyMetaT - The type of the meta class.
*/
template<typename PropertyMetaT>
void Property<PropertyMetaT>::cmd_result() const
{
_prop_cmd_result_value(value, get_type());
}
/**
* @brief Results the minimum value of the property
*
* @tparam PropertyMetaT - The type of the meta class.
*/
template<typename PropertyMetaT>
void Property<PropertyMetaT>::cmd_result_min() const
{
_prop_cmd_result_value(meta.get_min(), get_type());
}
/**
* @brief Results the default value of the property
*
* @tparam PropertyMetaT - The type of the meta class.
*/
template<typename PropertyMetaT>
void Property<PropertyMetaT>::cmd_result_def() const
{
_prop_cmd_result_value(meta.get_def(), get_type());
}
/**
* @brief Results the maximum value of the property
*
* @tparam PropertyMetaT - The type of the meta class.
*/
template<typename PropertyMetaT>
void Property<PropertyMetaT>::cmd_result_max() const
{
_prop_cmd_result_value(meta.get_max(), get_type());
}
/**
* @brief Packs the property into a buffer
*
* @tparam PropertyMetaT - The type of the meta class.
* @param buf - The buffer to pack the property into.
*/
template<typename PropertyMetaT>
void Property<PropertyMetaT>::pack(std::vector<uint8_t>& buf) const
{
size_t data_size = get_size();
vectorput(buf, data_size);
switch(get_type())
{
case PropertyType::Int:
{
vectorput(buf, get_int());
break;
}
case PropertyType::Float:
{
vectorput(buf, get_float());
break;
}
case PropertyType::Color:
{
vectorput(buf, get_color());
break;
}
case PropertyType::IntVec:
{
vectorput(buf, get_ivec());
break;
}
case PropertyType::FloatVec:
{
vectorput(buf, get_fvec());
break;
}
case PropertyType::String:
{
std::string data = get_string();
// Null-terminator not needed, since we know the size
vectorput(buf, reinterpret_cast<const uint8_t*>(data.data()), data.size());
break;
}
default: break;
}
}
/**
* @brief Unpacks the property from a buffer
*
* @tparam PropertyMetaT - The type of the meta class.
* @param buf - The buffer to unpack the property from.
* @param buf_size - The size of the buffer.
*
* @return The number of bytes read from the buffer.
*/
template<typename PropertyMetaT>
size_t Property<PropertyMetaT>::unpack(const uint8_t* buf, size_t buf_size)
{
size_t buf_read = 0;
const size_t* data_size_packed = nullptr;
if(buf_size <= sizeof(*data_size_packed))
{
// conoutf("Error unpacking prop '%s': not enough data to get the size!", meta.get_name().c_str());
return 0;
}
data_size_packed = reinterpret_cast<const size_t*>(buf);
buf_read += sizeof(*data_size_packed);
static auto size_check = [&](size_t needed_size)
{
if(buf_size - buf_read < needed_size)
{
// conoutf("Error unpacking prop '%s': not enough data!", meta.get_name().c_str());
return false;
}
return true;
};
switch(get_type())
{
case PropertyType::Int:
case PropertyType::Color:
case PropertyType::Float:
if(*data_size_packed != get_size())
{
// conoutf("Error unpacking prop '%s': unexpected data size! Wanted: %lu, got: %lu",
// meta.get_name().c_str(), get_size(), *data_size_packed);
return 0;
}
break;
default: break;
}
switch(get_type())
{
case PropertyType::Int:
{
if(!size_check(get_size()))
{
return 0;
}
int ival;
memcpy(&ival, buf + buf_read, get_size());
buf_read += get_size();
value = ival;
break;
}
case PropertyType::Float:
{
if(!size_check(get_size()))
{
return 0;
}
float fval;
memcpy(&fval, buf + buf_read, get_size());
buf_read += get_size();
value = fval;
break;
}
case PropertyType::Color:
{
if(!size_check(get_size()))
{
return 0;
}
bvec cval;
memcpy(&cval, buf + buf_read, get_size());
buf_read += get_size();
value = cval;
break;
}
case PropertyType::IntVec:
{
if(!size_check(get_size()))
{
return 0;
}
ivec ivecval;
memcpy(&ivecval, buf + buf_read, get_size());
buf_read += get_size();
value = ivecval;
break;
}
case PropertyType::FloatVec:
{
if(!size_check(get_size()))
{
return 0;
}
vec vecval;
memcpy(&vecval, buf + buf_read, get_size());
buf_read += get_size();
value = vecval;
break;
}
case PropertyType::String:
{
if(!size_check(*data_size_packed))
{
return 0;
}
std::string sval(reinterpret_cast<const char*>(buf + buf_read), *data_size_packed);
buf_read += *data_size_packed;
value = sval;
break;
}
default: break;
}
return buf_read;
}
/**
* @brief Creates a new property with the given meta class.
*
* @tparam PropertyMetaT - The type of the meta class.
* @param _meta - The meta class.
*/
template<typename PropertyMetaT>
Property<PropertyMetaT>::Property(const PropertyMetaT& _meta) : meta(_meta)
{
reset();
}
/**
* @brief Gets the type of the property definition.
*
* @return PropertyType
*/
PropertyType PropertyMeta::get_type() const
{
return type;
}
/**
* @brief Gets the minimum value of the property definition.
*
* @return PropertyValue
*/
PropertyValue PropertyMeta::get_min() const
{
return min;
}
/**
* @brief Gets the default value of the property definition.
*
* @return PropertyValue
*/
PropertyValue PropertyMeta::get_def() const
{
return def;
}
/**
* @brief Gets the maximum value of the property definition.
*
* @return PropertyValue
*/
PropertyValue PropertyMeta::get_max() const
{
return max;
}
/**
* @brief Gets the name of the property definition.
*
* @return std::string
*/
std::string PropertyMeta::get_name() const
{
return name;
}
void PropertyMeta::changed(std::any argument) const
{
if(on_change)
{
on_change(argument);
}
}
//////////////////////
// Helper functions //
//////////////////////
/**
* @brief Finds a property with the given name in the given array.
*
* @tparam PropertyT - Property class type
* @tparam N - Size of the array
* @param name - The name of the property to find.
* @param props - The array of properties to search.
* @return PropertyT* - The property if found, nullptr otherwise.
*/
template<typename PropertyT, std::size_t N>
PropertyT* find_prop(std::string name, std::array<PropertyT, N>& props)
{
for(PropertyT& prop : props)
{
if(prop.get_name() == name)
{
return ∝
}
}
return nullptr;
}
/**
* @brief Finds a property with the given name in the given array.
*
* @tparam PropertyT - Property class type
* @tparam N - Size of the array
* @param name - The name of the property to find.
* @param props - The array of properties to search.
* @return PropertyT* - The property if found, nullptr otherwise.
*/
template<typename PropertyT, std::size_t N>
const PropertyT* find_prop(std::string name, const std::array<PropertyT, N>& props)
{
for(const PropertyT& prop : props)
{
if(prop.get_name() == name)
{
return ∝
}
}
return nullptr;
}
/**
* @brief Sets the value of a property with the given name in the given array.
*
* @tparam PropertyT - Property class type
* @tparam N - Size of the array
* @param name - The name of the property to set.
* @param value - The value to set the property to. Value is clamped to the property's min/max values, if applicable.
* @param props - The array of properties to search.
* @param on_change_arg - Optional argument to pass to the property's on_change callback.
* @return true - The property was found and set.
*/
template<typename PropertyT, std::size_t N>
bool set_prop(std::string name, PropertyValue value, std::array<PropertyT, N>& props,
std::any on_change_arg = std::any())
{
PropertyT* prop = find_prop(name, props);
if(!prop)
{
// conoutf("Property %s not found!", name.c_str());
return false;
}
prop->set(value, on_change_arg);
return true;
}
/**
* @brief Finds a property definition with the given name in the given array.
*
* @tparam PropertyMetaT - PropertyMeta class type
* @tparam N - Size of the array
* @param name - The name of the property definition to find.
* @param prop_metas - The array of property definitions to search.
* @return const PropertyMetaT* - The property definition if found, nullptr otherwise.
*/
template<typename PropertyMetaT, std::size_t N>
const PropertyMetaT* find_prop_meta(std::string name, const std::array<PropertyMetaT, N>& prop_metas)
{
for(const PropertyMetaT& prop_meta : prop_metas)
{
if(prop_meta.get_name() == name)
{
return &prop_meta;
}
}
return nullptr;
}
/**
* @brief Packs the given properties into the given buffer.
*
* @tparam PropertyT - Property class type
* @tparam N - Size of the array
* @param props - The array of properties to pack.
* @param buf - The buffer to pack the properties into.
*/
template<typename PropertyT, std::size_t N>
void pack_props(const std::array<PropertyT, N>& props, std::vector<uint8_t>& buf)
{
for(const PropertyT& prop : props)
{
prop.pack(buf);
}
}
/**
* @brief Unpacks the given buffer into the given properties.
*
* @tparam PropertyT - Property class type
* @tparam N - Size of the array
* @param buf - The buffer to unpack.
* @param props - The array of properties to unpack into.
* @return size_t - The number of properties unpacked.
*/
template<typename PropertyT, std::size_t N>
size_t unpack_props(const std::vector<uint8_t>& buf, std::array<PropertyT, N>& props)
{
size_t prop_idx = 0;
size_t read_pos = 0;
while(read_pos < buf.size())
{
if(prop_idx >= N)
{
break;
}
PropertyT &prop = props[prop_idx];
int unpacked_size = prop.unpack(buf.data() + read_pos, buf.size() - read_pos);
if(!unpacked_size)
{
break;
}
read_pos += unpacked_size;
prop_idx++;
}
return prop_idx;
}
template<std::size_t N, typename PropertyT, std::size_t... Is, typename PropertyMetaT>
std::array<PropertyT, N> _make_props_array_impl(std::index_sequence<Is...>,
const PropertyMetaT (&prop_meta)[N])
{
return { PropertyT(prop_meta[Is])... };
}
/**
* @brief Creates an array of properties from the given property definitions.
*
* @tparam N - Size of the array
* @tparam PropertyT - Property class type
* @tparam Is - Index sequence
* @tparam PropertyMetaT - PropertyMeta class type
* @param prop_meta - The array of property definitions to create properties from.
* @return std::array<PropertyT, N> - The array of properties bound to meta information.
*/
template<std::size_t N, typename PropertyT, std::size_t... Is, typename PropertyMetaT>
std::array<PropertyT, N> make_props_array(const PropertyMetaT (&prop_meta)[N])
{
return _make_props_array_impl<N, PropertyT>(std::make_index_sequence<N>{}, prop_meta);
}
}; // namespace prop
#endif // PROP_H_