ElementIdMap.h
Go to the documentation of this file.
1 /*--------------------------------------------------------------------------------------+
2 |
3 | Supplied under applicable software license agreement.
4 |
5 | Copyright (c) 2018 Bentley Systems, Incorporated. All rights reserved.
6 |
7 +---------------------------------------------------------------------------------------*/
8 #pragma once
9 
11 #include "DgnPlatform.h"
12 
14 
15 
16 //=======================================================================================
18 //=======================================================================================
19 
20 template <class _ForeignId> struct FixedSizeForeignIdIO
21 {
22  StatusInt Store (_ForeignId const& id, DataExternalizer& sink) { sink.put((UInt8 const*) &id, sizeof (id)); return SUCCESS;}
23  StatusInt Load (_ForeignId& id, DataInternalizer& source) { source.get((UInt8*) &id, sizeof (id)); return SUCCESS;}
24 };
25 
26 
27 //=======================================================================================
96 
98 template <class _ForeignId,
99  class _Compare = std::less<_ForeignId>,
100  class _ForeignIdIO = FixedSizeForeignIdIO <_ForeignId> >
101 
103 {
104 
105  bmap <_ForeignId, T_AUInt64,_Compare> m_idMap;
106  T_AUInt64 m_highestId;
107  mutable _ForeignIdIO m_io;
108 
109 public:
110 
111  ElementIdMap (_ForeignIdIO const& io) : m_io (io), m_highestId (0) { }
112 
113 /*---------------------------------------------------------------------------------**/
120 T_AUInt64 GetId (_ForeignId const& foreignId)
121  {
122  bmap <_ForeignId, T_AUInt64, _Compare>::const_iterator found = m_idMap.find (foreignId);
123 
124  if (found != m_idMap.end())
125  return found->second;
126 
127  return m_idMap[foreignId] = ++m_highestId;
128  }
129 
130 /*---------------------------------------------------------------------------------**/
135 StatusInt Store (DataExternalizer& sink) const
136  {
137  for (bmap <_ForeignId, T_AUInt64, _Compare>::const_iterator curr = m_idMap.begin(); curr != m_idMap.end(); curr++)
138  {
139  sink.put (curr->second);
140  m_io.Store (curr->first, sink);
141  }
142  return SUCCESS;
143  }
144 
145 /*---------------------------------------------------------------------------------**/
150 StatusInt Load (DataInternalizer& source)
151  {
152  size_t minimumIdSize = 2;
153  size_t minimumEntrySize = sizeof (T_AUInt64) + minimumIdSize;
154  StatusInt status = SUCCESS;
155 
156 
157  source.rewind ();
158 
159  while (SUCCESS == status && source.getRemainingSize () > sizeof (UInt32) + minimumEntrySize)
160  {
161  _ForeignId foreignId;
162  T_AUInt64 elementId;
163 
164  source.get (&elementId);
165 
166  if (SUCCESS == (status = m_io.Load (foreignId, source)))
167  m_idMap[foreignId] = elementId;
168 
169  if (elementId > m_highestId)
170  m_highestId = m_highestId;
171 
172  };
173  return SUCCESS;
174  }
175 
176 /*---------------------------------------------------------------------------------**/
182 BeFileName GetMapFileName (WStringCR baseName)
183  {
184  WString device, dir, name, extension;
185  BeFileName mapFileName;
186 
187  BeFileName::ParseName (&device, &dir, &name, &extension, baseName.c_str());
188  extension = extension + L".DgnIdMap";
189  mapFileName.BuildName (device.c_str(), dir.c_str(), name.c_str(), extension.c_str());
190 
191  return mapFileName;
192  }
193 
194 /*---------------------------------------------------------------------------------**/
199 StatusInt Load (WStringCR baseFileName)
200  {
201  FILE* mapFile;
202 
203  if (NULL == (mapFile = _wfopen (GetMapFileName (baseFileName), L"rb")))
204  return ERROR;
205 
206  fseek (mapFile, 0, SEEK_END);
207  size_t mapFileSize = ftell (mapFile);
208 
209  bvector<byte> fileBuffer (mapFileSize);
210 
211  fseek (mapFile, 0, SEEK_SET);
212  size_t count = fread (&fileBuffer.front(), 1, mapFileSize, mapFile);
213 
214  BeAssert (count == mapFileSize);
215 
216  DataInternalizer source (&fileBuffer.front(), mapFileSize);
217  StatusInt status = Load (source);
218 
219  fclose (mapFile);
220  return status;
221  }
222 
223 /*---------------------------------------------------------------------------------**/
228 StatusInt Store (WStringCR baseFileName)
229  {
230  StatusInt status;
231  DataExternalizer sink;
232 
233  if (SUCCESS != (status = Store (sink)))
234  return status;
235 
236  if (sink.getBytesWritten() == 0)
237  return ERROR;
238 
239  FILE* mapFile;
240  if (NULL == (mapFile = _wfopen (GetMapFileName (baseFileName), L"wb")))
241  {
242  BeAssert (false);
243  return ERROR;
244  }
245 
246  fwrite (sink.getBuf(), 1, sink.getBytesWritten(), mapFile);
247  fclose (mapFile);
248  return SUCCESS;
249  }
250 
251 }; // ElementIdMap
252 
253 
254 //=======================================================================================
256 //=======================================================================================
258 {
259  StatusInt Store (WStringCR id, DataExternalizer& sink) { sink.put(id); return SUCCESS;}
260  StatusInt Load (WStringR id, DataInternalizer& source) { source.get(id); return SUCCESS;}
261 };
262 
264 //=======================================================================================
266 //=======================================================================================
267 struct StringToElementIdMap : T_StringToElementIdMap
268 {
269  StringToElementIdMap () : T_StringToElementIdMap (WStringIdIO()) { }
270 };
271 
272 
273 
T_AUInt64 GetId(_ForeignId const &foreignId)
Return elementId for supplied foreignId.
Definition: ElementIdMap.h:120
Object for I/O for foreign string keys.
Definition: ElementIdMap.h:257
Definition: Bentley.h:212
StatusInt Load(WStringR id, DataInternalizer &source)
Definition: ElementIdMap.h:260
#define END_BENTLEY_DGNPLATFORM_NAMESPACE
Definition: DgnPlatformBaseType.r.h:69
struct Bentley::WString const & WStringCR
Definition: Bentley.h:239
#define SEEK_END
Definition: mdlio.h:20
StatusInt Store(WStringCR baseFileName)
Store map.
Definition: ElementIdMap.h:228
size_type count(const key_type &__x) const
Definition: stdcxx/bstdmap.h:277
Map for foreign string keys.
Definition: ElementIdMap.h:267
uint8_t UInt8
Definition: Bentley.r.h:116
ElementIDMap is a template for classes which store a mapping from some "foreign" object ID to DGN Ele...
Definition: ElementIdMap.h:102
uint32_t UInt32
Definition: Bentley.r.h:128
#define NULL
Definition: Bentley.h:157
StatusInt Load(_ForeignId &id, DataInternalizer &source)
Definition: ElementIdMap.h:23
FixedSizeForeignIdIO - Default ForeignId I/0 class - handles fixed size keys.
Definition: ElementIdMap.h:20
StatusInt Store(WStringCR id, DataExternalizer &sink)
Definition: ElementIdMap.h:259
StatusInt Load(DataInternalizer &source)
Load map.
Definition: ElementIdMap.h:150
StatusInt Store(_ForeignId const &id, DataExternalizer &sink)
Definition: ElementIdMap.h:22
StatusInt Store(DataExternalizer &sink) const
Store map.
Definition: ElementIdMap.h:135
#define fwrite
Definition: BeConsole.h:94
StatusInt Load(WStringCR baseFileName)
Load map.
Definition: ElementIdMap.h:199
#define BEGIN_BENTLEY_DGNPLATFORM_NAMESPACE
Definition: DgnPlatformBaseType.r.h:68
int StatusInt
Definition: Bentley.h:222
Definition: Bentley.h:210
#define BeAssert(_Expression)
BeAssert performs the same function as the standard assert, plus it prevents the most common cases of...
Definition: BeAssert.h:56
#define SEEK_SET
Definition: mdlio.h:18
ElementIdMap(_ForeignIdIO const &io)
Definition: ElementIdMap.h:111
BeFileName GetMapFileName(WStringCR baseName)
Definition: ElementIdMap.h:182
StringToElementIdMap()
Definition: ElementIdMap.h:269
ElementIdMap< WString, std::less< WString const & >, WStringIdIO > T_StringToElementIdMap
Definition: ElementIdMap.h:263
A string class that has many of the same capabilities as std::string, plus additional functions such ...
Definition: WString.h:51

Copyright © 2017 Bentley Systems, Incorporated. All rights reserved.