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."
This commit is contained in:
@@ -31,11 +31,17 @@
|
||||
namespace osgDB
|
||||
{
|
||||
|
||||
class InputException
|
||||
class InputException : public osg::Referenced
|
||||
{
|
||||
public:
|
||||
InputException( const std::string& field, const std::string& err )
|
||||
: _field(field), _error(err) {}
|
||||
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; }
|
||||
@@ -65,25 +71,25 @@ public:
|
||||
bool getUseFloatMatrix() const { return _useFloatMatrix; }
|
||||
|
||||
// Serialization related functions
|
||||
InputStream& operator>>( bool& b ) { _in->readBool(b); return *this; }
|
||||
InputStream& operator>>( char& c ) { _in->readChar(c); return *this; }
|
||||
InputStream& operator>>( signed char& c ) { _in->readSChar(c); return *this; }
|
||||
InputStream& operator>>( unsigned char& c ) { _in->readUChar(c); return *this; }
|
||||
InputStream& operator>>( short& s ) { _in->readShort(s); return *this; }
|
||||
InputStream& operator>>( unsigned short& s ) { _in->readUShort(s); return *this; }
|
||||
InputStream& operator>>( int& i ) { _in->readInt(i); return *this; }
|
||||
InputStream& operator>>( unsigned int& i ) { _in->readUInt(i); return *this; }
|
||||
InputStream& operator>>( long& l ) { _in->readLong(l); return *this; }
|
||||
InputStream& operator>>( unsigned long& l ) { _in->readULong(l); return *this; }
|
||||
InputStream& operator>>( float& f ) { _in->readFloat(f); return *this; }
|
||||
InputStream& operator>>( double& d ) { _in->readDouble(d); return *this; }
|
||||
InputStream& operator>>( std::string& s ) { _in->readString(s); return *this; }
|
||||
InputStream& operator>>( std::istream& (*fn)(std::istream&) ) { _in->readStream(fn); return *this; }
|
||||
InputStream& operator>>( std::ios_base& (*fn)(std::ios_base&) ) { _in->readBase(fn); return *this; }
|
||||
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); return *this; }
|
||||
InputStream& operator>>( ObjectProperty& prop ) { _in->readProperty(prop); return *this; }
|
||||
InputStream& operator>>( ObjectMark& mark ) { _in->readMark(mark); 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 );
|
||||
@@ -116,9 +122,9 @@ public:
|
||||
{ ptr = static_cast<T*>(readObject()); return *this; }
|
||||
|
||||
// Convenient methods for reading
|
||||
inline bool matchString( const std::string& str );
|
||||
inline void advanceToCurrentEndBracket();
|
||||
inline void readWrappedString( std::string& str );
|
||||
bool matchString( const std::string& str );
|
||||
void advanceToCurrentEndBracket();
|
||||
void readWrappedString( std::string& str );
|
||||
void readCharArray( char* s, unsigned int size ) { _in->readCharArray(s, size); }
|
||||
|
||||
// Global reading functions
|
||||
@@ -134,8 +140,12 @@ public:
|
||||
void readSchema( std::istream& fin );
|
||||
void resetSchema();
|
||||
|
||||
// Exception handlers
|
||||
inline void throwException( const std::string& msg );
|
||||
const InputException* getException() const { return _exception.get(); }
|
||||
|
||||
protected:
|
||||
inline void checkStream() const;
|
||||
inline void checkStream();
|
||||
void setWrapperSchema( const std::string& name, const std::string& properties );
|
||||
|
||||
template<typename T>
|
||||
@@ -146,68 +156,22 @@ protected:
|
||||
|
||||
int _byteSwap;
|
||||
bool _useFloatMatrix;
|
||||
std::string _currentField;
|
||||
InputIterator* _in;
|
||||
bool _forceReadingImage;
|
||||
std::vector<std::string> _fields;
|
||||
osg::ref_ptr<InputIterator> _in;
|
||||
osg::ref_ptr<InputException> _exception;
|
||||
};
|
||||
|
||||
bool InputStream::matchString( const std::string& str )
|
||||
void InputStream::throwException( const std::string& msg )
|
||||
{
|
||||
if ( !isBinary() )
|
||||
{
|
||||
std::string s; *this >> s;
|
||||
if ( s==str ) return true;
|
||||
else _in->getStream()->seekg( -(int)(s.length()), std::ios::cur );
|
||||
}
|
||||
return false;
|
||||
_exception = new InputException(_fields, msg);
|
||||
}
|
||||
|
||||
void InputStream::advanceToCurrentEndBracket()
|
||||
{
|
||||
if ( isBinary() )
|
||||
return;
|
||||
|
||||
std::string passString;
|
||||
unsigned int blocks = 0;
|
||||
while ( !_in->getStream()->eof() )
|
||||
{
|
||||
passString.clear();
|
||||
*this >> passString;
|
||||
|
||||
if ( passString=="}" )
|
||||
{
|
||||
if ( blocks<=0 ) return;
|
||||
else blocks--;
|
||||
}
|
||||
else if ( passString=="{" )
|
||||
blocks++;
|
||||
}
|
||||
}
|
||||
|
||||
void InputStream::readWrappedString( std::string& str )
|
||||
{
|
||||
*this >> str;
|
||||
if ( !isBinary() )
|
||||
{
|
||||
if ( str[0]=='\"' )
|
||||
{
|
||||
if ( str.size()==1 || (*str.rbegin())!='\"' )
|
||||
{
|
||||
char ch;
|
||||
do
|
||||
{
|
||||
_in->getStream()->get( ch ); checkStream();
|
||||
str.append( 1, ch );
|
||||
} while ( ch!='\"' );
|
||||
}
|
||||
str = str.substr(1, str.size()-2);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void InputStream::checkStream() const
|
||||
void InputStream::checkStream()
|
||||
{
|
||||
_in->checkStream();
|
||||
if ( _in->isFailed() )
|
||||
throw InputException(_currentField, "InputStream: Failed to read from stream.");
|
||||
throwException( "InputStream: Failed to read from stream." );
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -30,11 +30,17 @@
|
||||
namespace osgDB
|
||||
{
|
||||
|
||||
class OutputException
|
||||
class OutputException : public osg::Referenced
|
||||
{
|
||||
public:
|
||||
OutputException( const std::string& field, const std::string& err )
|
||||
: _field(field), _error(err) {}
|
||||
OutputException( 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; }
|
||||
@@ -70,6 +76,7 @@ public:
|
||||
virtual ~OutputStream();
|
||||
|
||||
bool isBinary() const { return _out->isBinary(); }
|
||||
const std::string& getSchemaName() const { return _schemaName; }
|
||||
|
||||
void setWriteImageHint( WriteImageHint hint ) { _writeImageHint = hint; }
|
||||
WriteImageHint getWriteImageHint() const { return _writeImageHint; }
|
||||
@@ -125,7 +132,7 @@ public:
|
||||
{ writeObject(ptr.get()); return *this; }
|
||||
|
||||
// Convenient methods for writing
|
||||
inline void writeWrappedString( const std::string& str );
|
||||
void writeWrappedString( const std::string& str );
|
||||
void writeCharArray( const char* s, unsigned int size ) { _out->writeCharArray(s, size); }
|
||||
|
||||
// Global writing functions
|
||||
@@ -140,6 +147,10 @@ public:
|
||||
// Schema handlers
|
||||
void writeSchema( std::ostream& fout );
|
||||
|
||||
// Exception handlers
|
||||
inline void throwException( const std::string& msg );
|
||||
const OutputException* getException() const { return _exception.get(); }
|
||||
|
||||
protected:
|
||||
template<typename T>
|
||||
void writeArrayImplementation( const T*, int writeSize, unsigned int numInRow=1 );
|
||||
@@ -151,21 +162,17 @@ protected:
|
||||
ObjectMap _objectMap;
|
||||
|
||||
WriteImageHint _writeImageHint;
|
||||
std::string _currentField;
|
||||
std::vector<std::string> _fields;
|
||||
std::string _schemaName;
|
||||
std::string _compressorName;
|
||||
std::stringstream _compressSource;
|
||||
OutputIterator* _out;
|
||||
osg::ref_ptr<OutputIterator> _out;
|
||||
osg::ref_ptr<OutputException> _exception;
|
||||
};
|
||||
|
||||
void OutputStream::writeWrappedString( const std::string& str )
|
||||
void OutputStream::throwException( const std::string& msg )
|
||||
{
|
||||
if ( !isBinary() )
|
||||
{
|
||||
std::string wrappedStr = std::string("\"") + str + std::string("\"");
|
||||
*this << wrappedStr;
|
||||
}
|
||||
else
|
||||
*this << str;
|
||||
_exception = new OutputException(_fields, msg);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -137,18 +137,32 @@ public:
|
||||
virtual bool read( InputStream& is, osg::Object& obj )
|
||||
{
|
||||
C& object = OBJECT_CAST<C&>(obj);
|
||||
if ( !is.isBinary() && !is.matchString(_name) )
|
||||
return true;
|
||||
if ( is.isBinary() )
|
||||
{
|
||||
bool ok = false; is >> ok;
|
||||
if ( !ok ) return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
if ( !is.matchString(_name) )
|
||||
return true;
|
||||
}
|
||||
return (*_reader)(is, object);
|
||||
}
|
||||
|
||||
virtual bool write( OutputStream& os, const osg::Object& obj )
|
||||
{
|
||||
const C& object = OBJECT_CAST<const C&>(obj);
|
||||
if ( !os.isBinary() )
|
||||
bool ok = (*_checker)(object);
|
||||
if ( os.isBinary() )
|
||||
{
|
||||
if ( !(*_checker)(object) ) return true;
|
||||
os << _name;
|
||||
os << ok;
|
||||
if ( !ok ) return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
if ( !ok ) return true;
|
||||
os << PROPERTY(_name.c_str());
|
||||
}
|
||||
return (*_writer)(os, object);
|
||||
}
|
||||
@@ -158,6 +172,8 @@ public:
|
||||
protected:
|
||||
std::string _name;
|
||||
Checker _checker;
|
||||
|
||||
public:
|
||||
Reader _reader;
|
||||
Writer _writer;
|
||||
};
|
||||
@@ -219,7 +235,7 @@ public:
|
||||
}
|
||||
else if ( ParentType::_defaultValue!=(object.*_getter)() )
|
||||
{
|
||||
os << ParentType::_name;
|
||||
os << PROPERTY((ParentType::_name).c_str());
|
||||
if ( _useHex ) os << std::hex;
|
||||
os << (object.*_getter)();
|
||||
if ( _useHex ) os << std::dec;
|
||||
@@ -228,9 +244,11 @@ public:
|
||||
return true;
|
||||
}
|
||||
|
||||
protected:
|
||||
public:
|
||||
Getter _getter;
|
||||
Setter _setter;
|
||||
|
||||
protected:
|
||||
bool _useHex;
|
||||
};
|
||||
|
||||
@@ -274,12 +292,12 @@ public:
|
||||
}
|
||||
else if ( ParentType::_defaultValue!=(object.*_getter)() )
|
||||
{
|
||||
os << ParentType::_name << (object.*_getter)() << std::endl;
|
||||
os << PROPERTY((ParentType::_name).c_str()) << (object.*_getter)() << std::endl;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
protected:
|
||||
public:
|
||||
Getter _getter;
|
||||
Setter _setter;
|
||||
};
|
||||
@@ -323,7 +341,7 @@ public:
|
||||
}
|
||||
else if ( ParentType::_defaultValue!=(object.*_getter)() )
|
||||
{
|
||||
os << ParentType::_name << (object.*_getter)() << std::endl;
|
||||
os << PROPERTY((ParentType::_name).c_str()) << (object.*_getter)() << std::endl;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
@@ -343,6 +361,7 @@ protected:
|
||||
}
|
||||
}
|
||||
|
||||
public:
|
||||
Getter _getter;
|
||||
Setter _setter;
|
||||
};
|
||||
@@ -385,12 +404,12 @@ public:
|
||||
}
|
||||
else if ( ParentType::_defaultValue!=(object.*_getter)() )
|
||||
{
|
||||
os << ParentType::_name << GLENUM((object.*_getter)()) << std::endl;
|
||||
os << PROPERTY((ParentType::_name).c_str()) << GLENUM((object.*_getter)()) << std::endl;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
protected:
|
||||
public:
|
||||
Getter _getter;
|
||||
Setter _setter;
|
||||
};
|
||||
@@ -435,14 +454,14 @@ public:
|
||||
}
|
||||
else if ( ParentType::_defaultValue!=(object.*_getter)() )
|
||||
{
|
||||
os << ParentType::_name;
|
||||
os << PROPERTY((ParentType::_name).c_str());
|
||||
os.writeWrappedString( (object.*_getter)() );
|
||||
os << std::endl;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
protected:
|
||||
public:
|
||||
Getter _getter;
|
||||
Setter _setter;
|
||||
};
|
||||
@@ -498,7 +517,7 @@ public:
|
||||
}
|
||||
else if ( ParentType::_defaultValue!=(object.*_getter)() )
|
||||
{
|
||||
os << ParentType::_name << hasObject;
|
||||
os << PROPERTY((ParentType::_name).c_str()) << hasObject;
|
||||
if ( hasObject )
|
||||
{
|
||||
os << BEGIN_BRACKET << std::endl;
|
||||
@@ -510,7 +529,7 @@ public:
|
||||
return true;
|
||||
}
|
||||
|
||||
protected:
|
||||
public:
|
||||
Getter _getter;
|
||||
Setter _setter;
|
||||
};
|
||||
@@ -566,7 +585,7 @@ public:
|
||||
}
|
||||
else if ( ParentType::_defaultValue!=(object.*_getter)() )
|
||||
{
|
||||
os << ParentType::_name << hasObject;
|
||||
os << PROPERTY((ParentType::_name).c_str()) << hasObject;
|
||||
if ( hasObject )
|
||||
{
|
||||
os << BEGIN_BRACKET << std::endl;
|
||||
@@ -578,7 +597,7 @@ public:
|
||||
return true;
|
||||
}
|
||||
|
||||
protected:
|
||||
public:
|
||||
Getter _getter;
|
||||
Setter _setter;
|
||||
};
|
||||
@@ -631,14 +650,16 @@ public:
|
||||
}
|
||||
else if ( ParentType::_defaultValue!=(object.*_getter)() )
|
||||
{
|
||||
os << ParentType::_name << getString((object.*_getter)()) << std::endl;
|
||||
os << PROPERTY((ParentType::_name).c_str()) << getString((object.*_getter)()) << std::endl;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
protected:
|
||||
public:
|
||||
Getter _getter;
|
||||
Setter _setter;
|
||||
|
||||
protected:
|
||||
IntLookup _lookup;
|
||||
};
|
||||
|
||||
@@ -706,7 +727,7 @@ public:
|
||||
}
|
||||
else if ( size>0 )
|
||||
{
|
||||
os << ParentType::_name << size << BEGIN_BRACKET << std::endl;
|
||||
os << PROPERTY((ParentType::_name).c_str()) << size << BEGIN_BRACKET << std::endl;
|
||||
for ( ConstIterator itr=list.begin();
|
||||
itr!=list.end(); ++itr )
|
||||
{
|
||||
@@ -717,7 +738,7 @@ public:
|
||||
return true;
|
||||
}
|
||||
|
||||
protected:
|
||||
public:
|
||||
Getter _getter;
|
||||
Setter _setter;
|
||||
};
|
||||
@@ -845,7 +866,7 @@ protected:
|
||||
serializer->add(#VALUE, MyClass::VALUE)
|
||||
|
||||
#define END_ENUM_SERIALIZER() \
|
||||
wrapper->addSerializer(serializer.get()); }
|
||||
wrapper->addSerializer(serializer); }
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -8,7 +8,7 @@
|
||||
namespace osgDB
|
||||
{
|
||||
|
||||
class OSGDB_EXPORT OutputIterator
|
||||
class OSGDB_EXPORT OutputIterator : public osg::Referenced
|
||||
{
|
||||
public:
|
||||
OutputIterator() : _out(0) {}
|
||||
@@ -44,7 +44,7 @@ protected:
|
||||
std::ostream* _out;
|
||||
};
|
||||
|
||||
class OSGDB_EXPORT InputIterator
|
||||
class OSGDB_EXPORT InputIterator : public osg::Referenced
|
||||
{
|
||||
public:
|
||||
InputIterator() : _in(0), _failed(false) {}
|
||||
@@ -54,7 +54,9 @@ public:
|
||||
std::istream* getStream() { return _in; }
|
||||
const std::istream* getStream() const { return _in; }
|
||||
|
||||
void checkStream() const { if (_in->rdstate()&_in->failbit) _failed = true; }
|
||||
bool isFailed() const { return _failed; }
|
||||
|
||||
virtual bool isBinary() const = 0;
|
||||
|
||||
virtual void readBool( bool& b ) = 0;
|
||||
@@ -79,9 +81,6 @@ public:
|
||||
virtual void readCharArray( char* s, unsigned int size ) = 0;
|
||||
|
||||
protected:
|
||||
void checkStream() const
|
||||
{ if (_in->rdstate()&_in->failbit) _failed = true; }
|
||||
|
||||
std::istream* _in;
|
||||
mutable bool _failed;
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user