Remove need for BaseClassDefinition

This commit is contained in:
Scott Kyle 2016-04-16 16:46:16 -07:00
parent 999900ff00
commit 7c97a1752e
8 changed files with 20 additions and 33 deletions

View File

@ -68,27 +68,22 @@ using MethodMap = std::map<std::string, typename T::FunctionCallback>;
template<typename T> template<typename T>
using PropertyMap = std::map<std::string, PropertyType<T>>; using PropertyMap = std::map<std::string, PropertyType<T>>;
template<typename T, typename U> template<typename T, typename U, typename V = void>
struct ClassDefinition { struct ClassDefinition {
using Internal = U; using Internal = U;
using Parent = V;
// Every specialization *must* at least have a name. // Every subclass *must* at least have a name.
std::string name; // std::string const name;
};
template<typename T, typename U = void>
struct BaseClassDefinition {
// This pointer does not need to be set.
ClassDefinition<T, U>* superclass;
// ClassDefinition specializations should inherit from this class and override what's needed below. // ClassDefinition specializations should inherit from this class and override what's needed below.
ConstructorType<T>* constructor; ConstructorType<T>* const constructor = nullptr;
MethodMap<T> static_methods; MethodMap<T> const static_methods = {};
PropertyMap<T> static_properties; PropertyMap<T> const static_properties = {};
MethodMap<T> methods; MethodMap<T> const methods = {};
PropertyMap<T> properties; PropertyMap<T> const properties = {};
IndexPropertyType<T> index_accessor; IndexPropertyType<T> const index_accessor = {};
StringPropertyType<T> string_accessor; StringPropertyType<T> const string_accessor = {};
}; };
template<typename T, typename ClassType> template<typename T, typename ClassType>

View File

@ -27,7 +27,7 @@ namespace js {
class Collection {}; class Collection {};
template<typename T> template<typename T>
struct CollectionClass : ClassDefinition<T, Collection>, BaseClassDefinition<T> { struct CollectionClass : ClassDefinition<T, Collection> {
std::string const name = "Collection"; std::string const name = "Collection";
}; };

View File

@ -58,7 +58,7 @@ struct List {
}; };
template<typename T> template<typename T>
struct ListClass : ClassDefinition<T, realm::List>, BaseClassDefinition<T, CollectionClass<T>> { struct ListClass : ClassDefinition<T, realm::List, CollectionClass<T>> {
using List = List<T>; using List = List<T>;
std::string const name = "List"; std::string const name = "List";

View File

@ -48,7 +48,7 @@ struct RealmObject {
}; };
template<typename T> template<typename T>
struct RealmObjectClass : ClassDefinition<T, realm::Object>, BaseClassDefinition<T> { struct RealmObjectClass : ClassDefinition<T, realm::Object> {
using RealmObject = RealmObject<T>; using RealmObject = RealmObject<T>;
const std::string name = "RealmObject"; const std::string name = "RealmObject";

View File

@ -199,7 +199,7 @@ class Realm {
}; };
template<typename T> template<typename T>
struct RealmClass : ClassDefinition<T, SharedRealm>, BaseClassDefinition<T> { struct RealmClass : ClassDefinition<T, SharedRealm> {
using Realm = Realm<T>; using Realm = Realm<T>;
std::string const name = "Realm"; std::string const name = "Realm";

View File

@ -58,7 +58,7 @@ struct Results {
}; };
template<typename T> template<typename T>
struct ResultsClass : ClassDefinition<T, realm::Results>, BaseClassDefinition<T, CollectionClass<T>> { struct ResultsClass : ClassDefinition<T, realm::Results, CollectionClass<T>> {
using Results = Results<T>; using Results = Results<T>;
std::string const name = "Results"; std::string const name = "Results";

View File

@ -40,6 +40,7 @@ template<typename ClassType>
class ObjectWrap { class ObjectWrap {
public: public:
using Internal = typename ClassType::Internal; using Internal = typename ClassType::Internal;
using ParentClassType = typename ClassType::Parent;
operator Internal*() const { operator Internal*() const {
return m_object.get(); return m_object.get();
@ -186,11 +187,6 @@ private:
} }
} }
template<typename U>
static JSClassRef get_superclass(ClassDefinition<U>*) {
return ObjectWrap<U>::get_class();
}
static std::vector<JSStaticFunction> get_methods(const MethodMap &methods) { static std::vector<JSStaticFunction> get_methods(const MethodMap &methods) {
std::vector<JSStaticFunction> functions; std::vector<JSStaticFunction> functions;
functions.reserve(methods.size() + 1); functions.reserve(methods.size() + 1);
@ -227,7 +223,7 @@ private:
std::vector<JSStaticFunction> methods; std::vector<JSStaticFunction> methods;
std::vector<JSStaticValue> properties; std::vector<JSStaticValue> properties;
definition.parentClass = get_superclass(s_class.superclass); definition.parentClass = ObjectWrap<ParentClassType>::get_class();
definition.className = s_class.name.c_str(); definition.className = s_class.name.c_str();
definition.finalize = finalize; definition.finalize = finalize;

View File

@ -86,6 +86,7 @@ static inline void setup_property(v8::Local<TargetType> target, const std::strin
template<typename ClassType> template<typename ClassType>
class ObjectWrap : public Nan::ObjectWrap { class ObjectWrap : public Nan::ObjectWrap {
using Internal = typename ClassType::Internal; using Internal = typename ClassType::Internal;
using ParentClassType = typename ClassType::Parent;
static ClassType s_class; static ClassType s_class;
@ -93,11 +94,6 @@ class ObjectWrap : public Nan::ObjectWrap {
ObjectWrap(Internal* object = nullptr) : m_object(object) {} ObjectWrap(Internal* object = nullptr) : m_object(object) {}
template<typename U>
static v8::Local<v8::FunctionTemplate> get_superclass(ClassDefinition<U>*) {
return ObjectWrap<U>::get_template();
}
static void get_nonexistent_property(v8::Local<v8::String> property, Nan::NAN_PROPERTY_GETTER_ARGS_TYPE info) { static void get_nonexistent_property(v8::Local<v8::String> property, Nan::NAN_PROPERTY_GETTER_ARGS_TYPE info) {
// Do nothing. This function exists only to prevent a crash where it is used. // Do nothing. This function exists only to prevent a crash where it is used.
} }
@ -149,7 +145,7 @@ class ObjectWrap : public Nan::ObjectWrap {
tpl->SetClassName(name); tpl->SetClassName(name);
instance_tpl->SetInternalFieldCount(1); instance_tpl->SetInternalFieldCount(1);
v8::Local<v8::FunctionTemplate> super_tpl = get_superclass(s_class.superclass); v8::Local<v8::FunctionTemplate> super_tpl = ObjectWrap<ParentClassType>::get_template();
if (!super_tpl.IsEmpty()) { if (!super_tpl.IsEmpty()) {
tpl->Inherit(super_tpl); tpl->Inherit(super_tpl);
} }