OSG  3.4.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
OutputStream.h
Go to the documentation of this file.
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/Version>
19 #include <osg/Vec2>
20 #include <osg/Vec3>
21 #include <osg/Vec4>
22 #include <osg/Quat>
23 #include <osg/Matrix>
24 #include <osg/BoundingBox>
25 #include <osg/BoundingSphere>
26 #include <osg/Array>
27 #include <osg/PrimitiveSet>
28 #include <osgDB/ReaderWriter>
29 #include <osgDB/StreamOperator>
30 #include <iostream>
31 #include <sstream>
32 
33 namespace osgDB
34 {
35 
37 {
38 public:
39  OutputException( const std::vector<std::string>& fields, const std::string& err ) : _error(err)
40  {
41  for ( unsigned int i=0; i<fields.size(); ++i )
42  {
43  _field += fields[i];
44  _field += " ";
45  }
46  }
47 
48  const std::string& getField() const { return _field; }
49  const std::string& getError() const { return _error; }
50 
51 protected:
52  std::string _field;
53  std::string _error;
54 };
55 
57 {
58 public:
59  typedef std::map<const osg::Array*, unsigned int> ArrayMap;
60  typedef std::map<const osg::Object*, unsigned int> ObjectMap;
61 
62  enum WriteType
63  {
64  WRITE_UNKNOWN = 0,
67  WRITE_OBJECT
68  };
69 
71  {
72  WRITE_USE_IMAGE_HINT = 0,
76  WRITE_EXTERNAL_FILE
77  };
78 
79  OutputStream( const osgDB::Options* options );
80  virtual ~OutputStream();
81 
82  void setFileVersion( const std::string& d, int v ) { _domainVersionMap[d] = v; }
83  int getFileVersion( const std::string& d=std::string() ) const;
84 
85  bool isBinary() const { return _out->isBinary(); }
86  const std::string& getSchemaName() const { return _schemaName; }
87  const osgDB::Options* getOptions() const { return _options.get(); }
88 
89  void setWriteImageHint( WriteImageHint hint ) { _writeImageHint = hint; }
90  WriteImageHint getWriteImageHint() const { return _writeImageHint; }
91 
92  // Serialization related functions
93  OutputStream& operator<<( bool b ) { _out->writeBool(b); return *this; }
94  OutputStream& operator<<( char c ) { _out->writeChar(c); return *this; }
95  OutputStream& operator<<( signed char c) { _out->writeChar(c); return *this; }
96  OutputStream& operator<<( unsigned char c ) { _out->writeUChar(c); return *this; }
97  OutputStream& operator<<( short s ) { _out->writeShort(s); return *this; }
98  OutputStream& operator<<( unsigned short s ) { _out->writeUShort(s); return *this; }
99  OutputStream& operator<<( int i ) { _out->writeInt(i); return *this; }
100  OutputStream& operator<<( unsigned int i ) { _out->writeUInt(i); return *this; }
101  OutputStream& operator<<( long l ) { _out->writeLong(l); return *this; }
102  OutputStream& operator<<( unsigned long l ) { _out->writeULong(l); return *this; }
103  OutputStream& operator<<( float f ) { _out->writeFloat(f); return *this; }
104  OutputStream& operator<<( double d ) { _out->writeDouble(d); return *this; }
105  OutputStream& operator<<( const std::string& s ) { _out->writeString(s); return *this; }
106  OutputStream& operator<<( const char* s ) { _out->writeString(s); return *this; }
107  OutputStream& operator<<( std::ostream& (*fn)(std::ostream&) ) { _out->writeStream(fn); return *this; }
108  OutputStream& operator<<( std::ios_base& (*fn)(std::ios_base&) ) { _out->writeBase(fn); return *this; }
109 
110  OutputStream& operator<<( const ObjectGLenum& value ) { _out->writeGLenum(value); return *this; }
111  OutputStream& operator<<( const ObjectProperty& prop ) { _out->writeProperty(prop); return *this; }
112  OutputStream& operator<<( const ObjectMark& mark ) { _out->writeMark(mark); return *this; }
113 
114  OutputStream& operator<<( const osg::Vec2b& v );
115  OutputStream& operator<<( const osg::Vec3b& v );
116  OutputStream& operator<<( const osg::Vec4b& v );
117  OutputStream& operator<<( const osg::Vec2ub& v );
118  OutputStream& operator<<( const osg::Vec3ub& v );
119  OutputStream& operator<<( const osg::Vec4ub& v );
120  OutputStream& operator<<( const osg::Vec2s& v );
121  OutputStream& operator<<( const osg::Vec3s& v );
122  OutputStream& operator<<( const osg::Vec4s& v );
123  OutputStream& operator<<( const osg::Vec2us& v );
124  OutputStream& operator<<( const osg::Vec3us& v );
125  OutputStream& operator<<( const osg::Vec4us& v );
126  OutputStream& operator<<( const osg::Vec2i& v );
127  OutputStream& operator<<( const osg::Vec3i& v );
128  OutputStream& operator<<( const osg::Vec4i& v );
129  OutputStream& operator<<( const osg::Vec2ui& v );
130  OutputStream& operator<<( const osg::Vec3ui& v );
131  OutputStream& operator<<( const osg::Vec4ui& v );
132  OutputStream& operator<<( const osg::Vec2f& v );
133  OutputStream& operator<<( const osg::Vec3f& v );
134  OutputStream& operator<<( const osg::Vec4f& v );
135  OutputStream& operator<<( const osg::Vec2d& v );
136  OutputStream& operator<<( const osg::Vec3d& v );
137  OutputStream& operator<<( const osg::Vec4d& v );
138  OutputStream& operator<<( const osg::Quat& q );
139  OutputStream& operator<<( const osg::Plane& p );
140  OutputStream& operator<<( const osg::Matrixf& mat );
141  OutputStream& operator<<( const osg::Matrixd& mat );
146 
147  OutputStream& operator<<( const osg::Image* img ) { writeImage(img); return *this; }
148  OutputStream& operator<<( const osg::Array* a ) { if (OPENSCENEGRAPH_SOVERSION>=112) writeObject(a); else writeArray(a); return *this; }
149  OutputStream& operator<<( const osg::PrimitiveSet* p ) { if (OPENSCENEGRAPH_SOVERSION>=112) writeObject(p); else writePrimitiveSet(p); return *this; }
150  OutputStream& operator<<( const osg::Object* obj ) { writeObject(obj); return *this; }
151 
152  OutputStream& operator<<( const osg::ref_ptr<osg::Image>& ptr ) { writeImage(ptr.get()); return *this; }
153  OutputStream& operator<<( const osg::ref_ptr<osg::Array>& ptr ) { if (OPENSCENEGRAPH_SOVERSION>=112) writeObject(ptr.get()); else writeArray(ptr.get()); return *this; }
154  OutputStream& operator<<( const osg::ref_ptr<osg::PrimitiveSet>& ptr ) { if (OPENSCENEGRAPH_SOVERSION>=112) writeObject(ptr.get()); else writePrimitiveSet(ptr.get()); return *this; }
155 
156  template<typename T> OutputStream& operator<<( const osg::ref_ptr<T>& ptr ) { writeObject(ptr.get()); return *this; }
157 
158  // Convenient methods for writing
159  void writeWrappedString( const std::string& str ) { _out->writeWrappedString(str); }
160  void writeCharArray( const char* s, unsigned int size ) { _out->writeCharArray(s, size); }
161 
162  // method for converting all data structure sizes to unsigned int to ensure architecture portability.
163  template<typename T>
164  void writeSize(T size) { *this<<static_cast<unsigned int>(size); }
165 
166  // Global writing functions
167  void writeArray( const osg::Array* a );
168  void writePrimitiveSet( const osg::PrimitiveSet* p );
169  void writeImage( const osg::Image* img );
170  void writeObject( const osg::Object* obj );
171  void writeObjectFields( const osg::Object* obj );
172  void writeObjectFields( const osg::Object* obj, const std::string& compoundName );
173 
175  void setOutputIterator( OutputIterator* oi ) { _out = oi; }
176 
178  void start( OutputIterator* outIterator, WriteType type );
179 
180  void compress( std::ostream* ostream );
181 
182  // Schema handlers
183  void writeSchema( std::ostream& fout );
184 
185  // Exception handlers
186  inline void throwException( const std::string& msg );
187  const OutputException* getException() const { return _exception.get(); }
188 
189  // Property & mask variables
193 
194 protected:
195  template<typename T>
196  void writeArrayImplementation( const T*, int write_size, unsigned int numInRow=1 );
197 
198  unsigned int findOrCreateArrayID( const osg::Array* array, bool& newID );
199  unsigned int findOrCreateObjectID( const osg::Object* obj, bool& newID );
200 
201  ArrayMap _arrayMap;
202  ObjectMap _objectMap;
203 
204  typedef std::map<std::string, int> VersionMap;
205  VersionMap _domainVersionMap;
209 
210  typedef std::map<std::string, std::string> SchemaMap;
211  SchemaMap _inbuiltSchemaMap;
212  std::vector<std::string> _fields;
213  std::string _schemaName;
214  std::string _compressorName;
215  std::stringstream _compressSource;
219 };
220 
221 void OutputStream::throwException( const std::string& msg )
222 {
223  _exception = new OutputException(_fields, msg);
224 }
225 
226 }
227 
228 #endif
void writeWrappedString(const std::string &str)
Definition: OutputStream.h:159
void writeCharArray(const char *s, unsigned int size)
Definition: OutputStream.h:160
void setWriteImageHint(WriteImageHint hint)
Definition: OutputStream.h:89
const std::string & getSchemaName() const
Definition: OutputStream.h:86
OutputException(const std::vector< std::string > &fields, const std::string &err)
Definition: OutputStream.h:39
OutputStream & operator<<(const ObjectGLenum &value)
Definition: OutputStream.h:110
ObjectProperty PROPERTY
Definition: OutputStream.h:190
OutputStream & operator<<(const osg::Image *img)
Definition: OutputStream.h:147
OutputStream & operator<<(const std::string &s)
Definition: OutputStream.h:105
OutputStream & operator<<(unsigned short s)
Definition: OutputStream.h:98
OutputStream & operator<<(unsigned long l)
Definition: OutputStream.h:102
OutputStream & operator<<(unsigned char c)
Definition: OutputStream.h:96
OutputStream & operator<<(std::ios_base &(*fn)(std::ios_base &))
Definition: OutputStream.h:108
OutputStream & operator<<(long l)
Definition: OutputStream.h:101
OutputStream & operator<<(unsigned int i)
Definition: OutputStream.h:100
OutputStream & operator<<(int i)
Definition: OutputStream.h:99
OutputStream & operator<<(const osg::Object *obj)
Definition: OutputStream.h:150
OutputStream & operator<<(const osg::Array *a)
Definition: OutputStream.h:148
const osgDB::Options * getOptions() const
Definition: OutputStream.h:87
OutputStream & operator<<(const ObjectProperty &prop)
Definition: OutputStream.h:111
OutputStream & operator<<(short s)
Definition: OutputStream.h:97
const std::string & getField() const
Definition: OutputStream.h:48
OutputStream & operator<<(const ObjectMark &mark)
Definition: OutputStream.h:112
void throwException(const std::string &msg)
Definition: OutputStream.h:221
std::string _schemaName
Definition: OutputStream.h:213
osg::ref_ptr< const osgDB::Options > _options
Definition: OutputStream.h:218
OutputStream & operator<<(std::ostream &(*fn)(std::ostream &))
Definition: OutputStream.h:107
const std::string & getError() const
Definition: OutputStream.h:49
OutputStream & operator<<(const char *s)
Definition: OutputStream.h:106
OutputStream & operator<<(double d)
Definition: OutputStream.h:104
OutputStream & operator<<(signed char c)
Definition: OutputStream.h:95
OutputStream & operator<<(const osg::PrimitiveSet *p)
Definition: OutputStream.h:149
OutputStream & operator<<(float f)
Definition: OutputStream.h:103
std::stringstream _compressSource
Definition: OutputStream.h:215
WriteImageHint _writeImageHint
Definition: OutputStream.h:206
void setFileVersion(const std::string &d, int v)
Definition: OutputStream.h:82
osg::ref_ptr< OutputIterator > _out
Definition: OutputStream.h:216
std::ostream & operator<<(std::ostream &output, const Vec2f &vec)
Definition: io_utils.h:40
VersionMap _domainVersionMap
Definition: OutputStream.h:205
ObjectMark BEGIN_BRACKET
Definition: OutputStream.h:191
std::map< std::string, int > VersionMap
Definition: OutputStream.h:204
Definition: Archive.h:24
bool isBinary() const
Definition: OutputStream.h:85
WriteImageHint getWriteImageHint() const
Definition: OutputStream.h:90
const OutputException * getException() const
Definition: OutputStream.h:187
GLint GLenum GLsizei GLsizei GLsizei GLint GLenum GLenum type
Definition: GLU.h:71
SchemaMap _inbuiltSchemaMap
Definition: OutputStream.h:211
#define OSGDB_EXPORT
Definition: Export.h:39
std::vector< std::string > _fields
Definition: OutputStream.h:212
A plane class. It can be used to represent an infinite plane.
Definition: Plane.h:33
OutputStream & operator<<(bool b)
Definition: OutputStream.h:93
OutputStream & operator<<(char c)
Definition: OutputStream.h:94
std::string _compressorName
Definition: OutputStream.h:214
void setOutputIterator(OutputIterator *oi)
set an output iterator, used directly when not using OutputStream with a traditional file releated st...
Definition: OutputStream.h:175
#define OPENSCENEGRAPH_SOVERSION
Definition: Version.h:24
Definition: Quat.h:29
std::map< const osg::Array *, unsigned int > ArrayMap
Definition: OutputStream.h:59
std::map< std::string, std::string > SchemaMap
Definition: OutputStream.h:210
void writeSize(T size)
Definition: OutputStream.h:164
ObjectMark END_BRACKET
Definition: OutputStream.h:192
osg::ref_ptr< OutputException > _exception
Definition: OutputStream.h:217
std::map< const osg::Object *, unsigned int > ObjectMap
Definition: OutputStream.h:60
void writeArray(Output &fw, Iterator first, Iterator last, int noItemsPerLine=0)