root/OpenSceneGraph/trunk/include/osgDB/OutputStream @ 10986

Revision 10986, 6.9 kB (checked in by robert, 5 years ago)

From Wang Rui, refactored the InputStream/OutputStream? operations so that the binar/ascii foramts are implemented via subclasses.

Line 
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_OUTPUTSTREAM
16#define OSGDB_OUTPUTSTREAM
17
18#include <osg/Vec2>
19#include <osg/Vec3>
20#include <osg/Vec4>
21#include <osg/Quat>
22#include <osg/Matrix>
23#include <osg/Array>
24#include <osg/PrimitiveSet>
25#include <osgDB/ReaderWriter>
26#include <osgDB/StreamOperator>
27#include <iostream>
28#include <sstream>
29
30namespace osgDB
31{
32
33class OutputException
34{
35public:
36    OutputException( const std::string& field, const std::string& err )
37    : _field(field), _error(err) {}
38   
39    const std::string& getField() const { return _field; }
40    const std::string& getError() const { return _error; }
41   
42protected:
43    std::string _field;
44    std::string _error;
45};
46
47class OSGDB_EXPORT OutputStream
48{
49public:
50    typedef std::map<const osg::Array*, unsigned int> ArrayMap;
51    typedef std::map<const osg::Object*, unsigned int> ObjectMap;
52   
53    enum WriteType
54    {
55        WRITE_UNKNOWN = 0,
56        WRITE_SCENE,
57        WRITE_IMAGE
58    };
59   
60    enum WriteImageHint
61    {
62        WRITE_USE_IMAGE_HINT = 0,  /*!< Use image hint, write inline data or use external */
63        WRITE_USE_EXTERNAL,        /*!< Use external file on disk and write only the filename */
64        WRITE_INLINE_DATA,         /*!< Write Image::data() to stream */
65        WRITE_INLINE_FILE,         /*!< Write the image file itself to stream */
66        WRITE_EXTERNAL_FILE        /*!< Write Image::data() to disk and use it as external file */
67    };
68   
69    OutputStream( const osgDB::Options* options );
70    virtual ~OutputStream();
71   
72    bool isBinary() const { return _out->isBinary(); }
73   
74    void setWriteImageHint( WriteImageHint hint ) { _writeImageHint = hint; }
75    WriteImageHint getWriteImageHint() const { return _writeImageHint; }
76   
77    // Serialization related functions
78    OutputStream& operator<<( bool b ) { _out->writeBool(b); return *this; }
79    OutputStream& operator<<( char c ) { _out->writeChar(c); return *this; }
80    OutputStream& operator<<( unsigned char c ) { _out->writeUChar(c); return *this; }
81    OutputStream& operator<<( short s ) { _out->writeShort(s); return *this; }
82    OutputStream& operator<<( unsigned short s ) { _out->writeUShort(s); return *this; }
83    OutputStream& operator<<( int i ) { _out->writeInt(i); return *this; }
84    OutputStream& operator<<( unsigned int i ) { _out->writeUInt(i); return *this; }
85    OutputStream& operator<<( long l ) { _out->writeLong(l); return *this; }
86    OutputStream& operator<<( unsigned long l ) { _out->writeULong(l); return *this; }
87    OutputStream& operator<<( float f ) { _out->writeFloat(f); return *this; }
88    OutputStream& operator<<( double d ) { _out->writeDouble(d); return *this; }
89    OutputStream& operator<<( const std::string& s ) { _out->writeString(s); return *this; }
90    OutputStream& operator<<( std::ostream& (*fn)(std::ostream&) ) { _out->writeStream(fn); return *this; }
91    OutputStream& operator<<( std::ios_base& (*fn)(std::ios_base&) ) { _out->writeBase(fn); return *this; }
92   
93    OutputStream& operator<<( const ObjectGLenum& value ) { _out->writeGLenum(value); return *this; }
94    OutputStream& operator<<( const ObjectProperty& prop ) { _out->writeProperty(prop); return *this; }
95    OutputStream& operator<<( const ObjectMark& mark ) { _out->writeMark(mark); return *this; }
96   
97    OutputStream& operator<<( const osg::Vec2b& v );
98    OutputStream& operator<<( const osg::Vec3b& v );
99    OutputStream& operator<<( const osg::Vec4b& v );
100    OutputStream& operator<<( const osg::Vec4ub& v );
101    OutputStream& operator<<( const osg::Vec2s& v );
102    OutputStream& operator<<( const osg::Vec3s& v );
103    OutputStream& operator<<( const osg::Vec4s& v );
104    OutputStream& operator<<( const osg::Vec2f& v );
105    OutputStream& operator<<( const osg::Vec3f& v );
106    OutputStream& operator<<( const osg::Vec4f& v );
107    OutputStream& operator<<( const osg::Vec2d& v );
108    OutputStream& operator<<( const osg::Vec3d& v );
109    OutputStream& operator<<( const osg::Vec4d& v );
110    OutputStream& operator<<( const osg::Quat& q );
111    OutputStream& operator<<( const osg::Plane& p );
112    OutputStream& operator<<( const osg::Matrixf& mat );
113    OutputStream& operator<<( const osg::Matrixd& mat );
114   
115    OutputStream& operator<<( const osg::Array* a ) { writeArray(a); return *this; }
116    OutputStream& operator<<( const osg::Image* img ) { writeImage(img); return *this; }
117    OutputStream& operator<<( const osg::PrimitiveSet* p ) { writePrimitiveSet(p); return *this; }
118    OutputStream& operator<<( const osg::Object* obj ) { writeObject(obj); return *this; }
119   
120    OutputStream& operator<<( const osg::ref_ptr<osg::Array>& ptr ) { writeArray(ptr.get()); return *this; }
121    OutputStream& operator<<( const osg::ref_ptr<osg::Image>& ptr ) { writeImage(ptr.get()); return *this; }
122    OutputStream& operator<<( const osg::ref_ptr<osg::PrimitiveSet>& ptr ) { writePrimitiveSet(ptr.get()); return *this; }
123   
124    template<typename T> OutputStream& operator<<( const osg::ref_ptr<T>& ptr )
125    { writeObject(ptr.get()); return *this; }
126   
127    // Convenient methods for writing
128    inline void writeWrappedString( const std::string& str );
129    void writeCharArray( const char* s, unsigned int size ) { _out->writeCharArray(s, size); }
130   
131    // Global writing functions
132    void writeArray( const osg::Array* a );
133    void writePrimitiveSet( const osg::PrimitiveSet* p );
134    void writeImage( const osg::Image* img );
135    void writeObject( const osg::Object* obj );
136   
137    void start( OutputIterator* outIterator, WriteType type );
138    void compress( std::ostream* ostream );
139   
140    // Schema handlers
141    void writeSchema( std::ostream& fout );
142   
143protected:
144    template<typename T>
145    void writeArrayImplementation( const T*, int writeSize, unsigned int numInRow=1 );
146   
147    unsigned int findOrCreateArrayID( const osg::Array* array );
148    unsigned int findOrCreateObjectID( const osg::Object* obj );
149   
150    ArrayMap _arrayMap;
151    ObjectMap _objectMap;
152   
153    WriteImageHint _writeImageHint;
154    std::string _currentField;
155    std::string _compressorName;
156    std::stringstream _compressSource;
157    OutputIterator* _out;
158};
159
160void OutputStream::writeWrappedString( const std::string& str )
161{
162    if ( !isBinary() )
163    {
164        std::string wrappedStr = std::string("\"") + str + std::string("\"");
165        *this << wrappedStr;
166    }
167    else
168        *this << str;
169}
170
171}
172
173#endif
Note: See TracBrowser for help on using the browser.