ObserverLoader.h
Go to the documentation of this file.
1 /*
2  * Copyright 2015-2019 CNRS-UM LIRMM, CNRS-AIST JRL
3  */
4 
5 #pragma once
6 
10 #include <mc_observers/api.h>
11 #include <mc_rtc/config.h>
12 #include <mc_rtc/loader.h>
13 
14 #include <memory>
15 #include <mutex>
16 
17 namespace mc_observers
18 {
19 
24 {
25 public:
33  template<typename... Args>
34  static std::shared_ptr<mc_observers::Observer> get_observer(const std::string & name, const Args &... args)
35  {
36  std::lock_guard<std::mutex> guard{mtx};
37  init();
38  return observer_loader->create_object(name, args...);
39  }
40 
48  inline static std::string get_observer_runtime_directory(const std::string & name) noexcept
49  {
50  std::lock_guard<std::mutex> guard{mtx};
51  init();
52  return observer_loader->get_object_runtime_directory(name);
53  }
54 
55  template<typename RetT, typename... Args>
56  static void register_object(const std::string & name, std::function<RetT *(const Args &...)> callback)
57  {
58  std::unique_lock<std::mutex> guard{mtx};
59  init();
60  observer_loader->register_object(name, callback);
61  }
62 
66  static inline void update_module_path(const std::vector<std::string> & paths)
67  {
68  std::lock_guard<std::mutex> guard{mtx};
69  init();
70  observer_loader->load_libraries(paths);
71  }
72 
74  static inline void clear()
75  {
76  std::lock_guard<std::mutex> guard{mtx};
77  init(true);
78  observer_loader->clear();
79  }
80 
84  static bool has_observer(const std::string & name)
85  {
86  std::lock_guard<std::mutex> guard{mtx};
87  init();
88  return observer_loader->has_object(name);
89  }
90 
91  static void set_verbosity(bool verbose)
92  {
93  std::lock_guard<std::mutex> guard{mtx};
94  verbose_ = verbose;
95  if(observer_loader) { observer_loader->set_verbosity(verbose); }
96  }
97 
99  static std::vector<std::string> available_observers()
100  {
101  std::lock_guard<std::mutex> guard{mtx};
102  init();
103  return observer_loader->objects();
104  }
105 
106 private:
107  static inline void init(bool skip_default_path = false)
108  {
109  if(!observer_loader)
110  {
111  try
112  {
113  std::vector<std::string> default_path = {};
114  if(!skip_default_path) { default_path.push_back(mc_rtc::MC_OBSERVERS_INSTALL_PREFIX); }
115  observer_loader.reset(
116  new mc_rtc::ObjectLoader<mc_observers::Observer>("MC_RTC_OBSERVER_MODULE", default_path, verbose_));
117  }
118  catch(const mc_rtc::LoaderException & exc)
119  {
120  mc_rtc::log::error("Failed to initialize ObserverLoader: {}", exc.what());
121  throw(exc);
122  }
123  }
124  }
125  static std::unique_ptr<mc_rtc::ObjectLoader<mc_observers::Observer>> observer_loader;
126  static bool verbose_;
127  static std::mutex mtx;
128 };
129 
130 } // namespace mc_observers
mc_observers::ObserverLoader::get_observer
static std::shared_ptr< mc_observers::Observer > get_observer(const std::string &name, const Args &... args)
Definition: ObserverLoader.h:34
mc_rtc::LoaderException::what
virtual const char * what() const noexcept override
Definition: loader.h:47
mc_rtc::ObjectLoader
ltdl wrapper for factory-like classes
Definition: loader.h:37
MC_OBSERVERS_DLLAPI
#define MC_OBSERVERS_DLLAPI
Definition: api.h:47
mc_observers::ObserverLoader::set_verbosity
static void set_verbosity(bool verbose)
Definition: ObserverLoader.h:91
Observer.h
mc_observers::ObserverLoader::clear
static void clear()
Definition: ObserverLoader.h:74
mc_observers::ObserverLoader::available_observers
static std::vector< std::string > available_observers()
Definition: ObserverLoader.h:99
mc_observers::ObserverLoader::update_module_path
static void update_module_path(const std::vector< std::string > &paths)
Definition: ObserverLoader.h:66
mc_observers::ObserverLoader::get_observer_runtime_directory
static std::string get_observer_runtime_directory(const std::string &name) noexcept
Definition: ObserverLoader.h:48
mc_observers::ObserverLoader::register_object
static void register_object(const std::string &name, std::function< RetT *(const Args &...)> callback)
Definition: ObserverLoader.h:56
mc_rtc::LoaderException
Exception thrown by loader interface.
Definition: loader.h:42
mc_observers
Definition: BodySensorObserver.h:13
mc_observers::ObserverLoader
Load Observer instances from shared libraries.
Definition: ObserverLoader.h:23
mc_rtc::log::error
void error(Args &&... args)
Definition: logging.h:63
loader.h
ltdl wrapper for mc_rtc purpose
mc_observers::ObserverLoader::has_observer
static bool has_observer(const std::string &name)
Definition: ObserverLoader.h:84
api.h