CFx SDK Documentation  2022 SP0
GsFilerV100Impl.h
Go to the documentation of this file.
1 // Copyright (C) 2002-2017, Open Design Alliance (the "Alliance").
3 // All rights reserved.
4 //
5 // This software and its documentation and related materials are owned by
6 // the Alliance. The software may only be incorporated into application
7 // programs owned by members of the Alliance, subject to a signed
8 // Membership Agreement and Supplemental Software License Agreement with the
9 // Alliance. The structure and organization of this software are the valuable
10 // trade secrets of the Alliance and its suppliers. The software is also
11 // protected by copyright law and international treaty provisions. Application
12 // programs incorporating this software must include the following statement
13 // with their copyright notices:
14 //
15 // This application incorporates Teigha(R) software pursuant to a license
16 // agreement with Open Design Alliance.
17 // Teigha(R) Copyright (C) 2002-2017 by Open Design Alliance.
18 // All rights reserved.
19 //
20 // By use of this software, its documentation or related materials, you
21 // acknowledge and accept the above terms.
23 
24 #ifndef __OdGsFilerV100Impl_H__
25 #define __OdGsFilerV100Impl_H__
26 
27 #include "TD_PackPush.h"
28 #include "Ge/GeMatrix3d.h"
29 #include "Ge/GeExtents3d.h"
30 #include "DbBaseDatabase.h"
31 #include "GsFiler.h"
32 #include "OdStack.h"
33 #include "RxObjectImpl.h"
34 
35 
36 // Database linker
38 {
39 public:
41  virtual ~OdGsFilerDbLinker() { }
42 public:
43  // Get database hash
44  virtual OdBinaryData getDbHash(const OdDbBaseDatabase *pDb) const;
45 
46  // Transform database handle
47  virtual OdUInt64 stubToDbHandle(OdDbStub *pStub) const = 0;
48  virtual OdDbStub *handleToDbStub(OdUInt64 nHandle) const = 0;
49 
50  // Util methods
51  virtual OdDbBaseDatabase *getDatabase() const = 0;
52  virtual OdDbBaseDatabase *getDatabase(OdDbStub *pStub) const = 0;
53 };
54 
56 
57 
58 
59 #define STL_USING_MAP
60 #define STL_USING_SET
61 #include "OdaSTL.h"
62 
63 // Implement substitutor
65 {
66  template<OdUInt32 nBytes> struct DataTyp
67  {
68  OdUInt8 m_data[nBytes];
69  static int compare(const DataTyp &t1, const DataTyp &t2)
70  {
71  for (OdUInt32 nb = 0; nb < nBytes; nb++)
72  {
73  if (t1.m_data[nb] != t2.m_data[nb])
74  return int(t1.m_data[nb]) - int(t2.m_data[nb]);
75  }
76  return 0;
77  }
78  bool operator ==(const DataTyp &t2) const { return compare(*this, t2) == 0; }
79  bool operator !=(const DataTyp &t2) const { return compare(*this, t2) != 0; }
80  bool operator <(const DataTyp &t2) const { return compare(*this, t2) < 0; }
81  bool operator <=(const DataTyp &t2) const { return compare(*this, t2) <= 0; }
82  bool operator >(const DataTyp &t2) const { return compare(*this, t2) > 0; }
83  bool operator >=(const DataTyp &t2) const { return compare(*this, t2) >= 0; }
84 
85  operator const void*() const { return this; }
86  };
87  struct VoidsSet
88  {
89  void *m_pVoid;
91  };
92  struct VoidArry : public VoidsSet
93  {
96  : m_nVoids(0)
97  {
98  m_pVoid = NULL;
99  m_pAct = NULL;
100  }
101  ~VoidArry() { clear(); }
102  VoidsSet *getSet() { return (VoidsSet*)m_pVoid; }
104  if (m_nVoids == 1)
105  return this;
106  return getSet() + n;
107  }
108  bool contains(void *pVoid);
109  void add(void *pVoid, OdGsFiler::SubstitutionActuator *pAct);
110  void kill(void *pVoid);
111  void clear();
112  };
113 
114  struct AbstractTyp
115  {
117  AbstractTyp() : m_pContainer(NULL) { }
118  };
119  typedef std::map<OdUInt32, AbstractTyp> RegsMap;
120  typedef std::map<OdUInt32, AbstractTyp> SubstsMap;
123 
124  template<OdUInt32 nBytes> struct Procs
125  {
127  typedef std::map<ProcTyp, ProcTyp> RegMap;
128  typedef std::map<ProcTyp, VoidArry> SubstMap;
129  static void makeSubst(void *pPlace, const void *pSubstitution)
130  {
131  *(ProcTyp*)(pPlace) = *(const ProcTyp*)(pSubstitution);
132  }
133  static void runSubst(void *pPlace, const void *pSubstitution, OdGsFiler::SubstitutionActuator *pActuator)
134  {
135  ODA_ASSERT(pActuator);
136  pActuator->applySubstitution(pPlace, pSubstitution, makeSubst);
137  }
138  static void subst(OdGsFiler_SubstitutorImpl &impl, const void *pValue, void *pPlace, OdGsFiler::SubstitutionActuator *pAct, const void *pSubstitution, bool bClear)
139  {
140  if (pPlace && pSubstitution)
141  {
142  runSubst(pPlace, pSubstitution, pAct);
143  return;
144  }
145  if (pValue)
146  {
147  if (pPlace)
148  {
149  RegMap *pRegMap = (RegMap*)impl.m_regs[nBytes].m_pContainer;
150  OD_TYPENAME RegMap::iterator itReg = pRegMap->find(*(const ProcTyp*)pValue);
151  if (itReg != pRegMap->end())
152  {
153  runSubst(pPlace, itReg->second, pAct);
154  if (bClear)
155  pRegMap->erase(itReg);
156  }
157  }
158  else if (pSubstitution)
159  {
160  SubstMap *pSubMap = (SubstMap*)impl.m_substs[nBytes].m_pContainer;
161  OD_TYPENAME SubstMap::iterator itSub = pSubMap->find(*(const ProcTyp*)pValue);
162  if (itSub != pSubMap->end())
163  {
164  for (OdInt32 n = 0; n < itSub->second.m_nVoids; n++)
165  runSubst(itSub->second.at(n)->m_pVoid, pSubstitution, itSub->second.at(n)->m_pAct);
166  if (bClear)
167  pSubMap->erase(itSub);
168  }
169  }
170  else
171  {
172  RegMap *pRegMap = (RegMap*)impl.m_regs[nBytes].m_pContainer;
173  SubstMap *pSubMap = (SubstMap*)impl.m_substs[nBytes].m_pContainer;
174  if (pRegMap && pSubMap)
175  {
176  OD_TYPENAME RegMap::iterator itReg = pRegMap->find(*(const ProcTyp*)pValue);
177  OD_TYPENAME SubstMap::iterator itSub = pSubMap->find(*(const ProcTyp*)pValue);
178  if ((itReg != pRegMap->end()) && (itSub != pSubMap->end()))
179  {
180  for (OdInt32 n = 0; n < itSub->second.m_nVoids; n++)
181  runSubst(itSub->second.at(n)->m_pVoid, itReg->second, itSub->second.at(n)->m_pAct);
182  }
183  if (bClear)
184  {
185  if (itReg != pRegMap->end())
186  pRegMap->erase(itReg);
187  if (itSub != pSubMap->end())
188  pSubMap->erase(itSub);
189  }
190  }
191  else if (bClear)
192  {
193  if (pRegMap)
194  {
195  OD_TYPENAME RegMap::iterator itReg = pRegMap->find(*(const ProcTyp*)pValue);
196  if (itReg != pRegMap->end())
197  pRegMap->erase(itReg);
198  }
199  if (pSubMap)
200  {
201  OD_TYPENAME SubstMap::iterator itSub = pSubMap->find(*(const ProcTyp*)pValue);
202  if (itSub != pSubMap->end())
203  pSubMap->erase(itSub);
204  }
205  }
206  }
207  return;
208  }
209  RegMap *pRegMap = (RegMap*)impl.m_regs[nBytes].m_pContainer;
210  SubstMap *pSubMap = (SubstMap*)impl.m_substs[nBytes].m_pContainer;
211  if (pRegMap && pSubMap)
212  {
213  OD_TYPENAME RegMap::iterator itReg = pRegMap->begin();
214  while (itReg != pRegMap->end())
215  {
216  OD_TYPENAME SubstMap::iterator itSub = pSubMap->find(itReg->first);
217  if (itSub != pSubMap->end())
218  {
219  for (OdInt32 n = 0; n < itSub->second.m_nVoids; n++)
220  runSubst(itSub->second.at(n)->m_pVoid, itReg->second, itSub->second.at(n)->m_pAct);
221  }
222  itReg++;
223  }
224  }
225  if (bClear)
226  {
227  if (pRegMap) { delete pRegMap; impl.m_regs[nBytes].m_pContainer = NULL; }
228  if (pSubMap) { delete pSubMap; impl.m_substs[nBytes].m_pContainer = NULL; }
229  }
230  }
231  static void reg(OdGsFiler_SubstitutorImpl &impl, const void *pValue, const void *pSubstitution, bool bRegister, bool bImmediate)
232  {
233  if (bRegister)
234  {
235  OdGsFiler_SubstitutorImpl::AbstractTyp &cont = impl.m_regs[nBytes];
236  if (!cont.m_pContainer)
237  cont.m_pContainer = new RegMap;
238  RegMap &pMap = *(RegMap*)cont.m_pContainer;
239  pMap[*(const ProcTyp*)pValue] = *(const ProcTyp*)pSubstitution;
240  }
241  if (bImmediate)
242  subst(impl, pValue, NULL, NULL, pSubstitution, false);
243  }
244  static void req(OdGsFiler_SubstitutorImpl &impl, void *pPlace, OdGsFiler::SubstitutionActuator *pAct, const void *pValue, bool bRegister, bool bImmediate)
245  {
246  if (bRegister)
247  {
248  if (bImmediate)
249  {
250  RegMap *pRegMap = (RegMap*)impl.m_regs[nBytes].m_pContainer;
251  if (pRegMap)
252  {
253  OD_TYPENAME RegMap::iterator itReg = pRegMap->find(*(const ProcTyp*)pValue);
254  if (itReg != pRegMap->end())
255  {
256  runSubst(pPlace, itReg->second, pAct);
257  return;
258  }
259  }
260  }
262  if (!cont.m_pContainer)
263  cont.m_pContainer = new SubstMap;
264  SubstMap &pMap = *(SubstMap*)cont.m_pContainer;
265  pMap[*(const ProcTyp*)pValue].add(pPlace, pAct);
266  }
267  else if (bImmediate)
268  subst(impl, pValue, pPlace, pAct, NULL, false);
269  }
270  static void clear(OdGsFiler_SubstitutorImpl &impl, const void *pValue)
271  {
272  RegMap *pRegMap = (RegMap*)impl.m_regs[nBytes].m_pContainer;
273  SubstMap *pSubMap = (SubstMap*)impl.m_substs[nBytes].m_pContainer;
274  if (pValue)
275  {
276  if (pRegMap)
277  {
278  OD_TYPENAME RegMap::iterator it = pRegMap->find(*(const ProcTyp*)pValue);
279  if (it != pRegMap->end())
280  pRegMap->erase(it);
281  }
282  if (pSubMap)
283  {
284  OD_TYPENAME SubstMap::iterator it = pSubMap->find(*(const ProcTyp*)pValue);
285  if (it != pSubMap->end())
286  pSubMap->erase(it);
287  }
288  }
289  else
290  {
291  if (pRegMap) { delete pRegMap; impl.m_regs[nBytes].m_pContainer = NULL; }
292  if (pSubMap) { delete pSubMap; impl.m_substs[nBytes].m_pContainer = NULL; }
293  }
294  }
295  };
296 
298  static struct OdRxObjectSubstitutionActuator : public OdGsFiler::SubstitutionActuator
299  {
300  OdRxObjectSubstitutionActuator() { }
301  virtual void applySubstitution(void *pPlace, const void *pValue, SetPtrFunc pSetFunc)
302  {
303  OdGsFiler::SubstitutionActuator::applySubstitution(pPlace, pValue, pSetFunc);
304  if (pPlace)
305  (*reinterpret_cast<OdRxObjectPtr*>(pPlace))->addRef();
306  }
307  } g_rxObjectActuator;
308 
309  // Register known substitution
310  virtual void registerSubstitution(const void *pValue, const void *pSubstitution, OdUInt32 size = sizeof(OdIntPtr), bool bRegister = true, bool bImmediate = false);
311  // Register substitution request
312  virtual void requestSubstitution(void *pPlace, const void *pValue, OdUInt32 size = sizeof(OdIntPtr), bool bRegister = true, bool bImmediate = true);
313  // Register substitution request for smart pointer
314  virtual void requestSubstitution(OdBaseObjectPtr *pPlace, const void *pValue, bool bRegister = true, bool bImmediate = true);
315  // Register substitution with non-default behavior
316  virtual void requestSubstitution(void *pPlace, OdGsFiler::SubstitutionActuator *pActuator, const void *pValue, OdUInt32 size = sizeof(OdIntPtr), bool bRegister = true, bool bImmediate = true);
317  // Clear substitutions
318  virtual void clearSubstitutions(const void *pValue = NULL, OdUInt32 size = 0);
319  // Run substitutions
320  virtual void runSubstitutions(const void *pValue = NULL, OdUInt32 size = 0, bool bClear = true);
321 #undef REG_FUNCCALL
323 };
324 
325 
326 // Implement V1 version filer
328 protected:
329  enum Flags {
330  kOpenedForWrite = (1 << 0),
331 
332  kLastFlag = kOpenedForWrite
333  };
334 protected:
343  struct WrSectionData {
347  : m_type(OdGsFiler::kEOFSection)
348  , m_begin(0)
349  { }
351  : m_type(section)
352  , m_begin(tell)
353  { }
354  };
357  typedef std::map<OdString, OdRxObjectPtr> ArbDataMap;
359  typedef std::set<const void *> RegPtrsSet;
361 protected:
362  bool isWriting() const { return GETBIT(m_nFlags, kOpenedForWrite); }
363  void setWriting(bool bSet) { SETBIT(m_nFlags, kOpenedForWrite, bSet); }
364 
365  void assertWr() const { if (!isWriting()) throw OdError(eNotOpenForWrite); }
366  void assertRd() const { if (isWriting()) throw OdError(eNotOpenForRead); }
367 public:
369  : m_nSectionsToWrite(OdUInt64(-1))
370  , m_nSectionsToRead(OdUInt64(-1))
371  , m_nFlags(0)
372  , m_nVersion(kV2)
373  , m_nCurSection(kEOFSection)
374  , m_nCurSectionSize(0)
375  {
377  }
379  {
380  }
381 
385 
386  virtual bool setStream(OdStreamBuf *pStream, bool bWrite = false);
387  virtual OdStreamBufPtr getStream() const;
388 
389  virtual void setDatabase(const OdDbBaseDatabase *pDb);
390  virtual OdDbBaseDatabase *getDatabase() const;
391 
392  virtual void setVersion(OdUInt32 nVersion);
393  virtual OdUInt32 version() const;
394 
395  virtual void wrDbHash(const OdDbBaseDatabase *pDb);
396  virtual bool checkDbHash(const OdDbBaseDatabase *pDb);
397 
398  virtual void setWriteSections(OdUInt64 nSections);
399  virtual void setWriteSection(Section section, bool bSet);
400  virtual bool isWriteSection(Section section) const;
401 
402  virtual void setReadSections(OdUInt64 nSections);
403  virtual void setReadSection(Section section, bool bSet);
404  virtual bool isReadSection(Section section) const;
405 
406  virtual void wrSectionBegin(Section section);
407  virtual void wrSectionEnd(Section section);
408  virtual void wrEOFSection();
409 
410  virtual Section rdSection() const;
411  virtual Section curSection() const;
412  virtual void skipSection() const;
413  virtual void rdBackSection() const;
414  virtual bool checkEOF() const;
415 
416  virtual Substitutor *subst() const;
417  virtual void makeSubstitutions(bool bClear = true) const;
418  virtual OdRxObjectPtr getSubstitutor() const;
419  virtual void setSubstitutor(OdRxObject *pSubst);
420 
421  virtual void setArbitraryData(const OdChar *pName, OdRxObject *pObject);
422  virtual OdRxObjectPtr getArbitraryData(const OdChar *pName) const;
423  virtual bool hasArbitraryData(const OdChar *pName) const;
424  virtual void clearArbitraryData();
425 
426  virtual void registerPtr(const void *pPtr);
427  virtual void unregisterPtr(const void *pPtr);
428  virtual bool isPtrRegistered(const void *pPtr) const;
429  virtual void clearRegisteredPtrs();
430 
431  virtual void wrHandle(OdDbStub *pHandle);
432  virtual OdDbStub *rdHandle() const;
433 
434  virtual void wrClass(OdRxObject *pObj);
435  virtual OdRxObjectPtr rdClass() const;
436 
437  virtual void wrRawData(const void *pData, OdUInt32 nDataSize);
438  virtual void rdRawData(void *pData, OdUInt32 nDataSize) const;
439 };
440 
441 #include "TD_PackPop.h"
442 
443 #endif // __OdGsFilerV100Impl_H__
#define ODA_ASSERT(exp)
Definition: DebugStuff.h:49
#define GS_TOOLKIT_EXPORT
Definition: GsExport.h:37
OdSmartPtr< OdGsFilerDbLinker > OdGsFilerDbLinkerPtr
#define NULL
Definition: GsProperties.h:177
#define OD_TYPENAME
Definition: OdPlatform.h:581
unsigned int OdUInt32
int OdInt32
unsigned char OdUInt8
ptrdiff_t OdIntPtr
wchar_t OdChar
bool operator>=(const OdString &s1, const OdString &s2)
Definition: OdString.h:1351
bool operator<=(const OdString &s1, const OdString &s2)
Definition: OdString.h:1329
bool operator<(const OdString &s1, const OdString &s2)
Definition: OdString.h:1284
bool operator>(const OdString &s1, const OdString &s2)
Definition: OdString.h:1307
#define SETBIT(flags, bit, value)
Definition: OdaDefs.h:497
#define GETBIT(flags, bit)
Definition: OdaDefs.h:498
virtual OdUInt64 stubToDbHandle(OdDbStub *pStub) const =0
virtual OdDbBaseDatabase * getDatabase() const =0
virtual OdDbStub * handleToDbStub(OdUInt64 nHandle) const =0
virtual OdBinaryData getDbHash(const OdDbBaseDatabase *pDb) const
virtual OdDbBaseDatabase * getDatabase(OdDbStub *pStub) const =0
virtual ~OdGsFilerDbLinker()
virtual void wrHandle(OdDbStub *pHandle)
void assertWr() const
virtual void wrSectionEnd(Section section)
virtual void setReadSections(OdUInt64 nSections)
virtual OdDbStub * rdHandle() const
virtual OdRxObjectPtr rdClass() const
std::map< OdString, OdRxObjectPtr > ArbDataMap
virtual bool checkDbHash(const OdDbBaseDatabase *pDb)
virtual OdDbBaseDatabase * getDatabase() const
virtual bool setStream(OdStreamBuf *pStream, bool bWrite=false)
virtual void skipSection() const
void assertRd() const
virtual bool hasArbitraryData(const OdChar *pName) const
virtual bool isWriteSection(Section section) const
OdUInt32 headerTestChunk() const
virtual void unregisterPtr(const void *pPtr)
virtual void wrEOFSection()
bool isWriting() const
OdSmartPtr< OdGsFiler_SubstitutorImpl > m_subst
virtual void wrRawData(const void *pData, OdUInt32 nDataSize)
virtual Section rdSection() const
virtual OdRxObjectPtr getArbitraryData(const OdChar *pName) const
virtual void wrDbHash(const OdDbBaseDatabase *pDb)
OdGsFilerDbLinkerPtr m_pLinker
virtual void setArbitraryData(const OdChar *pName, OdRxObject *pObject)
std::set< const void * > RegPtrsSet
virtual void setWriteSection(Section section, bool bSet)
virtual void rdBackSection() const
virtual void wrClass(OdRxObject *pObj)
OdStreamBufPtr m_pStream
virtual void rdRawData(void *pData, OdUInt32 nDataSize) const
virtual OdRxObjectPtr getSubstitutor() const
virtual Section curSection() const
OdUInt64 m_nSectionsToWrite
virtual void setVersion(OdUInt32 nVersion)
virtual void setSubstitutor(OdRxObject *pSubst)
virtual void wrSectionBegin(Section section)
virtual void setDatabase(const OdDbBaseDatabase *pDb)
virtual void clearArbitraryData()
virtual void clearRegisteredPtrs()
virtual bool isPtrRegistered(const void *pPtr) const
virtual OdUInt32 version() const
virtual bool isReadSection(Section section) const
virtual void setReadSection(Section section, bool bSet)
virtual Substitutor * subst() const
virtual bool checkEOF() const
virtual void makeSubstitutions(bool bClear=true) const
virtual void registerPtr(const void *pPtr)
OdUInt64 m_nSectionsToRead
void setWriting(bool bSet)
virtual void setWriteSections(OdUInt64 nSections)
OdUInt32 m_nCurSectionSize
OdStack< WrSectionData > m_sectionStack
virtual OdStreamBufPtr getStream() const
static OdSmartPtr< TInterface > createObject()
Definition: RxObjectImpl.h:107
GLsizeiptr size
Definition: gles2_ext.h:182
bool DAI_EXPORT operator!=(const OdDAI::OdSelect &left, const OdDAI::OdSelect &right)
bool DAI_EXPORT operator==(const OdFileDescriptionAuto &left, const OdFileDescriptionAuto &right)
virtual void applySubstitution(void *pPlace, const void *pValue, SetPtrFunc pSetFunc)
Definition: GsFiler.h:105
virtual void clearSubstitutions(const void *pValue=NULL, OdUInt32 size=0)=0
static int compare(const DataTyp &t1, const DataTyp &t2)
std::map< ProcTyp, VoidArry > SubstMap
static void subst(OdGsFiler_SubstitutorImpl &impl, const void *pValue, void *pPlace, OdGsFiler::SubstitutionActuator *pAct, const void *pSubstitution, bool bClear)
std::map< ProcTyp, ProcTyp > RegMap
static void clear(OdGsFiler_SubstitutorImpl &impl, const void *pValue)
OdGsFiler_SubstitutorImpl::DataTyp< nBytes > ProcTyp
static void reg(OdGsFiler_SubstitutorImpl &impl, const void *pValue, const void *pSubstitution, bool bRegister, bool bImmediate)
static void runSubst(void *pPlace, const void *pSubstitution, OdGsFiler::SubstitutionActuator *pActuator)
static void req(OdGsFiler_SubstitutorImpl &impl, void *pPlace, OdGsFiler::SubstitutionActuator *pAct, const void *pValue, bool bRegister, bool bImmediate)
static void makeSubst(void *pPlace, const void *pSubstitution)
void add(void *pVoid, OdGsFiler::SubstitutionActuator *pAct)
OdGsFiler::SubstitutionActuator * m_pAct
std::map< OdUInt32, AbstractTyp > SubstsMap
virtual void requestSubstitution(void *pPlace, OdGsFiler::SubstitutionActuator *pActuator, const void *pValue, OdUInt32 size=sizeof(OdIntPtr), bool bRegister=true, bool bImmediate=true)
virtual void requestSubstitution(OdBaseObjectPtr *pPlace, const void *pValue, bool bRegister=true, bool bImmediate=true)
virtual void registerSubstitution(const void *pValue, const void *pSubstitution, OdUInt32 size=sizeof(OdIntPtr), bool bRegister=true, bool bImmediate=false)
virtual void runSubstitutions(const void *pValue=NULL, OdUInt32 size=0, bool bClear=true)
std::map< OdUInt32, AbstractTyp > RegsMap
virtual void requestSubstitution(void *pPlace, const void *pValue, OdUInt32 size=sizeof(OdIntPtr), bool bRegister=true, bool bImmediate=true)
virtual void clearSubstitutions(const void *pValue=NULL, OdUInt32 size=0)
static OdGsFiler::SubstitutionActuator g_defActuator
WrSectionData(OdGsFiler::Section section, OdUInt32 tell)