OpenSceneGraph 3.6.5
InputStream
Go to the documentation of this file.
1/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2010 Robert Osfield
2 *
3 * This library is open source and may be redistributed and/or modified under
4 * the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
5 * (at your option) any later version. The full license is in LICENSE file
6 * included with this distribution, and on the openscenegraph.org website.
7 *
8 * This library is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * OpenSceneGraph Public License for more details.
12*/
13// Written by Wang Rui, (C) 2010
14
15#ifndef OSGDB_INPUTSTREAM
16#define OSGDB_INPUTSTREAM
17
18#include <osg/Endian>
19#include <osg/Vec2>
20#include <osg/Vec3>
21#include <osg/Vec4>
22#include <osg/Vec2i>
23#include <osg/Vec3i>
24#include <osg/Vec4i>
25#include <osg/Vec2ui>
26#include <osg/Vec3ui>
27#include <osg/Vec4ui>
28#include <osg/Quat>
29#include <osg/Matrix>
30#include <osg/BoundingBox>
31#include <osg/BoundingSphere>
32#include <osg/Array>
33#include <osg/PrimitiveSet>
34#include <osgDB/ReaderWriter>
35#include <osgDB/StreamOperator>
36#include <osgDB/Options>
37#include <iostream>
38#include <sstream>
39
40namespace osgDB
41{
42
44{
45public:
46 InputException( const std::vector<std::string>& fields, const std::string& err ) : _error(err)
47 {
48 for ( unsigned int i=0; i<fields.size(); ++i )
49 {
50 _field += fields[i];
51 _field += " ";
52 }
53 }
54
55 const std::string& getField() const { return _field; }
56 const std::string& getError() const { return _error; }
57
58protected:
59 std::string _field;
60 std::string _error;
61};
62
64{
65public:
66 typedef std::map< unsigned int, osg::ref_ptr<osg::Array> > ArrayMap;
67 typedef std::map< unsigned int, osg::ref_ptr<osg::Object> > IdentifierMap;
68
76
77 InputStream( const osgDB::Options* options );
78 virtual ~InputStream();
79
80 void setFileVersion( const std::string& d, int v ) { _domainVersionMap[d] = v; }
81 int getFileVersion( const std::string& d=std::string() ) const;
82
83 bool isBinary() const { return _in->isBinary(); }
84 const osgDB::Options* getOptions() const { return _options.get(); }
85
86 // Serialization related functions
87 InputStream& operator>>( bool& b ) { _in->readBool(b); checkStream(); return *this; }
88 InputStream& operator>>( char& c ) { _in->readChar(c); checkStream(); return *this; }
89 InputStream& operator>>( signed char& c ) { _in->readSChar(c); checkStream(); return *this; }
90 InputStream& operator>>( unsigned char& c ) { _in->readUChar(c); checkStream(); return *this; }
91 InputStream& operator>>( short& s ) { _in->readShort(s); checkStream(); return *this; }
92 InputStream& operator>>( unsigned short& s ) { _in->readUShort(s); checkStream(); return *this; }
93 InputStream& operator>>( int& i ) { _in->readInt(i); checkStream(); return *this; }
94 InputStream& operator>>( unsigned int& i ) { _in->readUInt(i); checkStream(); return *this; }
95 InputStream& operator>>( long& l ) { _in->readLong(l); checkStream(); return *this; }
96 InputStream& operator>>( unsigned long& l ) { _in->readULong(l); checkStream(); return *this; }
97 InputStream& operator>>( float& f ) { _in->readFloat(f); checkStream(); return *this; }
98 InputStream& operator>>( double& d ) { _in->readDouble(d); checkStream(); return *this; }
99 InputStream& operator>>( std::string& s ) { _in->readString(s); checkStream(); return *this; }
100 InputStream& operator>>( std::istream& (*fn)(std::istream&) ) { _in->readStream(fn); checkStream(); return *this; }
101 InputStream& operator>>( std::ios_base& (*fn)(std::ios_base&) ) { _in->readBase(fn); checkStream(); return *this; }
102
103 InputStream& operator>>( ObjectGLenum& value ) { _in->readGLenum(value); checkStream(); return *this; }
104 InputStream& operator>>( ObjectProperty& prop ) { _in->readProperty(prop); checkStream(); return *this; }
105 InputStream& operator>>( ObjectMark& mark ) { _in->readMark(mark); checkStream(); return *this; }
106
139
140 InputStream& operator>>( osg::ref_ptr<osg::Image>& ptr ) { ptr = readImage(); return *this; }
141 InputStream& operator>>( osg::ref_ptr<osg::Array>& ptr ) { if (_fileVersion>=112) ptr = readObjectOfType<osg::Array>(); else ptr = readArray(); return *this; }
143
144 template<typename T> InputStream& operator>>( osg::ref_ptr<T>& ptr )
145 { ptr = readObjectOfType<T>(); return *this; }
146
147 // Convenient methods for reading
148 bool matchString( const std::string& str ) { return _in->matchString(str); }
149 void advanceToCurrentEndBracket() { _in->advanceToCurrentEndBracket(); }
150 void readWrappedString( std::string& str ) { _in->readWrappedString(str); checkStream(); }
151 void readCharArray( char* s, unsigned int size ) { _in->readCharArray(s, size); }
152 void readComponentArray( char* s, unsigned int numElements, unsigned int numComponentsPerElements, unsigned int componentSizeInBytes) { _in->readComponentArray( s, numElements, numComponentsPerElements, componentSizeInBytes); }
153
154 // readSize() use unsigned int for all sizes.
155 unsigned int readSize() { unsigned int size; *this>>size; return size; }
156
157 // Global reading functions
160 osg::ref_ptr<osg::Image> readImage(bool readFromExternal=true);
161
162 template<typename T>
164 {
166 T* ptr = dynamic_cast<T*>(obj.get());
167 if (ptr) { return ptr; }
168 else return 0;
169 }
170
172
173 osg::ref_ptr<osg::Object> readObjectFields( const std::string& className, unsigned int id, osg::Object* existingObj=0);
174
175 template<typename T>
176 osg::ref_ptr<T> readObjectFieldsOfType( const std::string& className, unsigned int id, osg::Object* existingObj=0)
177 {
178 osg::ref_ptr<osg::Object> obj = readObjectFields(className, id, existingObj);
179 T* ptr = dynamic_cast<T*>(obj.get());
180 if (ptr) { return ptr; }
181 else return 0;
182 }
183
185 void setInputIterator( InputIterator* ii ) { _in = ii; }
186
189
191
192 // Schema handlers
193 void readSchema( std::istream& fin );
195
196 // Exception handlers
197 inline void throwException( const std::string& msg );
198 const InputException* getException() const { return _exception.get(); }
199
200 // Property & mask variables
204
205protected:
206 inline void checkStream();
207 void setWrapperSchema( const std::string& name, const std::string& properties );
208
209 template<typename T>
210 void readArrayImplementation( T* a, unsigned int numComponentsPerElements, unsigned int componentSizeInBytes );
211
214
215 typedef std::map<std::string, int> VersionMap;
220 std::vector<std::string> _fields;
224
225 // object to used to read field properties that will be discarded.
227
228 // store here to avoid a new and a leak in InputStream::decompress
229 std::stringstream* _dataDecompress;
230};
231
232void InputStream::throwException( const std::string& msg )
233{
235}
236
238{
239 _in->checkStream();
240 if ( _in->isFailed() )
241 throwException( "InputStream: Failed to read from stream." );
242}
243
244}
245
246#endif
BoundingBoxImpl< Vec3d > BoundingBoxd
Definition BoundingBox:252
BoundingBoxImpl< Vec3f > BoundingBoxf
Definition BoundingBox:251
BoundingSphereImpl< Vec3f > BoundingSpheref
Definition BoundingSphere:302
BoundingSphereImpl< Vec3d > BoundingSphered
Definition BoundingSphere:303
The osgDB library provides support for reading and writing scene graphs, providing a plugin framework...
Definition Archive:24
Definition Matrixd:27
Definition Matrixf:27
Base class/standard interface for objects which require IO support, cloning and reference counting.
Definition Object:61
A plane class.
Definition Plane:34
A quaternion class.
Definition Quat:30
Smart pointer for handling referenced counted objects.
Definition ref_ptr:32
T * get() const
Definition ref_ptr:117
Base class for providing reference counted objects.
Definition Referenced:44
General purpose float triple.
Definition Vec2b:26
General purpose double pair, uses include representation of texture coordinates.
Definition Vec2d:29
General purpose float pair.
Definition Vec2f:29
General purpose integer pair.
Definition Vec2i:22
Definition Vec2s:20
General purpose unsigned byte pair.
Definition Vec2ub:22
General purpose integer pair.
Definition Vec2ui:22
Definition Vec2us:20
General purpose float triple.
Definition Vec3b:26
General purpose double triple for use as vertices, vectors and normals.
Definition Vec3d:30
General purpose float triple for use as vertices, vectors and normals.
Definition Vec3f:29
General purpose integer triple.
Definition Vec3i:22
Definition Vec3s:20
General purpose float triple.
Definition Vec3ub:26
General purpose integer triple.
Definition Vec3ui:22
Definition Vec3us:20
General purpose float triple.
Definition Vec4b:26
General purpose double quad.
Definition Vec4d:29
General purpose float quad.
Definition Vec4f:28
General purpose integer quad.
Definition Vec4i:22
General purpose float quad.
Definition Vec4s:26
General purpose float quad.
Definition Vec4ub:28
General purpose integer quad.
Definition Vec4ui:22
General purpose float quad.
Definition Vec4us:26
Definition DataTypes:94
Definition DataTypes:105
Definition DataTypes:129
Definition InputStream:44
const std::string & getError() const
Definition InputStream:56
const std::string & getField() const
Definition InputStream:55
std::string _field
Definition InputStream:59
std::string _error
Definition InputStream:60
InputException(const std::vector< std::string > &fields, const std::string &err)
Definition InputStream:46
InputStream & operator>>(osg::Vec4b &v)
InputStream & operator>>(osg::Vec3i &v)
std::stringstream * _dataDecompress
Definition InputStream:229
InputStream & operator>>(bool &b)
Definition InputStream:87
osg::ref_ptr< InputException > _exception
Definition InputStream:222
InputStream & operator>>(osg::ref_ptr< T > &ptr)
Definition InputStream:144
std::map< std::string, int > VersionMap
Definition InputStream:215
void readCharArray(char *s, unsigned int size)
Definition InputStream:151
void readArrayImplementation(T *a, unsigned int numComponentsPerElements, unsigned int componentSizeInBytes)
std::vector< std::string > _fields
Definition InputStream:220
InputStream & operator>>(ObjectProperty &prop)
Definition InputStream:104
ArrayMap _arrayMap
Definition InputStream:212
InputStream & operator>>(signed char &c)
Definition InputStream:89
InputStream & operator>>(osg::Vec2i &v)
void setInputIterator(InputIterator *ii)
set an input iterator, used directly when not using InputStream with a traditional file related strea...
Definition InputStream:185
InputStream & operator>>(osg::Vec2d &v)
InputStream & operator>>(osg::Vec3d &v)
osg::ref_ptr< osg::Object > _dummyReadObject
Definition InputStream:226
void setFileVersion(const std::string &d, int v)
Definition InputStream:80
InputStream & operator>>(osg::Vec3s &v)
osg::ref_ptr< osg::PrimitiveSet > readPrimitiveSet()
InputStream & operator>>(unsigned short &s)
Definition InputStream:92
InputStream & operator>>(osg::ref_ptr< osg::Image > &ptr)
Definition InputStream:140
InputStream & operator>>(int &i)
Definition InputStream:93
bool matchString(const std::string &str)
Definition InputStream:148
InputStream & operator>>(osg::Vec3ui &v)
void throwException(const std::string &msg)
Definition InputStream:232
InputStream & operator>>(osg::Vec2ui &v)
osg::ref_ptr< const osgDB::Options > _options
Definition InputStream:223
InputStream & operator>>(unsigned int &i)
Definition InputStream:94
ReadType start(InputIterator *)
start reading from InputStream treating it as a traditional file related stream, handles headers and ...
InputStream & operator>>(osg::Quat &q)
InputStream & operator>>(std::istream &(*fn)(std::istream &))
Definition InputStream:100
osg::ref_ptr< osg::Object > readObjectFields(const std::string &className, unsigned int id, osg::Object *existingObj=0)
InputStream & operator>>(osg::Vec3b &v)
const InputException * getException() const
Definition InputStream:198
void readWrappedString(std::string &str)
Definition InputStream:150
int _fileVersion
Definition InputStream:217
void checkStream()
Definition InputStream:237
InputStream & operator>>(float &f)
Definition InputStream:97
bool isBinary() const
Definition InputStream:83
ReadType
Definition InputStream:70
@ READ_SCENE
Definition InputStream:72
@ READ_OBJECT
Definition InputStream:74
@ READ_UNKNOWN
Definition InputStream:71
@ READ_IMAGE
Definition InputStream:73
InputStream & operator>>(osg::Vec3f &v)
InputStream & operator>>(osg::Vec4s &v)
InputStream & operator>>(osg::ref_ptr< osg::Array > &ptr)
Definition InputStream:141
InputStream & operator>>(osg::ref_ptr< osg::PrimitiveSet > &ptr)
Definition InputStream:142
InputStream & operator>>(osg::Vec3us &v)
InputStream & operator>>(unsigned long &l)
Definition InputStream:96
const osgDB::Options * getOptions() const
Definition InputStream:84
std::map< unsigned int, osg::ref_ptr< osg::Object > > IdentifierMap
Definition InputStream:67
virtual ~InputStream()
std::map< unsigned int, osg::ref_ptr< osg::Array > > ArrayMap
Definition InputStream:66
osg::ref_ptr< osg::Array > readArray()
InputStream & operator>>(osg::Matrixf &mat)
void setWrapperSchema(const std::string &name, const std::string &properties)
InputStream & operator>>(osg::BoundingBoxd &bb)
osg::ref_ptr< osg::Image > readImage(bool readFromExternal=true)
InputStream(const osgDB::Options *options)
osg::ref_ptr< osg::Object > readObject(osg::Object *existingObj=0)
InputStream & operator>>(osg::BoundingSphered &bs)
InputStream & operator>>(osg::Vec4i &v)
InputStream & operator>>(char &c)
Definition InputStream:88
ObjectMark END_BRACKET
Definition InputStream:203
bool _forceReadingImage
Definition InputStream:219
InputStream & operator>>(ObjectGLenum &value)
Definition InputStream:103
void readComponentArray(char *s, unsigned int numElements, unsigned int numComponentsPerElements, unsigned int componentSizeInBytes)
Definition InputStream:152
InputStream & operator>>(osg::Vec2b &v)
osg::ref_ptr< T > readObjectFieldsOfType(const std::string &className, unsigned int id, osg::Object *existingObj=0)
Definition InputStream:176
InputStream & operator>>(long &l)
Definition InputStream:95
osg::ref_ptr< InputIterator > _in
Definition InputStream:221
ObjectMark BEGIN_BRACKET
Definition InputStream:202
int getFileVersion(const std::string &d=std::string()) const
InputStream & operator>>(osg::Vec3ub &v)
InputStream & operator>>(osg::Matrixd &mat)
InputStream & operator>>(osg::Plane &p)
InputStream & operator>>(std::ios_base &(*fn)(std::ios_base &))
Definition InputStream:101
InputStream & operator>>(short &s)
Definition InputStream:91
InputStream & operator>>(double &d)
Definition InputStream:98
InputStream & operator>>(osg::Vec2s &v)
InputStream & operator>>(osg::Vec2us &v)
InputStream & operator>>(osg::Vec4ui &v)
InputStream & operator>>(osg::Vec4us &v)
InputStream & operator>>(osg::Vec2ub &v)
void readSchema(std::istream &fin)
InputStream & operator>>(std::string &s)
Definition InputStream:99
IdentifierMap _identifierMap
Definition InputStream:213
unsigned int readSize()
Definition InputStream:155
InputStream & operator>>(osg::BoundingBoxf &bb)
InputStream & operator>>(osg::Vec4f &v)
InputStream & operator>>(osg::Vec2f &v)
void advanceToCurrentEndBracket()
Definition InputStream:149
InputStream & operator>>(osg::Vec4d &v)
VersionMap _domainVersionMap
Definition InputStream:216
InputStream & operator>>(osg::BoundingSpheref &bs)
osg::ref_ptr< T > readObjectOfType()
Definition InputStream:163
InputStream & operator>>(osg::Vec4ub &v)
bool _useSchemaData
Definition InputStream:218
InputStream & operator>>(unsigned char &c)
Definition InputStream:90
ObjectProperty PROPERTY
Definition InputStream:201
InputStream & operator>>(ObjectMark &mark)
Definition InputStream:105
Options base class used for passing options into plugins to control their operation.
Definition Options:30
Definition StreamOperator:83
#define OSGDB_EXPORT
Definition Export:39

osg logo
Generated at Sun Jul 27 2025 00:00:00 for the OpenSceneGraph by doxygen 1.14.0.