RavEngine
Loading...
Searching...
No Matches
SnXmlMemoryPoolStreams.h
1// Redistribution and use in source and binary forms, with or without
2// modification, are permitted provided that the following conditions
3// are met:
4// * Redistributions of source code must retain the above copyright
5// notice, this list of conditions and the following disclaimer.
6// * Redistributions in binary form must reproduce the above copyright
7// notice, this list of conditions and the following disclaimer in the
8// documentation and/or other materials provided with the distribution.
9// * Neither the name of NVIDIA CORPORATION nor the names of its
10// contributors may be used to endorse or promote products derived
11// from this software without specific prior written permission.
12//
13// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY
14// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16// PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
17// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21// OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24//
25// Copyright (c) 2008-2022 NVIDIA Corporation. All rights reserved.
26// Copyright (c) 2004-2008 AGEIA Technologies, Inc. All rights reserved.
27// Copyright (c) 2001-2004 NovodeX AG. All rights reserved.
28
29#ifndef SN_XML_MEMORY_POOL_STREAMS_H
30#define SN_XML_MEMORY_POOL_STREAMS_H
31
32#include "foundation/PxTransform.h"
33#include "foundation/PxIO.h"
34#include "SnXmlMemoryPool.h"
35
36namespace physx {
37
38 template<typename TDataType>
40 {
41 bool force_compile_error;
42 };
43
44
45#define XML_DEFINE_DEFAULT_VALUE(type, defVal ) \
46 template<> \
47 struct XmlDefaultValue<type> \
48 { \
49 type getDefaultValue() { return type(defVal); } \
50 };
51
52 XML_DEFINE_DEFAULT_VALUE(PxU8, 0)
53 XML_DEFINE_DEFAULT_VALUE(PxI8, 0)
54 XML_DEFINE_DEFAULT_VALUE(PxU16, 0)
55 XML_DEFINE_DEFAULT_VALUE(PxI16, 0)
56 XML_DEFINE_DEFAULT_VALUE(PxU32, 0)
57 XML_DEFINE_DEFAULT_VALUE(PxI32, 0)
58 XML_DEFINE_DEFAULT_VALUE(PxU64, 0)
59 XML_DEFINE_DEFAULT_VALUE(PxI64, 0)
60 XML_DEFINE_DEFAULT_VALUE(PxF32, 0)
61 XML_DEFINE_DEFAULT_VALUE(PxF64, 0)
62
63#undef XML_DEFINE_DEFAULT_VALUE
64
65 template<>
67 {
68 PxVec3 getDefaultValue() { return PxVec3( 0,0,0 ); }
69 };
70
71 template<>
73 {
74 PxTransform getDefaultValue() { return PxTransform(PxIdentity); }
75 };
76
77 template<>
79 {
80 PxQuat getDefaultValue() { return PxQuat(PxIdentity); }
81 };
82
90template<typename TAllocatorType>
92{
93 TAllocatorType* mManager;
94 mutable PxU32 mWriteOffset;
95 mutable PxU32 mReadOffset;
96 PxU8* mBuffer;
97 PxU32 mCapacity;
98
99
100 MemoryBufferBase( TAllocatorType* inManager )
101 : mManager( inManager )
102 , mWriteOffset( 0 )
103 , mReadOffset( 0 )
104 , mBuffer( NULL )
105 , mCapacity( 0 )
106 {
107 }
108 virtual ~MemoryBufferBase()
109 {
110 mManager->deallocate( mBuffer );
111 }
112 PxU8* releaseBuffer()
113 {
114 clear();
115 mCapacity = 0;
116 PxU8* retval(mBuffer);
117 mBuffer = NULL;
118 return retval;
119 }
120 void clear()
121 {
122 mWriteOffset = mReadOffset = 0;
123 }
124
125 virtual PxU32 read(void* dest, PxU32 count)
126 {
127 bool fits = ( mReadOffset + count ) <= mWriteOffset;
128 PX_ASSERT( fits );
129 if ( fits )
130 {
131 PxMemCopy( dest, mBuffer + mReadOffset, count );
132 mReadOffset += count;
133 return count;
134 }
135 return 0;
136 }
137
138 inline void checkCapacity( PxU32 inNewCapacity )
139 {
140 if ( mCapacity < inNewCapacity )
141 {
142 PxU32 newCapacity = 32;
143 while( newCapacity < inNewCapacity )
144 newCapacity = newCapacity << 1;
145
146 PxU8* newData( mManager->allocate( newCapacity ) );
147 if ( mWriteOffset )
148 PxMemCopy( newData, mBuffer, mWriteOffset );
149 mManager->deallocate( mBuffer );
150 mBuffer = newData;
151 mCapacity = newCapacity;
152 }
153 }
154
155 virtual PxU32 write(const void* src, PxU32 count)
156 {
157 checkCapacity( mWriteOffset + count );
158 PxMemCopy( mBuffer + mWriteOffset, src, count );
159 mWriteOffset += count;
160 return count;
161 }
162};
163
164class MemoryBuffer : public MemoryBufferBase<CMemoryPoolManager >
165{
166public:
168};
169
170}
171
172#endif
Definition SnXmlMemoryPool.h:246
Definition SnXmlMemoryPoolStreams.h:165
Input stream class for I/O.
Definition PxIO.h:50
Output stream class for I/O.
Definition PxIO.h:114
This is a quaternion class. For more information on quaternion mathematics consult a mathematics sour...
Definition PxQuat.h:50
class representing a rigid euclidean transform as a quaternion and a vector
Definition PxTransform.h:49
3 Element vector class.
Definition PxVec3.h:50
Sorts an array of objects in ascending order, assuming that the predicate implements the < operator:
Definition PxBoxController.h:39
PX_FORCE_INLINE void * PxMemCopy(void *dest, const void *src, PxU32 count)
Copies the bytes of one memory block to another. The memory blocks must not overlap.
Definition PxMemory.h:83
Definition SnXmlMemoryPoolStreams.h:92
virtual PxU32 read(void *dest, PxU32 count)
read from the stream. The number of bytes read may be less than the number requested.
Definition SnXmlMemoryPoolStreams.h:125
virtual PxU32 write(const void *src, PxU32 count)
write to the stream. The number of bytes written may be less than the number sent.
Definition SnXmlMemoryPoolStreams.h:155
Definition SnXmlMemoryPoolStreams.h:40