Safir SDK Core
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
TypeUtilities.h
Go to the documentation of this file.
1 /******************************************************************************
2 *
3 * Copyright Saab AB, 2004-2013 (http://safir.sourceforge.net)
4 *
5 * Created by: Joel Ottosson / joot
6 *
7 *******************************************************************************
8 *
9 * This file is part of Safir SDK Core.
10 *
11 * Safir SDK Core is free software: you can redistribute it and/or modify
12 * it under the terms of version 3 of the GNU General Public License as
13 * published by the Free Software Foundation.
14 *
15 * Safir SDK Core is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more Internals.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with Safir SDK Core. If not, see <http://www.gnu.org/licenses/>.
22 *
23 ******************************************************************************/
24 #ifndef __DOTS_INTERNAL_TYPE_UTILITIES_H__
25 #define __DOTS_INTERNAL_TYPE_UTILITIES_H__
26 
27 #include <Safir/Dob/Typesystem/ToolSupport/Internal/BasicTypeOperations.h>
28 
32 namespace Safir
33 {
34 namespace Dob
35 {
36 namespace Typesystem
37 {
38 namespace ToolSupport
39 {
40 namespace TypeUtilities
41 {
49  template <class RepositoryT>
50  const char* GetTypeName(const RepositoryT* repository, DotsC_TypeId typeId)
51  {
52  return Safir::Dob::Typesystem::ToolSupport::Internal::BasicTypeOperations::TypeIdToTypeName(repository, typeId);
53  }
54 
55  inline DotsC_TypeId CalculateTypeId(const std::string& name)
56  {
57  return LlufId_Generate64(name.c_str());
58  }
59 
66  inline const char* GetTypeName(DotsC_MemberType memberType)
67  {
68  return Safir::Dob::Typesystem::ToolSupport::Internal::BasicTypeOperations::MemberTypeToString(memberType).c_str();
69  }
70 
79  template <class RepositoryT>
80  bool IsOfType(const RepositoryT* repository, DotsC_TypeId tid, DotsC_TypeId ofTid)
81  {
82  if (tid==ofTid)
83  {
84  return true;
85  }
86  return Safir::Dob::Typesystem::ToolSupport::Internal::BasicTypeOperations::IsOfType(repository, ObjectMemberType, tid, ObjectMemberType, ofTid);
87  }
88 
96  template <class EnumDescriptionT>
97  int GetIndexOfEnumValue(const EnumDescriptionT* description, const std::string& valueName) //Supports short name and fully qualified name. Ex: 'Monday' and 'MyEnumType.Monday'
98  {
99  size_t pos=valueName.rfind('.');
100  if (pos==std::string::npos)
101  {
102  for (int i=0; i<description->GetNumberOfValues(); ++i)
103  {
104  if (valueName==description->GetValueName(i))
105  {
106  return i;
107  }
108  }
109  }
110  else
111  {
112  std::string strippedValueName=valueName.substr(pos+1);
113  for (int i=0; i<description->GetNumberOfValues(); ++i)
114  {
115  if (strippedValueName==description->GetValueName(i))
116  {
117  return i;
118  }
119  }
120  }
121  return -1;
122  }
123 
131  template <class PropertyDescriptionT, class MemberDescriptionT>
132  DotsC_MemberIndex GetPropertyMemberIndex(const PropertyDescriptionT* pd, const std::string& memberName)
133  {
134  for (int i=0; i<pd->GetNumberOfMembers(); ++i)
135  {
136  const MemberDescriptionT* md=pd->GetMember(i);
137  if (memberName==md->GetName())
138  {
139  return static_cast<DotsC_MemberIndex>(i);
140  }
141  }
142  return -1;
143  }
144 
153  template <class ClassDescriptionT, class ParameterDescriptionT>
154  const ParameterDescriptionT* GetParameterByName(const ClassDescriptionT* cd, const std::string& paramName)
155  {
156  size_t dot=paramName.rfind('.');
157  if (dot==std::string::npos)
158  {
159  for (int i=0; i<cd->GetNumberOfParameters(); ++i)
160  {
161  const ParameterDescriptionT* pd=cd->GetParameter(i);
162  if (paramName==pd->GetName())
163  {
164  return pd;
165  }
166  }
167  }
168  else
169  {
170  std::string shortName=paramName.substr(dot+1);
171  for (int i=0; i<cd->GetNumberOfParameters(); ++i)
172  {
173  const ParameterDescriptionT* pd=cd->GetParameter(i);
174  if (shortName==pd->GetName())
175  {
176  return pd;
177  }
178  }
179  }
180  return NULL;
181  }
182 
186  template <class RepT, class Traits=Safir::Dob::Typesystem::ToolSupport::TypeRepositoryTraits<RepT> >
188  {
189  typedef typename Traits::RepositoryType RepositoryType;
190  typedef typename Traits::ClassDescriptionType ClassDescriptionType;
191  typedef typename Traits::ParameterDescriptionType ParameterDescriptionType;
192 
199  const ParameterDescriptionType* operator()(const RepositoryType* rep, const std::string& parameterName) const
200  {
201  size_t pos=parameterName.rfind('.');
202  if (pos==std::string::npos)
203  {
204  return NULL;
205  }
206 
207  std::string className=parameterName.substr(0, pos);
208  const ClassDescriptionType* cd=rep->GetClass(CalculateTypeId(className));
209  if (!cd)
210  {
211  return NULL;
212  }
213 
214  return GetParameterByName<ClassDescriptionType, ParameterDescriptionType>(cd, parameterName.substr(pos+1));
215  }
216  };
217 }
218 }
219 }
220 }
221 } //end namespace Safir::Dob::Typesystem::Internal::TypeUtilities
222 
223 #endif
const ParameterDescriptionType * operator()(const RepositoryType *rep, const std::string &parameterName) const
Get ParameterDescription from a fully qualified name.
Definition: TypeUtilities.h:199
Helper class to get ParameterDescription from a fully qualified name without having the ClassDescript...
Definition: TypeUtilities.h:187
Traits::ClassDescriptionType ClassDescriptionType
Definition: TypeUtilities.h:190
bool IsOfType(const RepositoryT *repository, DotsC_TypeId tid, DotsC_TypeId ofTid)
Check if a type is the same or a subtype of another type.
Definition: TypeUtilities.h:80
DotsC_MemberIndex GetPropertyMemberIndex(const PropertyDescriptionT *pd, const std::string &memberName)
Get index of a property member.
Definition: TypeUtilities.h:132
Traits::RepositoryType RepositoryType
Definition: TypeUtilities.h:189
const char * GetTypeName(const RepositoryT *repository, DotsC_TypeId typeId)
Finds corresponding type name to a typeId.
Definition: TypeUtilities.h:50
int GetIndexOfEnumValue(const EnumDescriptionT *description, const std::string &valueName)
Get the index (ordinal) of an enumeration value.
Definition: TypeUtilities.h:97
Traits::ParameterDescriptionType ParameterDescriptionType
Definition: TypeUtilities.h:191
const ParameterDescriptionT * GetParameterByName(const ClassDescriptionT *cd, const std::string &paramName)
Get parameter by name when the classDesription is already retrieved.
Definition: TypeUtilities.h:154
DotsC_TypeId CalculateTypeId(const std::string &name)
Definition: TypeUtilities.h:55