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
void addConstructor(TypeConstructor *tb)
Definition: TypeInfo.cpp:114
virtual bool composeType(base::DataSourceBase::shared_ptr source, base::DataSourceBase::shared_ptr result) const
virtual void set(param_t t)=0
virtual base::DataSourceBase::shared_ptr decomposeType(base::DataSourceBase::shared_ptr source) const
EnumTypeInfo(std::string type)
TypeConstructor * newConstructor(Function *foo, bool automatic=false)
std::map< T, std::string > to_string
std::map< T, std::string > MapType
static AssignableDataSource< T > * narrow(base::DataSourceBase *db)
TypeInfoRepository::shared_ptr Types()
Definition: Types.cpp:48
boost::intrusive_ptr< DataSource< T > > shared_ptr
Definition: DataSource.hpp:115
boost::intrusive_ptr< AssignableDataSource< T > > shared_ptr
Definition: DataSource.hpp:198
static DataSource< T > * narrow(base::DataSourceBase *db)
bool installTypeInfoObject(TypeInfo *ti)
bool installTypeInfoObject(TypeInfo *ti)
boost::intrusive_ptr< DataSourceBase > shared_ptr
Contains TaskContext, Activity, OperationCaller, Operation, Property, InputPort, OutputPort, Attribute.
Definition: Activity.cpp:53
static T int_to_enum(int i)
static int enum_to_int(T e)
static Logger & log()
Definition: Logger.hpp:350
static Logger::LogFunction endlog()
Definition: Logger.hpp:362
boost::intrusive_ptr< ValueDataSource< T > > shared_ptr
Definition: DataSources.hpp:72


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