CFx SDK Documentation  2023 SP0
FlatMemStream.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 
25 
26 
27 // FlatMemStream.h: interface for the OdMemoryStream class.
28 //
30 
31 #if !defined(_ODFLATMEMSTREAM_H_INCLUDED_)
32 #define _ODFLATMEMSTREAM_H_INCLUDED_
33 
34 #include "TD_PackPush.h"
35 
36 #include "OdStreamBuf.h"
37 
38 class OdFlatMemStream;
43 
51 
52 template <class TBase = OdEmptyBaseClass>
53 class OdStreamPosImpl : public TBase {
54 protected:
57 public:
58  OdUInt64 left() { return m_nEndPos - m_nCurPos; }
59  OdUInt64 length() { return m_nEndPos; }
60  OdUInt64 tell() { return m_nCurPos; }
62  bool isEof() { return left()==0; }
63 };
64 
70 template <class TBase = OdEmptyBaseClass>
71 class OdFlatMemStreamImpl : public OdStreamPosImpl<TBase> {
72 protected:
75  void* m_pMemData;
76 public:
80  OdUInt8* data() { return (OdUInt8*)m_pMemData; }
81 
83 protected:
90  virtual void append(OdUInt64 numBytes) { throw OdError(eNotOpenForWrite); }
91 
96 public:
97  inline OdFlatMemStreamImpl() { init(0, 0, 0); }
98 
99  OdFlatMemStreamImpl(void* buffer, OdUInt64 numBytes, OdUInt64 nCurPos)
100  { init(buffer, numBytes, nCurPos); }
101 
108  inline void init(void* buffer, OdUInt64 numBytes, OdUInt64 curPosition = 0);
109 
111  void getBytes(void* buffer, OdUInt32 numBytes);
112 
113  // overrides existing byte(s)
115  void putBytes(const void* buffer, OdUInt32 numBytes);
116 };
117 
118 
125 {
126 protected:
127  inline OdFlatMemStream() {}
128 public:
136  static OdFlatMemStreamPtr createNew(void* buffer, OdUInt64 numBytes, OdUInt64 curPosition = 0);
137 
138  void copyDataTo(OdStreamBuf* pDestination, OdUInt64 sourceStart, OdUInt64 sourceEnd);
139 };
140 
141 
148 {
149  void init(OdUInt64 numBytes) { OdFlatMemStream::init(odrxAlloc((size_t)numBytes), numBytes); }
150 public:
159 };
160 
161 
162 template<class TBase>
163 inline void OdFlatMemStreamImpl<TBase>::init(void* pMemData, OdUInt64 nSize, OdUInt64 nCurPos) {
164  m_pMemData = pMemData;
165  m_nEndPos = nCurPos + nSize;
166  m_nCurPos = nCurPos;
167 }
168 
169 
170 template<class TBase>
172  OdUInt64 nNewPos;
173  switch(whence) {
174  case OdDb::kSeekFromEnd:
175  nNewPos = m_nEndPos + offset;
176  break;
178  nNewPos = m_nCurPos + offset;
179  break;
181  nNewPos = offset;
182  break;
183  default:
184  throw OdError(eInvalidInput);
185  break;
186  };
187  if(nNewPos > m_nEndPos)
188  throw OdError(eEndOfFile);
189  return (m_nCurPos = nNewPos);
190 }
191 
192 template<class TBase>
194  if(left()==0)
195  throw OdError(eEndOfFile);
196  return *(data() + m_nCurPos++);
197 }
198 
199 template<class TBase>
201  if(left() < nLen)
202  throw OdError(eEndOfFile);
203  ::memcpy(buffer, data()+m_nCurPos, nLen);
204  m_nCurPos += nLen;
205 }
206 
207 template<class TBase>
209  if(m_nCurPos < m_nEndPos)
210  *(data() + m_nCurPos++) = val;
211  else
212  throw OdError(eEndOfFile);
213 }
214 
215 template<class TBase>
216 inline void OdFlatMemStreamImpl<TBase>::putBytes(const void* buffer, OdUInt32 nLen) {
217  if(m_nCurPos + nLen <= m_nEndPos) {
218  ::memcpy(data()+m_nCurPos, buffer, nLen);
219  m_nCurPos += nLen;
220  } else {
221  throw OdError(eEndOfFile);
222  }
223 }
224 
225 
226 #include "TD_PackPop.h"
227 
228 #endif // !defined(_ODFLATMEMSTREAM_H_INCLUDED_)
229 
OdSmartPtr< OdFlatMemStream > OdFlatMemStreamPtr
Definition: FlatMemStream.h:38
ALLOCDLL_EXPORT void * odrxAlloc(size_t nBytes)
ALLOCDLL_EXPORT void odrxFree(void *pMemBlock)
unsigned int OdUInt32
unsigned char OdUInt8
#define FIRSTDLL_EXPORT
Definition: RootExport.h:39
ODRX_DECLARE_MEMBERS(OdFlatMemStream)
static OdFlatMemStreamPtr createNew(void *buffer, OdUInt64 numBytes, OdUInt64 curPosition=0)
void copyDataTo(OdStreamBuf *pDestination, OdUInt64 sourceStart, OdUInt64 sourceEnd)
OdFlatMemStreamImpl(void *buffer, OdUInt64 numBytes, OdUInt64 nCurPos)
Definition: FlatMemStream.h:99
void putByte(OdUInt8 value)
void putBytes(const void *buffer, OdUInt32 numBytes)
virtual void append(OdUInt64 numBytes)
Definition: FlatMemStream.h:90
void init(void *buffer, OdUInt64 numBytes, OdUInt64 curPosition=0)
void getBytes(void *buffer, OdUInt32 numBytes)
static OdFlatMemStreamPtr createNew(OdUInt64 numBytes)
Definition: Int64.h:43
OdUInt64 seek(OdInt64 offset, OdDb::FilerSeekType seekType)
OdUInt64 left()
Definition: FlatMemStream.h:58
OdUInt64 m_nCurPos
Definition: FlatMemStream.h:56
OdUInt64 length()
Definition: FlatMemStream.h:59
OdUInt64 m_nEndPos
Definition: FlatMemStream.h:55
OdUInt64 tell()
Definition: FlatMemStream.h:60
GLuint buffer
Definition: gles2_ext.h:178
GLint GLenum GLsizei GLsizei GLint GLsizei const void * data
Definition: gles2_ext.h:110
GLintptr offset
Definition: gles2_ext.h:183
GLsizei const GLfloat * value
Definition: gles2_ext.h:302
FilerSeekType
Definition: OdStreamBuf.h:49
@ kSeekFromCurrent
Definition: OdStreamBuf.h:51
@ kSeekFromStart
Definition: OdStreamBuf.h:50
@ kSeekFromEnd
Definition: OdStreamBuf.h:52