ManagedIceObjectPlugin.h
Go to the documentation of this file.
1 /*
2 * This file is part of ArmarX.
3 *
4 * Copyright (C) 2011-2021, High Performance Humanoid Technologies (H2T), Karlsruhe Institute of Technology (KIT), all rights reserved.
5 *
6 * ArmarX is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
9 *
10 * ArmarX is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 *
18 * @package ArmarXCore::core
19 * @author Raphael Grimm (raphael dot grimm at kit dot edu)
20 * @author Rainer Kartmann (rainer dot kartmann at kit dot edu)
21 * @date 2021
22 * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
23 * GNU General Public License
24 */
25 
26 #pragma once
27 
28 #include <memory>
29 #include <string>
30 #include <vector>
31 
32 // Forward declarations
33 namespace std::experimental
34 {
35  inline namespace fundamentals_v2
36  {
37  template <typename T>
38  class observer_ptr;
39 
40  template<typename _Tp>
41  observer_ptr<_Tp>
42  make_observer(_Tp* __p) noexcept;
43  }
44 }
45 
46 namespace armarx
47 {
48  class ManagedIceObject;
49 
51  {
52  public:
53 
54  virtual ~ManagedIceObjectPlugin() = default;
55 
56 
57  protected:
58 
60 
61 
62  virtual void preOnInitComponent() {}
63  virtual void postOnInitComponent() {}
64 
65  virtual void preOnConnectComponent() {}
66  virtual void postOnConnectComponent() {}
67 
68  virtual void preOnDisconnectComponent() {}
69  virtual void postOnDisconnectComponent() {}
70 
71  virtual void preOnExitComponent() {}
72  virtual void postOnExitComponent() {}
73 
74 
75  friend class ManagedIceObject;
76 
77 
78  public:
79 
80  template<class T>
81  bool parentDerives() const
82  {
83  return dynamic_cast<T*>(_parent);
84  }
85 
87  const ManagedIceObject& parent() const;
88 
89  template<class T>
90  T& parent()
91  {
92  return dynamic_cast<T&>(ManagedIceObjectPlugin::parent());
93  }
94  template<class T>
95  const T& parent() const
96  {
97  return dynamic_cast<const T&>(ManagedIceObjectPlugin::parent());
98  }
99 
100 
101  protected:
102 
103  template <class PluginT, class...ParamsT>
104  PluginT* addPlugin(const std::string prefix = "", ParamsT && ...params)
105  {
106  static_assert(std::is_base_of_v<ManagedIceObjectPlugin, PluginT>);
107 
108  std::unique_ptr<ManagedIceObjectPlugin>& ptr = this->getPlugin(typeid(PluginT), prefix);
109  // If ptr != nullptr, we already got an instance of this plugin.
110  if (!ptr)
111  {
112  ptr.reset(new PluginT(parent(), prefix, params...));
113  }
114  return static_cast<PluginT*>(ptr.get());
115  }
116 
117  template <class PluginT, class...ParamsT>
118  void addPlugin(PluginT*& targ, const std::string prefix = "", ParamsT && ...params)
119  {
120  checkOutParameter(targ);
121  targ = this->addPlugin<PluginT>(prefix, params...);
122  }
123 
124  template <class PluginT, class...ParamsT>
125  void addPlugin(std::experimental::observer_ptr<PluginT>& targ, const std::string prefix = "", ParamsT && ...params)
126  {
127  checkOutParameter(targ.get());
128  targ = std::experimental::make_observer(this->addPlugin<PluginT>(prefix, params...));
129  }
130 
133 
134  const std::string& prefix() const;
135  std::string makePropertyName(const std::string& name);
136 
137 
138  private:
139 
140  /// Wraps a usage of ARMARX_CHECK_NULL to avoid an include.
141  static void checkOutParameter(void* targ);
142  /// Wraps a call to parent().getPluginPointer() to allow forward declaration of MangedIceObject.
143  std::unique_ptr<ManagedIceObjectPlugin>& getPlugin(const std::type_info& typeInfo, const std::string prefix = "");
144 
145 
146  private:
147 
148  ManagedIceObject* const _parent;
149  std::string _prefix;
150  std::vector<ManagedIceObjectPlugin*> _dependsOn;
151 
152  };
153 
154 }
155 
armarx::ManagedIceObjectPlugin::postOnExitComponent
virtual void postOnExitComponent()
Definition: ManagedIceObjectPlugin.h:72
std::experimental
Definition: ManagedIceObject.h:48
armarx::ManagedIceObjectPlugin::postOnConnectComponent
virtual void postOnConnectComponent()
Definition: ManagedIceObjectPlugin.h:66
armarx::ManagedIceObjectPlugin::addPluginDependency
void addPluginDependency(ManagedIceObjectPlugin *dependedOn)
Definition: ManagedIceObjectPlugin.cpp:42
armarx::ManagedIceObjectPlugin::~ManagedIceObjectPlugin
virtual ~ManagedIceObjectPlugin()=default
armarx::ManagedIceObjectPlugin::prefix
const std::string & prefix() const
Definition: ManagedIceObjectPlugin.cpp:63
armarx::ManagedIceObjectPlugin::parent
ManagedIceObject & parent()
Definition: ManagedIceObjectPlugin.cpp:69
armarx::ManagedIceObjectPlugin::parent
const T & parent() const
Definition: ManagedIceObjectPlugin.h:95
armarx::ManagedIceObjectPlugin::addPlugin
void addPlugin(std::experimental::observer_ptr< PluginT > &targ, const std::string prefix="", ParamsT &&...params)
Definition: ManagedIceObjectPlugin.h:125
armarx::ManagedIceObjectPlugin::parent
T & parent()
Definition: ManagedIceObjectPlugin.h:90
std::experimental::fundamentals_v2::make_observer
observer_ptr< _Tp > make_observer(_Tp *__p) noexcept
std::experimental::fundamentals_v2::observer_ptr
Definition: ManagedIceObject.h:53
armarx::ManagedIceObjectPlugin::addPlugin
PluginT * addPlugin(const std::string prefix="", ParamsT &&...params)
Definition: ManagedIceObjectPlugin.h:104
armarx::ManagedIceObjectPlugin::postOnInitComponent
virtual void postOnInitComponent()
Definition: ManagedIceObjectPlugin.h:63
armarx::ManagedIceObjectPlugin::postOnDisconnectComponent
virtual void postOnDisconnectComponent()
Definition: ManagedIceObjectPlugin.h:69
armarx::ManagedIceObjectPlugin::preOnExitComponent
virtual void preOnExitComponent()
Definition: ManagedIceObjectPlugin.h:71
armarx::ManagedIceObjectPlugin::preOnConnectComponent
virtual void preOnConnectComponent()
Definition: ManagedIceObjectPlugin.h:65
armarx::ManagedIceObject
The ManagedIceObject is the base class for all ArmarX objects.
Definition: ManagedIceObject.h:163
armarx::ManagedIceObjectPlugin::preOnInitComponent
virtual void preOnInitComponent()
Definition: ManagedIceObjectPlugin.h:62
armarx::ManagedIceObjectPlugin::ManagedIceObjectPlugin
ManagedIceObjectPlugin(ManagedIceObject &parent, std::string pre)
Definition: ManagedIceObjectPlugin.cpp:36
armarx::ManagedIceObjectPlugin::makePropertyName
std::string makePropertyName(const std::string &name)
Definition: ManagedIceObjectPlugin.cpp:53
armarx::ManagedIceObjectPlugin::preOnDisconnectComponent
virtual void preOnDisconnectComponent()
Definition: ManagedIceObjectPlugin.h:68
armarx::ManagedIceObjectPlugin
Definition: ManagedIceObjectPlugin.h:50
T
float T
Definition: UnscentedKalmanFilterTest.cpp:35
armarx::ManagedIceObjectPlugin::parentDerives
bool parentDerives() const
Definition: ManagedIceObjectPlugin.h:81
armarx::ManagedIceObjectPlugin::addPlugin
void addPlugin(PluginT *&targ, const std::string prefix="", ParamsT &&...params)
Definition: ManagedIceObjectPlugin.h:118
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:28