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
|
/*
* Anope IRC Services
*
* Copyright (C) 2011-2016 Anope Team <team@anope.org>
*
* This file is part of Anope. Anope is free software; you can
* redistribute it and/or modify it under the terms of the GNU
* General Public License as published by the Free Software
* Foundation, version 2.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, see see <http://www.gnu.org/licenses/>.
*/
#pragma once
#include "anope.h"
#include "base.h"
#include "extensible.h"
#include "event.h"
namespace Serialize
{
class Object;
class TypeBase;
class FieldBase;
template<typename> class FieldTypeBase;
template<typename, typename> class CommonFieldBase;
template<typename, typename> class Field;
template<typename, typename> class ObjectField;
template<typename> class Storage;
template<typename T, typename> class Type;
template<typename T> class Reference;
extern std::vector<FieldBase *> serializableFields;
extern Object *GetID(Serialize::TypeBase *type, ID id);
template<typename T>
inline T GetObject();
template<typename T>
inline std::vector<T> GetObjects_(TypeBase *);
template<typename T>
inline std::vector<T> GetObjects(const Anope::string &name);
template<typename T>
inline std::vector<T> GetObjects();
template<typename T>
inline T New();
extern void GC();
extern void Unregister(Module *);
struct Edge
{
Object *other;
FieldBase *field;
bool direction;
Edge(Object *o, FieldBase *f, bool d) : other(o), field(f), direction(d) { }
bool operator==(const Edge &e) const
{
return other == e.other && field == e.field && direction == e.direction;
}
};
extern std::multimap<Anope::string, Anope::string> child_types;
extern void SetParent(const Anope::string &child, const Anope::string &parent);
extern std::vector<Serialize::TypeBase *> GetTypes(const Anope::string &name);
}
class CoreExport Serialize::Object : public Extensible, public virtual Base
{
private:
friend class Serialize::FieldBase;
/* The type of item this object is */
TypeBase *s_type;
std::map<TypeBase *, std::vector<Edge>> edges;
std::vector<Edge> GetEdges(TypeBase *);
public:
static constexpr const char *NAME = "object";
Object(TypeBase *type);
Object(TypeBase *type, ID);
virtual ~Object();
virtual void Delete();
/* Unique ID for this object */
ID id;
void AddEdge(Object *other, FieldBase *field);
void RemoveEdge(Object *other, FieldBase *field);
/**
* Get an object of type T that this object references.
*/
template<typename T>
T GetRef()
{
std::vector<T> t = GetRefs<T>();
return !t.empty() ? t[0] : nullptr;
}
/**
* Gets all objects of type T that this object references
*/
template<typename T>
std::vector<T> GetRefs();
std::vector<Object *> GetRefs(Serialize::TypeBase *);
/**
* Get the value of a field on this object.
*/
template<
typename Type,
template<typename, typename> class Field, // Field type being read
typename TypeImpl,
typename T // type of the Extensible
>
T Get(Field<TypeImpl, T> Type::*field)
{
static_assert(std::is_base_of<Object, TypeImpl>::value, "");
static_assert(std::is_base_of<Serialize::TypeBase, Type>::value, "");
Type *t = static_cast<Type *>(s_type);
Field<TypeImpl, T>& f = t->*field;
return f.GetField(f.Upcast(this));
}
/**
* Get the value of a field on this object. Allows specifying return
* type if the return type can't be inferred.
*/
template<typename Ret, typename Field, typename Type>
Ret Get(Field Type::*field)
{
static_assert(std::is_base_of<Serialize::TypeBase, Type>::value, "");
Type *t = static_cast<Type *>(s_type);
Field& f = t->*field;
return f.GetField(f.Upcast(this));
}
/**
* Set the value of a field on this object
*/
template<
typename Type,
template<typename, typename> class Field,
typename TypeImpl,
typename T
>
void Set(Field<TypeImpl, T> Type::*field, const T& value)
{
static_assert(std::is_base_of<Object, TypeImpl>::value, "");
static_assert(std::is_base_of<Serialize::TypeBase, Type>::value, "");
Type *t = static_cast<Type *>(s_type);
Field<TypeImpl, T>& f = t->*field;
f.SetField(f.Upcast(this), value);
}
/**
* Set the value of a field on this object
*/
template<typename Field, typename Type, typename T>
void Set(Field Type::*field, const T& value)
{
static_assert(std::is_base_of<Serialize::TypeBase, Type>::value, "");
Type *t = static_cast<Type *>(s_type);
Field& f = t->*field;
f.SetField(f.Upcast(this), value);
}
/** Get the type of serializable object this is
* @return The serializable object type
*/
TypeBase* GetSerializableType() const { return this->s_type; }
/** Set the value of a field on this object, by field name
*/
template<typename T>
void SetS(const Anope::string &name, const T &what);
/** Unset a field on this object, by field name
*/
template<typename T>
void UnsetS(const Anope::string &name);
/** Test if a field is set. Only useful with extensible fields,
* which can unset (vs set to the default value)
*/
bool HasFieldS(const Anope::string &name);
/** Whether or not the object can be garbage collected.
* Should be true unless your object holds in-memory only
* state.
*/
virtual bool CanGC() { return true; }
void Wipe();
};
class CoreExport Serialize::TypeBase : public Service
{
Anope::string name;
Module *owner = nullptr;
public:
static constexpr const char *NAME = "typebase";
std::set<Object *> objects;
TypeBase(Module *owner, const Anope::string &n);
void Unregister();
/** Gets the name for this type
* @return The name, eg "NickAlias"
*/
const Anope::string &GetName() { return this->name; }
/** Create a new object of this type.
*/
virtual Object *Create() anope_abstract;
/** Get or otherwise create an object of this type
* with the given ID.
*/
virtual Object *Require(Serialize::ID) anope_abstract;
/** Find a field on this type
*/
FieldBase *GetField(const Anope::string &name);
/** Get all fields of this type
*/
std::vector<FieldBase *> GetFields();
Module* GetOwner() const { return this->owner; }
static TypeBase *Find(const Anope::string &name);
static std::vector<TypeBase *> GetTypes();
};
template<typename T, typename Base = Serialize::TypeBase>
class Serialize::Type : public Base
{
public:
Type(Module *module) : Base(module, T::NAME) { }
Type(Module *module, const Anope::string &name) : Base(module, name) { }
Object *Create() override
{
return new T(this);
}
Object *Require(Serialize::ID id) override
{
return RequireID(id);
}
T* RequireID(ID id)
{
Object *s = Serialize::GetID(this, id);
if (s == nullptr)
return new T(this, id);
if (s->GetSerializableType() != this)
{
Anope::Logger.Debug("Mismatch for required id {0}, is of type {1} but wants {2}", id, s->GetSerializableType()->GetName(), this->GetName());
return nullptr;
}
return static_cast<T *>(s);
}
};
/** A reference to a serializable object. Serializable objects may not always
* exist in memory at all times, like if they exist in an external database,
* so you can't hold pointers to them. Instead, hold a Serialize::Reference
* which will properly fetch the object on demand from the underlying database
* system.
*/
template<typename T>
class Serialize::Reference
{
protected:
bool valid = false;
TypeBase *type;
/* ID of the object which we reference */
ID id;
public:
Serialize::Reference<T>& operator=(T* obj)
{
if (obj != nullptr)
{
type = obj->GetSerializableType();
id = obj->id;
valid = true;
}
else
{
valid = false;
}
return *this;
}
explicit operator bool() const
{
return Dereference() != nullptr;
}
operator T*() const { return Dereference(); }
T* operator*() const { return Dereference(); }
T* operator->() const { return Dereference(); }
private:
T* Dereference() const
{
if (!valid)
return nullptr;
Object *targ = GetID(type, id);
if (targ != nullptr && targ->GetSerializableType() == type)
return anope_dynamic_static_cast<T*>(targ);
EventReturn result = EventManager::Get()->Dispatch(&Event::SerializeEvents::OnSerializeDeref, id, type);
if (result == EVENT_ALLOW)
return anope_dynamic_static_cast<T *>(type->Require(id));
return nullptr;
}
};
/** A field, associated with a type.
*/
class Serialize::FieldBase : public Service
{
public:
static constexpr const char *NAME = "fieldbase";
Anope::string serialize_type; // type the field is on
Anope::string serialize_name; // field name
/** For fields which reference other Objects. If true, when
* the object the field references gets deleted, this object
* gets deleted too.
*/
bool depends = false;
bool is_object = false;
FieldBase(Module *, const Anope::string &, const Anope::string &, bool);
virtual ~FieldBase();
void Unregister();
/** Serialize value of this field on the given object to string form
*/
virtual Anope::string SerializeToString(Object *s) anope_abstract;
/** Unserialize value of this field on the given object from string form
*/
virtual void UnserializeFromString(Object *s, const Anope::string &) anope_abstract;
/** Test if the given object has the given field, only usefil for extensible fields
*/
virtual bool HasFieldS(Object *) anope_abstract;
/** Unset this field on the given object
*/
virtual void UnsetS(Object *) anope_abstract;
virtual void Uncache(Object *) anope_abstract;
virtual Anope::string GetTypeName() { return ""; }
};
template<typename T>
class Serialize::FieldTypeBase : public FieldBase
{
public:
using FieldBase::FieldBase;
/** Set this field to the given value on the given object.
*/
virtual void SetFieldS(Object *, const T &) anope_abstract;
};
/** Base class for serializable fields and serializable object fields.
*/
template<
typename TypeImpl, // Serializable type
typename T // actual type of field
>
class Serialize::CommonFieldBase : public FieldTypeBase<T>
{
static_assert(std::is_base_of<Object, TypeImpl>::value, "");
/** Extensible storage for value of fields. Only used if field
* isn't set. Note extensible fields can be "unset", where field
* pointers are never unset, but are T().
*/
ExtensibleItem<T> *ext = nullptr;
/** Storage pointer in the TypeImpl object for this field.
*/
Serialize::Storage<T> TypeImpl::*storage = nullptr;
protected:
/** Set the value of a field in storage
*/
void Set_(TypeImpl *object, const T &value)
{
if (storage != nullptr)
{
Serialize::Storage<T> &s = object->*storage;
s.t = value;
s.cached = true;
}
else if (ext != nullptr)
ext->Set(object, value);
else
throw CoreException("No field or ext");
}
/* Get the value of a field from storage
*/
T* Get_(TypeImpl *object)
{
if (storage != nullptr)
return &(object->*storage).t;
else if (ext != nullptr)
return ext->Get(object);
else
throw CoreException("No field or ext");
}
/** Unset a field from storage
*/
void Unset_(TypeImpl *object)
{
if (storage != nullptr)
{
Serialize::Storage<T> &s = object->*storage;
s.t = T();
s.cached = false;
}
else if (ext != nullptr)
ext->Unset(object);
else
throw CoreException("No field or ext");
}
/** Check is a field is set. Only useful for
* extensible storage. Returns true for field storage.
*/
bool HasField_(TypeImpl *object)
{
if (storage != nullptr)
return true;
else if (ext != nullptr)
return ext->HasExt(object);
else
throw CoreException("No field or ext");
}
bool Cached_(TypeImpl *object)
{
if (storage != nullptr)
return (object->*storage).cached;
else if (ext != nullptr)
return ext->HasExt(object);
else
throw CoreException("No field or ext");
}
public:
CommonFieldBase(Module *creator, const Anope::string &n, bool d)
: FieldTypeBase<T>(creator, n, TypeImpl::NAME, d)
{
ext = new ExtensibleItem<T>(creator, TypeImpl::NAME, n);
}
CommonFieldBase(Serialize::TypeBase *t,
const Anope::string &n,
Serialize::Storage<T> TypeImpl::*s,
bool d)
: FieldTypeBase<T>(t->GetOwner(), n, t->GetName(), d)
, storage(s)
{
}
~CommonFieldBase()
{
delete ext;
}
/** Get the value of this field on the given object
*/
virtual T GetField(TypeImpl *) anope_abstract;
/** Unset this field on the given object
*/
virtual void UnsetField(TypeImpl *) anope_abstract;
void UnsetS(Object *s) override
{
UnsetField(Upcast(s));
}
bool HasFieldS(Object *s) override
{
return HasField(Upcast(s));
}
/** Cast a serializable object of type Object to type TypeImpl,
* if appropriate
*/
TypeImpl* Upcast(Object *s)
{
if (this->serialize_type != s->GetSerializableType()->GetName())
{
return nullptr;
}
return anope_dynamic_static_cast<TypeImpl *>(s);
}
bool HasField(TypeImpl *s)
{
EventReturn result = EventManager::Get()->Dispatch(&Event::SerializeEvents::OnSerializeHasField, s, this);
if (result != EVENT_CONTINUE)
return true;
return this->HasField_(s);
}
void Uncache(Object *s) override
{
Unset_(Upcast(s));
}
};
/** Class for all fields that aren't to other serializable objects
*/
template<typename TypeImpl, typename T>
class Serialize::Field : public CommonFieldBase<TypeImpl, T>
{
public:
Field(Module *creator, const Anope::string &n) : CommonFieldBase<TypeImpl, T>(creator, n, false)
{
}
Field(TypeBase *t, const Anope::string &n, Serialize::Storage<T> TypeImpl::*f) : CommonFieldBase<TypeImpl, T>(t, n, f, false)
{
}
T GetField(TypeImpl *s) override
{
T* t = this->Get_(s);
// If this field is cached
if (t && this->Cached_(s))
return *t;
// Query modules
Anope::string value;
EventReturn result = EventManager::Get()->Dispatch(&Event::SerializeEvents::OnSerializeGet, s, this, value);
if (result == EVENT_ALLOW)
{
// module returned us data, so we unserialize it
T t2 = this->Unserialize(value);
// set & cache
OnSet(s, t2);
this->Set_(s, t2);
return t2;
}
return t ? *t : T();
}
void SetFieldS(Object *s, const T &value) override
{
SetField(this->Upcast(s), value);
}
/**
* Override to hook to changes in the internally
* cached value
*/
virtual void OnSet(TypeImpl *s, const T &value) { }
void SetField(TypeImpl *s, const T &value)
{
Anope::string strvalue = this->Serialize(value);
EventManager::Get()->Dispatch(&Event::SerializeEvents::OnSerializeSet, s, this, strvalue);
OnSet(s, value);
this->Set_(s, value);
}
void UnsetField(TypeImpl *s) override
{
EventManager::Get()->Dispatch(&Event::SerializeEvents::OnSerializeUnset, s, this);
this->Unset_(s);
}
Anope::string Serialize(const T& t)
{
try
{
return stringify(t);
}
catch (const ConvertException &)
{
return "";
}
}
T Unserialize(const Anope::string &str)
{
if (str.empty())
return T();
try
{
return convertTo<T>(str);
}
catch (const ConvertException &)
{
return T();
}
}
Anope::string SerializeToString(Object *s) override
{
T t = GetField(this->Upcast(s));
return this->Serialize(t);
}
void UnserializeFromString(Object *s, const Anope::string &v) override
{
T t = this->Unserialize(v);
SetField(this->Upcast(s), t);
}
void Set(Extensible *obj, const T &value)
{
TypeImpl *s = anope_dynamic_static_cast<TypeImpl *>(obj);
SetField(s, value);
}
void Unset(Extensible *obj)
{
TypeImpl *s = anope_dynamic_static_cast<TypeImpl *>(obj);
this->UnsetField(s);
}
T Get(Extensible *obj)
{
TypeImpl *s = anope_dynamic_static_cast<TypeImpl *>(obj);
return this->GetField(s);
}
bool HasExt(Extensible *obj)
{
TypeImpl *s = anope_dynamic_static_cast<TypeImpl *>(obj);
return this->HasField(s);
}
};
/** Class for all fields that contain a reference to another serializable object.
*/
template<typename TypeImpl, typename T>
class Serialize::ObjectField : public CommonFieldBase<TypeImpl, T>
{
public:
ObjectField(Module *creator, const Anope::string &n) : CommonFieldBase<TypeImpl, T>(creator, n, false)
{
this->is_object = true;
}
ObjectField(TypeBase *t, const Anope::string &n, Serialize::Storage<T> TypeImpl::*field, bool d = false) : CommonFieldBase<TypeImpl, T>(t, n, field, d)
{
this->is_object = true;
}
T GetField(TypeImpl *s) override
{
T *t = this->Get_(s);
if (t && this->Cached_(s))
return *t;
Anope::string type;
ID sid;
EventReturn result = EventManager::Get()->Dispatch(&Event::SerializeEvents::OnSerializeGetSerializable, s, this, type, sid);
if (result != EVENT_CONTINUE)
{
Serialize::TypeBase *base = Serialize::TypeBase::Find(type);
if (base == nullptr)
{
Anope::Logger.Debug2("OnSerializeGetSerializable returned unknown type {0}", type);
return nullptr;
}
T t2 = result == EVENT_ALLOW ? static_cast<T>(base->Require(sid)) : nullptr;
OnSet(s, t2);
this->Set_(s, t2);
return t2;
}
return t ? *t : T();
}
void SetFieldS(Object *s, const T &value) override
{
SetField(this->Upcast(s), value);
}
virtual void OnSet(TypeImpl *s, T value) { }
void SetField(TypeImpl *s, T value)
{
EventManager::Get()->Dispatch(&Event::SerializeEvents::OnSerializeSetSerializable, s, this, value);
T *old = this->Get_(s);
if (old != nullptr && *old != nullptr)
s->RemoveEdge(*old, this);
OnSet(s, value);
this->Set_(s, value);
if (value != nullptr)
s->AddEdge(value, this);
}
void UnsetField(TypeImpl *s) override
{
EventManager::Get()->Dispatch(&Event::SerializeEvents::OnSerializeUnsetSerializable, s, this);
this->Unset_(s);
}
Anope::string SerializeToString(Object *s) override
{
T t = GetField(this->Upcast(s));
return this->Serialize(t);
}
void UnserializeFromString(Object *s, const Anope::string &v) override
{
T t = this->Unserialize(v);
SetField(this->Upcast(s), t);
}
Anope::string Serialize(const T& t)
{
return t ? t->GetSerializableType()->GetName() + ":" + stringify(t->id) : "";
}
T Unserialize(const Anope::string &str)
{
size_t c = str.rfind(':');
if (c == Anope::string::npos)
return nullptr;
Anope::string type = str.substr(0, c);
ID id;
try
{
id = convertTo<ID>(str.substr(c + 1));
}
catch (const ConvertException &)
{
return nullptr;
}
TypeBase *t = TypeBase::Find(type);
if (!t)
{
return nullptr;
}
return anope_dynamic_static_cast<T>(t->Require(id));
}
Anope::string GetTypeName() override
{
const char* const name = std::remove_pointer<T>::type::NAME;
return name;
}
};
template<typename T>
class Serialize::Storage
{
public:
T t = T();
bool cached = false;
};
template<typename T>
T Serialize::GetObject()
{
std::vector<T> v = GetObjects<T>();
return v.empty() ? nullptr : v[0];
}
template<typename T>
std::vector<T> Serialize::GetObjects_(TypeBase *type)
{
std::vector<T> o;
std::vector<ID> ids;
EventReturn result = EventManager::Get()->Dispatch(&Event::SerializeEvents::OnSerializeList, type, ids);
if (result == EVENT_ALLOW)
{
for (ID id : ids)
{
Object *s = type->Require(id);
if (s)
o.push_back(anope_dynamic_static_cast<T>(s));
}
return o;
}
std::transform(type->objects.begin(), type->objects.end(), std::back_inserter(o), [](Object *e) { return anope_dynamic_static_cast<T>(e); });
return o;
}
template<typename T>
std::vector<T> Serialize::GetObjects(const Anope::string &name)
{
std::vector<T> objs;
for (TypeBase *t : GetTypes(name))
for (T obj : GetObjects_<T>(t))
objs.push_back(obj);
return objs;
}
template<typename T>
std::vector<T> Serialize::GetObjects()
{
const char* const name = std::remove_pointer<T>::type::NAME;
return GetObjects<T>(name);
}
template<typename T>
T Serialize::New()
{
const char* const name = std::remove_pointer<T>::type::NAME;
Serialize::TypeBase *type = TypeBase::Find(name);
if (type == nullptr)
{
Anope::Logger.Debug("Serialize::New with unknown type {0}", name);
return nullptr;
}
return static_cast<T>(type->Create());
}
inline std::vector<Serialize::Object *> Serialize::Object::GetRefs(Serialize::TypeBase *type)
{
std::vector<Serialize::TypeBase *> types = GetTypes(type->GetName());
std::vector<Object *> objs;
if (types.empty())
{
Anope::Logger.Debug("GetRefs for unknown type on #{0} type {1} named {2}", this->id, s_type->GetName(), type->GetName());
return objs;
}
for (Serialize::TypeBase *t : types)
for (const Serialize::Edge &edge : GetEdges(t))
if (!edge.direction)
objs.push_back(edge.other);
return objs;
}
template<typename T>
std::vector<T> Serialize::Object::GetRefs()
{
const char* const name = std::remove_pointer<T>::type::NAME;
std::vector<Serialize::TypeBase *> types = GetTypes(name);
std::vector<T> objs;
if (types.empty())
{
Anope::Logger.Debug("GetRefs for unknown type on #{0} type {1} named {2}", this->id, s_type->GetName(), name);
return objs;
}
for (Serialize::TypeBase *t : types)
for (const Serialize::Edge &edge : GetEdges(t))
if (!edge.direction)
objs.push_back(anope_dynamic_static_cast<T>(edge.other));
return objs;
}
template<typename T>
void Serialize::Object::SetS(const Anope::string &name, const T &what)
{
FieldBase *field = s_type->GetField(name);
if (field == nullptr)
{
Anope::Logger.Debug("Set for unknown field {0} on {1}", name, s_type->GetName());
return;
}
FieldTypeBase<T> *fieldt = static_cast<FieldTypeBase<T> *>(field);
fieldt->SetFieldS(this, what);
}
template<typename T>
void Serialize::Object::UnsetS(const Anope::string &name)
{
FieldBase *field = s_type->GetField(name);
if (field == nullptr)
{
Anope::Logger.Debug("Unset for unknown field {0} on {1}", name, s_type->GetName());
return;
}
FieldTypeBase<T> *fieldt = static_cast<FieldTypeBase<T> *>(field);
fieldt->UnsetS(this);
}
inline bool Serialize::Object::HasFieldS(const Anope::string &name)
{
FieldBase *field = s_type->GetField(name);
if (field == nullptr)
{
Anope::Logger.Debug("HasField for unknown field {0} on {1}", name, s_type->GetName());
return false;
}
FieldTypeBase<void *> *fieldt = static_cast<FieldTypeBase<void *> *>(field);
return fieldt->HasFieldS(this);
}
|