From b33ddfc42f55bdc59e647f4f3855d27a9b0134b7 Mon Sep 17 00:00:00 2001
From: Joaquim Casals <jcasals@iri.upc.edu>
Date: Mon, 25 Feb 2019 12:04:48 +0100
Subject: [PATCH] Added Loader layer, reverted to Raw loading

---
 hello_plugin/CMakeLists.txt      |  6 +++--
 hello_plugin/hello_plugin.cpp    | 14 +++++++----
 hello_plugin/loader.hpp          | 43 ++++++++++++++++++++++++++++++++
 hello_plugin/params_autoconf.cpp |  9 +++----
 4 files changed, 60 insertions(+), 12 deletions(-)
 create mode 100644 hello_plugin/loader.hpp

diff --git a/hello_plugin/CMakeLists.txt b/hello_plugin/CMakeLists.txt
index 4582cc409..ff20e6375 100644
--- a/hello_plugin/CMakeLists.txt
+++ b/hello_plugin/CMakeLists.txt
@@ -5,8 +5,10 @@ INCLUDE_DIRECTORIES(${CMAKE_CURRENT_SOURCE_DIR})
 # ADD_EXECUTABLE(hello_plugin hello_plugin.cpp sensor_odom_2D.cpp processor_odom_2D.cpp)
 ADD_EXECUTABLE(hello_plugin hello_plugin.cpp)
 ADD_EXECUTABLE(params_autoconf params_autoconf.cpp)
-target_link_libraries(hello_plugin class_loader boost_system console_bridge wolf yaml-cpp ${CERES_LIBRARIES})
-target_link_libraries(params_autoconf class_loader boost_system console_bridge wolf yaml-cpp )
+# target_link_libraries(hello_plugin class_loader boost_system console_bridge wolf yaml-cpp ${CERES_LIBRARIES})
+# target_link_libraries(params_autoconf class_loader boost_system console_bridge wolf yaml-cpp )
+target_link_libraries(hello_plugin boost_system wolf yaml-cpp ${CERES_LIBRARIES} dl)
+target_link_libraries(params_autoconf boost_system wolf yaml-cpp dl)
 # These lines always at the end
 SET(HDRS_PLUGIN ${HDRS_PLUGIN}   PARENT_SCOPE    )
 SET(SRCS_PLUGIN ${SRCS_PLUGIN}    PARENT_SCOPE    )
diff --git a/hello_plugin/hello_plugin.cpp b/hello_plugin/hello_plugin.cpp
index 8091a6f1f..0f73b2ffe 100644
--- a/hello_plugin/hello_plugin.cpp
+++ b/hello_plugin/hello_plugin.cpp
@@ -4,7 +4,6 @@
  *  Created on: Nov 12, 2018
  *      Author: jcasals
  */
-#include <class_loader/class_loader.hpp>
 #include "base/sensor/sensor_base.h"
 #include "base/wolf.h"
 // #include "sensor_odom_2D.cpp"
@@ -16,13 +15,13 @@
 #include "../hello_wolf/feature_range_bearing.h"
 #include "../hello_wolf/constraint_range_bearing.h"
 #include "../hello_wolf/landmark_point_2D.h"
+#include "loader.hpp"
 #include "base/processor/processor_odom_2D.h"
 
 #include "base/solver/solver_factory.h"
 #include "base/ceres_wrapper/ceres_manager.h"
 
 using namespace std;
-using namespace class_loader;
 using namespace wolf;
 using namespace Eigen;
 
