changed: gcc8 base update
[opensg.git] / Source / System / FileIO / OSB / OSGOSBElementBase.cpp
blob2d6f0112e8f00555698419cd751cdd50f8c4836b
1 /*---------------------------------------------------------------------------*\
2 * OpenSG *
3 * *
4 * *
5 * Copyright (C) 2007 by the OpenSG Forum *
6 * *
7 * www.opensg.org *
8 * *
9 * contact: dirk@opensg.org, gerrit.voss@vossg.org, jbehr@zgdv.de *
10 * *
11 \*---------------------------------------------------------------------------*/
12 /*---------------------------------------------------------------------------*\
13 * License *
14 * *
15 * This library is free software; you can redistribute it and/or modify it *
16 * under the terms of the GNU Library General Public License as published *
17 * by the Free Software Foundation, version 2. *
18 * *
19 * This library is distributed in the hope that it will be useful, but *
20 * WITHOUT ANY WARRANTY; without even the implied warranty of *
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
22 * Library General Public License for more details. *
23 * *
24 * You should have received a copy of the GNU Library General Public *
25 * License along with this library; if not, write to the Free Software *
26 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. *
27 * *
28 \*---------------------------------------------------------------------------*/
29 /*---------------------------------------------------------------------------*\
30 * Changes *
31 * *
32 * *
33 * *
34 * *
35 * *
36 * *
37 \*---------------------------------------------------------------------------*/
39 #include "OSGOSBElementBase.h"
41 OSG_USING_NAMESPACE
43 /*-------------------------------------------------------------------------*/
44 /* OSBElementBase::BinaryReadHandler */
45 /*-------------------------------------------------------------------------*/
47 /*! \class OSG::OSBElementBase::BinaryReadHandler
48 Helper class for binary read operations.
51 /*! \fn OSG::OSBElementBase::read(const std::string& typeName)
52 Reads the object of type \a typeName and stores it as its container.
54 \param[in] typeName Type of the object to read.
57 /*! \fn OSG::OSBElementBase::postRead(void)
58 Performs any post processing on the object read by this element. Usually
59 this is used to perform conversions from older file formats and similar
60 tasks.
63 /*! \fn OSG::OSBElementBase::preWrite(FieldContainer * const fc)
64 Examines the FieldContainer \a fc and creates elements for all containers
65 referenced by any of its fields and calls preWrite for these elements.
66 This recursively traverses the part of the scene that can be reached from
67 the root and records the objects to be written.
69 \param[in] fc The container this element is processing.
72 /*! \fn OSG::OSBElementBase::write(void)
73 Writes the container it is responsible for.
76 /*-------------------------------------------------------------------------*/
77 /* Constructor */
79 OSBElementBase::BinaryReadHandler::BinaryReadHandler(std::istream &inStream)
80 : BinaryDataHandler(1 ),
81 _readMemory (8192 ),
82 _is (inStream)
84 readBufAdd(&_readMemory[0], _readMemory.size());
87 /*-------------------------------------------------------------------------*/
88 /* Destructor */
90 OSBElementBase::BinaryReadHandler::~BinaryReadHandler(void)
94 /*-------------------------------------------------------------------------*/
95 /* Read */
97 /*! Reads \a size bytes from the underlying stream to the position indicated
98 by \a mem.
100 \param[in] mem Destination for read data.
101 \param[in] size Number of bytes to read.
103 void
104 OSBElementBase::BinaryReadHandler::read(MemoryHandle mem, UInt32 size)
106 _is.read(reinterpret_cast<char *>(mem), size);
109 /*-------------------------------------------------------------------------*/
110 /* OSBElementBase::BinaryWriteHandler */
111 /*-------------------------------------------------------------------------*/
113 /*! \class OSG::OSBElementBase::BinaryWriteHandler
114 Helper class for binary write operations.
117 /*-------------------------------------------------------------------------*/
118 /* Constructor */
120 OSBElementBase::BinaryWriteHandler::BinaryWriteHandler(std::ostream &outStream)
121 : BinaryDataHandler(1 ),
122 _writeMemory (8192 ),
123 _os (outStream)
125 writeBufAdd(&_writeMemory[0], _writeMemory.size());
128 /*-------------------------------------------------------------------------*/
129 /* Destructor */
131 /*! Destructor.
133 OSBElementBase::BinaryWriteHandler::~BinaryWriteHandler(void)
135 // nothing to do.
138 /*-------------------------------------------------------------------------*/
139 /* Write */
141 /*! Writes \a size bytes from the position indicated by \a mem to the
142 underlying stream.
144 \param[in] mem Data to write.
145 \param[in] size Number of bytes to write.
147 void
148 OSBElementBase::BinaryWriteHandler::write(MemoryHandle mem, UInt32 size)
150 _os.write(reinterpret_cast<const char *>(mem), size);
153 /*-------------------------------------------------------------------------*/
154 /* OSBElementBase::PtrFieldInfo */
155 /*-------------------------------------------------------------------------*/
157 /*! \class OSG::OSBElementBase::PtrFieldInfo
158 This holds information about a SField or MField that holds pointers to
159 FieldContainers (or derived types).
161 \dev
162 It is needed while reading an OSB file, because containers pointed to from
163 fields of the currently read container might not be loaded yet. To resolve
164 this, for each such field an instance of this type is created and the
165 container ids read from the file are stored therein. After the postRead
166 processing is complete, this information is used to fill the pointer fields
167 with the correct data.
168 \enddev
171 /*-------------------------------------------------------------------------*/
172 /* Constructor */
174 /*! Constructs an new PtrFieldInfo instance that describes the field with the
175 given \a fieldId of the given FieldContainer \a fc.
177 OSBElementBase::PtrFieldInfo::PtrFieldInfo(
178 FieldContainer * const fc, UInt32 fieldId) :
180 _fc (fc ),
181 _fieldId (fieldId),
182 _ptrIds ( ),
183 _bindings ( ),
184 _bHandledField(false )
188 OSBElementBase::PtrFieldInfo::PtrFieldInfo(const PtrFieldInfo &other) :
189 _fc (other._fc ),
190 _fieldId (other._fieldId ),
191 _ptrIds (other._ptrIds ),
192 _bindings (other._bindings ),
193 _bHandledField(other._bHandledField)
197 /*-------------------------------------------------------------------------*/
198 /* Destructor */
200 /*! Destructor.
202 OSBElementBase::PtrFieldInfo::~PtrFieldInfo(void)
204 // nothing to do.
207 /*-------------------------------------------------------------------------*/
208 /* OSBElementBase */
209 /*-------------------------------------------------------------------------*/
211 /*! \class OSG::OSBElementBase
212 The base class for all OSB IO "elements". An element is a type that
213 provides the ability to read and write one or more OpenSG classes
214 (especially FieldContainers).
216 \dev
217 This class holds a pointer to the OSG::OSBRootElement that holds the
218 shared state for the IO operation and the FieldContainer this element
219 is responsible for.
220 To create your own element you probably want to derive from
221 OSBCommonElement that provides functions that cover the typical steps
222 necessary during IO.
223 \enddev
226 /*-------------------------------------------------------------------------*/
227 /* Constants */
229 const UInt16 OSBElementBase::OSGOSBHeaderVersion100 = 100;
230 const UInt16 OSBElementBase::OSGOSBHeaderVersion200 = 200;
231 // const UInt16 OSBElementBase::OSGOSBHeaderVersion201 = 201;
233 const std::string OSBElementBase::OSGOSB_HEADER_ID_1 ("OpenSG binary V1.0" );
234 const std::string OSBElementBase::OSGOSB_HEADER_ID_2 ("OpenSG binary V2.0" );
235 // const std::string OSBElementBase::OSGOSB_HEADER_ID_201("OpenSG binary V2.01");
237 /*-------------------------------------------------------------------------*/
238 /* Constructor */
240 OSBElementBase::OSBElementBase(OSBRootElement *root, const UInt16 version)
241 : _container (NULL ),
242 _rootElement(root ),
243 _fcIdFile (0 ),
244 _version (version)
248 /*-------------------------------------------------------------------------*/
249 /* Destructor */
251 /*! Destructor.
253 OSBElementBase::~OSBElementBase(void)
255 // nothing to do.