Orocos Real-Time Toolkit  2.8.3
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 
125 #define ORO_CREATE_COMPONENT(CNAME) \
126 extern "C" { \
127  RTT_EXPORT RTT::TaskContext* createComponent(std::string instance_name); \
128  RTT::TaskContext* createComponent(std::string instance_name) \
129  { \
130  return new CNAME(instance_name); \
131  } \
132  RTT_EXPORT std::string getComponentType(); \
133  std::string getComponentType() \
134  { \
135  return ORO_LIST_COMPONENT_TYPE_str(CNAME); \
136  } \
137 } /* extern "C" */
138 
146 #define ORO_CREATE_COMPONENT_LIBRARY() \
147 RTT::FactoryMap* RTT::ComponentFactories::Factories = 0; \
148 extern "C" { \
149  RTT_EXPORT RTT::TaskContext* createComponentType(std::string instance_name, std::string type_name) \
150  { \
151  if( RTT::ComponentFactories::Instance().count(type_name) ) \
152  return RTT::ComponentFactories::Instance()[type_name](instance_name); \
153  return 0; \
154  } \
155  RTT_EXPORT std::vector<std::string> getComponentTypeNames() \
156  { \
157  std::vector<std::string> ret; \
158  RTT::FactoryMap::iterator it; \
159  for(it = RTT::ComponentFactories::Instance().begin(); it != RTT::ComponentFactories::Instance().end(); ++it) { \
160  ret.push_back(it->first); \
161  } \
162  return ret; \
163  } \
164  RTT_EXPORT RTT::FactoryMap* getComponentFactoryMap() { return &RTT::ComponentFactories::Instance(); } \
165 } /* extern "C" */
166 
167 #else
168 
169 #if !defined(OCL_STATIC) && !defined(RTT_STATIC) && !defined(RTT_DLL_EXPORT)
170 #warning "You're compiling with static library settings. The resulting component library \
171  will not be loadable at runtime with the deployer.\
172  Compile with -DRTT_COMPONENT to enable dynamic loadable components, \
173  or use -DRTT_STATIC to suppress this warning."
174 #endif
175 
176 // Static OCL library:
177 // Identical to ORO_LIST_COMPONENT_TYPE:
178 #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)); } }
179 #define ORO_CREATE_COMPONENT_LIBRARY() __attribute__((weak)) RTT::FactoryMap* RTT::ComponentFactories::Factories = 0;
180 
181 #endif
182 
200 #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)); } }
201 
205 #define ORO_CREATE_COMPONENT_TYPE( ) ORO_CREATE_COMPONENT_LIBRARY( )
206 
207 #endif
208 
209 //#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:51
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