CFx SDK Documentation  2020SP3
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 
FIRSTDLL_EXPORT
#define FIRSTDLL_EXPORT
Definition: RootExport.h:39
OdFlatMemStreamImpl
Definition: FlatMemStream.h:71
OdDb::kSeekFromCurrent
@ kSeekFromCurrent
Definition: OdStreamBuf.h:51
OdUInt8
unsigned char OdUInt8
Definition: OdPlatformSettings.h:759
OdInt64
Definition: Int64.h:43
OdFlatMemStreamPtr
OdSmartPtr< OdFlatMemStream > OdFlatMemStreamPtr
Definition: FlatMemStream.h:38
buffer
GLuint buffer
Definition: gles2_ext.h:178
OdFlatMemStreamImpl::append
virtual void append(OdUInt64 numBytes)
Definition: FlatMemStream.h:90
OdFlatMemStreamImpl::getBytes
void getBytes(void *buffer, OdUInt32 numBytes)
Definition: FlatMemStream.h:200
OdEmptyBaseClass
Definition: FlatMemStream.h:50
OdFlatMemStreamManaged::createNew
static OdFlatMemStreamPtr createNew(OdUInt64 numBytes)
OdFlatMemStreamManaged
Definition: FlatMemStream.h:148
OdFlatMemStreamImpl::m_pMemData
void * m_pMemData
Definition: FlatMemStream.h:75
OdFlatMemStreamManaged::~OdFlatMemStreamManaged
~OdFlatMemStreamManaged()
Definition: FlatMemStream.h:151
TD_PackPop.h
OdDb::FilerSeekType
FilerSeekType
Definition: OdStreamBuf.h:49
OdUInt32
unsigned int OdUInt32
Definition: OdPlatformSettings.h:783
data
GLint GLenum GLsizei GLsizei GLint GLsizei const void * data
Definition: gles2_ext.h:110
odrxFree
ALLOCDLL_EXPORT void odrxFree(void *pMemBlock)
OdFlatMemStreamImpl::getByte
OdUInt8 getByte()
Definition: FlatMemStream.h:193
OdFlatMemStream::OdFlatMemStream
OdFlatMemStream()
Definition: FlatMemStream.h:127
OdFlatMemStream
Definition: FlatMemStream.h:125
OdStreamBuf.h
OdSmartPtr
Definition: SmartPtr.h:58
OdFlatMemStreamImpl::OdFlatMemStreamImpl
OdFlatMemStreamImpl(void *buffer, OdUInt64 numBytes, OdUInt64 nCurPos)
Definition: FlatMemStream.h:99
OdDb::kSeekFromStart
@ kSeekFromStart
Definition: OdStreamBuf.h:50
offset
GLintptr offset
Definition: gles2_ext.h:183
OdStreamPosImpl::m_nEndPos
OdUInt64 m_nEndPos
Definition: FlatMemStream.h:55
OdFlatMemStream::createNew
static OdFlatMemStreamPtr createNew(void *buffer, OdUInt64 numBytes, OdUInt64 curPosition=0)
OdStreamPosImpl
Definition: FlatMemStream.h:53
OdStreamPosImpl::seek
OdUInt64 seek(OdInt64 offset, OdDb::FilerSeekType seekType)
Definition: FlatMemStream.h:171
OdFlatMemStreamImpl::OdFlatMemStreamImpl
OdFlatMemStreamImpl()
Definition: FlatMemStream.h:97
TD_PackPush.h
OdStreamPosImpl::isEof
bool isEof()
Definition: FlatMemStream.h:62
OdDb::kSeekFromEnd
@ kSeekFromEnd
Definition: OdStreamBuf.h:52
OdFlatMemStream::ODRX_DECLARE_MEMBERS
ODRX_DECLARE_MEMBERS(OdFlatMemStream)
odrxAlloc
ALLOCDLL_EXPORT void * odrxAlloc(size_t nBytes)
OdFlatMemStream::copyDataTo
void copyDataTo(OdStreamBuf *pDestination, OdUInt64 sourceStart, OdUInt64 sourceEnd)
OdStreamBuf
Definition: OdStreamBuf.h:67
OdStreamPosImpl::m_nCurPos
OdUInt64 m_nCurPos
Definition: FlatMemStream.h:56
OdFlatMemStreamImpl::init
void init(void *buffer, OdUInt64 numBytes, OdUInt64 curPosition=0)
Definition: FlatMemStream.h:163
OdError
Definition: OdError.h:43
value
GLsizei const GLfloat * value
Definition: gles2_ext.h:302
OdFlatMemStreamImpl::putBytes
void putBytes(const void *buffer, OdUInt32 numBytes)
Definition: FlatMemStream.h:216
OdFlatMemStreamImpl::putByte
void putByte(OdUInt8 value)
Definition: FlatMemStream.h:208
OdStreamPosImpl::tell
OdUInt64 tell()
Definition: FlatMemStream.h:60
OdStreamPosImpl::length
OdUInt64 length()
Definition: FlatMemStream.h:59
OdStreamPosImpl::left
OdUInt64 left()
Definition: FlatMemStream.h:58
OdFlatMemStreamImpl::data
OdUInt8 * data()
Definition: FlatMemStream.h:80
OdUInt64
Definition: Int64.h:137