Orocos Real-Time Toolkit  2.9.0
EnumTypeInfo.hpp
Go to the documentation of this file.
1 /***************************************************************************
2  tag: The SourceWorks Tue Sep 7 00:55:18 CEST 2010 EnumTypeInfo.hpp
3 
4  EnumTypeInfo.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 #include "../rtt-config.h"
40 #include "Types.hpp"
41 #include "TemplateTypeInfo.hpp"
42 #include "TemplateConstructor.hpp"
43 
44 namespace RTT
45 {
46  namespace types
47  {
48 
55  template<class T>
56  struct EnumTypeInfo: public TemplateTypeInfo<T, false>
57  {
58  protected:
59  static int enum_to_int(T e)
60  {
61  return (int) e;
62  }
63 
64  static T int_to_enum(int i)
65  {
66  return (T) i;
67  }
68 
69  std::map<T,std::string> to_string;
70  typedef std::map<T,std::string> MapType;
71  public:
72  EnumTypeInfo(std::string type) :
73  TemplateTypeInfo<T, false> (type)
74  {
75  }
76 
78  if (!Types()->type("int")) {
79  log(Error) << "Failed to register enum <-> int conversion because type int is not known in type system."<<endlog();
80  return false;
81  } else {
83  Types()->type("int")->addConstructor(newConstructor(
85  }
87 
88  // Don't delete us, we're memory-managed.
89  return false;
90  }
91 
100  {
101  // First, try a plain update.
102  if (result->update(source.get()))
103  return true;
104  // try conversion from int to enum:
106  internal::DataSource<int>::narrow( source.get() );
107  if (ds)
108  {
111  assert(menum);
112  menum->set( (T)ds->get() );
113  return true;
114  }
115  // try conversion from string to enum:
118  if (dss)
119  {
122  assert(menum);
123  for( typename MapType::const_iterator it = to_string.begin(); it != to_string.end(); ++it)
124  if ( it->second == dss->get() ) {
125  menum->set( it->first );
126  return true;
127  }
128  }
129  // no conversion possible.
130  return false;
131  }
132 
141  {
142  // Convert enum to string
144  if ( ds )
145  {
146  // if not available, just convert to int.
147  if ( to_string.count( ds->get() ) == 0 ) {
148  //log(Warning) << "No enum-to-string mapping defined for enum " << this->getTypeName() <<". Converting to int."<<endlog();
149  return new internal::ValueDataSource<int>( ds->get() );
150  }
152  return vds;
153  }
154  // convert string to enum
156  }
157  };
158  }
159 }
virtual result_t get() const =0
Return the data as type T.
void addConstructor(TypeConstructor *tb)
Add a constructor/convertor object.
Definition: TypeInfo.cpp:114
This template class allows user types to be used in all Orocos primitives.
Type information for Enum types for which they are convertible to int.
virtual bool composeType(base::DataSourceBase::shared_ptr source, base::DataSourceBase::shared_ptr result) const
Composition also checks if source is an int or string, and if so, converts it to the enum of type T k...
virtual void set(param_t t)=0
Set this DataSource with a value.
virtual base::DataSourceBase::shared_ptr decomposeType(base::DataSourceBase::shared_ptr source) const
Converts the enum to a string in case source is an enum and the string mapping is known...
EnumTypeInfo(std::string type)
TypeConstructor * newConstructor(Function *foo, bool automatic=false)
Create a new Constructor.
std::map< T, std::string > to_string
std::map< T, std::string > MapType
A class for representing a user type, and which can build instances of that type. ...
Definition: TypeInfo.hpp:67
TypeInfoRepository::shared_ptr Types()
Obtain a pointer to the global type system.
Definition: Types.cpp:48
boost::intrusive_ptr< DataSource< T > > shared_ptr
Definition: DataSource.hpp:115
boost::intrusive_ptr< AssignableDataSource< T > > shared_ptr
Use this type to store a pointer to an AssignableDataSource.
Definition: DataSource.hpp:198
static AssignableDataSource< T > * narrow(base::DataSourceBase *db)
This method narrows a base::DataSourceBase to a typeded AssignableDataSource, possibly returning a ne...
Definition: DataSource.inl:72
bool installTypeInfoObject(TypeInfo *ti)
Installs the type info object in the global data source type info handler and adds any additional fea...
static DataSource< T > * narrow(base::DataSourceBase *db)
This method narrows a base::DataSourceBase to a typeded DataSource, possibly returning a new object...
Definition: DataSource.inl:66
bool installTypeInfoObject(TypeInfo *ti)
Installs the type info object in the global data source type info handler and adds any additional fea...
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
static T int_to_enum(int i)
A simple, yet very useful DataSource, which keeps a value, and returns it in its get() method...
Definition: DataSources.hpp:60
static int enum_to_int(T e)
boost::intrusive_ptr< ValueDataSource< T > > shared_ptr
Definition: DataSources.hpp:72