CFx SDK Documentation 2024 SP0
Loading...
Searching...
No Matches
OdPlatformStreamer.h
Go to the documentation of this file.
1
2// Copyright (C) 2002-2022, 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 Open Design Alliance software pursuant to a license
16// agreement with Open Design Alliance.
17// Open Design Alliance Copyright (C) 2002-2022 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#ifndef _OD_PLATFORMSTREAMER_H_
26#define _OD_PLATFORMSTREAMER_H_
27
28#include "OdPlatform.h"
29#include "OdStreamBuf.h"
30#include "OdString.h"
31
37{
38public:
43 static OdInt16 rdInt16 (OdStreamBuf& streamBuf);
48 static OdUInt16 rdUInt16 (OdStreamBuf& streamBuf);
53 static OdInt32 rdInt32 (OdStreamBuf& streamBuf);
58 static OdInt64 rdInt64 (OdStreamBuf& streamBuf);
63 static float rdFloat (OdStreamBuf& streamBuf);
68 static double rdDouble (OdStreamBuf& streamBuf);
69
75 static void rd2Doubles(OdStreamBuf& streamBuf, void* doubles);
76
82 static void rd3Doubles(OdStreamBuf& streamBuf, void* doubles);
83
90 static void rdDoubles (OdStreamBuf& streamBuf, int numDoubles, void* doubles);
91
97 static void wrInt16 (OdStreamBuf& streamBuf, OdInt16 value);
103 static void wrUInt16 (OdStreamBuf& streamBuf, OdUInt16 value);
109 static void wrInt32 (OdStreamBuf& streamBuf, OdInt32 value);
115 static void wrInt64 (OdStreamBuf& streamBuf, OdInt64 value);
121 static void wrFloat (OdStreamBuf& out, float val);
127 static void wrDouble (OdStreamBuf& streamBuf, double value);
133 static void wr2Doubles(OdStreamBuf& streamBuf, const void* doubles);
139 static void wr3Doubles(OdStreamBuf& streamBuf, const void* doubles);
146 static void wrDoubles (OdStreamBuf& streamBuf, int numDoubles, const void* doubles);
147
161
173 inline static void putUnicodeStrToBuffer(const OdString& val, OdUInt8*& buffer);
174
178 inline static void wrString(OdStreamBuf& streamBuf, const OdString& val);
179
183 inline static OdString rdString(OdStreamBuf& streamBuf);
184};
185
187{
188 OdInt16 res;
189 streamBuf.getBytes(&res, sizeof(res));
191 return res;
192}
194{
195 OdUInt16 res;
196 streamBuf.getBytes(&res, sizeof(res));
198 return res;
199}
201{
202 OdInt32 res;
203 streamBuf.getBytes(&res, sizeof(res));
205 return res;
206}
208{
209 OdInt64 res;
210 streamBuf.getBytes(&res, sizeof(res));
211 odSwap8Bytes(&res);
212 return res;
213}
215{
216 float res;
217 streamBuf.getBytes(&res, sizeof(res));
218 odSwap4Bytes(&res);
219 return res;
220}
222{
223 double res;
224 ODA_ASSUME(sizeof(res) == 8)
225 streamBuf.getBytes(&res, 8);
226 odSwap8Bytes(&res);
227 // if unnormalized or NaN or infinity, set it to 0.0
228 if (!isValidNonZeroIEEEDouble((OdUInt8 *)&res))
229 return 0.0;
230 return res;
231}
232
233inline void OdPlatformStreamer::rd2Doubles(OdStreamBuf& streamBuf, void* pRes2Doubles)
234{
235 ODA_ASSUME(sizeof(double) == 8)
236 streamBuf.getBytes(pRes2Doubles, sizeof(double)*2);
237 fixDouble((double*)pRes2Doubles);
238 fixDouble((double*)pRes2Doubles+1);
239}
240inline void OdPlatformStreamer::rd3Doubles(OdStreamBuf& streamBuf, void* pRes3Doubles)
241{
242 ODA_ASSUME(sizeof(double) == 8)
243 streamBuf.getBytes(pRes3Doubles, sizeof(double)*3);
244 fixDouble((double*)pRes3Doubles);
245 fixDouble((double*)pRes3Doubles+1);
246 fixDouble((double*)pRes3Doubles+2);
247}
248inline void OdPlatformStreamer::rdDoubles(OdStreamBuf& streamBuf, int n, void* pResDoubles)
249{
250 ODA_ASSUME(sizeof(double) == 8)
251 streamBuf.getBytes(pResDoubles, sizeof(double)*n);
252 double* pD = (double*)pResDoubles;
253 while (n--) { fixDouble(pD++); }
254}
255
257{
259 streamBuf.putBytes(&val, sizeof(val));
260}
262{
264 streamBuf.putBytes(&val, sizeof(val));
265}
267{
269 streamBuf.putBytes(&val, sizeof(val));
270}
272{
273 odSwap8Bytes(&val);
274 streamBuf.putBytes(&val, sizeof(val));
275}
276inline void OdPlatformStreamer::wrFloat(OdStreamBuf& streamBuf, float val)
277{
278 odSwap4Bytes(&val);
279 streamBuf.putBytes(&val, sizeof(val));
280}
281inline void OdPlatformStreamer::wrDouble(OdStreamBuf& streamBuf, double val)
282{
283 ODA_ASSUME(sizeof(double) == 8)
284 odSwap8Bytes(&val);
285 streamBuf.putBytes(&val, sizeof(val));
286}
287
289{
290 OdString ret;
291#if ODCHAR_IS_INT16LE
292 if (size == -1)
293 {
294 // null terminated
295 ret = (OdChar*)buffer;
296 buffer += ((ret.getLength() + 1) * 2);
297 }
298 else
299 {
300 OdUInt8* pBuf = (OdUInt8*)ret.getBuffer(size+1);
301 OD_BYTES_FROM_BUFFPTR(buffer, pBuf, (((size_t)size)<<1));
302 ret.releaseBuffer(size);
303 }
304#else
305 OdInt32 finalSize = size;
306 if (size == -1 && buffer)
307 {
308 // null terminated
309 size = 1;
310 OdUInt16* tmp16 = (OdUInt16*)buffer;
311 for ( ; *tmp16; tmp16++)
312 size++;
313 finalSize = size - 1;
314 }
315 OdChar* pBuf = ret.getBuffer(size + 1);
316 for (OdInt32 i = 0; i < size; i++)
317 {
319 }
320 *pBuf = L'\0';
321 ret.releaseBuffer(finalSize);
322#endif
323
324 return ret;
325}
326
327#ifndef ODCHAR_IS_INT16LE
328#error "OdaDefs.h must be included before OdPlatformStreamer.h"
329#endif
330
332{
333 OdInt32 nLen = val.getLength();
334#if ODCHAR_IS_INT16LE
335 OD_BYTES_TO_BUFFPTR(buffer, val.c_str(), nLen*sizeof(OdChar));
336#else
337 const OdChar* p = val.c_str();
338 for (OdInt32 i = 0; i < nLen; i++)
339 {
341 }
342#endif
343}
344
346{
347 OdInt32 nLen = val.getLength();
348 wrInt32(streamBuf, nLen);
349#if ODCHAR_IS_INT16LE
350 streamBuf.putBytes(val.c_str(), nLen * 2);
351#else
352 const OdChar* p = val.c_str();
353 for (OdInt32 i = 0; i < nLen; ++i)
354 wrUInt16(streamBuf, (OdUInt16)*p++);
355#endif
356}
357
359{
360 OdString ret;
361 OdInt32 nLen = rdInt32(streamBuf);
362 OdChar* pBuf = ret.getBufferSetLength(nLen);
363#if ODCHAR_IS_INT16LE
364 streamBuf.getBytes(pBuf, nLen * 2);
365#else
366 for (OdInt32 i = 0; i < nLen; ++i)
367 *pBuf++ = rdUInt16(streamBuf);
368#endif
369 return ret;
370}
371
372
373// Verify that preprocessor symbol ODCHAR_IS_INT16LE has been correctly defined
374#if ODCHAR_IS_INT16LE
375#ifdef ODA_BIGENDIAN
376#error "ODCHAR_IS_INT16LE is defined for a big endian platform"
377#endif
378ODA_ASSUME(sizeof(OdChar) == 2);
379#endif
380
381
382#ifdef ODA_BIGENDIAN
383
384inline void OdPlatformStreamer::wr2Doubles(OdStreamBuf& streamBuf, const void* p2Doubles)
385{
386 wrDouble(streamBuf, *(((double*)p2Doubles)+0));
387 wrDouble(streamBuf, *(((double*)p2Doubles)+1));
388}
389inline void OdPlatformStreamer::wr3Doubles(OdStreamBuf& streamBuf, const void* p3Doubles)
390{
391 wrDouble(streamBuf, *(((double*)p3Doubles)+0));
392 wrDouble(streamBuf, *(((double*)p3Doubles)+1));
393 wrDouble(streamBuf, *(((double*)p3Doubles)+2));
394}
395inline void OdPlatformStreamer::wrDoubles(OdStreamBuf& streamBuf, int n, const void* pDoubles)
396{
397 const double* pD = (const double*)pDoubles;
398 while (n--)
399 {
400 wrDouble(streamBuf, *pD);
401 pD++;
402 }
403}
404
405#else // #ifdef ODA_BIGENDIAN
406
407inline void OdPlatformStreamer::wr2Doubles(OdStreamBuf& streamBuf, const void* p2Doubles)
408{
409 ODA_ASSUME(sizeof(double) == 8)
410 streamBuf.putBytes(p2Doubles, sizeof(double)*2);
411}
412inline void OdPlatformStreamer::wr3Doubles(OdStreamBuf& streamBuf, const void* p3Doubles)
413{
414 ODA_ASSUME(sizeof(double) == 8)
415 streamBuf.putBytes(p3Doubles, sizeof(double)*3);
416}
417inline void OdPlatformStreamer::wrDoubles(OdStreamBuf& streamBuf, int n, const void* pDoubles)
418{
419 ODA_ASSUME(sizeof(double) == 8)
420 streamBuf.putBytes(pDoubles, sizeof(double) * n);
421}
422
423#endif // ODA_BIGENDIAN
424
425#endif // _OD_PLATFORMSTREAMER_H_
426
#define ODA_ASSUME(expr)
Definition: DebugStuff.h:180
#define OD_INT16_TO_BUFFPTR(pBuffPtr, val)
Definition: OdPlatform.h:861
#define odSwap4BytesNumber(n)
Definition: OdPlatform.h:730
bool isValidNonZeroIEEEDouble(const OdUInt8 *buf)
Definition: OdPlatform.h:748
#define odSwap4Bytes(bytes)
Definition: OdPlatform.h:732
#define OD_BYTES_TO_BUFFPTR(pBuffPtr, FromBuff, nCount)
Definition: OdPlatform.h:799
void fixDouble(double *pD)
Definition: OdPlatform.h:764
#define OD_INT16_FROM_BUFFPTR(pBuffPtr)
Definition: OdPlatform.h:854
#define odSwap8Bytes(bytes)
Definition: OdPlatform.h:731
#define OD_BYTES_FROM_BUFFPTR(pBuffPtr, ResBuff, nCount)
Definition: OdPlatform.h:796
#define odSwap2BytesNumber(n)
Definition: OdPlatform.h:729
short OdInt16
unsigned short OdUInt16
int OdInt32
unsigned char OdUInt8
wchar_t OdChar
Definition: Int64.h:43
static void rd3Doubles(OdStreamBuf &streamBuf, void *doubles)
static OdString getUnicodeStrFromBuffer(const OdUInt8 *&buffer, OdInt32 size)
static OdInt64 rdInt64(OdStreamBuf &streamBuf)
static OdUInt16 rdUInt16(OdStreamBuf &streamBuf)
static void wrUInt16(OdStreamBuf &streamBuf, OdUInt16 value)
static OdString rdString(OdStreamBuf &streamBuf)
static OdInt16 rdInt16(OdStreamBuf &streamBuf)
static float rdFloat(OdStreamBuf &streamBuf)
static void wr2Doubles(OdStreamBuf &streamBuf, const void *doubles)
static void wrInt16(OdStreamBuf &streamBuf, OdInt16 value)
static void wrFloat(OdStreamBuf &out, float val)
static OdInt32 rdInt32(OdStreamBuf &streamBuf)
static void wrDoubles(OdStreamBuf &streamBuf, int numDoubles, const void *doubles)
static void rd2Doubles(OdStreamBuf &streamBuf, void *doubles)
static double rdDouble(OdStreamBuf &streamBuf)
static void wrInt64(OdStreamBuf &streamBuf, OdInt64 value)
static void wr3Doubles(OdStreamBuf &streamBuf, const void *doubles)
static void wrString(OdStreamBuf &streamBuf, const OdString &val)
static void rdDoubles(OdStreamBuf &streamBuf, int numDoubles, void *doubles)
static void wrDouble(OdStreamBuf &streamBuf, double value)
static void wrInt32(OdStreamBuf &streamBuf, OdInt32 value)
static void putUnicodeStrToBuffer(const OdString &val, OdUInt8 *&buffer)
virtual void getBytes(void *buffer, OdUInt32 numBytes)
virtual void putBytes(const void *buffer, OdUInt32 numBytes)
OdChar * getBufferSetLength(int length)
OdChar * getBuffer(int minBufLength)
const OdChar * c_str() const
Definition: OdString.h:203
void releaseBuffer(int newLength=-1)
int getLength() const
Definition: OdString.h:133
GLuint buffer
Definition: gles2_ext.h:178
GLsizeiptr size
Definition: gles2_ext.h:182
GLsizei const GLfloat * value
Definition: gles2_ext.h:302