RavEngine
Loading...
Searching...
No Matches
SnXmlSimpleXmlWriter.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_SIMPLE_XML_WRITER_H
30#define SN_XML_SIMPLE_XML_WRITER_H
31
32#include "foundation/PxArray.h"
33#include "SnXmlMemoryPoolStreams.h"
34
35namespace physx { namespace Sn {
37 {
38 public:
39
41 {
43 TXmlWriterType& mWriter;
44 STagWatcher( const STagWatcher& inOther );
45 STagWatcher& operator-( const STagWatcher& inOther );
46 STagWatcher( TXmlWriterType& inWriter, const char* inTagName )
47 : mWriter( inWriter )
48 {
49 mWriter.beginTag( inTagName );
50 }
51 ~STagWatcher() { mWriter.endTag(); }
52 };
53
54 virtual ~XmlWriter(){}
55 virtual void beginTag( const char* inTagname ) = 0;
56 virtual void endTag() = 0;
57 virtual void addAttribute( const char* inName, const char* inValue ) = 0;
58 virtual void writeContentTag( const char* inTag, const char* inContent ) = 0;
59 virtual void addContent( const char* inContent ) = 0;
60 virtual PxU32 tabCount() = 0;
61 };
62
63 template<typename TStreamType>
64 class XmlWriterImpl : public XmlWriter
65 {
67 TStreamType& mStream;
68 XmlWriterImpl( const XmlWriterImpl& inOther );
69 XmlWriterImpl& operator=( const XmlWriterImpl& inOther );
71 bool mTagOpen;
72 PxU32 mInitialTagDepth;
73 public:
74
75 XmlWriterImpl( TStreamType& inStream, PxAllocatorCallback& inAllocator, PxU32 inInitialTagDepth = 0 )
76 : mWrapper( inAllocator )
77 , mStream( inStream )
78 , mTags( mWrapper )
79 , mTagOpen( false )
80 , mInitialTagDepth( inInitialTagDepth )
81 {
82 }
83 virtual ~XmlWriterImpl()
84 {
85 while( mTags.size() )
86 endTag();
87 }
88 PxU32 tabCount() { return mTags.size() + mInitialTagDepth; }
89
90 void writeTabs( PxU32 inSize )
91 {
92 inSize += mInitialTagDepth;
93 for ( PxU32 idx =0; idx < inSize; ++idx )
94 mStream << "\t";
95 }
96 void beginTag( const char* inTagname )
97 {
98 closeTag();
99 writeTabs(mTags.size());
100 mTags.pushBack( inTagname );
101 mStream << "<" << inTagname;
102 mTagOpen = true;
103 }
104 void addAttribute( const char* inName, const char* inValue )
105 {
106 PX_ASSERT( mTagOpen );
107 mStream << " " << inName << "=" << "\"" << inValue << "\"";
108 }
109 void closeTag(bool useNewline = true)
110 {
111 if ( mTagOpen )
112 {
113 mStream << " " << ">";
114 if (useNewline )
115 mStream << "\n";
116 }
117 mTagOpen = false;
118 }
119 void doEndOpenTag()
120 {
121 mStream << "</" << mTags.back() << ">" << "\n";
122 }
123 void endTag()
124 {
125 PX_ASSERT( mTags.size() );
126 if ( mTagOpen )
127 mStream << " " << "/>" << "\n";
128 else
129 {
130 writeTabs(mTags.size()-1);
131 doEndOpenTag();
132 }
133 mTagOpen = false;
134 mTags.popBack();
135 }
136 void addContent( const char* inContent )
137 {
138 closeTag(false);
139 mStream << inContent;
140 }
141 void writeContentTag( const char* inTag, const char* inContent )
142 {
143 beginTag( inTag );
144 addContent( inContent );
145 doEndOpenTag();
146 mTags.popBack();
147 }
148 void insertXml( const char* inXml )
149 {
150 closeTag();
151 mStream << inXml;
152 }
153 };
154
155 struct BeginTag
156 {
157 const char* mTagName;
158 BeginTag( const char* inTagName )
159 : mTagName( inTagName ) { }
160 };
161
162 struct EndTag
163 {
164 EndTag() {}
165 };
166
167 struct Att
168 {
169 const char* mAttName;
170 const char* mAttValue;
171 Att( const char* inAttName, const char* inAttValue )
172 : mAttName( inAttName )
173 , mAttValue( inAttValue ) { }
174 };
175
176 struct Content
177 {
178 const char* mContent;
179 Content( const char* inContent )
180 : mContent( inContent ) { }
181 };
182
183
184 struct ContentTag
185 {
186 const char* mTagName;
187 const char* mContent;
188 ContentTag( const char* inTagName, const char* inContent )
189 : mTagName( inTagName )
190 , mContent( inContent ) { }
191 };
192
193 inline XmlWriter& operator<<( XmlWriter& inWriter, const BeginTag& inTag ) { inWriter.beginTag( inTag.mTagName ); return inWriter; }
194 inline XmlWriter& operator<<( XmlWriter& inWriter, const EndTag& inTag ) { inWriter.endTag(); return inWriter; }
195 inline XmlWriter& operator<<( XmlWriter& inWriter, const Att& inTag ) { inWriter.addAttribute(inTag.mAttName, inTag.mAttValue); return inWriter; }
196 inline XmlWriter& operator<<( XmlWriter& inWriter, const Content& inTag ) { inWriter.addContent(inTag.mContent); return inWriter; }
197 inline XmlWriter& operator<<( XmlWriter& inWriter, const ContentTag& inTag ) { inWriter.writeContentTag(inTag.mTagName, inTag.mContent); return inWriter; }
198
199 inline void writeProperty( XmlWriter& inWriter, MemoryBuffer& tempBuffer, const char* inPropName )
200 {
201 PxU8 data = 0;
202 tempBuffer.write( &data, sizeof(PxU8) );
203 inWriter.writeContentTag( inPropName, reinterpret_cast<const char*>( tempBuffer.mBuffer ) );
204 tempBuffer.clear();
205 }
206
207 template<typename TDataType>
208 inline void writeProperty( XmlWriter& inWriter, MemoryBuffer& tempBuffer, const char* inPropName, TDataType inValue )
209 {
210 tempBuffer << inValue;
211 writeProperty( inWriter, tempBuffer, inPropName );
212 }
213} }
214#endif
Abstract base class for an application defined memory allocator that can be used by the Nv library.
Definition PxAllocatorCallback.h:53
PX_FORCE_INLINE uint32_t size() const
Definition PxArray.h:242
PX_FORCE_INLINE T & pushBack(const T &a)
Definition PxArray.h:296
PX_INLINE T popBack()
Definition PxArray.h:311
PX_FORCE_INLINE const T & back() const
Definition PxArray.h:224
Definition SnXmlSimpleXmlWriter.h:37
Sorts an array of objects in ascending order, assuming that the predicate implements the < operator:
Definition PxBoxController.h:39
Definition SnXmlSerialization.cpp:111
Definition SnXmlSimpleXmlWriter.h:41
Helper struct to encapsulate the user allocator callback Useful for array and hash templates.
Definition PxProfileAllocatorWrapper.h:46
Helper struct to encapsulate the array.
Definition PxProfileAllocatorWrapper.h:152