Orocos Real-Time Toolkit  2.8.3
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 "../ConnPolicy.hpp"
45 #include "../internal/DataSources.hpp"
46 #include "../typekit/Types.hpp"
47 #include "../rtt-fwd.hpp"
48 #include "../internal/mystd.hpp"
49 #include "../types/TemplateConstructor.hpp"
50 #ifdef OS_RT_MALLOC
51 #include "../rt_string.hpp"
52 #endif
53 
54 namespace RTT
55 {
56  using namespace std;
57  using namespace detail;
58 
59  namespace {
60 #ifndef ORO_EMBEDDED
61  // CONSTRUCTORS
62  struct array_ctor
63  : public std::unary_function<int, const std::vector<double>&>
64  {
65  typedef const std::vector<double>& (Signature)( int );
66  mutable boost::shared_ptr< std::vector<double> > ptr;
67  array_ctor()
68  : ptr( new std::vector<double>() ) {}
69  const std::vector<double>& operator()( int size ) const
70  {
71  ptr->resize( size );
72  return *(ptr);
73  }
74  };
75 
80  struct array_varargs_ctor
81  {
82  typedef const std::vector<double>& result_type;
83  typedef double argument_type;
84  result_type operator()( const std::vector<double>& args ) const
85  {
86  return args;
87  }
88  };
89 
94  typedef NArityDataSource<array_varargs_ctor> ArrayDataSource;
95 
100  struct ArrayBuilder
101  : public TypeConstructor
102  {
103  virtual DataSourceBase::shared_ptr build(const std::vector<DataSourceBase::shared_ptr>& args) const {
104  if (args.size() == 0 )
106  ArrayDataSource::shared_ptr vds = new ArrayDataSource();
107  for(unsigned int i=0; i != args.size(); ++i) {
108  DataSource<double>::shared_ptr dsd = boost::dynamic_pointer_cast< DataSource<double> >( args[i] );
109  if (dsd)
110  vds->add( dsd );
111  else
113  }
114  return vds;
115  }
116 
117  };
118 
119  struct array_ctor2
120  : public std::binary_function<int, double, const std::vector<double>&>
121  {
122  typedef const std::vector<double>& (Signature)( int, double );
123  mutable boost::shared_ptr< std::vector<double> > ptr;
124  array_ctor2()
125  : ptr( new std::vector<double>() ) {}
126  const std::vector<double>& operator()( int size, double value ) const
127  {
128  ptr->resize( size );
129  ptr->assign( size, value );
130  return *(ptr);
131  }
132  };
133 
134  double float_to_double( float val ) {return double(val);}
135  float double_to_float( double val ) {return float(val);}
136 
137 
138  int float_to_int(float f) { return int(f); }
139  float int_to_float(int i) { return float(i); }
140  int double_to_int(double f) { return int(f); }
141  double int_to_double(int i) { return double(i); }
142  unsigned int int_to_uint(int i) { return (unsigned int)(i); }
143  int uint_to_int(unsigned int ui) { return int(ui); }
144 #endif
145  bool flow_to_bool(FlowStatus fs) { return fs != NoData ; }
146  bool send_to_bool(SendStatus ss) { return ss == SendSuccess; }
147  bool int_to_bool(int i) { return i != 0; }
148  int bool_to_int(bool b) { return int(b); }
149 
150  struct string_ctor
151  : public std::unary_function<int, const std::string&>
152  {
153  mutable boost::shared_ptr< std::string > ptr;
154  typedef const std::string& (Signature)( int );
155  string_ctor()
156  : ptr( new std::string() ) {}
157  const std::string& operator()( int size ) const
158  {
159  ptr->resize( size );
160  return *(ptr);
161  }
162  };
163 
164 #ifdef OS_RT_MALLOC
165  struct rt_string_ctor_int
166  : public std::unary_function<int, const RTT::rt_string&>
167  {
168  mutable boost::shared_ptr< rt_string > ptr;
169  typedef const rt_string& (Signature)( int );
170  rt_string_ctor_int()
171  : ptr( new rt_string() ) {}
172  const rt_string& operator()( int size ) const
173  {
174  ptr->resize( size );
175  return *(ptr);
176  }
177  };
178 
179  struct rt_string_ctor_string
180  : public std::unary_function<const std::string&, const RTT::rt_string&>
181  {
182  mutable boost::shared_ptr< rt_string > ptr;
183  typedef const rt_string& (Signature)( std::string const& );
184  rt_string_ctor_string()
185  : ptr( new rt_string() ) {}
186  const rt_string& operator()( std::string const& arg ) const
187  {
188  *ptr = arg.c_str();
189  return *(ptr);
190  }
191  };
192 
193  struct string_ctor_rt_string
194  : public std::unary_function<const rt_string&, const string&>
195  {
196  mutable boost::shared_ptr< string > ptr;
197  typedef const string& (Signature)( rt_string const& );
198  string_ctor_rt_string()
199  : ptr( new string() ) {}
200  const string& operator()( rt_string const& arg ) const
201  {
202  *ptr = arg.c_str();
203  return *(ptr);
204  }
205  };
206 
207 #endif
208  }
209 
211  {
213 #ifndef ORO_EMBEDDED
214  ti->type("double")->addConstructor( newConstructor( &float_to_double, true ));
215  ti->type("double")->addConstructor( newConstructor( &int_to_double, true ));
216  ti->type("float")->addConstructor( newConstructor( &int_to_float, true ));
217  ti->type("float")->addConstructor( newConstructor( &double_to_float, true ));
218  ti->type("int")->addConstructor( newConstructor( &float_to_int, false ));
219  ti->type("int")->addConstructor( newConstructor( &double_to_int, false ));
220  ti->type("int")->addConstructor( newConstructor( &uint_to_int, true ));
221  ti->type("int")->addConstructor( newConstructor( &bool_to_int, true ));
222  ti->type("uint")->addConstructor( newConstructor( &int_to_uint, true ));
223  ti->type("string")->addConstructor( newConstructor( string_ctor() ) );
224 #ifdef OS_RT_MALLOC
225  ti->type("rt_string")->addConstructor( newConstructor( rt_string_ctor_int() ) );
226  ti->type("rt_string")->addConstructor( newConstructor( rt_string_ctor_string() ) );
227  ti->type("string")->addConstructor( newConstructor( string_ctor_rt_string() ) );
228 #endif
229  ti->type("bool")->addConstructor( newConstructor( &flow_to_bool, true ) );
230  ti->type("bool")->addConstructor( newConstructor( &send_to_bool, true ) );
231  ti->type("bool")->addConstructor( newConstructor( &int_to_bool, true ) );
232 #endif
233  return true;
234  }
235 }
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.
Definition: FlowStatus.hpp:54
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:51
boost::shared_ptr< TypeInfoRepository > shared_ptr