Safir SDK Core
Loading...
Searching...
No Matches
BlobReader.h
Go to the documentation of this file.
1/******************************************************************************
2*
3* Copyright Saab AB, 2004-2015 (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_BLOB_READER_H__
25#define __DOTS_INTERNAL_BLOB_READER_H__
26
27#include <string>
28#include <vector>
29#include <sstream>
31#include <Safir/Dob/Typesystem/ToolSupport/Internal/InternalDefs.h>
32#include <Safir/Dob/Typesystem/ToolSupport/Internal/Blob.h>
33
34namespace Safir
35{
36namespace Dob
37{
38namespace Typesystem
39{
40namespace ToolSupport
41{
63 template <class RepositoryT, class Traits=Safir::Dob::Typesystem::ToolSupport::TypeRepositoryTraits<RepositoryT> >
65 {
66 public:
67 typedef typename Traits::RepositoryType RepositoryType;
68 typedef typename Traits::ClassDescriptionType ClassDescriptionType;
69 typedef typename Traits::MemberDescriptionType MemberDescriptionType;
70 typedef typename Traits::PropertyDescriptionType PropertyDescriptionType;
71 typedef typename Traits::ExceptionDescriptionType ExceptionDescriptionType;
72 typedef typename Traits::ParameterDescriptionType ParameterDescriptionType;
73 typedef typename Traits::EnumDescriptionType EnumDescriptionType;
74 typedef typename Traits::MemberMappingDescriptionType MemberMappingDescriptionType;
75 typedef typename Traits::PropertyMappingDescriptionType PropertyMappingDescriptionType;
76 typedef typename Traits::CreateRoutineDescriptionType CreateRoutineDescriptionType;
77
83 static DotsC_Int32 GetSize(const char* blob) {return Internal::Blob::GetSize(blob);}
84
90 static DotsC_TypeId GetTypeId(const char* blob) {return Internal::Blob::GetTypeId(blob);}
91
97 BlobReader(const RepositoryT* rep, const char* blob)
98 :m_repository(rep)
99 ,m_blob(blob)
100 ,m_classDescription(rep->GetClass(m_blob.TypeId()))
101 ,m_memberDescription(NULL)
102 ,m_memberIndex(-1)
103 {
104 }
105
106 BlobReader(const BlobReader&) = delete;
107 BlobReader& operator=(const BlobReader&) = delete;
108
113 DotsC_Int32 Size() const {return m_blob.Size();}
114
119 DotsC_TypeId TypeId() const {return m_blob.TypeId();}
120
131 bool IsChangedTopLevel(DotsC_MemberIndex member) const
132 {
133 return m_blob.IsChangedTopLevel(member);
134 }
135
142 {
143 for (int member=0; member<m_classDescription->GetNumberOfMembers(); ++member)
144 {
145 if (IsChangedRecursive(member))
146 {
147 return true;
148 }
149 }
150 return false;
151 }
152
158 bool IsChangedRecursive(DotsC_MemberIndex member) const
159 {
160 MoveToMember(member);
161 const DotsC_CollectionType collectionType = m_memberDescription->GetCollectionType();
162
163 //Check top level change flag for things that have it.
164 if (collectionType == SequenceCollectionType || collectionType == DictionaryCollectionType)
165 {
166 if (m_blob.IsChangedTopLevel(member))
167 {
168 return true;
169 }
170 }
171
172 const int size = m_blob.NumberOfValues(member);
173
174 //sequences are a bit different from other members, since
175 //they do not have change flags on the values. But they
176 //do potentially have nestled change flags if it is a
177 //sequence of objects.
178 if (collectionType == SequenceCollectionType)
179 {
180 if (m_memberDescription->GetMemberType() == ObjectMemberType)
181 {
182 for (int index = 0; index < size; ++index)
183 {
184 bool dummy=false, isNull=false;
185 m_blob.ValueStatus(member, index, isNull, dummy);
186 if (!isNull)
187 {
188 std::pair<const char*, DotsC_Int32> obj=m_blob.GetValueBinary(member, index);
189 const BlobReader inner(m_repository, obj.first);
190 if (inner.IsChangedRecursive())
191 {
192 return true;
193 }
194 }
195 }
196 }
197 }
198 else
199 {
200 //first check all the change flags that are directly in the member
201 for (int index = 0; index < size; ++index)
202 {
203 if (IsChangedHere(member, index))
204 {
205 return true;
206 }
207 }
208 //ok, if there are objects in here we need to recurse
209
210 if (m_memberDescription->GetMemberType() == ObjectMemberType)
211 {
212 for (int index = 0; index < size; ++index)
213 {
214 bool dummy=false, isNull=false;
215 m_blob.ValueStatus(member, index, isNull, dummy);
216 if (!isNull)
217 {
218 std::pair<const char*, DotsC_Int32> obj=m_blob.GetValueBinary(member, index);
219 const BlobReader inner(m_repository, obj.first);
220 if (inner.IsChangedRecursive())
221 {
222 return true;
223 }
224 }
225 }
226 }
227 }
228
229 //no changes...
230 return false;
231 }
232
242 bool IsChangedHere(DotsC_MemberIndex member,
243 DotsC_Int32 index) const
244 {
245#ifndef NDEBUG
246 MoveToMember(member);
247 assert(m_memberDescription->GetCollectionType() != SequenceCollectionType);
248 //array index is asserted in Blob.cpp
249#endif
250 return m_blob.IsChangedHere(member,index);
251 }
252
253
259 int NumberOfValues(DotsC_MemberIndex member) const {return m_blob.NumberOfValues(member);}
260
268 void ReadStatus(DotsC_MemberIndex member, int valueIndex, bool& isNull, bool& isChanged) const
269 {
270 MoveToMember(member);
271 m_blob.ValueStatus(member, valueIndex, isNull, isChanged);
272 }
273
282 template <class Key>
283 Key ReadKey(DotsC_MemberIndex member, int valueIndex) const
284 {
285 MoveToMember(member);
286 if (m_memberDescription->GetCollectionType()!=DictionaryCollectionType)
287 {
288 ThrowWrongCollectionType();
289 }
290 return Internal::BlobUtils::Reader<Key>::Key(m_blob, member, valueIndex);
291 }
292
302 template <class Val>
303 void ReadValue(DotsC_MemberIndex member, int valueIndex, Val& val, bool& isNull, bool& isChanged) const
304 {
305 MoveToMember(member);
306 m_blob.ValueStatus(member, valueIndex, isNull, isChanged);
307 if (!isNull)
308 {
309 val=Internal::BlobUtils::Reader<Val>::Value(m_blob, member, valueIndex);
310 }
311 }
312
313 private:
314 const RepositoryType* m_repository;
315 Safir::Dob::Typesystem::ToolSupport::Internal::Blob m_blob;
316 const ClassDescriptionType* m_classDescription;
317 mutable const MemberDescriptionType* m_memberDescription;
318 mutable DotsC_MemberIndex m_memberIndex;
319
320 inline void MoveToMember(DotsC_MemberIndex member) const
321 {
322 if (m_memberIndex!=member)
323 {
324 m_memberDescription=m_classDescription->GetMember(member);
325 m_memberIndex=member;
326 }
327 }
328
329 inline void ThrowWrongCollectionType() const
330 {
331 std::ostringstream os;
332 os<<"Trying to write data of wrong collection type to a blob for member '"<<m_memberDescription->GetName()<<"' in class '"<<m_classDescription->GetName()<<"'";
333 throw std::logic_error(os.str());
334 }
335
337 };
338}
339}
340}
341} //end namespace Safir::Dob::Typesystem::ToolSupport
342
343#endif
This namespace contains all the functionality and definitions of the SAFIR SDK.
Definition Backdoor.h:31
DotsC_TypeId TypeId
A unique type identifier.
Definition Defs.h:218
This class is used to unpack and read blobs created by the BlobWriter class.
Definition BlobReader.h:65
static DotsC_TypeId GetTypeId(const char *blob)
Static method.
Definition BlobReader.h:90
DotsC_TypeId TypeId() const
Get the type id of the blob.
Definition BlobReader.h:119
BlobReader(const RepositoryT *rep, const char *blob)
Constructor - Creates a reader object that unpacks the blob and makes it possible to read its content...
Definition BlobReader.h:97
void ReadStatus(DotsC_MemberIndex member, int valueIndex, bool &isNull, bool &isChanged) const
Convenience method for checking if a member is null.
Definition BlobReader.h:268
Traits::RepositoryType RepositoryType
Definition BlobReader.h:67
Traits::ExceptionDescriptionType ExceptionDescriptionType
Definition BlobReader.h:71
Traits::CreateRoutineDescriptionType CreateRoutineDescriptionType
Definition BlobReader.h:76
Key ReadKey(DotsC_MemberIndex member, int valueIndex) const
Reads the key element of a member value.
Definition BlobReader.h:283
Traits::EnumDescriptionType EnumDescriptionType
Definition BlobReader.h:73
bool IsChangedRecursive(DotsC_MemberIndex member) const
Check change flag on a member, recursively.
Definition BlobReader.h:158
friend struct Internal::BlobUtils::BlobAccess
Definition BlobReader.h:336
Traits::PropertyMappingDescriptionType PropertyMappingDescriptionType
Definition BlobReader.h:75
Traits::ClassDescriptionType ClassDescriptionType
Definition BlobReader.h:68
int NumberOfValues(DotsC_MemberIndex member) const
Get the number of values for the member.
Definition BlobReader.h:259
bool IsChangedTopLevel(DotsC_MemberIndex member) const
Check if the member is changed at top level.
Definition BlobReader.h:131
DotsC_Int32 Size() const
Get the size of the blob.
Definition BlobReader.h:113
BlobReader & operator=(const BlobReader &)=delete
bool IsChangedRecursive() const
Check change flags on all members, recursively.
Definition BlobReader.h:141
void ReadValue(DotsC_MemberIndex member, int valueIndex, Val &val, bool &isNull, bool &isChanged) const
Read the value element of a member value.
Definition BlobReader.h:303
Traits::MemberMappingDescriptionType MemberMappingDescriptionType
Definition BlobReader.h:74
Traits::ParameterDescriptionType ParameterDescriptionType
Definition BlobReader.h:72
Traits::MemberDescriptionType MemberDescriptionType
Definition BlobReader.h:69
Traits::PropertyDescriptionType PropertyDescriptionType
Definition BlobReader.h:70
bool IsChangedHere(DotsC_MemberIndex member, DotsC_Int32 index) const
Check change flag on a member (non-recursively)
Definition BlobReader.h:242
static DotsC_Int32 GetSize(const char *blob)
Static method.
Definition BlobReader.h:83