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

Revision 13041, 8.1 kB (checked in by robert, 3 years ago)

Ran script to remove trailing spaces and tabs

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
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 : public osg::Referenced
34{
35public:
36    OutputException( const std::vector<std::string>& fields, const std::string& err ) : _error(err)
37    {
38        for ( unsigned int i=0; i<fields.size(); ++i )
39        {
40            _field += fields[i];
41            _field += " ";
42        }
43    }
44
45    const std::string& getField() const { return _field; }
46    const std::string& getError() const { return _error; }
47
48protected:
49    std::string _field;
50    std::string _error;
51};
52
53class OSGDB_EXPORT OutputStream
54{
55public:
56    typedef std::map<const osg::Array*, unsigned int> ArrayMap;
57    typedef std::map<const osg::Object*, unsigned int> ObjectMap;
58
59    enum WriteType
60    {
61        WRITE_UNKNOWN = 0,
62        WRITE_SCENE,
63        WRITE_IMAGE,
64        WRITE_OBJECT
65    };
66
67    enum WriteImageHint
68    {
69        WRITE_USE_IMAGE_HINT = 0,  /*!< Use image hint, write inline data or use external */
70        WRITE_USE_EXTERNAL,        /*!< Use external file on disk and write only the filename */
71        WRITE_INLINE_DATA,         /*!< Write Image::data() to stream */
72        WRITE_INLINE_FILE,         /*!< Write the image file itself to stream */
73        WRITE_EXTERNAL_FILE        /*!< Write Image::data() to disk and use it as external file */
74    };
75
76    OutputStream( const osgDB::Options* options );
77    virtual ~OutputStream();
78
79    bool isBinary() const { return _out->isBinary(); }
80    const std::string& getSchemaName() const { return _schemaName; }
81    const osgDB::Options* getOptions() const { return _options.get(); }
82
83    void setWriteImageHint( WriteImageHint hint ) { _writeImageHint = hint; }
84    WriteImageHint getWriteImageHint() const { return _writeImageHint; }
85
86    // Serialization related functions
87    OutputStream& operator<<( bool b ) { _out->writeBool(b); return *this; }
88    OutputStream& operator<<( char c ) { _out->writeChar(c); return *this; }
89    OutputStream& operator<<( unsigned char c ) { _out->writeUChar(c); return *this; }
90    OutputStream& operator<<( short s ) { _out->writeShort(s); return *this; }
91    OutputStream& operator<<( unsigned short s ) { _out->writeUShort(s); return *this; }
92    OutputStream& operator<<( int i ) { _out->writeInt(i); return *this; }
93    OutputStream& operator<<( unsigned int i ) { _out->writeUInt(i); return *this; }
94    OutputStream& operator<<( long l ) { _out->writeLong(l); return *this; }
95    OutputStream& operator<<( unsigned long l ) { _out->writeULong(l); return *this; }
96    OutputStream& operator<<( float f ) { _out->writeFloat(f); return *this; }
97    OutputStream& operator<<( double d ) { _out->writeDouble(d); return *this; }
98    OutputStream& operator<<( const std::string& s ) { _out->writeString(s); return *this; }
99    OutputStream& operator<<( const char* s ) { _out->writeString(s); return *this; }
100    OutputStream& operator<<( std::ostream& (*fn)(std::ostream&) ) { _out->writeStream(fn); return *this; }
101    OutputStream& operator<<( std::ios_base& (*fn)(std::ios_base&) ) { _out->writeBase(fn); return *this; }
102
103    OutputStream& operator<<( const ObjectGLenum& value ) { _out->writeGLenum(value); return *this; }
104    OutputStream& operator<<( const ObjectProperty& prop ) { _out->writeProperty(prop); return *this; }
105    OutputStream& operator<<( const ObjectMark& mark ) { _out->writeMark(mark); return *this; }
106
107    OutputStream& operator<<( const osg::Vec2b& v );
108    OutputStream& operator<<( const osg::Vec3b& v );
109    OutputStream& operator<<( const osg::Vec4b& v );
110    OutputStream& operator<<( const osg::Vec4ub& v );
111    OutputStream& operator<<( const osg::Vec2s& v );
112    OutputStream& operator<<( const osg::Vec3s& v );
113    OutputStream& operator<<( const osg::Vec4s& v );
114    OutputStream& operator<<( const osg::Vec2f& v );
115    OutputStream& operator<<( const osg::Vec3f& v );
116    OutputStream& operator<<( const osg::Vec4f& v );
117    OutputStream& operator<<( const osg::Vec2d& v );
118    OutputStream& operator<<( const osg::Vec3d& v );
119    OutputStream& operator<<( const osg::Vec4d& v );
120    OutputStream& operator<<( const osg::Quat& q );
121    OutputStream& operator<<( const osg::Plane& p );
122    OutputStream& operator<<( const osg::Matrixf& mat );
123    OutputStream& operator<<( const osg::Matrixd& mat );
124
125    OutputStream& operator<<( const osg::Array* a ) { writeArray(a); return *this; }
126    OutputStream& operator<<( const osg::Image* img ) { writeImage(img); return *this; }
127    OutputStream& operator<<( const osg::PrimitiveSet* p ) { writePrimitiveSet(p); return *this; }
128    OutputStream& operator<<( const osg::Object* obj ) { writeObject(obj); return *this; }
129
130    OutputStream& operator<<( const osg::ref_ptr<osg::Array>& ptr ) { writeArray(ptr.get()); return *this; }
131    OutputStream& operator<<( const osg::ref_ptr<osg::Image>& ptr ) { writeImage(ptr.get()); return *this; }
132    OutputStream& operator<<( const osg::ref_ptr<osg::PrimitiveSet>& ptr ) { writePrimitiveSet(ptr.get()); return *this; }
133
134    template<typename T> OutputStream& operator<<( const osg::ref_ptr<T>& ptr )
135    { writeObject(ptr.get()); return *this; }
136
137    // Convenient methods for writing
138    void writeWrappedString( const std::string& str ) { _out->writeWrappedString(str); }
139    void writeCharArray( const char* s, unsigned int size ) { _out->writeCharArray(s, size); }
140
141    // method for converting all data structure sizes to unsigned int to ensure architecture portability.
142    template<typename T>
143    void writeSize(T size) { *this<<static_cast<unsigned int>(size); }
144
145    // Global writing functions
146    void writeArray( const osg::Array* a );
147    void writePrimitiveSet( const osg::PrimitiveSet* p );
148    void writeImage( const osg::Image* img );
149    void writeObject( const osg::Object* obj );
150    void writeObjectFields( const osg::Object* obj );
151
152    /// set an output iterator, used directly when not using OutputStream with a traditional file releated stream.
153    void setOutputIterator( OutputIterator* oi ) { _out = oi; }
154
155    /// start writing to OutputStream treating it as a traditional file releated stream, handles headers and versioning
156    void start( OutputIterator* outIterator, WriteType type );
157
158    void compress( std::ostream* ostream );
159
160    // Schema handlers
161    void writeSchema( std::ostream& fout );
162
163    // Exception handlers
164    inline void throwException( const std::string& msg );
165    const OutputException* getException() const { return _exception.get(); }
166
167protected:
168    template<typename T>
169    void writeArrayImplementation( const T*, int write_size, unsigned int numInRow=1 );
170
171    unsigned int findOrCreateArrayID( const osg::Array* array, bool& newID );
172    unsigned int findOrCreateObjectID( const osg::Object* obj, bool& newID );
173
174    ArrayMap _arrayMap;
175    ObjectMap _objectMap;
176
177    WriteImageHint _writeImageHint;
178    bool _useSchemaData;
179    std::map<std::string, std::string> _inbuiltSchemaMap;
180    std::vector<std::string> _fields;
181    std::string _schemaName;
182    std::string _compressorName;
183    std::stringstream _compressSource;
184    osg::ref_ptr<OutputIterator> _out;
185    osg::ref_ptr<OutputException> _exception;
186    osg::ref_ptr<const osgDB::Options> _options;
187};
188
189void OutputStream::throwException( const std::string& msg )
190{
191    _exception = new OutputException(_fields, msg);
192}
193
194}
195
196#endif
Note: See TracBrowser for help on using the browser.