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 General Public *
13  * License as published by the Free Software Foundation; *
14  * version 2 of the License. *
15  * *
16  * As a special exception, you may use this file as part of a free *
17  * software library without restriction. Specifically, if other files *
18  * instantiate templates or use macros or inline functions from this *
19  * file, or you compile this file and link it with other files to *
20  * produce an executable, this file does not by itself cause the *
21  * resulting executable to be covered by the GNU General Public *
22  * License. This exception does not however invalidate any other *
23  * reasons why the executable file might be covered by the GNU General *
24  * Public License. *
25  * *
26  * This library is distributed in the hope that it will be useful, *
27  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
28  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
29  * General Public License for more details. *
30  * *
31  * You should have received a copy of the GNU General Public *
32  * License along with this library; if not, write to the Free Software *
33  * Foundation, Inc., 59 Temple Place, *
34  * Suite 330, Boston, MA 02111-1307 USA *
35  * *
36  ***************************************************************************/
37 
38 
47 #ifndef RTT_COMPONENT_HPP
48 #define RTT_COMPONENT_HPP
49 
50 #include <string>
51 #include <map>
52 #include <vector>
53 #include "rtt-fwd.hpp"
54 #include "rtt-config.h"
55 
56 namespace RTT
57 {
61  typedef TaskContext* (*ComponentLoaderSignature)(std::string instance_name);
62  typedef std::map<std::string,ComponentLoaderSignature> FactoryMap;
63 
71  {
76  RTT_HIDE static FactoryMap* Factories;
77  public:
78  RTT_HIDE static FactoryMap& Instance() {
79  if ( Factories == 0)
80  Factories = new FactoryMap();
81  return *Factories;
82  }
83  };
84 
89  template<class C>
91  {
92  public:
93  ComponentFactoryLoader(std::string type_name)
94  {
96  }
97 
98  static TaskContext* createComponent(std::string instance_name)
99  {
100  return new C(instance_name);
101  }
102  };
103 }
104 
105 // Helper macros.
106 #define ORO_CONCAT_LINE2(x,y) x##y
107 #define ORO_CONCAT_LINE1(x,y) ORO_CONCAT_LINE2(x,y)
108 #define ORO_CONCAT_LINE(x) ORO_CONCAT_LINE1(x,__LINE__)
109 
110 #define ORO_LIST_COMPONENT_TYPE_str(s) ORO_LIST_COMPONENT_TYPE__str(s)
111 #define ORO_LIST_COMPONENT_TYPE__str(s) #s
112 
113 // ORO_CREATE_COMPONENT and ORO_CREATE_COMPONENT_LIBRARY are only used in shared libraries.
114 #if defined(OCL_DLL_EXPORT) || defined (RTT_COMPONENT)
115 
116 #ifdef _MSC_VER
117 #pragma warning (disable:4190)
118 #endif
119 
120 #ifdef __clang__
121 #pragma clang diagnostic ignored "-Wreturn-type-c-linkage"
122 #endif
123 
140 #define ORO_CREATE_COMPONENT(CNAME) \
141 extern "C" { \
142  RTT_EXPORT RTT::TaskContext* createComponent(std::string instance_name); \
143  RTT::TaskContext* createComponent(std::string instance_name) \
144  { \
145  return new CNAME(instance_name); \
146  } \
147  RTT_EXPORT std::string getComponentType(); \
148  std::string getComponentType() \
149  { \
150  return ORO_LIST_COMPONENT_TYPE_str(CNAME); \
151  } \
152 } /* extern "C" */
153 
161 #define ORO_CREATE_COMPONENT_LIBRARY() \
162 RTT::FactoryMap* RTT::ComponentFactories::Factories = 0; \
163 extern "C" { \
164  RTT_EXPORT RTT::TaskContext* createComponentType(std::string instance_name, std::string type_name) \
165  { \
166  if( RTT::ComponentFactories::Instance().count(type_name) ) \
167  return RTT::ComponentFactories::Instance()[type_name](instance_name); \
168  return 0; \
169  } \
170  RTT_EXPORT std::vector<std::string> getComponentTypeNames() \
171  { \
172  std::vector<std::string> ret; \
173  RTT::FactoryMap::iterator it; \
174  for(it = RTT::ComponentFactories::Instance().begin(); it != RTT::ComponentFactories::Instance().end(); ++it) { \
175  ret.push_back(it->first); \
176  } \
177  return ret; \
178  } \
179  RTT_EXPORT RTT::FactoryMap* getComponentFactoryMap() { return &RTT::ComponentFactories::Instance(); } \
180 } /* extern "C" */
181 
182 #else
183 
184 #if !defined(OCL_STATIC) && !defined(RTT_STATIC) && !defined(RTT_DLL_EXPORT)
185 #warning "You're compiling with static library settings. The resulting component library \
186  will not be loadable at runtime with the deployer.\
187  Compile with -DRTT_COMPONENT to enable dynamic loadable components, \
188  or use -DRTT_STATIC to suppress this warning."
189 #endif
190 
191 // Static OCL library:
192 // Identical to ORO_LIST_COMPONENT_TYPE:
193 #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)); } }
194 #define ORO_CREATE_COMPONENT_LIBRARY() __attribute__((weak)) RTT::FactoryMap* RTT::ComponentFactories::Factories = 0;
195 
196 #endif
197 
215 #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)); } }
216 
220 #define ORO_CREATE_COMPONENT_TYPE( ) ORO_CREATE_COMPONENT_LIBRARY( )
221 
222 #endif
223 
224 //#undef ORO_CLOADER_CONCAT
static RTT_HIDE FactoryMap & Instance()
Definition: Component.hpp:78
std::map< std::string, ComponentLoaderSignature > FactoryMap
Definition: Component.hpp:62
static RTT_HIDE FactoryMap * Factories
Definition: Component.hpp:76
ComponentFactoryLoader(std::string type_name)
Definition: Component.hpp:93
Contains TaskContext, Activity, OperationCaller, Operation, Property, InputPort, OutputPort, Attribute.
Definition: Activity.cpp:53
static TaskContext * createComponent(std::string instance_name)
Definition: Component.hpp:98


rtt
Author(s): RTT Developers
autogenerated on Fri Oct 25 2019 03:59:32