openscenegraph
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
43class InputException : public osg::Referenced
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
63class OSGDB_EXPORT InputStream
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
69 enum ReadType
70 {
71 READ_UNKNOWN = 0,
72 READ_SCENE,
73 READ_IMAGE,
74 READ_OBJECT
75 };
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
107 InputStream& operator>>( osg::Vec2b& v );
108 InputStream& operator>>( osg::Vec3b& v );
109 InputStream& operator>>( osg::Vec4b& v );
110 InputStream& operator>>( osg::Vec2ub& v );
111 InputStream& operator>>( osg::Vec3ub& v );
112 InputStream& operator>>( osg::Vec4ub& v );
113 InputStream& operator>>( osg::Vec2s& v );
114 InputStream& operator>>( osg::Vec3s& v );
115 InputStream& operator>>( osg::Vec4s& v );
116 InputStream& operator>>( osg::Vec2us& v );
117 InputStream& operator>>( osg::Vec3us& v );
118 InputStream& operator>>( osg::Vec4us& v );
119 InputStream& operator>>( osg::Vec2i& v );
120 InputStream& operator>>( osg::Vec3i& v );
121 InputStream& operator>>( osg::Vec4i& v );
122 InputStream& operator>>( osg::Vec2ui& v );
123 InputStream& operator>>( osg::Vec3ui& v );
124 InputStream& operator>>( osg::Vec4ui& v );
125 InputStream& operator>>( osg::Vec2f& v );
126 InputStream& operator>>( osg::Vec3f& v );
127 InputStream& operator>>( osg::Vec4f& v );
128 InputStream& operator>>( osg::Vec2d& v );
129 InputStream& operator>>( osg::Vec3d& v );
130 InputStream& operator>>( osg::Vec4d& v );
131 InputStream& operator>>( osg::Quat& q );
132 InputStream& operator>>( osg::Plane& p );
133 InputStream& operator>>( osg::Matrixf& mat );
134 InputStream& operator>>( osg::Matrixd& mat );
135 InputStream& operator>>( osg::BoundingBoxf& bb );
136 InputStream& operator>>( osg::BoundingBoxd& bb );
137 InputStream& operator>>( osg::BoundingSpheref& bs );
138 InputStream& operator>>( osg::BoundingSphered& bs );
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; }
142 InputStream& operator>>( osg::ref_ptr<osg::PrimitiveSet>& ptr ) { if (_fileVersion>=112) ptr = readObjectOfType<osg::PrimitiveSet>(); else ptr = readPrimitiveSet(); 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
158 osg::ref_ptr<osg::Array> readArray();
159 osg::ref_ptr<osg::PrimitiveSet> readPrimitiveSet();
160 osg::ref_ptr<osg::Image> readImage(bool readFromExternal=true);
161
162 template<typename T>
163 osg::ref_ptr<T> readObjectOfType()
164 {
165 osg::ref_ptr<osg::Object> obj = readObject();
166 T* ptr = dynamic_cast<T*>(obj.get());
167 if (ptr) { return ptr; }
168 else return 0;
169 }
170
171 osg::ref_ptr<osg::Object> readObject( osg::Object* existingObj=0 );
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
184 /// set an input iterator, used directly when not using InputStream with a traditional file related stream.
185 void setInputIterator( InputIterator* ii ) { _in = ii; }
186
187 /// start reading from InputStream treating it as a traditional file related stream, handles headers and versioning
188 ReadType start( InputIterator* );
189
190 void decompress();
191
192 // Schema handlers
193 void readSchema( std::istream& fin );
194 void resetSchema();
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
201 ObjectProperty PROPERTY;
202 ObjectMark BEGIN_BRACKET;
203 ObjectMark END_BRACKET;
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
212 ArrayMap _arrayMap;
213 IdentifierMap _identifierMap;
214
215 typedef std::map<std::string, int> VersionMap;
216 VersionMap _domainVersionMap;
217 int _fileVersion;
218 bool _useSchemaData;
219 bool _forceReadingImage;
220 std::vector<std::string> _fields;
221 osg::ref_ptr<InputIterator> _in;
222 osg::ref_ptr<InputException> _exception;
223 osg::ref_ptr<const osgDB::Options> _options;
224
225 // object to used to read field properties that will be discarded.
226 osg::ref_ptr<osg::Object> _dummyReadObject;
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{
234 _exception = new InputException(_fields, msg);
235}
236
237void InputStream::checkStream()
238{
239 _in->checkStream();
240 if ( _in->isFailed() )
241 throwException( "InputStream: Failed to read from stream." );
242}
243
244}
245
246#endif