StreamOperator 3.73 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
#ifndef OSGDB_STREAMOPERATOR
#define OSGDB_STREAMOPERATOR

#include <iostream>
#include <sstream>
#include <osgDB/DataTypes>

namespace osgDB
{

class OSGDB_EXPORT OutputIterator : public osg::Referenced
{
public:
    OutputIterator() : _out(0) {}
    virtual ~OutputIterator() {}
    
    void setStream( std::ostream* ostream ) { _out = ostream; }
    std::ostream* getStream() { return _out; }
    const std::ostream* getStream() const { return _out; }
    
    virtual bool isBinary() const = 0;
    
    virtual void writeBool( bool b ) = 0;
    virtual void writeChar( char c ) = 0;
    virtual void writeUChar( unsigned char c ) = 0;
    virtual void writeShort( short s ) = 0;
    virtual void writeUShort( unsigned short s ) = 0;
    virtual void writeInt( int i ) = 0;
    virtual void writeUInt( unsigned int i ) = 0;
    virtual void writeLong( long l ) = 0;
    virtual void writeULong( unsigned long l ) = 0;
    virtual void writeFloat( float f ) = 0;
    virtual void writeDouble( double d ) = 0;
    virtual void writeString( const std::string& s ) = 0;
    virtual void writeStream( std::ostream& (*fn)(std::ostream&) ) = 0;
    virtual void writeBase( std::ios_base& (*fn)(std::ios_base&) ) = 0;
    
    virtual void writeGLenum( const ObjectGLenum& value ) = 0;
    virtual void writeProperty( const ObjectProperty& prop ) = 0;
    virtual void writeMark( const ObjectMark& mark ) = 0;
    virtual void writeCharArray( const char* s, unsigned int size ) = 0;
    virtual void writeWrappedString( const std::string& str ) = 0;
    
    virtual void flush() { _out->flush(); }
    
protected:
    // Return true if the manipulator is std::endl
    bool isEndl( std::ostream& (*fn)(std::ostream&) )
    {
#ifdef __sun
        // What a mess, but solaris does not like taking the address below
        std::stringstream ss;
        ss << fn;
        std::string s = ss.str();
        return !s.empty() && s[0] == '\n';
#else
        return fn==static_cast<std::ostream& (*)(std::ostream&)>(std::endl);
#endif
    }

    std::ostream* _out;
};

class OSGDB_EXPORT InputIterator : public osg::Referenced
{
public:
    InputIterator() : _in(0), _failed(false) {}
    virtual ~InputIterator() {}
    
    void setStream( std::istream* istream ) { _in = istream; }
    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;
    virtual void readChar( char& c ) = 0;
    virtual void readSChar( signed char& c ) = 0;
    virtual void readUChar( unsigned char& c ) = 0;
    virtual void readShort( short& s ) = 0;
    virtual void readUShort( unsigned short& s ) = 0;
    virtual void readInt( int& i ) = 0;
    virtual void readUInt( unsigned int& i ) = 0;
    virtual void readLong( long& l ) = 0;
    virtual void readULong( unsigned long& l ) = 0;
    virtual void readFloat( float& f ) = 0;
    virtual void readDouble( double& d ) = 0;
    virtual void readString( std::string& s ) = 0;
    virtual void readStream( std::istream& (*fn)(std::istream&) ) = 0;
    virtual void readBase( std::ios_base& (*fn)(std::ios_base&) ) = 0;
    
    virtual void readGLenum( ObjectGLenum& value ) = 0;
    virtual void readProperty( ObjectProperty& prop ) = 0;
    virtual void readMark( ObjectMark& mark ) = 0;
    virtual void readCharArray( char* s, unsigned int size ) = 0;
    virtual void readWrappedString( std::string& str ) = 0;
    
    virtual bool matchString( const std::string& /*str*/ ) { return false; }
    virtual void advanceToCurrentEndBracket() {}
    
protected:
    std::istream* _in;
    mutable bool _failed;
};

}

#endif