Change klass instance type on RMI
authorSangwan Kwon <sangwan.kwon@samsung.com>
Fri, 27 Dec 2019 06:38:13 +0000 (15:38 +0900)
committerSangwan Kwon <sangwan.kwon@samsung.com>
Mon, 30 Dec 2019 04:51:16 +0000 (13:51 +0900)
There are two main issues that the type is changed from shared_ptr
to reference.

First, if klass instance type is shared_ptr,
the destuctor of klass should be public not private.
(This coflicts with meyer singleton.)

Second, if we pass this pointer of object to EXPOSE method,
the object indicated this pointer should be destroyed by shared_ptr.
In such a case, the object should not be static type.
(It causes SEGFAULT by deallocating invalid pointer.)

Signed-off-by: Sangwan Kwon <sangwan.kwon@samsung.com>
CMakeLists.txt
src/vist/klass/functor.hpp
src/vist/klass/tests/functor.cpp
src/vist/main/main.cpp
src/vist/rmi/gateway.cpp
src/vist/rmi/gateway.hpp
src/vist/rmi/tests/rmi.cpp
src/vist/service/tests/core.cpp
src/vist/service/vistd.cpp
src/vist/service/vistd.hpp

index 25612060cd735dfcdb6a5d04681a35cf9e40dd28..bf7c104818464520c2832f97ad31a8dc08974424 100644 (file)
@@ -67,4 +67,7 @@ ADD_SUBDIRECTORY(data)
 ADD_SUBDIRECTORY(specs)
 ADD_SUBDIRECTORY(src)
 ADD_SUBDIRECTORY(plugins)
-ADD_SUBDIRECTORY(systemd)
+
+IF(DEFINED GBS_BUILD)
+       ADD_SUBDIRECTORY(systemd)
+ENDIF(DEFINED GBS_BUILD)
index 63cd9dbcee4448bfb9d610943546dd549fc705b0..6e61d4c492b4cc40e47d50063ab0ead21e17b2a2 100644 (file)
@@ -50,7 +50,7 @@ public:
        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;
@@ -65,7 +65,7 @@ private:
        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;
 };
 
@@ -87,7 +87,7 @@ Archive AbstractFunctor::invoke(Archive& archive)
 }
 
 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))
 {
 }
@@ -97,7 +97,7 @@ template<typename... Args>
 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>
@@ -136,32 +136,15 @@ auto Functor<R, K, Ps...>::operator()(T& tuple,
 }
 
 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));
 }
 
index 91b0360c719fdf967aa7488d74084406dad4652b..c425c4d1bf5f70ac5f5770b539cb97a88a3e0e26 100644 (file)
@@ -50,7 +50,7 @@ struct Foo {
 
 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);
@@ -72,8 +72,7 @@ TEST(FunctorTests, functor)
 
 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);
@@ -99,8 +98,7 @@ TEST(FunctorTests, functor_map)
 
 TEST(FunctorTests, archive)
 {
-       auto foo = std::make_shared<Foo>();
-
+       Foo foo;
        FunctorMap fooMap;
        fooMap["echo"] = make_functor_ptr(foo, &Foo::echo);
 
index 3288accdcf181950d138b8ea421d34c3571d58b3..3ceb010b5e63e834e5ddec95392ddf13f87bf929 100644 (file)
@@ -32,7 +32,7 @@ int main() try {
        LogStream::Init(std::make_shared<Dlog>());
 #endif
 
-       Vistd::Instance().start();
+       Vistd::Instance().Start();
 
        return EXIT_SUCCESS;
 } catch(const Exception<ErrCode>& e) {
index 02b2f064dd3a258b56cac7565786b040abf35e46..d8c176004cefb614a9c77254450d128ffa894e14 100644 (file)
@@ -71,7 +71,7 @@ private:
        std::unique_ptr<Server> server;
 };
 
-Gateway::Gateway(const std::string& path) : pImpl(new Impl(*this, path))
+Gateway::Gateway(const std::string& path) : pImpl(std::make_unique<Impl>(*this, path))
 {
 }
 
index 69a66769f1439db5bc8eede3417bc2aa0df338ad..4a836198c7c474e2e44637bc0dfd922ae072b93b 100644 (file)
@@ -47,7 +47,7 @@ public:
        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;
@@ -57,9 +57,9 @@ private:
 };
 
 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;
 }
 
index da5beb5b4df246e2af84288b3859576a5f021486..d73663e1aa88e9c3eb9dff2cd95a5e0709f68a98 100644 (file)
@@ -62,12 +62,12 @@ TEST(RmiTests, positive)
        // gateway-side
        Gateway gateway(sockPath);
 
-       auto foo = std::make_shared<Foo>();
+       Foo foo;
        gateway.expose(foo, "Foo::setName", &Foo::setName);
        gateway.expose(foo, "Foo::getName", &Foo::getName);
 
        // convenience macro
-       auto bar = std::make_shared<Bar>();
+       Bar bar;
        EXPOSE(gateway, bar, &Bar::plusTwo);
 
        auto client = std::thread([&]() {
index 8b329454e67fa4986554cfcb60f4add0558b5bdf..66aac2ae3fe6d8db50a0824cea7db1b3e68a999a 100644 (file)
@@ -21,7 +21,6 @@
 
 #include <iostream>
 #include <chrono>
-#include <thread>
 
 using namespace vist;
 
index a2c466137a7f0c9ea29c1a0bb0888d6aaaec5869..585f72bc45e74395e7064a6c0ea0f4f71aa4e532 100644 (file)
@@ -42,7 +42,8 @@ void Vistd::start()
        policy::PolicyManager::Instance();
 
        rmi::Gateway gateway(SOCK_ADDR);
-       EXPOSE(gateway, this, &Vistd::query);
+       EXPOSE(gateway, *this, &Vistd::query);
+
        gateway.start();
 }
 
index e5f4bb3e02dbe34f9df5f1df5fa11002093e997c..818684a2ad48707ab9608fe117586faa95d8c41c 100644 (file)
@@ -17,6 +17,7 @@
 #pragma once
 
 #include <map>
+#include <memory>
 #include <string>
 #include <vector>
 
@@ -25,10 +26,8 @@ namespace vist {
 using Row = std::map<std::string, std::string>;
 using Rows = std::vector<Row>;
 
-class Vistd final {
+class Vistd final : public std::enable_shared_from_this<Vistd> {
 public:
-       ~Vistd() = default;
-
        Vistd(const Vistd&) = delete;
        Vistd& operator=(const Vistd&) = delete;
 
@@ -49,10 +48,16 @@ public:
                return Vistd::Instance().query(statement);
        }
 
-       void start();
+       static void Start()
+       {
+               Vistd::Instance().start();
+       }
 
 private:
        explicit Vistd();
+       ~Vistd() = default;
+
+       void start();
 };
 
 } // namespace vist