Skip to content

Commit

Permalink
extract non-template base class of InstanceClassDefineBuilder<T>
Browse files Browse the repository at this point in the history
clang(release) UnitTest(Lua)
3097576 - 3048232 = 49,344
3048232 - 2974176 = 74,056
2974176 - 2901120 = 73,056
2901120 - 2773712 = 127,408
# 2774352 - 2774144 = 208
  • Loading branch information
LanderlYoung committed Mar 11, 2023
1 parent 6a0819f commit 16636e1
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 31 deletions.
16 changes: 6 additions & 10 deletions src/Native.h
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,8 @@ class ClassDefineState;
template <typename T>
class InstanceDefineBuilder;

class InstanceDefineBuilderState;

#define SCRIPTX_CLASS_DEFINE_FRIENDS \
template <typename TT> \
friend class ::script::ClassDefine; \
Expand All @@ -376,7 +378,8 @@ class InstanceDefineBuilder;
friend typename ::script::internal::ImplType<::script::ScriptEngine>::type; \
friend class ::script::internal::ClassDefineState; \
template <typename TT> \
friend class ::script::internal::InstanceDefineBuilder
friend class ::script::internal::InstanceDefineBuilder; \
friend class ::script::internal::InstanceDefineBuilderState

class StaticDefine {
class PropertyDefine {
Expand Down Expand Up @@ -421,14 +424,7 @@ constexpr inline size_t sizeof_helper_v = sizeof(T);
template <>
constexpr inline size_t sizeof_helper_v<void> = 0;

// template <typename T>
class InstanceDefine {
// static_assert(std::is_void_v<T> || std::is_base_of_v<ScriptClass, T>,
// "T must be subclass of ScriptClass, "
// "and can be void if no instance is required.");

using Constructor = InstanceConstructor;

class PropertyDefine {
using SetterCallback = InstanceSetterCallback;
using GetterCallback = InstanceGetterCallback;
Expand Down Expand Up @@ -466,12 +462,12 @@ class InstanceDefine {
* when null is returned, an exception is thrown.
* (Either inside the constructor, or if not, by the ScriptEngine).
*/
const Constructor constructor{};
const InstanceConstructor constructor{};
const std::vector<FunctionDefine> functions{};
const std::vector<PropertyDefine> properties{};
const size_t instanceSize; // = internal::sizeof_helper_v<T>;

InstanceDefine(Constructor constructor, std::vector<FunctionDefine> functions,
InstanceDefine(InstanceConstructor constructor, std::vector<FunctionDefine> functions,
std::vector<PropertyDefine> properties, size_t instanceSize)
: constructor(std::move(constructor)),
functions(std::move(functions)),
Expand Down
47 changes: 26 additions & 21 deletions src/Native.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -744,25 +744,34 @@ ScriptEngine::newNativeClass(T&&... args) {

namespace internal {

class InstanceDefineBuilderState {
public:
void* thiz = nullptr;

// instance
InstanceConstructor constructor_{};
std::vector<InstanceDefine::FunctionDefine> insFunctions_{};
std::vector<InstanceDefine::PropertyDefine> insProperties_{};
};

template <typename T>
class InstanceDefineBuilder {
class InstanceDefineBuilder : public InstanceDefineBuilderState {
template <typename...>
using sfina = ClassDefineBuilder<T>&;

ClassDefineBuilder<T>& thiz;
ClassDefineBuilder<T>& thiz() {
return *static_cast<ClassDefineBuilder<T>*>(InstanceDefineBuilderState::thiz);
};

protected:
// instance
InstanceDefine::Constructor constructor_{};
std::vector<InstanceDefine::FunctionDefine> insFunctions_{};
std::vector<InstanceDefine::PropertyDefine> insProperties_{};

explicit InstanceDefineBuilder(ClassDefineBuilder<T>& thiz) : thiz(thiz) {}
explicit InstanceDefineBuilder(ClassDefineBuilder<T>& thiz) {
InstanceDefineBuilderState::thiz = &thiz;
}

public:
ClassDefineBuilder<T>& constructor(InstanceConstructor constructor) {
constructor_ = std::move(constructor);
return thiz;
return thiz();
}

/**
Expand All @@ -774,29 +783,29 @@ class InstanceDefineBuilder {
ClassDefineBuilder<T>& constructor() {
static_assert(ClassConstructorHelper<T>::value);
constructor_ = internal::bindConstructor<T>();
return thiz;
return thiz();
}

/**
* A helper method to disallow construct of this Class. (It's constructor always throw exception)
*/
ClassDefineBuilder<T>& constructor(std::nullptr_t) {
constructor_ = [](const Arguments&) -> T* { return nullptr; };
return thiz;
return thiz();
}

ClassDefineBuilder<T>& instanceFunction(std::string name, InstanceFunctionCallback func) {
insFunctions_.push_back(
typename InstanceDefine::FunctionDefine{std::move(name), std::move(func), {}});
return thiz;
return thiz();
}

template <typename Func>
sfina<decltype(internal::bindInstanceFunc<T>(std::declval<Func>(), false))> instanceFunction(
std::string name, Func func, bool nothrow = kBindingNoThrowDefaultValue) {
insFunctions_.push_back(typename InstanceDefine::FunctionDefine{
std::move(name), internal::bindInstanceFunc<T>(std::move(func), nothrow), {}});
return thiz;
return thiz();
}

template <typename G, typename S = InstanceSetterCallback>
Expand All @@ -809,7 +818,7 @@ class InstanceDefineBuilder {
internal::bindInstanceGet<T>(std::forward<G>(getter), nothrow),
internal::bindInstanceSet<T>(std::forward<S>(setterCallback), nothrow),
{}});
return thiz;
return thiz();
}

template <typename S>
Expand All @@ -820,7 +829,7 @@ class InstanceDefineBuilder {
nullptr,
internal::bindInstanceSet<T>(std::forward<S>(setterCallback), nothrow),
{}});
return thiz;
return thiz();
}

template <typename P, typename BaseClass>
Expand All @@ -831,18 +840,14 @@ class InstanceDefineBuilder {
auto prop = internal::bindInstanceProp<T, BaseClass, P>(ptr, nothrow);
insProperties_.push_back(typename InstanceDefine::PropertyDefine{
std::move(name), std::move(prop.first), std::move(prop.second), {}});
return thiz;
return thiz();
}
};

// specialize for void
template <>
class InstanceDefineBuilder<void> {
class InstanceDefineBuilder<void> : public InstanceDefineBuilderState {
protected:
InstanceDefine::Constructor constructor_{};
std::vector<InstanceDefine::FunctionDefine> insFunctions_{};
std::vector<InstanceDefine::PropertyDefine> insProperties_{};

explicit InstanceDefineBuilder(ClassDefineBuilder<void>&) {}

public:
Expand Down

0 comments on commit 16636e1

Please sign in to comment.