RavEngine
Loading...
Searching...
No Matches
SnXmlImpl.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_IMPL_H
30#define SN_XML_IMPL_H
31
32#include "SnXmlMemoryPool.h"
33#include "foundation/PxString.h"
34#include "foundation/PxMemory.h"
35
36namespace physx { namespace Sn {
37
38typedef CMemoryPoolManager TMemoryPoolManager;
39
40namespace snXmlImpl {
41
42 inline PxU32 strLen( const char* inStr )
43 {
44 PxU32 len = 0;
45 if ( inStr )
46 {
47 while ( *inStr )
48 {
49 ++len;
50 ++inStr;
51 }
52 }
53 return len;
54 }
55}
56 inline const char* copyStr( PxAllocatorCallback& inAllocator, const char* inStr )
57 {
58 if ( inStr && *inStr )
59 {
60 PxU32 theLen = snXmlImpl::strLen( inStr );
61 //The memory will never be released by repx. If you want it released, you need to pass in a custom allocator
62 //that tracks all allocations and releases unreleased allocations yourself.
63 char* dest = reinterpret_cast<char* >( inAllocator.allocate( theLen + 1, "Repx::const char*", __FILE__, __LINE__ ) );
64 PxMemCopy( dest, inStr, theLen );
65 dest[theLen] = 0;
66 return dest;
67 }
68 return "";
69 }
70
71 template<typename TManagerType>
72 inline const char* copyStr( TManagerType* inMgr, const char* inStr )
73 {
74 if ( inStr && *inStr )
75 {
76 PxU32 theLen = snXmlImpl::strLen( inStr );
77 char* dest = reinterpret_cast<char* >( inMgr->allocate( theLen + 1 ) );
78 PxMemCopy( dest, inStr, theLen );
79 dest[theLen] = 0;
80 return dest;
81 }
82 return "";
83 }
84
85 inline void releaseStr( TMemoryPoolManager* inMgr, const char* inStr, PxU32 )
86 {
87 if ( inStr && *inStr )
88 {
89 inMgr->deallocate( reinterpret_cast< PxU8* >( const_cast<char*>( inStr ) ) );
90 }
91 }
92
93 inline void releaseStr( TMemoryPoolManager* inMgr, const char* inStr )
94 {
95 if ( inStr && *inStr )
96 {
97 PxU32 theLen = snXmlImpl::strLen( inStr );
98 releaseStr( inMgr, inStr, theLen );
99 }
100 }
101
102 struct XmlNode
103 {
104 const char* mName; //Never released until all collections are released
105 const char* mData; //Never released until all collections are released
106
107 XmlNode* mNextSibling;
108 XmlNode* mPreviousSibling;
109 XmlNode* mFirstChild;
110 XmlNode* mParent;
111 XmlNode( const XmlNode& );
112 XmlNode& operator=( const XmlNode& );
113
114 PX_INLINE void initPtrs()
115 {
116 mNextSibling = NULL;
117 mPreviousSibling = NULL;
118 mFirstChild = NULL;
119 mParent = NULL;
120 }
121
122 PX_INLINE XmlNode( const char* inName = "", const char* inData = "" )
123 : mName( inName )
124 , mData( inData )
125 { initPtrs(); }
126
127 void addChild( XmlNode* inItem )
128 {
129 inItem->mParent = this;
130 if ( mFirstChild == NULL )
131 mFirstChild = inItem;
132 else
133 {
134 XmlNode* theNode = mFirstChild;
135 //Follow the chain till the end.
136 while( theNode->mNextSibling != NULL )
137 theNode = theNode->mNextSibling;
138 theNode->mNextSibling = inItem;
139 inItem->mPreviousSibling = theNode;
140 }
141 }
142
143 PX_INLINE XmlNode* findChildByName( const char* inName )
144 {
145 for ( XmlNode* theNode = mFirstChild; theNode; theNode = theNode->mNextSibling )
146 {
147 XmlNode* theRepXNode = theNode;
148 if ( physx::Pxstricmp( theRepXNode->mName, inName ) == 0 )
149 return theNode;
150 }
151 return NULL;
152 }
153
154 PX_INLINE void orphan()
155 {
156 if ( mParent )
157 {
158 if ( mParent->mFirstChild == this )
159 mParent->mFirstChild = mNextSibling;
160 }
161 if ( mPreviousSibling )
162 mPreviousSibling->mNextSibling = mNextSibling;
163 if ( mNextSibling )
164 mNextSibling->mPreviousSibling = mPreviousSibling;
165 if ( mFirstChild )
166 mFirstChild->mParent = NULL;
167 initPtrs();
168 }
169 };
170
171 inline XmlNode* allocateRepXNode( TMemoryPoolManager* inManager, const char* inName, const char* inData )
172 {
173 XmlNode* retval = inManager->allocate<XmlNode>();
174 retval->mName = copyStr( inManager, inName );
175 retval->mData = copyStr( inManager, inData );
176 return retval;
177 }
178
179 inline void release( TMemoryPoolManager* inManager, XmlNode* inNode )
180 {
181 //We *don't* release the strings associated with the node
182 //because they could be shared. Instead, we just let them 'leak'
183 //in some sense, at least until the memory manager itself is deleted.
184 //DO NOT UNCOMMENT THE LINES BELOW!!
185 //releaseStr( inManager, inNode->mName );
186 //releaseStr( inManager, inNode->mData );
187 inManager->deallocate( inNode );
188 }
189
190 static PX_INLINE void releaseNodeAndChildren( TMemoryPoolManager* inManager, XmlNode* inNode )
191 {
192 if ( inNode->mFirstChild )
193 {
194 XmlNode* childNode( inNode->mFirstChild );
195 while( childNode )
196 {
197 XmlNode* _node( childNode );
198 childNode = _node->mNextSibling;
199 releaseNodeAndChildren( inManager, _node );
200 }
201 }
202 inNode->orphan();
203 release( inManager, inNode );
204 }
205
206 static XmlNode* copyRepXNodeAndSiblings( TMemoryPoolManager* inManager, const XmlNode* inNode, XmlNode* inParent );
207
208 static XmlNode* copyRepXNode( TMemoryPoolManager* inManager, const XmlNode* inNode, XmlNode* inParent = NULL )
209 {
210 XmlNode* newNode( allocateRepXNode( inManager, NULL, NULL ) );
211 newNode->mName = inNode->mName; //Some light structural sharing
212 newNode->mData = inNode->mData; //Some light structural sharing
213 newNode->mParent = inParent;
214 if ( inNode->mFirstChild )
215 newNode->mFirstChild = copyRepXNodeAndSiblings( inManager, inNode->mFirstChild, newNode );
216 return newNode;
217 }
218
219 static XmlNode* copyRepXNodeAndSiblings( TMemoryPoolManager* inManager, const XmlNode* inNode, XmlNode* inParent )
220 {
221 XmlNode* sibling = inNode->mNextSibling;
222 if ( sibling ) sibling = copyRepXNodeAndSiblings( inManager, sibling, inParent );
223 XmlNode* newNode = copyRepXNode( inManager, inNode, inParent );
224 newNode->mNextSibling = sibling;
225 if ( sibling ) sibling->mPreviousSibling = newNode;
226 return newNode;
227 }
228
229 inline bool isBigEndian() { int i = 1; return *(reinterpret_cast<char*>(&i))==0; }
230
231
233 {
234 const char* mName;
235 bool mOpen;
236 NameStackEntry( const char* nm ) : mName( nm ), mOpen( false ) {}
237 };
238
240} }
241
242
243#endif
Definition SnXmlMemoryPool.h:246
#define PX_INLINE
Definition PxPreprocessor.h:320
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 SnXmlImpl.h:233
Definition SnXmlImpl.h:103
Helper struct to encapsulate the array.
Definition PxProfileAllocatorWrapper.h:152