SkReflected - avoid double registration, auto register base types, etc.

Bug: skia:
Change-Id: I99913987ec5de044ecc9302335771f59d85126dc
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/198243
Reviewed-by: Brian Osman <brianosman@google.com>
Commit-Queue: Brian Osman <brianosman@google.com>
This commit is contained in:
Brian Osman 2019-03-06 15:00:36 -05:00 committed by Skia Commit-Bot
parent 93092ff457
commit 93c47cc8e3

View File

@ -49,6 +49,7 @@ public:
const char* fName;
const Type* fBase;
Factory fFactory;
bool fRegistered = false;
bool isDerivedFrom(const Type* t) const {
const Type* base = fBase;
@ -65,6 +66,7 @@ public:
virtual const Type* getType() const = 0;
static const Type* GetType() {
static Type gType{ "SkReflected", nullptr, nullptr };
RegisterOnce(&gType);
return &gType;
}
@ -73,10 +75,6 @@ public:
return thisType == t || thisType->isDerivedFrom(t);
}
static void Register(const Type* type) {
gTypes.push_back(type);
}
static sk_sp<SkReflected> CreateInstance(const char* name) {
for (const Type* type : gTypes) {
if (0 == strcmp(name, type->fName)) {
@ -90,6 +88,14 @@ public:
static void VisitTypes(std::function<void(const Type*)> visitor);
protected:
static void RegisterOnce(Type* type) {
if (!type->fRegistered) {
gTypes.push_back(type);
type->fRegistered = true;
}
}
private:
static SkSTArray<16, const Type*, true> gTypes;
};
@ -100,6 +106,7 @@ private:
} \
static const Type* GetType() { \
static Type gType{ #TYPE, BASE::GetType(), CreateProc }; \
RegisterOnce(&gType); \
return &gType; \
} \
const Type* getType() const override { return GetType(); }
@ -107,11 +114,12 @@ private:
#define REFLECTED_ABSTRACT(TYPE, BASE) \
static const Type* GetType() { \
static Type gType{ #TYPE, BASE::GetType(), nullptr }; \
RegisterOnce(&gType); \
return &gType; \
} \
const Type* getType() const override { return GetType(); }
#define REGISTER_REFLECTED(TYPE) SkReflected::Register(TYPE::GetType())
#define REGISTER_REFLECTED(TYPE) TYPE::GetType()
///////////////////////////////////////////////////////////////////////////////