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

Revision 11018, 6.9 kB (checked in by robert, 5 years ago)

From Wang Rui, "Changes:

1. Rewrite the reading/writing exception handlers to work like the ive
plugin exceptions.
2. Write a header writing/checking function in ReaderWriterOSG2.cpp,
which may help decide if the stream is ascii or binary. The
readInputIterator() function will return null pointer if the input
file is nither osgb nor osgt format, which indicates that the old .osg
format could be used here, in case we've merged the two plugins
together.
3. Add a new ForceReadingImage? option in the InputStream?, which will
allocate an empty image object with the filename if specifed external
image file is missed. It may be useful for format converting in some
cases.
4. Add new osgParticle wrappers, as well as some modification to the
osgParticle headers, for instance, change isEnabled() to getEnabled().
5. Some fixes to the osg serialization wrappers."

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