InputStream 9.7 KB

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