1/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2010 Robert Osfield
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.
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.
13// Written by Wang Rui, (C) 2010
15#ifndef OSGDB_INPUTSTREAM
16#define OSGDB_INPUTSTREAM
30#include <osg/BoundingBox>
31#include <osg/BoundingSphere>
33#include <osg/PrimitiveSet>
34#include <osgDB/ReaderWriter>
35#include <osgDB/StreamOperator>
36#include <osgDB/Options>
43class InputException : public osg::Referenced
46 InputException( const std::vector<std::string>& fields, const std::string& err ) : _error(err)
48 for ( unsigned int i=0; i<fields.size(); ++i )
55 const std::string& getField() const { return _field; }
56 const std::string& getError() const { return _error; }
63class OSGDB_EXPORT InputStream
66 typedef std::map< unsigned int, osg::ref_ptr<osg::Array> > ArrayMap;
67 typedef std::map< unsigned int, osg::ref_ptr<osg::Object> > IdentifierMap;
77 InputStream( const osgDB::Options* options );
78 virtual ~InputStream();
80 void setFileVersion( const std::string& d, int v ) { _domainVersionMap[d] = v; }
81 int getFileVersion( const std::string& d=std::string() ) const;
83 bool isBinary() const { return _in->isBinary(); }
84 const osgDB::Options* getOptions() const { return _options.get(); }
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; }
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; }
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 );
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; }
144 template<typename T> InputStream& operator>>( osg::ref_ptr<T>& ptr )
145 { ptr = readObjectOfType<T>(); return *this; }
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); }
154 // readSize() use unsigned int for all sizes.
155 unsigned int readSize() { unsigned int size; *this>>size; return size; }
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);
163 osg::ref_ptr<T> readObjectOfType()
165 osg::ref_ptr<osg::Object> obj = readObject();
166 T* ptr = dynamic_cast<T*>(obj.get());
167 if (ptr) { return ptr; }
171 osg::ref_ptr<osg::Object> readObject( osg::Object* existingObj=0 );
173 osg::ref_ptr<osg::Object> readObjectFields( const std::string& className, unsigned int id, osg::Object* existingObj=0);
176 osg::ref_ptr<T> readObjectFieldsOfType( const std::string& className, unsigned int id, osg::Object* existingObj=0)
178 osg::ref_ptr<osg::Object> obj = readObjectFields(className, id, existingObj);
179 T* ptr = dynamic_cast<T*>(obj.get());
180 if (ptr) { return ptr; }
184 /// set an input iterator, used directly when not using InputStream with a traditional file related stream.
185 void setInputIterator( InputIterator* ii ) { _in = ii; }
187 /// start reading from InputStream treating it as a traditional file related stream, handles headers and versioning
188 ReadType start( InputIterator* );
193 void readSchema( std::istream& fin );
196 // Exception handlers
197 inline void throwException( const std::string& msg );
198 const InputException* getException() const { return _exception.get(); }
200 // Property & mask variables
201 ObjectProperty PROPERTY;
202 ObjectMark BEGIN_BRACKET;
203 ObjectMark END_BRACKET;
206 inline void checkStream();
207 void setWrapperSchema( const std::string& name, const std::string& properties );
210 void readArrayImplementation( T* a, unsigned int numComponentsPerElements, unsigned int componentSizeInBytes );
213 IdentifierMap _identifierMap;
215 typedef std::map<std::string, int> VersionMap;
216 VersionMap _domainVersionMap;
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;
225 // object to used to read field properties that will be discarded.
226 osg::ref_ptr<osg::Object> _dummyReadObject;
228 // store here to avoid a new and a leak in InputStream::decompress
229 std::stringstream* _dataDecompress;
232void InputStream::throwException( const std::string& msg )
234 _exception = new InputException(_fields, msg);
237void InputStream::checkStream()
240 if ( _in->isFailed() )
241 throwException( "InputStream: Failed to read from stream." );