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
33namespace 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> make_observer(_Tp* __p) noexcept;
42 } // namespace fundamentals_v2
43} // namespace std::experimental
44
45namespace armarx
46{
47 class ManagedIceObject;
48
50 {
51 public:
52 virtual ~ManagedIceObjectPlugin() = default;
53
54
55 protected:
57
58 virtual void
60 {
61 }
62
63 virtual void
67
68 virtual void
72
73 virtual void
77
78 virtual void
82
83 virtual void
87
88 virtual void
90 {
91 }
92
93 virtual void
97
98
99 friend class ManagedIceObject;
100
101
102 public:
103 template <class T>
104 bool
106 {
107 return dynamic_cast<T*>(_parent);
108 }
109
111 const ManagedIceObject& parent() const;
112
113 template <class T>
114 T&
116 {
117 return dynamic_cast<T&>(ManagedIceObjectPlugin::parent());
118 }
119
120 template <class T>
121 const T&
122 parent() const
123 {
124 return dynamic_cast<const T&>(ManagedIceObjectPlugin::parent());
125 }
126
127
128 protected:
129 template <class PluginT, class... ParamsT>
130 PluginT*
131 addPlugin(const std::string prefix = "", ParamsT&&... params)
132 {
133 static_assert(std::is_base_of_v<ManagedIceObjectPlugin, PluginT>);
134
135 std::unique_ptr<ManagedIceObjectPlugin>& ptr = this->getPlugin(typeid(PluginT), prefix);
136 // If ptr != nullptr, we already got an instance of this plugin.
137 if (!ptr)
138 {
139 ptr.reset(new PluginT(parent(), prefix, params...));
140 }
141 return static_cast<PluginT*>(ptr.get());
142 }
143
144 template <class PluginT, class... ParamsT>
145 void
146 addPlugin(PluginT*& targ, const std::string prefix = "", ParamsT&&... params)
147 {
148 checkOutParameter(targ);
149 targ = this->addPlugin<PluginT>(prefix, params...);
150 }
151
152 template <class PluginT, class... ParamsT>
153 void
155 const std::string prefix = "",
156 ParamsT&&... params)
157 {
158 checkOutParameter(targ.get());
160 }
161
163 void
165
166 const std::string& prefix() const;
167 std::string makePropertyName(const std::string& name);
168
169
170 private:
171 /// Wraps a usage of ARMARX_CHECK_NULL to avoid an include.
172 static void checkOutParameter(void* targ);
173 /// Wraps a call to parent().getPluginPointer() to allow forward declaration of MangedIceObject.
174 std::unique_ptr<ManagedIceObjectPlugin>& getPlugin(const std::type_info& typeInfo,
175 const std::string prefix = "");
176
177
178 private:
179 ManagedIceObject* const _parent;
180 std::string _prefix;
181 std::vector<ManagedIceObjectPlugin*> _dependsOn;
182 };
183
184} // namespace armarx
void addPluginDependency(ManagedIceObjectPlugin *dependedOn)
void addPlugin(PluginT *&targ, const std::string prefix="", ParamsT &&... params)
PluginT * addPlugin(const std::string prefix="", ParamsT &&... params)
std::string makePropertyName(const std::string &name)
void addPlugin(std::experimental::observer_ptr< PluginT > &targ, const std::string prefix="", ParamsT &&... params)
const std::string & prefix() const
ManagedIceObjectPlugin(ManagedIceObject &parent, std::string pre)
virtual ~ManagedIceObjectPlugin()=default
The ManagedIceObject is the base class for all ArmarX objects.
This file offers overloads of toIce() and fromIce() functions for STL container types.
observer_ptr< _Tp > make_observer(_Tp *__p) noexcept