@@ -40,10 +39,15 @@ int main(int argc, char** argv) {
        a segmentation fault. Likewise, it seems that these ClassLoaders must be allocated at the heap, because
        the constructor refuses to build an object if I try to do local (stack) allocation, i.e `ClassLoader(it)` is not allowed but `new ClassLoader(it)` is.
      **/
-    vector<ClassLoader*> class_loaders = vector<ClassLoader*>();
+    // vector<ClassLoader*> class_loaders = vector<ClassLoader*>();
+    // for(auto it : parser.getFiles()) {
+    //     auto c = new ClassLoader(it);
+    //     class_loaders.push_back(c);
+    // }
+    auto loaders = vector<Loader*>();
     for(auto it : parser.getFiles()) {
-        auto c = new ClassLoader(it);
-        class_loaders.push_back(c);
+        auto l = new LoaderRaw(it);
+        loaders.push_back(l);
     }
     ProblemPtr problem = Problem::create("PO 2D");
     auto sensorMap = map<string, SensorBasePtr>();
diff --git a/hello_plugin/loader.hpp b/hello_plugin/loader.hpp
new file mode 100644
index 000000000..4f36eeecb
--- /dev/null
+++ b/hello_plugin/loader.hpp
@@ -0,0 +1,43 @@
+#ifndef LOADER_HPP
+#define LOADER_HPP
+#include <dlfcn.h>
+class Loader{
+protected:
+    std::string path_;
+public:
+    Loader(std::string _file){
+        this->path_ = _file;
+    }
+    virtual void load() = 0;
+    virtual void close() = 0;
+};
+class LoaderRaw: public Loader{
+    void* resource_;
+public:
+    LoaderRaw(std::string _file):
+        Loader(_file)
+    {
+        //
+    }
+    ~LoaderRaw(){
+        this->close();
+    }
+    void load(){
+        this->resource_ = dlopen(this->path_.c_str(), RTLD_LAZY);
+        if(not this->resource_)
+            throw std::runtime_error("Couldn't load resource with path " + this->path_);
+    }
+    void close(){
+        if(this->resource_) dlclose(this->resource_);
+    }
+};
+// class LoaderPlugin: public Loader{
+//     ClassLoader* resource_;
+//     void load(){
+//         this->resource_ = new ClassLoader(this->path_);
+//     }
+//     void close(){
+//         delete this->resource_;
+//     }
+// };
+#endif
\ No newline at end of file
diff --git a/hello_plugin/params_autoconf.cpp b/hello_plugin/params_autoconf.cpp
index ead3ae34a..e987769c1 100644
--- a/hello_plugin/params_autoconf.cpp
+++ b/hello_plugin/params_autoconf.cpp
@@ -4,7 +4,6 @@
  *  Created on: Feb 15, 2019
  *      Author: jcasals
  */
-#include <class_loader/class_loader.hpp>
 #include "base/sensor/sensor_base.h"
 #include "base/wolf.h"
 // #include "sensor_odom_2D.cpp"
@@ -16,13 +15,13 @@
 #include "../hello_wolf/feature_range_bearing.h"
 #include "../hello_wolf/constraint_range_bearing.h"
 #include "../hello_wolf/landmark_point_2D.h"
+#include "loader.hpp"
 #include "base/processor/processor_odom_2D.h"
 
 #include "base/solver/solver_factory.h"
 #include "base/ceres_wrapper/ceres_manager.h"
 
 using namespace std;
-using namespace class_loader;
 using namespace wolf;
 using namespace Eigen;
 
@@ -40,10 +39,10 @@ int main(int argc, char** argv) {
        a segmentation fault. Likewise, it seems that these ClassLoaders must be allocated at the heap, because
        the constructor refuses to build an object if I try to do local (stack) allocation, i.e `ClassLoader(it)` is not allowed but `new ClassLoader(it)` is.
      **/
-    vector<ClassLoader*> class_loaders = vector<ClassLoader*>();
+    auto loaders = vector<Loader*>();
     for(auto it : parser.getFiles()) {
-        auto c = new ClassLoader(it);
-        class_loaders.push_back(c);
+        auto l = new LoaderRaw(it);
+        loaders.push_back(l);
     }
     ProblemPtr problem = Problem::create("PO 2D");
     auto sensorMap = map<string, SensorBasePtr>();
-- 
GitLab