Safir SDK Core
Loading...
Searching...
No Matches
TypeUtilities.h
Go to the documentation of this file.
1/******************************************************************************
2*
3* Copyright Saab AB, 2004-2023 (http://safirsdkcore.com)
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
32namespace Safir
33{
34namespace Dob
35{
36namespace Typesystem
37{
38namespace ToolSupport
39{
40namespace TypeUtilities
41{
47 inline DotsC_TypeId CalculateTypeId(const std::string& name)
48 {
49 return LlufId_Generate64(name.c_str());
50 }
51
58 inline const char* GetTypeName(DotsC_MemberType memberType)
59 {
60 return Safir::Dob::Typesystem::ToolSupport::Internal::BasicTypeOperations::MemberTypeToString(memberType).c_str();
61 }
62
70 template <class RepositoryT>
71 const char* GetTypeName(const RepositoryT* repository, DotsC_TypeId typeId)
72 {
73 return Safir::Dob::Typesystem::ToolSupport::Internal::BasicTypeOperations::TypeIdToTypeName(repository, typeId);
74 }
75
83 template <class RepositoryT, class DescriptionT>
84 const char* GetTypeName(const RepositoryT* repository, const DescriptionT* member)
85 {
86 DotsC_MemberType mt=member->GetMemberType();
87 if (mt==EnumerationMemberType || mt==ObjectMemberType)
88 {
89 return GetTypeName(repository, member->GetTypeId());
90 }
91 return GetTypeName(mt);
92 }
93
102 template <class RepositoryT>
103 bool IsOfType(const RepositoryT* repository, DotsC_TypeId tid, DotsC_TypeId ofTid)
104 {
105 if (tid==ofTid)
106 {
107 return true;
108 }
109 return Safir::Dob::Typesystem::ToolSupport::Internal::BasicTypeOperations::IsOfType(repository, ObjectMemberType, tid, ObjectMemberType, ofTid);
110 }
111
119 template <class EnumDescriptionT>
120 int GetIndexOfEnumValue(const EnumDescriptionT* description, const std::string& valueName) //Supports short name and fully qualified name. Ex: 'Monday' and 'MyEnumType.Monday'
121 {
122 size_t pos=valueName.rfind('.');
123 if (pos==std::string::npos)
124 {
125 for (int i=0; i<description->GetNumberOfValues(); ++i)
126 {
127 if (valueName==description->GetValueName(i))
128 {
129 return i;
130 }
131 }
132 }
133 else
134 {
135 std::string strippedValueName=valueName.substr(pos+1);
136 for (int i=0; i<description->GetNumberOfValues(); ++i)
137 {
138 if (strippedValueName==description->GetValueName(i))
139 {
140 return i;
141 }
142 }
143 }
144 return -1;
145 }
146
154 template <class PropertyDescriptionT, class MemberDescriptionT>
155 DotsC_MemberIndex GetPropertyMemberIndex(const PropertyDescriptionT* pd, const std::string& memberName)
156 {
157 for (int i=0; i<pd->GetNumberOfMembers(); ++i)
158 {
159 const MemberDescriptionT* md=pd->GetMember(i);
160 if (memberName==md->GetName())
161 {
162 return static_cast<DotsC_MemberIndex>(i);
163 }
164 }
165 return -1;
166 }
167
176 template <class ClassDescriptionT, class ParameterDescriptionT>
177 const ParameterDescriptionT* GetParameterByName(const ClassDescriptionT* cd, const std::string& paramName)
178 {
179 size_t dot=paramName.rfind('.');
180 if (dot==std::string::npos)
181 {
182 for (int i=0; i<cd->GetNumberOfParameters(); ++i)
183 {
184 const ParameterDescriptionT* pd=cd->GetParameter(i);
185 if (paramName==pd->GetName())
186 {
187 return pd;
188 }
189 }
190 }
191 else
192 {
193 std::string shortName=paramName.substr(dot+1);
194 for (int i=0; i<cd->GetNumberOfParameters(); ++i)
195 {
196 const ParameterDescriptionT* pd=cd->GetParameter(i);
197 if (shortName==pd->GetName())
198 {
199 return pd;
200 }
201 }
202 }
203 return NULL;
204 }
205
209 template <class RepT, class Traits=Safir::Dob::Typesystem::ToolSupport::TypeRepositoryTraits<RepT> >
211 {
212 typedef typename Traits::RepositoryType RepositoryType;
213 typedef typename Traits::ClassDescriptionType ClassDescriptionType;
214 typedef typename Traits::ParameterDescriptionType ParameterDescriptionType;
215
222 const ParameterDescriptionType* operator()(const RepositoryType* rep, const std::string& parameterName) const
223 {
224 size_t pos=parameterName.rfind('.');
225 if (pos==std::string::npos)
226 {
227 return NULL;
228 }
229
230 std::string className=parameterName.substr(0, pos);
231 const ClassDescriptionType* cd=rep->GetClass(CalculateTypeId(className));
232 if (!cd)
233 {
234 return NULL;
235 }
236
237 return GetParameterByName<ClassDescriptionType, ParameterDescriptionType>(cd, parameterName.substr(pos+1));
238 }
239 };
240
246 inline DotsC_Int64 ToUnifiedDictionaryKey(DotsC_Int64 key) {return key;}
247 inline DotsC_Int64 ToUnifiedDictionaryKey(DotsC_Int32 key) {return static_cast<DotsC_Int64>(key);}
248 inline DotsC_Int64 ToUnifiedDictionaryKey(const std::string& key) {return LlufId_Generate64(key.c_str());}
249 inline DotsC_Int64 ToUnifiedDictionaryKey(const DotsC_EntityId& key)
250 {
251 std::ostringstream os;
252 os<<key.typeId<<key.instanceId;
253 return LlufId_Generate64(os.str().c_str());
254 }
255
256 template <class ParameterDescriptionT, class KeyT>
257 int GetDictionaryIndexFromKey(const ParameterDescriptionT* pd, const KeyT& key)
258 {
259 assert(pd->GetCollectionType()==DictionaryCollectionType);
260 return pd->GetIndexByUnifiedKey(ToUnifiedDictionaryKey(key));
261 }
262
263}
264}
265}
266}
267} //end namespace Safir::Dob::Typesystem::ToolSupport::TypeUtilities
268
269#endif
This namespace contains all the functionality and definitions of the SAFIR SDK.
Definition Backdoor.h:31
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:103
DotsC_MemberIndex GetPropertyMemberIndex(const PropertyDescriptionT *pd, const std::string &memberName)
Get index of a property member.
Definition TypeUtilities.h:155
int GetIndexOfEnumValue(const EnumDescriptionT *description, const std::string &valueName)
Get the index (ordinal) of an enumeration value.
Definition TypeUtilities.h:120
DotsC_Int64 ToUnifiedDictionaryKey(DotsC_Int64 key)
ToUnifiedDictionaryKey - Convert all keys to an int64 that is the internal key format.
Definition TypeUtilities.h:246
DotsC_TypeId CalculateTypeId(const std::string &name)
Calculates a typeId from a string.
Definition TypeUtilities.h:47
const ParameterDescriptionT * GetParameterByName(const ClassDescriptionT *cd, const std::string &paramName)
Get parameter by name when the classDescription is already retrieved.
Definition TypeUtilities.h:177
const char * GetTypeName(DotsC_MemberType memberType)
Finds corresponding type name to a memberType.
Definition TypeUtilities.h:58
int GetDictionaryIndexFromKey(const ParameterDescriptionT *pd, const KeyT &key)
Definition TypeUtilities.h:257
Helper class to get ParameterDescription from a fully qualified name without having the ClassDescript...
Definition TypeUtilities.h:211
Traits::ParameterDescriptionType ParameterDescriptionType
Definition TypeUtilities.h:214
Traits::ClassDescriptionType ClassDescriptionType
Definition TypeUtilities.h:213
const ParameterDescriptionType * operator()(const RepositoryType *rep, const std::string &parameterName) const
Get ParameterDescription from a fully qualified name.
Definition TypeUtilities.h:222
Traits::RepositoryType RepositoryType
Definition TypeUtilities.h:212