root/OpenSceneGraph/trunk/include/osgDB/InputStream @ 13041

Revision 13041, 8.0 kB (checked in by robert, 3 years ago)

Ran script to remove trailing spaces and tabs

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
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/Quat>
23#include <osg/Matrix>
24#include <osg/Array>
25#include <osg/PrimitiveSet>
26#include <osgDB/ReaderWriter>
27#include <osgDB/StreamOperator>
28#include <osgDB/Options>
29#include <iostream>
30#include <sstream>
31
32namespace osgDB
33{
34
35class InputException : public osg::Referenced
36{
37public:
38    InputException( const std::vector<std::string>& fields, const std::string& err ) : _error(err)
39    {
40        for ( unsigned int i=0; i<fields.size(); ++i )
41        {
42            _field += fields[i];
43            _field += " ";
44        }
45    }
46
47    const std::string& getField() const { return _field; }
48    const std::string& getError() const { return _error; }
49
50protected:
51    std::string _field;
52    std::string _error;
53};
54
55class OSGDB_EXPORT InputStream
56{
57public:
58    typedef std::map< unsigned int, osg::ref_ptr<osg::Array> > ArrayMap;
59    typedef std::map< unsigned int, osg::ref_ptr<osg::Object> > IdentifierMap;
60
61    enum ReadType
62    {
63        READ_UNKNOWN = 0,
64        READ_SCENE,
65        READ_IMAGE,
66        READ_OBJECT
67    };
68
69    InputStream( const osgDB::Options* options );
70    virtual ~InputStream();
71
72    bool isBinary() const { return _in->isBinary(); }
73    int getFileVersion() const { return _fileVersion; }
74    const osgDB::Options* getOptions() const { return _options.get(); }
75
76    // Serialization related functions
77    InputStream& operator>>( bool& b ) { _in->readBool(b); checkStream(); return *this; }
78    InputStream& operator>>( char& c ) { _in->readChar(c); checkStream(); return *this; }
79    InputStream& operator>>( signed char& c ) { _in->readSChar(c); checkStream(); return *this; }
80    InputStream& operator>>( unsigned char& c ) { _in->readUChar(c); checkStream(); return *this; }
81    InputStream& operator>>( short& s ) { _in->readShort(s); checkStream(); return *this; }
82    InputStream& operator>>( unsigned short& s ) { _in->readUShort(s); checkStream(); return *this; }
83    InputStream& operator>>( int& i ) { _in->readInt(i); checkStream(); return *this; }
84    InputStream& operator>>( unsigned int& i ) { _in->readUInt(i); checkStream(); return *this; }
85    InputStream& operator>>( long& l ) { _in->readLong(l); checkStream(); return *this; }
86    InputStream& operator>>( unsigned long& l ) { _in->readULong(l); checkStream(); return *this; }
87    InputStream& operator>>( float& f ) { _in->readFloat(f); checkStream(); return *this; }
88    InputStream& operator>>( double& d ) { _in->readDouble(d); checkStream(); return *this; }
89    InputStream& operator>>( std::string& s ) { _in->readString(s); checkStream(); return *this; }
90    InputStream& operator>>( std::istream& (*fn)(std::istream&) ) { _in->readStream(fn); checkStream(); return *this; }
91    InputStream& operator>>( std::ios_base& (*fn)(std::ios_base&) ) { _in->readBase(fn); checkStream(); return *this; }
92
93    InputStream& operator>>( ObjectGLenum& value ) { _in->readGLenum(value); checkStream(); return *this; }
94    InputStream& operator>>( ObjectProperty& prop ) { _in->readProperty(prop); checkStream(); return *this; }
95    InputStream& operator>>( ObjectMark& mark ) { _in->readMark(mark); checkStream(); return *this; }
96
97    InputStream& operator>>( osg::Vec2b& v );
98    InputStream& operator>>( osg::Vec3b& v );
99    InputStream& operator>>( osg::Vec4b& v );
100    InputStream& operator>>( osg::Vec4ub& v );
101    InputStream& operator>>( osg::Vec2s& v );
102    InputStream& operator>>( osg::Vec3s& v );
103    InputStream& operator>>( osg::Vec4s& v );
104    InputStream& operator>>( osg::Vec2f& v );
105    InputStream& operator>>( osg::Vec3f& v );
106    InputStream& operator>>( osg::Vec4f& v );
107    InputStream& operator>>( osg::Vec2d& v );
108    InputStream& operator>>( osg::Vec3d& v );
109    InputStream& operator>>( osg::Vec4d& v );
110    InputStream& operator>>( osg::Quat& q );
111    InputStream& operator>>( osg::Plane& p );
112    InputStream& operator>>( osg::Matrixf& mat );
113    InputStream& operator>>( osg::Matrixd& mat );
114
115    InputStream& operator>>( osg::Array*& a ) { a = readArray(); return *this; }
116    InputStream& operator>>( osg::Image*& img ) { img = readImage(); return *this; }
117    InputStream& operator>>( osg::PrimitiveSet*& p ) { p = readPrimitiveSet(); return *this; }
118    InputStream& operator>>( osg::Object*& obj ) { obj = readObject(); return *this; }
119
120    InputStream& operator>>( osg::ref_ptr<osg::Array>& ptr ) { ptr = readArray(); return *this; }
121    InputStream& operator>>( osg::ref_ptr<osg::Image>& ptr ) { ptr = readImage(); return *this; }
122    InputStream& operator>>( osg::ref_ptr<osg::PrimitiveSet>& ptr ) { ptr = readPrimitiveSet(); return *this; }
123
124    template<typename T> InputStream& operator>>( osg::ref_ptr<T>& ptr )
125    { ptr = static_cast<T*>(readObject()); return *this; }
126
127    // Convenient methods for reading
128    bool matchString( const std::string& str ) { return _in->matchString(str); }
129    void advanceToCurrentEndBracket() { _in->advanceToCurrentEndBracket(); }
130    void readWrappedString( std::string& str ) { _in->readWrappedString(str); checkStream(); }
131    void readCharArray( char* s, unsigned int size ) { _in->readCharArray(s, size); }
132    void readComponentArray( char* s, unsigned int numElements, unsigned int numComponentsPerElements, unsigned int componentSizeInBytes) { _in->readComponentArray( s, numElements, numComponentsPerElements, componentSizeInBytes); }
133
134    // readSize() use unsigned int for all sizes.
135    unsigned int readSize() { unsigned int size; *this>>size; return size; }
136
137    // Global reading functions
138    osg::Array* readArray();
139    osg::PrimitiveSet* readPrimitiveSet();
140    osg::Image* readImage(bool readFromExternal=true);
141    osg::Object* readObject( osg::Object* existingObj=0 );
142    osg::Object* readObjectFields( const std::string& className, unsigned int id, osg::Object* existingObj=0);
143
144    /// set an input iterator, used directly when not using InputStream with a traditional file releated stream.
145    void setInputIterator( InputIterator* ii ) { _in = ii; }
146
147    /// start reading from InputStream treating it as a traditional file releated stream, handles headers and versioning
148    ReadType start( InputIterator* );
149
150    void decompress();
151
152    // Schema handlers
153    void readSchema( std::istream& fin );
154    void resetSchema();
155
156    // Exception handlers
157    inline void throwException( const std::string& msg );
158    const InputException* getException() const { return _exception.get(); }
159
160protected:
161    inline void checkStream();
162    void setWrapperSchema( const std::string& name, const std::string& properties );
163
164    template<typename T>
165    void readArrayImplementation( T* a, unsigned int numComponentsPerElements, unsigned int componentSizeInBytes );
166
167    ArrayMap _arrayMap;
168    IdentifierMap _identifierMap;
169
170    int _fileVersion;
171    bool _useSchemaData;
172    bool _forceReadingImage;
173    std::vector<std::string> _fields;
174    osg::ref_ptr<InputIterator> _in;
175    osg::ref_ptr<InputException> _exception;
176    osg::ref_ptr<const osgDB::Options> _options;
177
178    // store here to avoid a new and a leak in InputStream::decompress
179    std::stringstream* _dataDecompress;
180};
181
182void InputStream::throwException( const std::string& msg )
183{
184    _exception = new InputException(_fields, msg);
185}
186
187void InputStream::checkStream()
188{
189    _in->checkStream();
190    if ( _in->isFailed() )
191        throwException( "InputStream: Failed to read from stream." );
192}
193
194}
195
196#endif
Note: See TracBrowser for help on using the browser.