Orocos Real-Time Toolkit  2.9.0
TemplateConstructor.hpp
Go to the documentation of this file.
1 /***************************************************************************
2  tag: The SourceWorks Tue Sep 7 00:55:18 CEST 2010 TemplateConstructor.hpp
3 
4  TemplateConstructor.hpp - description
5  -------------------
6  begin : Tue September 07 2010
7  copyright : (C) 2010 The SourceWorks
8  email : peter@thesourceworks.com
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 #ifndef ORO_TEMPLATE_CONSTRUCTOR_HPP
40 #define ORO_TEMPLATE_CONSTRUCTOR_HPP
41 
42 #include "Types.hpp"
43 #include "../internal/FusedFunctorDataSource.hpp"
44 #include "../internal/CreateSequence.hpp"
45 #include "../Logger.hpp"
46 
47 #include <boost/type_traits/function_traits.hpp>
48 #include <boost/function.hpp>
49 
50 #include "../rtt-config.h"
51 
52 namespace RTT
53 {
54  namespace types {
62  template<class S>
63  struct TemplateConstructor
64  : public TypeConstructor
65  {
66  typedef typename boost::function_traits<S>::result_type result_type;
67  typedef typename boost::function_traits<S>::arg1_type arg1_type;
69 
70  boost::function<S> ff;
71  bool automatic;
72 
73  template<class FInit>
74  TemplateConstructor( FInit f, bool autom)
75  : ff(f), automatic(autom)
76  {}
77 
78  virtual base::DataSourceBase::shared_ptr build(const std::vector<base::DataSourceBase::shared_ptr>& args) const {
79  // number of arguments must be exact.
80  if ( args.size() != boost::function_traits<S>::arity )
82  try {
84  } catch(...) // wrong argument types
85  {}
87  }
88 
90  if ( boost::function_traits<S>::arity != 1) {
92  } else {
93  // The compiler should optimise this out...
94  // these checks are necessary because produce(args) calls convert, which could lead to endless loops.
95  // detect same type converion.
96  if ( arg->getTypeInfo() == internal::DataSourceTypeInfo<result_type>::getTypeInfo() ) {
97  return arg;
98  }
99  // detect invalid type conversion.
100  if ( arg->getTypeInfo() != internal::DataSourceTypeInfo<arg1_type>::getTypeInfo() ) {
102  }
103  // from now on, it should always succeed.
104  std::vector<base::DataSourceBase::shared_ptr> args;
105  args.push_back(arg);
106  base::DataSourceBase::shared_ptr ret = this->build(args);
107  assert( ret );
108  if (!automatic)
109  log(Warning) << "Conversion from " << arg->getTypeName() << " to " << ret->getTypeName() <<endlog();
110  return ret;
111  }
112  }
113  };
121  template<class Function>
122  TypeConstructor* newConstructor( Function* foo, bool automatic = false ) {
124  }
125 
134  template<class Object>
135  TypeConstructor* newConstructor( Object obj, bool automatic = false) {
137  }
138 }}
139 
140 #endif
The constructor classes allow to define type constructors or type conversions (convert type B from ty...
TemplateConstructor(FInit f, bool autom)
This class can create three kinds of Boost Fusion Sequences.
TypeConstructor * newConstructor(Function *foo, bool automatic=false)
Create a new Constructor.
static const types::TypeInfo * getTypeInfo()
Return the typeinfo object.
static type sources(std::vector< base::DataSourceBase::shared_ptr >::const_iterator args, int argnbr=1)
Converts a std::vector of DataSourceBase types into a boost::fusion Sequence of DataSources of the ty...
internal::create_sequence< typename boost::function_types::parameter_types< S >::type > SequenceFactory
boost::function_traits< S >::result_type result_type
virtual base::DataSourceBase::shared_ptr build(const std::vector< base::DataSourceBase::shared_ptr > &args) const
Inspect args and return a type constructed with these args if such a constructor exists.
boost::function_traits< S >::arg1_type arg1_type
virtual base::DataSourceBase::shared_ptr convert(base::DataSourceBase::shared_ptr arg) const
Automatic type conversion (float->double,...
This interface describes how constructors work.
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
A DataSource that calls a functor of signature Signature which gets its arguments from other data sou...