Orocos Real-Time Toolkit  2.9.0
Component.hpp
Go to the documentation of this file.
1 /***************************************************************************
2  tag: Peter Soetens Thu Jul 3 15:35:28 CEST 2008 Component.hpp
3 
4  Component.hpp - description
5  -------------------
6  begin : Thu July 03 2008
7  copyright : (C) 2008 Peter Soetens
8  email : peter.soetens@fmtc.be
9 
10  ***************************************************************************
11  * This library is free software; you can redistribute it and/or *
12  * modify it under the terms of the GNU Lesser General Public *
13  * License as published by the Free Software Foundation; either *
14  * version 2.1 of the License, or (at your option) any later version. *
15  * *
16  * This library is distributed in the hope that it will be useful, *
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
19  * Lesser General Public License for more details. *
20  * *
21  * You should have received a copy of the GNU Lesser General Public *
22  * License along with this library; if not, write to the Free Software *
23  * Foundation, Inc., 59 Temple Place, *
24  * Suite 330, Boston, MA 02111-1307 USA *
25  * *
26  ***************************************************************************/
27 
36 #ifndef RTT_COMPONENT_HPP
37 #define RTT_COMPONENT_HPP
38 
39 #include <string>
40 #include <map>
41 #include <vector>
42 #include "rtt-fwd.hpp"
43 #include "rtt-config.h"
44 
45 namespace RTT
46 {
50  typedef TaskContext* (*ComponentLoaderSignature)(std::string instance_name);
51  typedef std::map<std::string,ComponentLoaderSignature> FactoryMap;
52 
60  {
65  RTT_HIDE static FactoryMap* Factories;
66  public:
67  RTT_HIDE static FactoryMap& Instance() {
68  if ( Factories == 0)
69  Factories = new FactoryMap();
70  return *Factories;
71  }
72  };
73 
78  template<class C>
80  {
81  public:
82  ComponentFactoryLoader(std::string type_name)
83  {
85  }
86 
87  static TaskContext* createComponent(std::string instance_name)
88  {
89  return new C(instance_name);
90  }
91  };
92 }
93 
94 // Helper macros.
95 #define ORO_CONCAT_LINE2(x,y) x##y
96 #define ORO_CONCAT_LINE1(x,y) ORO_CONCAT_LINE2(x,y)
97 #define ORO_CONCAT_LINE(x) ORO_CONCAT_LINE1(x,__LINE__)
98 
99 #define ORO_LIST_COMPONENT_TYPE_str(s) ORO_LIST_COMPONENT_TYPE__str(s)
100 #define ORO_LIST_COMPONENT_TYPE__str(s) #s
101 
102 // ORO_CREATE_COMPONENT and ORO_CREATE_COMPONENT_LIBRARY are only used in shared libraries.
103 #if defined(OCL_DLL_EXPORT) || defined (RTT_COMPONENT)
104 
105 #ifdef _MSC_VER
106 #pragma warning (disable:4190)
107 #endif
108 
109 #ifdef __clang__
110 #pragma clang diagnostic ignored "-Wreturn-type-c-linkage"
111 #endif
112 
129 #define ORO_CREATE_COMPONENT(CNAME) \
130 extern "C" { \
131  RTT_EXPORT RTT::TaskContext* createComponent(std::string instance_name); \
132  RTT::TaskContext* createComponent(std::string instance_name) \
133  { \
134  return new CNAME(instance_name); \
135  } \
136  RTT_EXPORT std::string getComponentType(); \
137  std::string getComponentType() \
138  { \
139  return ORO_LIST_COMPONENT_TYPE_str(CNAME); \
140  } \
141 } /* extern "C" */
142 
150 #define ORO_CREATE_COMPONENT_LIBRARY() \
151 RTT::FactoryMap* RTT::ComponentFactories::Factories = 0; \
152 extern "C" { \
153  RTT_EXPORT RTT::TaskContext* createComponentType(std::string instance_name, std::string type_name) \
154  { \
155  if( RTT::ComponentFactories::Instance().count(type_name) ) \
156  return RTT::ComponentFactories::Instance()[type_name](instance_name); \
157  return 0; \
158  } \
159  RTT_EXPORT std::vector<std::string> getComponentTypeNames() \
160  { \
161  std::vector<std::string> ret; \
162  RTT::FactoryMap::iterator it; \
163  for(it = RTT::ComponentFactories::Instance().begin(); it != RTT::ComponentFactories::Instance().end(); ++it) { \
164  ret.push_back(it->first); \
165  } \
166  return ret; \
167  } \
168  RTT_EXPORT RTT::FactoryMap* getComponentFactoryMap() { return &RTT::ComponentFactories::Instance(); } \
169 } /* extern "C" */
170 
171 #else
172 
173 #if !defined(OCL_STATIC) && !defined(RTT_STATIC) && !defined(RTT_DLL_EXPORT)
174 #warning "You're compiling with static library settings. The resulting component library \
175  will not be loadable at runtime with the deployer.\
176  Compile with -DRTT_COMPONENT to enable dynamic loadable components, \
177  or use -DRTT_STATIC to suppress this warning."
178 #endif
179 
180 // Static OCL library:
181 // Identical to ORO_LIST_COMPONENT_TYPE:
182 #define ORO_CREATE_COMPONENT(CLASS_NAME) namespace { namespace ORO_CONCAT_LINE(LOADER_) { RTT::ComponentFactoryLoader<CLASS_NAME> m_cloader(ORO_LIST_COMPONENT_TYPE_str(CLASS_NAME)); } }
183 #define ORO_CREATE_COMPONENT_LIBRARY() __attribute__((weak)) RTT::FactoryMap* RTT::ComponentFactories::Factories = 0;
184 
185 #endif
186 
204 #define ORO_LIST_COMPONENT_TYPE(CLASS_NAME) namespace { namespace ORO_CONCAT_LINE(LOADER_) { RTT::ComponentFactoryLoader<CLASS_NAME> m_cloader(ORO_LIST_COMPONENT_TYPE_str(CLASS_NAME)); } }
205 
209 #define ORO_CREATE_COMPONENT_TYPE( ) ORO_CREATE_COMPONENT_LIBRARY( )
210 
211 #endif
212 
213 //#undef ORO_CLOADER_CONCAT
static RTT_HIDE FactoryMap & Instance()
Definition: Component.hpp:67
#define RTT_HIDE
Definition: rtt-config.h:99
std::map< std::string, ComponentLoaderSignature > FactoryMap
Definition: Component.hpp:51
ComponentFactoryLoader(std::string type_name)
Definition: Component.hpp:82
The TaskContext is the C++ representation of an Orocos component.
Definition: TaskContext.hpp:93
Contains TaskContext, Activity, OperationCaller, Operation, Property, InputPort, OutputPort, Attribute.
Definition: Activity.cpp:52
static TaskContext * createComponent(std::string instance_name)
Definition: Component.hpp:87
A global variable storing all component factories added with ORO_LIST_COMPONENT_TYPE.
Definition: Component.hpp:59
A helper class storing a single component factory in case of static library deployments.
Definition: Component.hpp:79