OSG  3.4.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
InputStream.h
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 
40 namespace osgDB
41 {
42 
44 {
45 public:
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 
58 protected:
59  std::string _field;
60  std::string _error;
61 };
62 
64 {
65 public:
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,
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 
139 
140  InputStream& operator>>( osg::Image*& img ) { img = readImage(); return *this; }
141  InputStream& operator>>( osg::Array*& a ) { if (_fileVersion>=112) a = readObjectOfType<osg::Array>(); else a = readArray(); return *this; }
142  InputStream& operator>>( osg::PrimitiveSet*& p ) { if (_fileVersion>=112) p = readObjectOfType<osg::PrimitiveSet>(); else p = readPrimitiveSet(); return *this; }
143  InputStream& operator>>( osg::Object*& obj ) { obj = readObject(); return *this; }
144 
145  InputStream& operator>>( osg::ref_ptr<osg::Image>& ptr ) { ptr = readImage(); return *this; }
146  InputStream& operator>>( osg::ref_ptr<osg::Array>& ptr ) { if (_fileVersion>=112) ptr = readObjectOfType<osg::Array>(); else ptr = readArray(); return *this; }
147  InputStream& operator>>( osg::ref_ptr<osg::PrimitiveSet>& ptr ) { if (_fileVersion>=112) ptr = readObjectOfType<osg::PrimitiveSet>(); else ptr = readPrimitiveSet(); return *this; }
148 
149  template<typename T> InputStream& operator>>( osg::ref_ptr<T>& ptr )
150  { ptr = static_cast<T*>(readObject()); return *this; }
151 
152  // Convenient methods for reading
153  bool matchString( const std::string& str ) { return _in->matchString(str); }
154  void advanceToCurrentEndBracket() { _in->advanceToCurrentEndBracket(); }
155  void readWrappedString( std::string& str ) { _in->readWrappedString(str); checkStream(); }
156  void readCharArray( char* s, unsigned int size ) { _in->readCharArray(s, size); }
157  void readComponentArray( char* s, unsigned int numElements, unsigned int numComponentsPerElements, unsigned int componentSizeInBytes) { _in->readComponentArray( s, numElements, numComponentsPerElements, componentSizeInBytes); }
158 
159  // readSize() use unsigned int for all sizes.
160  unsigned int readSize() { unsigned int size; *this>>size; return size; }
161 
162  // Global reading functions
163  osg::Array* readArray();
164  osg::PrimitiveSet* readPrimitiveSet();
165  osg::Image* readImage(bool readFromExternal=true);
166 
167  template<typename T>
169  {
170  osg::ref_ptr<osg::Object> obj = readObject();
171  T* ptr = dynamic_cast<T*>(obj.get());
172  if (ptr) { obj.release(); return ptr; }
173  else return 0;
174  }
175 
176  osg::Object* readObject( osg::Object* existingObj=0 );
177 
178 
179  osg::Object* readObjectFields( const std::string& className, unsigned int id, osg::Object* existingObj=0);
180 
182  void setInputIterator( InputIterator* ii ) { _in = ii; }
183 
185  ReadType start( InputIterator* );
186 
187  void decompress();
188 
189  // Schema handlers
190  void readSchema( std::istream& fin );
191  void resetSchema();
192 
193  // Exception handlers
194  inline void throwException( const std::string& msg );
195  const InputException* getException() const { return _exception.get(); }
196 
197  // Property & mask variables
201 
202 protected:
203  inline void checkStream();
204  void setWrapperSchema( const std::string& name, const std::string& properties );
205 
206  template<typename T>
207  void readArrayImplementation( T* a, unsigned int numComponentsPerElements, unsigned int componentSizeInBytes );
208 
209  ArrayMap _arrayMap;
210  IdentifierMap _identifierMap;
211 
212  typedef std::map<std::string, int> VersionMap;
213  VersionMap _domainVersionMap;
217  std::vector<std::string> _fields;
221 
222  // object to used to read field properties that will be discarded.
224 
225  // store here to avoid a new and a leak in InputStream::decompress
226  std::stringstream* _dataDecompress;
227 };
228 
229 void InputStream::throwException( const std::string& msg )
230 {
231  _exception = new InputException(_fields, msg);
232 }
233 
235 {
236  _in->checkStream();
237  if ( _in->isFailed() )
238  throwException( "InputStream: Failed to read from stream." );
239 }
240 
241 }
242 
243 #endif
InputStream & operator>>(unsigned long &l)
Definition: InputStream.h:96
InputStream & operator>>(osg::Array *&a)
Definition: InputStream.h:141
InputStream & operator>>(unsigned int &i)
Definition: InputStream.h:94
InputStream & operator>>(std::istream &(*fn)(std::istream &))
Definition: InputStream.h:100
void setInputIterator(InputIterator *ii)
set an input iterator, used directly when not using InputStream with a traditional file releated stre...
Definition: InputStream.h:182
InputStream & operator>>(osg::PrimitiveSet *&p)
Definition: InputStream.h:142
void readCharArray(char *s, unsigned int size)
Definition: InputStream.h:156
osg::ref_ptr< InputException > _exception
Definition: InputStream.h:219
InputStream & operator>>(double &d)
Definition: InputStream.h:98
std::string _field
Definition: InputStream.h:59
InputStream & operator>>(signed char &c)
Definition: InputStream.h:89
InputStream & operator>>(std::ios_base &(*fn)(std::ios_base &))
Definition: InputStream.h:101
InputStream & operator>>(std::string &s)
Definition: InputStream.h:99
InputStream & operator>>(char &c)
Definition: InputStream.h:88
std::vector< std::string > _fields
Definition: InputStream.h:217
InputStream & operator>>(long &l)
Definition: InputStream.h:95
InputStream & operator>>(float &f)
Definition: InputStream.h:97
std::map< unsigned int, osg::ref_ptr< osg::Array > > ArrayMap
Definition: InputStream.h:66
VersionMap _domainVersionMap
Definition: InputStream.h:213
InputStream & operator>>(ObjectProperty &prop)
Definition: InputStream.h:104
InputStream & operator>>(ObjectGLenum &value)
Definition: InputStream.h:103
InputStream & operator>>(osg::ref_ptr< osg::Image > &ptr)
Definition: InputStream.h:145
const InputException * getException() const
Definition: InputStream.h:195
ObjectMark END_BRACKET
Definition: InputStream.h:200
InputStream & operator>>(bool &b)
Definition: InputStream.h:87
T * release()
Definition: ref_ptr.h:101
IdentifierMap _identifierMap
Definition: InputStream.h:210
std::map< std::string, int > VersionMap
Definition: InputStream.h:212
unsigned int readSize()
Definition: InputStream.h:160
osg::ref_ptr< const osgDB::Options > _options
Definition: InputStream.h:220
void readWrappedString(std::string &str)
Definition: InputStream.h:155
InputStream & operator>>(short &s)
Definition: InputStream.h:91
const std::string & getError() const
Definition: InputStream.h:56
InputStream & operator>>(unsigned char &c)
Definition: InputStream.h:90
InputStream & operator>>(ObjectMark &mark)
Definition: InputStream.h:105
InputStream & operator>>(unsigned short &s)
Definition: InputStream.h:92
InputStream & operator>>(osg::ref_ptr< T > &ptr)
Definition: InputStream.h:149
bool isBinary() const
Definition: InputStream.h:83
Definition: Archive.h:24
InputException(const std::vector< std::string > &fields, const std::string &err)
Definition: InputStream.h:46
InputStream & operator>>(osg::Object *&obj)
Definition: InputStream.h:143
T * get() const
Definition: ref_ptr.h:92
ObjectProperty PROPERTY
Definition: InputStream.h:198
void setFileVersion(const std::string &d, int v)
Definition: InputStream.h:80
osg::ref_ptr< osg::Object > _dummyReadObject
Definition: InputStream.h:223
#define OSGDB_EXPORT
Definition: Export.h:39
const osgDB::Options * getOptions() const
Definition: InputStream.h:84
A plane class. It can be used to represent an infinite plane.
Definition: Plane.h:33
void readComponentArray(char *s, unsigned int numElements, unsigned int numComponentsPerElements, unsigned int componentSizeInBytes)
Definition: InputStream.h:157
InputStream & operator>>(osg::ref_ptr< osg::PrimitiveSet > &ptr)
Definition: InputStream.h:147
std::string _error
Definition: InputStream.h:60
std::istream & operator>>(std::istream &input, Vec2f &vec)
Definition: io_utils.h:46
std::stringstream * _dataDecompress
Definition: InputStream.h:226
void throwException(const std::string &msg)
Definition: InputStream.h:229
InputStream & operator>>(osg::ref_ptr< osg::Array > &ptr)
Definition: InputStream.h:146
InputStream & operator>>(osg::Image *&img)
Definition: InputStream.h:140
void advanceToCurrentEndBracket()
Definition: InputStream.h:154
Definition: Quat.h:29
InputStream & operator>>(int &i)
Definition: InputStream.h:93
const std::string & getField() const
Definition: InputStream.h:55
osg::ref_ptr< InputIterator > _in
Definition: InputStream.h:218
std::map< unsigned int, osg::ref_ptr< osg::Object > > IdentifierMap
Definition: InputStream.h:67
ObjectMark BEGIN_BRACKET
Definition: InputStream.h:199
bool matchString(const std::string &str)
Definition: InputStream.h:153
void readImage(const osg::Image *image, O &operation)
Definition: ImageUtils.h:59