1 /*---------------------------------------------------------------------------*\
5 * Copyright (C) 2007 by the OpenSG Forum *
9 * contact: dirk@opensg.org, gerrit.voss@vossg.org, jbehr@zgdv.de *
11 \*---------------------------------------------------------------------------*/
12 /*---------------------------------------------------------------------------*\
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. *
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. *
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. *
28 \*---------------------------------------------------------------------------*/
29 /*---------------------------------------------------------------------------*\
37 \*---------------------------------------------------------------------------*/
39 #include "OSGOSBElementBase.h"
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
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 /*-------------------------------------------------------------------------*/
79 OSBElementBase::BinaryReadHandler::BinaryReadHandler(std::istream
&inStream
)
80 : BinaryDataHandler(1 ),
84 readBufAdd(&_readMemory
[0], _readMemory
.size());
87 /*-------------------------------------------------------------------------*/
90 OSBElementBase::BinaryReadHandler::~BinaryReadHandler(void)
94 /*-------------------------------------------------------------------------*/
97 /*! Reads \a size bytes from the underlying stream to the position indicated
100 \param[in] mem Destination for read data.
101 \param[in] size Number of bytes to read.
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 /*-------------------------------------------------------------------------*/
120 OSBElementBase::BinaryWriteHandler::BinaryWriteHandler(std::ostream
&outStream
)
121 : BinaryDataHandler(1 ),
122 _writeMemory (8192 ),
125 writeBufAdd(&_writeMemory
[0], _writeMemory
.size());
128 /*-------------------------------------------------------------------------*/
133 OSBElementBase::BinaryWriteHandler::~BinaryWriteHandler(void)
138 /*-------------------------------------------------------------------------*/
141 /*! Writes \a size bytes from the position indicated by \a mem to the
144 \param[in] mem Data to write.
145 \param[in] size Number of bytes to write.
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).
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.
171 /*-------------------------------------------------------------------------*/
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
) :
184 _bHandledField(false )
188 OSBElementBase::PtrFieldInfo::PtrFieldInfo(const PtrFieldInfo
&other
) :
190 _fieldId (other
._fieldId
),
191 _ptrIds (other
._ptrIds
),
192 _bindings (other
._bindings
),
193 _bHandledField(other
._bHandledField
)
197 /*-------------------------------------------------------------------------*/
202 OSBElementBase::PtrFieldInfo::~PtrFieldInfo(void)
207 /*-------------------------------------------------------------------------*/
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).
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
220 To create your own element you probably want to derive from
221 OSBCommonElement that provides functions that cover the typical steps
226 /*-------------------------------------------------------------------------*/
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 /*-------------------------------------------------------------------------*/
240 OSBElementBase::OSBElementBase(OSBRootElement
*root
, const UInt16 version
)
241 : _container (NULL
),
248 /*-------------------------------------------------------------------------*/
253 OSBElementBase::~OSBElementBase(void)