InputStream
Go to the documentation of this file.
/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2010 Robert Osfield
*
* This library is open source and may be redistributed and/or modified under
* the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
* (at your option) any later version. The full license is in LICENSE file
* included with this distribution, and on the openscenegraph.org website.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* OpenSceneGraph Public License for more details.
*/
// Written by Wang Rui, (C) 2010
#ifndef OSGDB_INPUTSTREAM
#define OSGDB_INPUTSTREAM
#include <osg/Endian>
#include <osg/Vec2>
#include <osg/Vec3>
#include <osg/Vec4>
#include <osg/Vec2i>
#include <osg/Vec3i>
#include <osg/Vec4i>
#include <osg/Vec2ui>
#include <osg/Vec3ui>
#include <osg/Vec4ui>
#include <osg/Quat>
#include <osg/Matrix>
#include <osg/BoundingBox>
#include <osg/BoundingSphere>
#include <osg/Array>
#include <osg/PrimitiveSet>
#include <osgDB/ReaderWriter>
#include <osgDB/StreamOperator>
#include <osgDB/Options>
#include <iostream>
#include <sstream>
namespace osgDB
{
class InputException : public osg::Referenced
{
public:
InputException( const std::vector<std::string>& fields, const std::string& err ) : _error(err)
{
for ( unsigned int i=0; i<fields.size(); ++i )
{
_field += fields[i];
_field += " ";
}
}
const std::string& getField() const { return _field; }
const std::string& getError() const { return _error; }
protected:
std::string _field;
std::string _error;
};
class OSGDB_EXPORT InputStream
{
public:
typedef std::map< unsigned int, osg::ref_ptr<osg::Array> > ArrayMap;
typedef std::map< unsigned int, osg::ref_ptr<osg::Object> > IdentifierMap;
enum ReadType
{
READ_UNKNOWN = 0,
READ_SCENE,
READ_IMAGE,
READ_OBJECT
};
InputStream( const osgDB::Options* options );
virtual ~InputStream();
void setFileVersion( const std::string& d, int v ) { _domainVersionMap[d] = v; }
int getFileVersion( const std::string& d=std::string() ) const;
bool isBinary() const { return _in->isBinary(); }
const osgDB::Options* getOptions() const { return _options.get(); }
// Serialization related functions
InputStream& operator>>( bool& b ) { _in->readBool(b); checkStream(); return *this; }
InputStream& operator>>( char& c ) { _in->readChar(c); checkStream(); return *this; }
InputStream& operator>>( signed char& c ) { _in->readSChar(c); checkStream(); return *this; }
InputStream& operator>>( unsigned char& c ) { _in->readUChar(c); checkStream(); return *this; }
InputStream& operator>>( short& s ) { _in->readShort(s); checkStream(); return *this; }
InputStream& operator>>( unsigned short& s ) { _in->readUShort(s); checkStream(); return *this; }
InputStream& operator>>( int& i ) { _in->readInt(i); checkStream(); return *this; }
InputStream& operator>>( unsigned int& i ) { _in->readUInt(i); checkStream(); return *this; }
InputStream& operator>>( long& l ) { _in->readLong(l); checkStream(); return *this; }
InputStream& operator>>( unsigned long& l ) { _in->readULong(l); checkStream(); return *this; }
InputStream& operator>>( float& f ) { _in->readFloat(f); checkStream(); return *this; }
InputStream& operator>>( double& d ) { _in->readDouble(d); checkStream(); return *this; }
InputStream& operator>>( std::string& s ) { _in->readString(s); checkStream(); return *this; }
InputStream& operator>>( std::istream& (*fn)(std::istream&) ) { _in->readStream(fn); checkStream(); return *this; }
InputStream& operator>>( std::ios_base& (*fn)(std::ios_base&) ) { _in->readBase(fn); checkStream(); return *this; }
InputStream& operator>>( ObjectGLenum& value ) { _in->readGLenum(value); checkStream(); return *this; }
InputStream& operator>>( ObjectProperty& prop ) { _in->readProperty(prop); checkStream(); return *this; }
InputStream& operator>>( ObjectMark& mark ) { _in->readMark(mark); checkStream(); return *this; }
InputStream& operator>>( osg::Vec2b& v );
InputStream& operator>>( osg::Vec3b& v );
InputStream& operator>>( osg::Vec4b& v );
InputStream& operator>>( osg::Vec2ub& v );
InputStream& operator>>( osg::Vec3ub& v );
InputStream& operator>>( osg::Vec4ub& v );
InputStream& operator>>( osg::Vec2s& v );
InputStream& operator>>( osg::Vec3s& v );
InputStream& operator>>( osg::Vec4s& v );
InputStream& operator>>( osg::Vec2us& v );
InputStream& operator>>( osg::Vec3us& v );
InputStream& operator>>( osg::Vec4us& v );
InputStream& operator>>( osg::Vec2i& v );
InputStream& operator>>( osg::Vec3i& v );
InputStream& operator>>( osg::Vec4i& v );
InputStream& operator>>( osg::Vec2ui& v );
InputStream& operator>>( osg::Vec3ui& v );
InputStream& operator>>( osg::Vec4ui& v );
InputStream& operator>>( osg::Vec2f& v );
InputStream& operator>>( osg::Vec3f& v );
InputStream& operator>>( osg::Vec4f& v );
InputStream& operator>>( osg::Vec2d& v );
InputStream& operator>>( osg::Vec3d& v );
InputStream& operator>>( osg::Vec4d& v );
InputStream& operator>>( osg::Quat& q );
InputStream& operator>>( osg::Plane& p );
InputStream& operator>>( osg::Matrixf& mat );
InputStream& operator>>( osg::Matrixd& mat );
InputStream& operator>>( osg::BoundingBoxf& bb );
InputStream& operator>>( osg::BoundingBoxd& bb );
InputStream& operator>>( osg::BoundingSpheref& bs );
InputStream& operator>>( osg::BoundingSphered& bs );
InputStream& operator>>( osg::ref_ptr<osg::Image>& ptr ) { ptr = readImage(); return *this; }
InputStream& operator>>( osg::ref_ptr<osg::Array>& ptr ) { if (_fileVersion>=112) ptr = readObjectOfType<osg::Array>(); else ptr = readArray(); return *this; }
InputStream& operator>>( osg::ref_ptr<osg::PrimitiveSet>& ptr ) { if (_fileVersion>=112) ptr = readObjectOfType<osg::PrimitiveSet>(); else ptr = readPrimitiveSet(); return *this; }
template<typename T> InputStream& operator>>( osg::ref_ptr<T>& ptr )
{ ptr = readObjectOfType<T>(); return *this; }
// Convenient methods for reading
bool matchString( const std::string& str ) { return _in->matchString(str); }
void advanceToCurrentEndBracket() { _in->advanceToCurrentEndBracket(); }
void readWrappedString( std::string& str ) { _in->readWrappedString(str); checkStream(); }
void readCharArray( char* s, unsigned int size ) { _in->readCharArray(s, size); }
void readComponentArray( char* s, unsigned int numElements, unsigned int numComponentsPerElements, unsigned int componentSizeInBytes) { _in->readComponentArray( s, numElements, numComponentsPerElements, componentSizeInBytes); }
// readSize() use unsigned int for all sizes.
unsigned int readSize() { unsigned int size; *this>>size; return size; }
// Global reading functions
osg::ref_ptr<osg::Array> readArray();
osg::ref_ptr<osg::PrimitiveSet> readPrimitiveSet();
osg::ref_ptr<osg::Image> readImage(bool readFromExternal=true);
template<typename T>
osg::ref_ptr<T> readObjectOfType()
{
osg::ref_ptr<osg::Object> obj = readObject();
T* ptr = dynamic_cast<T*>(obj.get());
if (ptr) { return ptr; }
else return 0;
}
osg::ref_ptr<osg::Object> readObject( osg::Object* existingObj=0 );
osg::ref_ptr<osg::Object> readObjectFields( const std::string& className, unsigned int id, osg::Object* existingObj=0);
template<typename T>
osg::ref_ptr<T> readObjectFieldsOfType( const std::string& className, unsigned int id, osg::Object* existingObj=0)
{
osg::ref_ptr<osg::Object> obj = readObjectFields(className, id, existingObj);
T* ptr = dynamic_cast<T*>(obj.get());
if (ptr) { return ptr; }
else return 0;
}
void setInputIterator( InputIterator* ii ) { _in = ii; }
ReadType start( InputIterator* );
void decompress();
// Schema handlers
void readSchema( std::istream& fin );
void resetSchema();
// Exception handlers
inline void throwException( const std::string& msg );
const InputException* getException() const { return _exception.get(); }
// Property & mask variables
ObjectProperty PROPERTY;
ObjectMark BEGIN_BRACKET;
ObjectMark END_BRACKET;
protected:
inline void checkStream();
void setWrapperSchema( const std::string& name, const std::string& properties );
template<typename T>
void readArrayImplementation( T* a, unsigned int numComponentsPerElements, unsigned int componentSizeInBytes );
ArrayMap _arrayMap;
IdentifierMap _identifierMap;
typedef std::map<std::string, int> VersionMap;
VersionMap _domainVersionMap;
int _fileVersion;
bool _useSchemaData;
bool _forceReadingImage;
std::vector<std::string> _fields;
osg::ref_ptr<InputIterator> _in;
osg::ref_ptr<InputException> _exception;
osg::ref_ptr<const osgDB::Options> _options;
// object to used to read field properties that will be discarded.
osg::ref_ptr<osg::Object> _dummyReadObject;
// store here to avoid a new and a leak in InputStream::decompress
std::stringstream* _dataDecompress;
};
void InputStream::throwException( const std::string& msg )
{
_exception = new InputException(_fields, msg);
}
void InputStream::checkStream()
{
_in->checkStream();
if ( _in->isFailed() )
throwException( "InputStream: Failed to read from stream." );
}
}
#endif
Array
BoundingBox
BoundingSphere
Endian
Matrix
Options
PrimitiveSet
Quat
ReaderWriter
StreamOperator
Vec2
Vec2i
Vec2ui
Vec3
Vec3i
Vec3ui
Vec4
Vec4i
Vec4ui
osgDB::InputException
Definition InputStream:44
osgDB::InputException::getError
const std::string & getError() const
Definition InputStream:56
osgDB::InputException::getField
const std::string & getField() const
Definition InputStream:55
osgDB::InputException::_field
std::string _field
Definition InputStream:59
osgDB::InputException::_error
std::string _error
Definition InputStream:60
osgDB::InputException::InputException
InputException(const std::vector< std::string > &fields, const std::string &err)
Definition InputStream:46
osgDB::InputIterator
Definition StreamOperator:83
osgDB::InputStream::operator>>
InputStream & operator>>(osg::Vec4b &v)
osgDB::InputStream::operator>>
InputStream & operator>>(osg::Vec3i &v)
osgDB::InputStream::_dataDecompress
std::stringstream * _dataDecompress
Definition InputStream:229
osgDB::InputStream::operator>>
InputStream & operator>>(bool &b)
Definition InputStream:87
osgDB::InputStream::_exception
osg::ref_ptr< InputException > _exception
Definition InputStream:222
osgDB::InputStream::operator>>
InputStream & operator>>(osg::ref_ptr< T > &ptr)
Definition InputStream:144
osgDB::InputStream::VersionMap
std::map< std::string, int > VersionMap
Definition InputStream:215
osgDB::InputStream::readCharArray
void readCharArray(char *s, unsigned int size)
Definition InputStream:151
osgDB::InputStream::readArrayImplementation
void readArrayImplementation(T *a, unsigned int numComponentsPerElements, unsigned int componentSizeInBytes)
osgDB::InputStream::_fields
std::vector< std::string > _fields
Definition InputStream:220
osgDB::InputStream::operator>>
InputStream & operator>>(ObjectProperty &prop)
Definition InputStream:104
osgDB::InputStream::_arrayMap
ArrayMap _arrayMap
Definition InputStream:212
osgDB::InputStream::operator>>
InputStream & operator>>(signed char &c)
Definition InputStream:89
osgDB::InputStream::operator>>
InputStream & operator>>(osg::Vec2i &v)
osgDB::InputStream::setInputIterator
void setInputIterator(InputIterator *ii)
set an input iterator, used directly when not using InputStream with a traditional file related strea...
Definition InputStream:185
osgDB::InputStream::operator>>
InputStream & operator>>(osg::Vec2d &v)
osgDB::InputStream::operator>>
InputStream & operator>>(osg::Vec3d &v)
osgDB::InputStream::_dummyReadObject
osg::ref_ptr< osg::Object > _dummyReadObject
Definition InputStream:226
osgDB::InputStream::setFileVersion
void setFileVersion(const std::string &d, int v)
Definition InputStream:80
osgDB::InputStream::operator>>
InputStream & operator>>(osg::Vec3s &v)
osgDB::InputStream::readPrimitiveSet
osg::ref_ptr< osg::PrimitiveSet > readPrimitiveSet()
osgDB::InputStream::operator>>
InputStream & operator>>(unsigned short &s)
Definition InputStream:92
osgDB::InputStream::operator>>
InputStream & operator>>(osg::ref_ptr< osg::Image > &ptr)
Definition InputStream:140
osgDB::InputStream::operator>>
InputStream & operator>>(int &i)
Definition InputStream:93
osgDB::InputStream::matchString
bool matchString(const std::string &str)
Definition InputStream:148
osgDB::InputStream::operator>>
InputStream & operator>>(osg::Vec3ui &v)
osgDB::InputStream::throwException
void throwException(const std::string &msg)
Definition InputStream:232
osgDB::InputStream::operator>>
InputStream & operator>>(osg::Vec2ui &v)
osgDB::InputStream::_options
osg::ref_ptr< const osgDB::Options > _options
Definition InputStream:223
osgDB::InputStream::operator>>
InputStream & operator>>(unsigned int &i)
Definition InputStream:94
osgDB::InputStream::start
ReadType start(InputIterator *)
start reading from InputStream treating it as a traditional file related stream, handles headers and ...
osgDB::InputStream::operator>>
InputStream & operator>>(osg::Quat &q)
osgDB::InputStream::operator>>
InputStream & operator>>(std::istream &(*fn)(std::istream &))
Definition InputStream:100
osgDB::InputStream::readObjectFields
osg::ref_ptr< osg::Object > readObjectFields(const std::string &className, unsigned int id, osg::Object *existingObj=0)
osgDB::InputStream::operator>>
InputStream & operator>>(osg::Vec3b &v)
osgDB::InputStream::getException
const InputException * getException() const
Definition InputStream:198
osgDB::InputStream::readWrappedString
void readWrappedString(std::string &str)
Definition InputStream:150
osgDB::InputStream::_fileVersion
int _fileVersion
Definition InputStream:217
osgDB::InputStream::checkStream
void checkStream()
Definition InputStream:237
osgDB::InputStream::operator>>
InputStream & operator>>(float &f)
Definition InputStream:97
osgDB::InputStream::isBinary
bool isBinary() const
Definition InputStream:83
osgDB::InputStream::ReadType
ReadType
Definition InputStream:70
osgDB::InputStream::READ_SCENE
@ READ_SCENE
Definition InputStream:72
osgDB::InputStream::READ_OBJECT
@ READ_OBJECT
Definition InputStream:74
osgDB::InputStream::READ_UNKNOWN
@ READ_UNKNOWN
Definition InputStream:71
osgDB::InputStream::READ_IMAGE
@ READ_IMAGE
Definition InputStream:73
osgDB::InputStream::operator>>
InputStream & operator>>(osg::Vec3f &v)
osgDB::InputStream::operator>>
InputStream & operator>>(osg::Vec4s &v)
osgDB::InputStream::operator>>
InputStream & operator>>(osg::ref_ptr< osg::Array > &ptr)
Definition InputStream:141
osgDB::InputStream::operator>>
InputStream & operator>>(osg::ref_ptr< osg::PrimitiveSet > &ptr)
Definition InputStream:142
osgDB::InputStream::operator>>
InputStream & operator>>(osg::Vec3us &v)
osgDB::InputStream::operator>>
InputStream & operator>>(unsigned long &l)
Definition InputStream:96
osgDB::InputStream::getOptions
const osgDB::Options * getOptions() const
Definition InputStream:84
osgDB::InputStream::IdentifierMap
std::map< unsigned int, osg::ref_ptr< osg::Object > > IdentifierMap
Definition InputStream:67
osgDB::InputStream::~InputStream
virtual ~InputStream()
osgDB::InputStream::ArrayMap
std::map< unsigned int, osg::ref_ptr< osg::Array > > ArrayMap
Definition InputStream:66
osgDB::InputStream::readArray
osg::ref_ptr< osg::Array > readArray()
osgDB::InputStream::operator>>
InputStream & operator>>(osg::Matrixf &mat)
osgDB::InputStream::decompress
void decompress()
osgDB::InputStream::setWrapperSchema
void setWrapperSchema(const std::string &name, const std::string &properties)
osgDB::InputStream::operator>>
InputStream & operator>>(osg::BoundingBoxd &bb)
osgDB::InputStream::resetSchema
void resetSchema()
osgDB::InputStream::readImage
osg::ref_ptr< osg::Image > readImage(bool readFromExternal=true)
osgDB::InputStream::InputStream
InputStream(const osgDB::Options *options)
osgDB::InputStream::readObject
osg::ref_ptr< osg::Object > readObject(osg::Object *existingObj=0)
osgDB::InputStream::operator>>
InputStream & operator>>(osg::BoundingSphered &bs)
osgDB::InputStream::operator>>
InputStream & operator>>(osg::Vec4i &v)
osgDB::InputStream::operator>>
InputStream & operator>>(char &c)
Definition InputStream:88
osgDB::InputStream::END_BRACKET
ObjectMark END_BRACKET
Definition InputStream:203
osgDB::InputStream::_forceReadingImage
bool _forceReadingImage
Definition InputStream:219
osgDB::InputStream::operator>>
InputStream & operator>>(ObjectGLenum &value)
Definition InputStream:103
osgDB::InputStream::readComponentArray
void readComponentArray(char *s, unsigned int numElements, unsigned int numComponentsPerElements, unsigned int componentSizeInBytes)
Definition InputStream:152
osgDB::InputStream::operator>>
InputStream & operator>>(osg::Vec2b &v)
osgDB::InputStream::readObjectFieldsOfType
osg::ref_ptr< T > readObjectFieldsOfType(const std::string &className, unsigned int id, osg::Object *existingObj=0)
Definition InputStream:176
osgDB::InputStream::operator>>
InputStream & operator>>(long &l)
Definition InputStream:95
osgDB::InputStream::_in
osg::ref_ptr< InputIterator > _in
Definition InputStream:221
osgDB::InputStream::BEGIN_BRACKET
ObjectMark BEGIN_BRACKET
Definition InputStream:202
osgDB::InputStream::getFileVersion
int getFileVersion(const std::string &d=std::string()) const
osgDB::InputStream::operator>>
InputStream & operator>>(osg::Vec3ub &v)
osgDB::InputStream::operator>>
InputStream & operator>>(osg::Matrixd &mat)
osgDB::InputStream::operator>>
InputStream & operator>>(osg::Plane &p)
osgDB::InputStream::operator>>
InputStream & operator>>(std::ios_base &(*fn)(std::ios_base &))
Definition InputStream:101
osgDB::InputStream::operator>>
InputStream & operator>>(short &s)
Definition InputStream:91
osgDB::InputStream::operator>>
InputStream & operator>>(double &d)
Definition InputStream:98
osgDB::InputStream::operator>>
InputStream & operator>>(osg::Vec2s &v)
osgDB::InputStream::operator>>
InputStream & operator>>(osg::Vec2us &v)
osgDB::InputStream::operator>>
InputStream & operator>>(osg::Vec4ui &v)
osgDB::InputStream::operator>>
InputStream & operator>>(osg::Vec4us &v)
osgDB::InputStream::operator>>
InputStream & operator>>(osg::Vec2ub &v)
osgDB::InputStream::readSchema
void readSchema(std::istream &fin)
osgDB::InputStream::operator>>
InputStream & operator>>(std::string &s)
Definition InputStream:99
osgDB::InputStream::_identifierMap
IdentifierMap _identifierMap
Definition InputStream:213
osgDB::InputStream::readSize
unsigned int readSize()
Definition InputStream:155
osgDB::InputStream::operator>>
InputStream & operator>>(osg::BoundingBoxf &bb)
osgDB::InputStream::operator>>
InputStream & operator>>(osg::Vec4f &v)
osgDB::InputStream::operator>>
InputStream & operator>>(osg::Vec2f &v)
osgDB::InputStream::advanceToCurrentEndBracket
void advanceToCurrentEndBracket()
Definition InputStream:149
osgDB::InputStream::operator>>
InputStream & operator>>(osg::Vec4d &v)
osgDB::InputStream::_domainVersionMap
VersionMap _domainVersionMap
Definition InputStream:216
osgDB::InputStream::operator>>
InputStream & operator>>(osg::BoundingSpheref &bs)
osgDB::InputStream::readObjectOfType
osg::ref_ptr< T > readObjectOfType()
Definition InputStream:163
osgDB::InputStream::operator>>
InputStream & operator>>(osg::Vec4ub &v)
osgDB::InputStream::_useSchemaData
bool _useSchemaData
Definition InputStream:218
osgDB::InputStream::operator>>
InputStream & operator>>(unsigned char &c)
Definition InputStream:90
osgDB::InputStream::PROPERTY
ObjectProperty PROPERTY
Definition InputStream:201
osgDB::InputStream::operator>>
InputStream & operator>>(ObjectMark &mark)
Definition InputStream:105
osgDB::ObjectMark
Definition DataTypes:129
osgDB::ObjectProperty
Definition DataTypes:105
osgDB::Options
Definition Options:30
osg::Matrixd
Definition Matrixd:27
osg::Matrixf
Definition Matrixf:27
osg::Object
Definition Object:61
osg::Plane
A plane class. It can be used to represent an infinite plane.
Definition Plane:34
osg::Quat
Definition Quat:30
osg::Referenced
Definition Referenced:44
osg::Vec2b
Definition Vec2b:26
osg::Vec2d
Definition Vec2d:29
osg::Vec2f
Definition Vec2f:29
osg::Vec2i
Definition Vec2i:22
osg::Vec2s
Definition Vec2s:20
osg::Vec2ub
Definition Vec2ub:22
osg::Vec2ui
Definition Vec2ui:22
osg::Vec2us
Definition Vec2us:20
osg::Vec3b
Definition Vec3b:26
osg::Vec3d
Definition Vec3d:30
osg::Vec3f
Definition Vec3f:29
osg::Vec3i
Definition Vec3i:22
osg::Vec3s
Definition Vec3s:20
osg::Vec3ub
Definition Vec3ub:26
osg::Vec3ui
Definition Vec3ui:22
osg::Vec3us
Definition Vec3us:20
osg::Vec4b
Definition Vec4b:26
osg::Vec4d
Definition Vec4d:29
osg::Vec4f
Definition Vec4f:28
osg::Vec4i
Definition Vec4i:22
osg::Vec4s
Definition Vec4s:26
osg::Vec4ub
Definition Vec4ub:28
osg::Vec4ui
Definition Vec4ui:22
osg::Vec4us
Definition Vec4us:26
osg::ref_ptr
Definition ref_ptr:32
osg::ref_ptr::get
T * get() const
Definition ref_ptr:117
osgDB
Definition Archive:24
osg::BoundingBoxd
BoundingBoxImpl< Vec3d > BoundingBoxd
Definition BoundingBox:252
osg::BoundingBoxf
BoundingBoxImpl< Vec3f > BoundingBoxf
Definition BoundingBox:251
osg::BoundingSpheref
BoundingSphereImpl< Vec3f > BoundingSpheref
Definition BoundingSphere:308
osg::BoundingSphered
BoundingSphereImpl< Vec3d > BoundingSphered
Definition BoundingSphere:309
OSGDB_EXPORT
#define OSGDB_EXPORT
Definition osgDB/Export:39
osgDB::ObjectGLenum
Definition DataTypes:94