Orocos Real-Time Toolkit  2.9.0
RealTimeTypekitConstructors.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  tag: Peter Soetens Mon Jun 26 13:25:56 CEST 2006 RealTimeTypekit.cxx
3 
4  RealTimeTypekit.cxx - description
5  -------------------
6  begin : Mon June 26 2006
7  copyright : (C) 2006 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  * Lesser 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 
39 
40 #include "rtt-typekit-config.h"
41 #include "RealTimeTypekit.hpp"
42 #include "../types/Types.hpp"
43 #include "../FlowStatus.hpp"
44 #include "../internal/DataSources.hpp"
45 #include "../typekit/Types.hpp"
46 #include "../rtt-fwd.hpp"
47 #include "../internal/mystd.hpp"
48 #include "../types/TemplateConstructor.hpp"
49 #ifdef OS_RT_MALLOC
50 #include "../rt_string.hpp"
51 #endif
52 
53 namespace RTT
54 {
55  using namespace std;
56  using namespace detail;
57 
58  namespace {
59 #ifndef ORO_EMBEDDED
60  // CONSTRUCTORS
61  struct array_ctor
62  : public std::unary_function<int, const std::vector<double>&>
63  {
64  typedef const std::vector<double>& (Signature)( int );
65  mutable boost::shared_ptr< std::vector<double> > ptr;
66  array_ctor()
67  : ptr( new std::vector<double>() ) {}
68  const std::vector<double>& operator()( int size ) const
69  {
70  ptr->resize( size );
71  return *(ptr);
72  }
73  };
74 
79  struct array_varargs_ctor
80  {
81  typedef const std::vector<double>& result_type;
82  typedef double argument_type;
83  result_type operator()( const std::vector<double>& args ) const
84  {
85  return args;
86  }
87  };
88 
93  typedef NArityDataSource<array_varargs_ctor> ArrayDataSource;
94 
99  struct ArrayBuilder
100  : public TypeConstructor
101  {
102  virtual DataSourceBase::shared_ptr build(const std::vector<DataSourceBase::shared_ptr>& args) const {
103  if (args.size() == 0 )
105  ArrayDataSource::shared_ptr vds = new ArrayDataSource();
106  for(unsigned int i=0; i != args.size(); ++i) {
107  DataSource<double>::shared_ptr dsd = boost::dynamic_pointer_cast< DataSource<double> >( args[i] );
108  if (dsd)
109  vds->add( dsd );
110  else
112  }
113  return vds;
114  }
115 
116  };
117 
118  struct array_ctor2
119  : public std::binary_function<int, double, const std::vector<double>&>
120  {
121  typedef const std::vector<double>& (Signature)( int, double );
122  mutable boost::shared_ptr< std::vector<double> > ptr;
123  array_ctor2()
124  : ptr( new std::vector<double>() ) {}
125  const std::vector<double>& operator()( int size, double value ) const
126  {
127  ptr->resize( size );
128  ptr->assign( size, value );
129  return *(ptr);
130  }
131  };
132 
133  double float_to_double( float val ) {return double(val);}
134  float double_to_float( double val ) {return float(val);}
135 
136  int float_to_int(float f) { return int(f); }
137  float int_to_float(int i) { return float(i); }
138  int double_to_int(double f) { return int(f); }
139  double int_to_double(int i) { return double(i); }
140  unsigned int int_to_uint(int i) { return (unsigned int)(i); }
141  int uint_to_int(unsigned int ui) { return int(ui); }
142 
143  // llong
144  long long float_to_llong(float f) { return (long long)(f); }
145  float llong_to_float(long long i) { return (float)(i); }
146  long long double_to_llong(double f) { return (long long)(f); }
147  double llong_to_double(long long i) { return (double)(i); }
148  long long int_to_llong(int i) { return (long long)(i); }
149  int llong_to_int(long long i) { return (int)(i); }
150  long long uint_to_llong(unsigned int i) { return (long long)(i); }
151  unsigned int llong_to_uint(long long i) { return (unsigned int)(i); }
152  bool llong_to_bool(long long i) { return i != 0; }
153 
154  // ullong
155  unsigned long long float_to_ullong(float f) { return (unsigned long long)(f); }
156  float ullong_to_float(unsigned long long i) { return (float)(i); }
157  unsigned long long double_to_ullong(double f) { return (unsigned long long)(f); }
158  double ullong_to_double(unsigned long long i) { return (double)(i); }
159  unsigned long long int_to_ullong(int i) { return (unsigned long long)(i); }
160  int ullong_to_int(unsigned long long i) { return (int)(i); }
161  unsigned long long uint_to_ullong(unsigned int i) { return (unsigned long long)(i); }
162  unsigned int ullong_to_uint(unsigned long long i) { return (unsigned int)(i); }
163  unsigned long long llong_to_ullong(long long i) { return (unsigned long long)(i); }
164  long long ullong_to_llong(unsigned long long i) { return (long long)(i); }
165 
166 #endif
167  bool flowstatus_to_bool(FlowStatus fs) { return fs != NoData ; }
168  bool writestatus_to_bool(WriteStatus fs) { return fs == WriteSuccess ; }
169  bool send_to_bool(SendStatus ss) { return ss == SendSuccess; }
170  bool int_to_bool(int i) { return i != 0; }
171  int bool_to_int(bool b) { return int(b); }
172 
173  struct string_ctor
174  : public std::unary_function<int, std::string>
175  {
176  typedef std::string (Signature)( int );
177  std::string operator()( int size ) const
178  {
179  return std::string( size, std::string::value_type() );
180  }
181  };
182 
183 #ifdef OS_RT_MALLOC
184  struct rt_string_ctor_int
185  : public std::unary_function<int, RTT::rt_string>
186  {
187  typedef rt_string (Signature)( int );
188  rt_string operator()( int size ) const
189  {
190  return rt_string( size, rt_string::value_type() );
191  }
192  };
193 
194  struct rt_string_ctor_string
195  : public std::unary_function<const std::string&, RTT::rt_string>
196  {
197  typedef rt_string (Signature)( std::string const& );
198  rt_string operator()( std::string const& arg ) const
199  {
200  return rt_string( arg.c_str() );
201  }
202  };
203 
204  struct string_ctor_rt_string
205  : public std::unary_function<const rt_string&, std::string>
206  {
207  typedef std::string (Signature)( rt_string const& );
208  std::string operator()( rt_string const& arg ) const
209  {
210  return std::string( arg.c_str() );
211  }
212  };
213 
214 #endif
215  }
216 
218  {
220 #ifndef ORO_EMBEDDED
221  ti->type("double")->addConstructor( newConstructor( &float_to_double, true ));
222  ti->type("double")->addConstructor( newConstructor( &int_to_double, true ));
223  ti->type("float")->addConstructor( newConstructor( &int_to_float, true ));
224  ti->type("float")->addConstructor( newConstructor( &double_to_float, true ));
225  ti->type("int")->addConstructor( newConstructor( &float_to_int, false ));
226  ti->type("int")->addConstructor( newConstructor( &double_to_int, false ));
227  ti->type("int")->addConstructor( newConstructor( &uint_to_int, true ));
228  ti->type("int")->addConstructor( newConstructor( &bool_to_int, true ));
229  ti->type("uint")->addConstructor( newConstructor( &int_to_uint, true ));
230 
231  // llong
232  ti->type("llong")->addConstructor( newConstructor( &float_to_llong, true ));
233  ti->type("float")->addConstructor( newConstructor( &llong_to_float, true ));
234  ti->type("llong")->addConstructor( newConstructor( &double_to_llong, true ));
235  ti->type("double")->addConstructor( newConstructor( &llong_to_double, true ));
236  ti->type("llong")->addConstructor( newConstructor( &int_to_llong, true ));
237  ti->type("int")->addConstructor( newConstructor( &llong_to_int, true ));
238  ti->type("llong")->addConstructor( newConstructor( &uint_to_llong, true ));
239  ti->type("uint")->addConstructor( newConstructor( &llong_to_uint, true ));
240  ti->type("bool")->addConstructor( newConstructor( &llong_to_bool, true ));
241 
242  // ullong
243  ti->type("ullong")->addConstructor( newConstructor( &float_to_ullong, true ));
244  ti->type("float")->addConstructor( newConstructor( &ullong_to_float, true ));
245  ti->type("ullong")->addConstructor( newConstructor( &double_to_ullong, true ));
246  ti->type("double")->addConstructor( newConstructor( &ullong_to_double, true ));
247  ti->type("ullong")->addConstructor( newConstructor( &int_to_ullong, true ));
248  ti->type("int")->addConstructor( newConstructor( &ullong_to_int, true ));
249  ti->type("ullong")->addConstructor( newConstructor( &uint_to_ullong, true ));
250  ti->type("uint")->addConstructor( newConstructor( &ullong_to_uint, true ));
251  ti->type("ullong")->addConstructor( newConstructor( &llong_to_ullong, true ));
252  ti->type("llong")->addConstructor( newConstructor( &ullong_to_llong, true ));
253 
254  ti->type("string")->addConstructor( newConstructor( string_ctor() ) );
255 #ifdef OS_RT_MALLOC
256  ti->type("rt_string")->addConstructor( newConstructor( rt_string_ctor_int() ) );
257  ti->type("rt_string")->addConstructor( newConstructor( rt_string_ctor_string() ) );
258  ti->type("string")->addConstructor( newConstructor( string_ctor_rt_string() ) );
259 #endif
260  ti->type("bool")->addConstructor( newConstructor( &flowstatus_to_bool, true ) );
261  ti->type("bool")->addConstructor( newConstructor( &writestatus_to_bool, true ) );
262  ti->type("bool")->addConstructor( newConstructor( &send_to_bool, true ) );
263  ti->type("bool")->addConstructor( newConstructor( &int_to_bool, true ) );
264 #endif
265  return true;
266  }
267 }
std::basic_string< char, std::char_traits< char >, RTT::os::rt_allocator< char > > rt_string
Real-time allocatable, dynamically-sized string.
Definition: rt_string.hpp:50
boost::intrusive_ptr< ArrayDataSource< T > > shared_ptr
FlowStatus
Returns the status of a data flow read operation.
Definition: FlowStatus.hpp:56
STL namespace.
SendStatus
Returns the status of a send() or collect() invocation.
Definition: SendStatus.hpp:53
TypeConstructor * newConstructor(Function *foo, bool automatic=false)
Create a new Constructor.
boost::intrusive_ptr< DataSource< double > > shared_ptr
Definition: DataSource.hpp:115
Returned when the send() succeeded, but the operation has not yet been executed by the receiving comp...
Definition: SendStatus.hpp:57
virtual bool loadConstructors()
Implement this method to load Scripting constructors of types, such as in C++.
boost::intrusive_ptr< DataSourceBase > shared_ptr
Use this type to store a pointer to a DataSourceBase.
Contains TaskContext, Activity, OperationCaller, Operation, Property, InputPort, OutputPort, Attribute.
Definition: Activity.cpp:52
boost::shared_ptr< TypeInfoRepository > shared_ptr
WriteStatus
Returns the status of a data flow write operation.
Definition: FlowStatus.hpp:66