using MemFunc = Function<R, K, Ps...>;
using Invokable = std::function<R(K&, Ps...)>;
- explicit Functor(std::shared_ptr<Klass> instance, MemFunc memFunc);
+ explicit Functor(Klass& instance, MemFunc memFunc);
template<typename... Args>
auto operator()(Args&&... args) -> typename MemFunc::Return;
template<typename T, std::size_t... I>
auto operator()(T& tuple, IndexSequence<I...>) -> typename MemFunc::Return;
- std::shared_ptr<Klass> instance;
+ Klass& instance;
MemFunc memFunc;
};
}
template<typename R, typename K, typename... Ps>
-Functor<R, K, Ps...>::Functor(std::shared_ptr<Klass> instance, MemFunc memFunc)
+Functor<R, K, Ps...>::Functor(Klass& instance, MemFunc memFunc)
: instance(instance), memFunc(std::move(memFunc))
{
}
auto Functor<R, K, Ps...>::operator()(Args&&... args) -> typename MemFunc::Return
{
const Invokable& invokable = this->memFunc.get();
- return invokable(*this->instance, std::forward<Args>(args)...);
+ return invokable(this->instance, std::forward<Args>(args)...);
}
template<typename R, typename K, typename... Ps>
}
template<typename R, typename K, typename... Ps>
-Functor<R, K, Ps...> make_functor(std::shared_ptr<K> instance, R (K::* member)(Ps...))
+Functor<R, K, Ps...> make_functor(K& instance, R (K::* member)(Ps...))
{
- if (instance == nullptr)
- THROW(ErrCode::LogicError) << "Instance can't be nullptr.";
-
return Functor<R, K, Ps...>(instance, make_function(member));
}
template<typename R, typename K, typename... Ps>
-std::shared_ptr<Functor<R, K, Ps...>> make_functor_ptr(K* instance,
- R (K::* member)(Ps...))
-{
- if (instance == nullptr)
- THROW(ErrCode::LogicError) << "Instance can't be nullptr.";
-
- std::shared_ptr<K> smartPtr(instance);
- return std::make_shared<Functor<R, K, Ps...>>(smartPtr, make_function(member));
-}
-
-template<typename R, typename K, typename... Ps>
-std::shared_ptr<Functor<R, K, Ps...>> make_functor_ptr(std::shared_ptr<K> instance,
+std::shared_ptr<Functor<R, K, Ps...>> make_functor_ptr(K& instance,
R (K::* member)(Ps...))
{
- if (instance == nullptr)
- THROW(ErrCode::LogicError) << "Instance can't be nullptr.";
-
return std::make_shared<Functor<R, K, Ps...>>(instance, make_function(member));
}
TEST(FunctorTests, functor)
{
- auto foo = std::make_shared<Foo>();
+ Foo foo;
auto fooSetName = make_functor(foo, &Foo::setName);
auto fooGetName = make_functor(foo, &Foo::getName);
auto fooEcho = make_functor(foo, &Foo::echo);
TEST(FunctorTests, functor_map)
{
- auto foo = std::make_shared<Foo>();
-
+ Foo foo;
FunctorMap fooMap;
fooMap["setName"] = make_functor_ptr(foo, &Foo::setName);
fooMap["getName"] = make_functor_ptr(foo, &Foo::getName);
TEST(FunctorTests, archive)
{
- auto foo = std::make_shared<Foo>();
-
+ Foo foo;
FunctorMap fooMap;
fooMap["echo"] = make_functor_ptr(foo, &Foo::echo);
void stop(void);
template<typename O, typename F>
- void expose(O&& object, const std::string& name, F&& func);
+ void expose(O& object, const std::string& name, F&& func);
private:
class Impl;
};
template<typename O, typename F>
-void Gateway::expose(O&& object, const std::string& name, F&& func)
+void Gateway::expose(O& object, const std::string& name, F&& func)
{
- auto functor = klass::make_functor_ptr(std::forward<O>(object), std::forward<F>(func));
+ auto functor = klass::make_functor_ptr(object, std::forward<F>(func));
this->functorMap[name] = functor;
}