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

View File

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

View File

@ -58,7 +58,7 @@ struct List {
};
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>;
std::string const name = "List";

View File

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

View File

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

View File

@ -58,7 +58,7 @@ struct Results {
};
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>;
std::string const name = "Results";

View File

@ -40,6 +40,7 @@ template<typename ClassType>
class ObjectWrap {
public:
using Internal = typename ClassType::Internal;
using ParentClassType = typename ClassType::Parent;
operator Internal*() const {
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) {
std::vector<JSStaticFunction> functions;
functions.reserve(methods.size() + 1);
@ -227,7 +223,7 @@ private:
std::vector<JSStaticFunction> methods;
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.finalize = finalize;

View File

@ -86,6 +86,7 @@ static inline void setup_property(v8::Local<TargetType> target, const std::strin
template<typename ClassType>
class ObjectWrap : public Nan::ObjectWrap {
using Internal = typename ClassType::Internal;
using ParentClassType = typename ClassType::Parent;
static ClassType s_class;
@ -93,11 +94,6 @@ class ObjectWrap : public Nan::ObjectWrap {
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) {
// 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);
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()) {
tpl->Inherit(super_tpl);
}