Skip to content

Commit

Permalink
Move Add and Remove to base class
Browse files Browse the repository at this point in the history
  • Loading branch information
davedoesdev committed Nov 14, 2019
1 parent 0b076ee commit 9ab1813
Show file tree
Hide file tree
Showing 8 changed files with 77 additions and 69 deletions.
8 changes: 4 additions & 4 deletions src/qlobber_base.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ struct Visit {

template<typename Value,
typename ValueStorage,
typename Remove,
typename RemoveValue,
typename MatchResult,
typename Context,
typename TestValue,
Expand Down Expand Up @@ -62,7 +62,7 @@ class QlobberBase {
}

void remove(const std::string& topic,
const std::optional<const Remove>& val) {
const std::optional<const RemoveValue>& val) {
std::unique_lock lock(mutex);
if (remove(val, 0, split(topic), trie) && options.cache_adds) {
shortcuts.erase(topic);
Expand Down Expand Up @@ -121,7 +121,7 @@ class QlobberBase {
virtual void initial_value_inserted(const Value& val) {}
virtual void add_value(ValueStorage& vals, const Value& val) = 0;
virtual bool remove_value(ValueStorage& vals,
const std::optional<const Remove>& val) = 0;
const std::optional<const RemoveValue>& val) = 0;
virtual bool test_values(const ValueStorage& vals,
const TestValue& val) = 0;
virtual void iter_values(typename coro_iter_t::push_type& sink,
Expand Down Expand Up @@ -160,7 +160,7 @@ class QlobberBase {
return add(val, i + 1, words, (*std::get<0>(sub_trie.v))[words[i]]);
}

bool remove(const std::optional<const Remove>& val,
bool remove(const std::optional<const RemoveValue>& val,
const std::size_t i,
const std::vector<std::string>& words,
const Trie& sub_trie) {
Expand Down
59 changes: 34 additions & 25 deletions src/qlobber_js_base.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,27 @@ template<typename Value,
typename JSValue,
typename MatchResult,
typename Context,
template<typename, typename, typename, typename, typename> typename Base,
template<typename, typename, typename, typename, typename, typename> typename Base,
typename RemoveValue = Value,
typename TestValue = Value,
typename IterValue = Value>
class QlobberJSCommon :
public Base<Value, MatchResult, Context, TestValue, IterValue> {
public Base<Value, MatchResult, Context, RemoveValue, TestValue, IterValue> {
public:
QlobberJSCommon(const Napi::CallbackInfo& info) :
Base<Value, MatchResult, Context, TestValue, IterValue>(JSOptions(info)) {}
Base<Value, MatchResult, Context, RemoveValue, TestValue, IterValue>(JSOptions(info)) {}

Napi::Value Add(const Napi::CallbackInfo& info) {
const auto topic = info[0].As<Napi::String>();
this->add(topic, get_add_value(info));
return info.This();
}

Napi::Value Remove(const Napi::CallbackInfo& info) {
const auto topic = info[0].As<Napi::String>();
this->remove(topic, get_remove_value(info));
return info.This();
}

Napi::Value Match(const Napi::CallbackInfo& info) {
const auto env = info.Env();
Expand All @@ -41,7 +54,7 @@ class QlobberJSCommon :

Napi::Value Test(const Napi::CallbackInfo& info) {
const auto topic = info[0].As<Napi::String>();
return Napi::Boolean::New(info.Env(), this->test(topic, get_test(info)));
return Napi::Boolean::New(info.Env(), this->test(topic, get_test_value(info)));
}

Napi::Value Clear(const Napi::CallbackInfo& info) {
Expand Down Expand Up @@ -199,7 +212,6 @@ class QlobberJSCommon :
return r;
}


Napi::Value GetShortcuts(const Napi::CallbackInfo& info) {
const auto env = info.Env();
const auto Map = env.Global().Get("Map").As<Napi::Function>();
Expand All @@ -225,13 +237,17 @@ class QlobberJSCommon :

virtual MatchResult NewMatchResult(const Napi::Env& env) = 0;

virtual TestValue get_test(const Napi::CallbackInfo& info) = 0;
virtual Value get_add_value(const Napi::CallbackInfo& info) = 0;

virtual std::optional<const RemoveValue> get_remove_value(const Napi::CallbackInfo& info) = 0;

virtual TestValue get_test_value(const Napi::CallbackInfo& info) = 0;
};

template<typename Value,
typename JSValue,
typename MatchResult,
template<typename, typename, typename, typename, typename> typename Base>
template<typename, typename, typename, typename, typename, typename> typename Base>
class QlobberJSBase :
public QlobberJSCommon<Value, JSValue, MatchResult, const std::nullptr_t, Base> {
public:
Expand All @@ -240,30 +256,23 @@ class QlobberJSBase :

virtual ~QlobberJSBase() {}

Napi::Value Add(const Napi::CallbackInfo& info) {
const auto topic = info[0].As<Napi::String>();
const auto val = info[1].As<JSValue>();
this->add(topic, val);
return info.This();
private:
std::nullptr_t get_context(const Napi::CallbackInfo& info) override {
return nullptr;
}

Napi::Value Remove(const Napi::CallbackInfo& info) {
const auto topic = info[0].As<Napi::String>();
if (info.Length() == 1) {
this->remove(topic, std::nullopt);
} else {
const auto val = info[1].As<JSValue>();
this->remove(topic, val);
}
return info.This();
Value get_add_value(const Napi::CallbackInfo& info) override {
return info[1].As<JSValue>();
}

private:
std::nullptr_t get_context(const Napi::CallbackInfo& info) override {
return nullptr;
std::optional<const Value> get_remove_value(const Napi::CallbackInfo& info) override {
if (info.Length() == 1) {
return std::nullopt;
}
return info[1].As<JSValue>();
}

Value get_test(const Napi::CallbackInfo& info) override {
Value get_test_value(const Napi::CallbackInfo& info) override {
return info[1].As<JSValue>();
}
};
Expand Down
3 changes: 2 additions & 1 deletion src/qlobber_set_base.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,15 @@ class SetStorage : public std::unordered_set<Value> {
template<typename Value,
typename MatchResult,
typename Context,
typename RemoveValue,
typename TestValue,
typename IterValue>
class QlobberSetBase;

template<typename Value,
typename MatchResult,
typename Context>
class QlobberSetBase<Value, MatchResult, Context, Value, Value> :
class QlobberSetBase<Value, MatchResult, Context, Value, Value, Value> :
public QlobberContainerBase<Value,
SetStorage,
MatchResult,
Expand Down
43 changes: 20 additions & 23 deletions src/qlobber_sub.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ class QlobberSub :
Napi::Array,
const std::optional<const std::string>,
QlobberSubBase,
std::string,
SubTest,
IterSub>,
public Napi::ObjectWrap<QlobberSub> {
Expand All @@ -18,34 +19,13 @@ class QlobberSub :
Napi::Array,
const std::optional<const std::string>,
QlobberSubBase,
std::string,
SubTest,
IterSub>(info),
Napi::ObjectWrap<QlobberSub>(info) {}

virtual ~QlobberSub() {}

Napi::Value Add(const Napi::CallbackInfo& info) {
const auto topic = info[0].As<Napi::String>();
const auto val = info[1].As<Napi::Object>();
add(topic, {
val.Get("clientId").As<Napi::String>(),
val.Get("topic").As<Napi::String>(),
static_cast<QoS>(val.Get("qos").As<Napi::Number>().Uint32Value())
});
return info.This();
}

Napi::Value Remove(const Napi::CallbackInfo& info) {
const auto topic = info[0].As<Napi::String>();
if (info.Length() == 1) {
remove(topic, std::nullopt);
} else {
const auto val = info[1].As<Napi::Object>();
remove(topic, val.Get("clientId").As<Napi::String>());
}
return info.This();
}

Napi::Value GetSubscriptionsCount(const Napi::CallbackInfo& info) {
return Napi::Number::New(info.Env(), subscriptionsCount);
}
Expand All @@ -62,7 +42,24 @@ class QlobberSub :
return Napi::Array::New(env);
}

SubTest get_test(const Napi::CallbackInfo& info) override {
Sub get_add_value(const Napi::CallbackInfo& info) override {
const auto val = info[1].As<Napi::Object>();
return {
val.Get("clientId").As<Napi::String>(),
val.Get("topic").As<Napi::String>(),
static_cast<QoS>(val.Get("qos").As<Napi::Number>().Uint32Value())
};
}

std::optional<const std::string> get_remove_value(const Napi::CallbackInfo& info) override {
if (info.Length() == 1) {
return std::nullopt;
}
const auto val = info[1].As<Napi::Object>();
return val.Get("clientId").As<Napi::String>();
}

SubTest get_test_value(const Napi::CallbackInfo& info) override {
const auto val = info[1].As<Napi::Object>();
return {
val.Get("clientId").As<Napi::String>(),
Expand Down
3 changes: 2 additions & 1 deletion src/qlobber_sub_base.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,13 @@ struct SubTest {
template<typename Value,
typename MatchResult,
typename Context,
typename RemoveValue,
typename TestValue,
typename IterValue>
class QlobberSubBase;

template<typename MatchResult, typename Context>
class QlobberSubBase<Sub, MatchResult, Context, SubTest, IterSub> :
class QlobberSubBase<Sub, MatchResult, Context, std::string, SubTest, IterSub> :
public QlobberBase<Sub,
SubStorage,
std::string,
Expand Down
24 changes: 11 additions & 13 deletions src/qlobber_true.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ class QlobberTrue :
bool,
const std::nullptr_t,
QlobberTrueBase,
std::nullptr_t,
std::nullptr_t>,
public Napi::ObjectWrap<QlobberTrue> {
public:
Expand All @@ -17,23 +18,12 @@ class QlobberTrue :
bool,
const std::nullptr_t,
QlobberTrueBase,
std::nullptr_t,
std::nullptr_t>(info),
Napi::ObjectWrap<QlobberTrue>(info) {}

virtual ~QlobberTrue() {}

Napi::Value Add(const Napi::CallbackInfo& info) {
const auto topic = info[0].As<Napi::String>();
add(topic, TrueValue());
return info.This();
}

Napi::Value Remove(const Napi::CallbackInfo& info) {
const auto topic = info[0].As<Napi::String>();
remove(topic, std::nullopt);
return info.This();
}

private:
std::nullptr_t get_context(const Napi::CallbackInfo&) override {
return nullptr;
Expand All @@ -43,7 +33,15 @@ class QlobberTrue :
return false;
}

std::nullptr_t get_test(const Napi::CallbackInfo&) override {
TrueValue get_add_value(const Napi::CallbackInfo&) override {
return TrueValue();
}

std::optional<const std::nullptr_t> get_remove_value(const Napi::CallbackInfo&) override {
return std::nullopt;
}

std::nullptr_t get_test_value(const Napi::CallbackInfo&) override {
return nullptr;
}
};
Expand Down
3 changes: 2 additions & 1 deletion src/qlobber_true_base.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,13 @@ struct TrueStorage {
template<typename Value,
typename MatchResult,
typename Context,
typename RemoveValue,
typename TestValue,
typename IterValue>
class QlobberTrueBase;

template<>
class QlobberTrueBase<TrueValue, bool, const std::nullptr_t, std::nullptr_t, TrueValue> :
class QlobberTrueBase<TrueValue, bool, const std::nullptr_t, std::nullptr_t, std::nullptr_t, TrueValue> :
public QlobberBase<TrueValue,
TrueStorage,
std::nullptr_t,
Expand Down
3 changes: 2 additions & 1 deletion src/qlobber_vec_base.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,15 @@ class VecStorage : public std::vector<Value> {
template<typename Value,
typename MatchResult,
typename Context,
typename RemoveValue,
typename TestValue,
typename IterValue>
class QlobberVecBase;

template<typename Value,
typename MatchResult,
typename Context>
class QlobberVecBase<Value, MatchResult, Context, Value, Value> :
class QlobberVecBase<Value, MatchResult, Context, Value, Value, Value> :
public QlobberContainerBase<Value,
VecStorage,
MatchResult,
Expand Down

0 comments on commit 9ab1813

Please sign in to comment.