root/OpenSceneGraph/trunk/include/osgDB/ReaderWriter @ 9890

Revision 9890, 18.6 kB (checked in by robert, 4 years ago)

Completed support for automatic detection of plugin features.

Cleaned up debug out of various plugins to ensure a clean osgconv --formats.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 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
14#ifndef OSGDB_READERWRITER
15#define OSGDB_READERWRITER 1
16
17#include <osg/Image>
18#include <osg/Shape>
19#include <osg/Node>
20
21#include <osgDB/AuthenticationMap>
22
23#include <deque>
24#include <list>
25#include <iosfwd>
26
27namespace osgDB {
28
29class Archive;
30
31/** list of directories to search through which searching for files. */
32typedef std::deque<std::string> FilePathList;
33
34/** pure virtual base class for reading and writing of non native formats. */
35class OSGDB_EXPORT ReaderWriter : public osg::Object
36{
37    public:
38   
39   
40        ReaderWriter():
41            osg::Object(true) {}
42           
43        ReaderWriter(const ReaderWriter& rw,const osg::CopyOp& copyop=osg::CopyOp::SHALLOW_COPY):
44            osg::Object(rw,copyop) {}
45
46        virtual ~ReaderWriter();
47
48        META_Object(osgDB,ReaderWriter);
49
50        typedef std::map<std::string, std::string> FormatDescriptionMap;
51        typedef std::list<std::string> FeatureList;
52
53        /** return which protocols are supported by ReaderWriter. */
54        virtual const FormatDescriptionMap& supportedProtocols() const { return _supportedProtocols; }
55       
56        /** return which list of file extensions supported by ReaderWriter. */
57        virtual const FormatDescriptionMap& supportedExtensions() const { return _supportedExtensions; }
58       
59        /** return which list of file extensions supported by ReaderWriter. */
60        virtual const FormatDescriptionMap& supportedOptions() const { return _supportedOptions; }
61
62        /** return true if ReaderWriter accepts specified file extension.*/
63        virtual bool acceptsExtension(const std::string& /*extension*/) const;
64
65        /// bit mask for setting up which feature types are available for read and/or write
66        enum Features
67        {
68            FEATURE_NONE               = 0,
69            FEATURE_READ_OBJECT        = 1<<0,
70            FEATURE_READ_IMAGE         = 1<<1,
71            FEATURE_READ_HEIGHT_FIELD  = 1<<2,
72            FEATURE_READ_NODE          = 1<<3,
73            FEATURE_READ_SHADER        = 1<<4,
74            FEATURE_WRITE_OBJECT       = 1<<5,
75            FEATURE_WRITE_IMAGE        = 1<<6,
76            FEATURE_WRITE_HEIGHT_FIELD = 1<<7,
77            FEATURE_WRITE_NODE         = 1<<8,
78            FEATURE_WRITE_SHADER       = 1<<9,
79            FEATURE_ALL                = FEATURE_READ_OBJECT        |
80                                         FEATURE_READ_IMAGE         |
81                                         FEATURE_READ_HEIGHT_FIELD  |
82                                         FEATURE_READ_NODE          |
83                                         FEATURE_READ_SHADER        |
84                                         FEATURE_WRITE_OBJECT       |
85                                         FEATURE_WRITE_IMAGE        |
86                                         FEATURE_WRITE_HEIGHT_FIELD |
87                                         FEATURE_WRITE_NODE         |
88                                         FEATURE_WRITE_SHADER       
89        };   
90        /** return available features*/
91        virtual Features supportedFeatures() const;
92
93        /** return feature as string */
94        static FeatureList featureAsString(Features feature);
95
96        /** Options base class used for passing options into plugins to control their operation.*/
97        class Options : public osg::Object
98        {
99            public:
100           
101           
102                /// bit mask for setting up which object types get cached by readObject/Image/HeightField/Node(filename) calls
103                enum CacheHintOptions
104                {   /// do not cache objects of any type
105                    CACHE_NONE          = 0,
106
107                    /// cache nodes loaded via readNode(filename)
108                    CACHE_NODES         = 1<<0,
109
110                    /// cache images loaded via readImage(filename)
111                    CACHE_IMAGES        = 1<<1,
112
113                    /// cache heightfield loaded via readHeightField(filename)
114                    CACHE_HEIGHTFIELDS  = 1<<2,
115
116                    /// cache heightfield loaded via readHeightField(filename)
117                    CACHE_ARCHIVES      = 1<<3,
118
119                    /// cache objects loaded via readObject(filename)
120                    CACHE_OBJECTS       = 1<<4,
121
122                    /// cache shaders loaded via readShader(filename)
123                    CACHE_SHADERS       = 1<<5,
124
125                    /// cache on all read*(filename) calls
126                    CACHE_ALL           = CACHE_NODES |
127                                          CACHE_IMAGES |
128                                          CACHE_HEIGHTFIELDS |
129                                          CACHE_ARCHIVES |
130                                          CACHE_OBJECTS |
131                                          CACHE_SHADERS
132                };
133               
134                /// range of options of whether to build kdtrees automatically on loading
135                enum BuildKdTreesHint
136                {
137                    NO_PREFERENCE,
138                    DO_NOT_BUILD_KDTREES,
139                    BUILD_KDTREES
140                };
141           
142
143                Options():
144                    osg::Object(true),
145                    _objectCacheHint(CACHE_ARCHIVES),
146                    _buildKdTreesHint(NO_PREFERENCE) {}
147                   
148                Options(const std::string& str):
149                    osg::Object(true),
150                    _str(str),
151                    _objectCacheHint(CACHE_ARCHIVES),
152                    _buildKdTreesHint(NO_PREFERENCE) {}
153               
154                Options(const Options& options,const osg::CopyOp& copyop=osg::CopyOp::SHALLOW_COPY):
155                    osg::Object(options,copyop),
156                    _str(options._str),
157                    _databasePaths(options._databasePaths),
158                    _objectCacheHint(options._objectCacheHint),
159                    _buildKdTreesHint(options._buildKdTreesHint),
160                    _pluginData(options._pluginData){}
161
162                META_Object(osgDB,Options);
163
164                /** Set the general Options string.*/
165                void setOptionString(const std::string& str) { _str = str; }
166
167                /** Get the general Options string.*/
168                const std::string& getOptionString() const { return _str; }
169
170                /** Set the database path to use a hint of where to look when loading models.*/
171                void setDatabasePath(const std::string& str) { _databasePaths.clear();  _databasePaths.push_back(str); }
172
173                /** Get the database path which is used a hint of where to look when loading models.*/
174                FilePathList& getDatabasePathList() { return _databasePaths; }
175
176                /** Get the const database path which is used a hint of where to look when loading models.*/
177                const FilePathList& getDatabasePathList() const { return _databasePaths; }
178
179
180                /** Set whether the Registry::ObjectCache should be used by default.*/
181                void setObjectCacheHint(CacheHintOptions useObjectCache) { _objectCacheHint = useObjectCache; }
182
183                /** Get whether the Registry::ObjectCache should be used by default.*/
184                CacheHintOptions getObjectCacheHint() const { return _objectCacheHint; }
185
186
187                /** Set whether the KdTrees should be built for geometry in the loader model. */
188                void setBuildKdTreesHint(BuildKdTreesHint hint) { _buildKdTreesHint = hint; }
189
190                /** Get whether the KdTrees should be built for geometry in the loader model. */
191                BuildKdTreesHint getBuildKdTreesHint() const { return _buildKdTreesHint; }
192
193
194                /** Set the password map to be used by plugins when access files from secure locations.*/
195                void setAuthenticationMap(AuthenticationMap* authenticationMap) { _authenticationMap = authenticationMap; }
196
197                /** Get the password map to be used by plugins when access files from secure locations.*/
198                const AuthenticationMap* getAuthenticationMap() const { return _authenticationMap.get(); }
199
200
201                /** Sets a plugindata value PluginData with a string */
202                void setPluginData(const std::string& s, void* v) const { _pluginData[s] = v; }
203
204                /** Get a value from the PluginData */
205                void* getPluginData(const std::string& s) { return _pluginData[s]; }
206
207                /** Get a value from the PluginData */
208                const void* getPluginData(const std::string& s) const
209                {
210                    PluginDataMap::const_iterator itr = _pluginData.find(s);
211                    return (itr == _pluginData.end()) ? 0 : itr->second;
212                }
213
214                /** Remove a value from the PluginData */
215                void removePluginData(const std::string& s) const { _pluginData.erase(s); }
216
217            protected:
218
219                virtual ~Options() {}
220
221                std::string                     _str;
222                FilePathList                    _databasePaths;
223                CacheHintOptions                _objectCacheHint;
224                BuildKdTreesHint                _buildKdTreesHint;
225                osg::ref_ptr<AuthenticationMap> _authenticationMap;
226
227                typedef std::map<std::string,void*> PluginDataMap;
228                mutable PluginDataMap _pluginData;
229        };
230
231
232        class OSGDB_EXPORT ReadResult
233        {
234            public:
235
236                enum ReadStatus
237                {
238                    NOT_IMPLEMENTED, //!< raad*() method not implemented in concreate ReaderWriter.
239                    FILE_NOT_HANDLED, //!< File is not appropriate for this file reader, due to some incompatibility, but *not* a read error.
240                    FILE_NOT_FOUND, //!< File could not be found or could not be read.
241                    FILE_LOADED, //!< File successfully found, loaded, and converted into osg.
242                    FILE_LOADED_FROM_CACHE, //!< File found in cache and returned.
243                    ERROR_IN_READING_FILE, //!< File found, loaded, but an error was encountered during processing.
244                    FILE_REQUESTED //!< Asyncronous file read has been requested, but returning immediatiely, keep polling plugin till file read has been completed.
245                };
246
247                ReadResult(ReadStatus status=FILE_NOT_HANDLED):_status(status) {}
248                ReadResult(const std::string& m):_status(ERROR_IN_READING_FILE),_message(m) {}
249                ReadResult(osg::Object* obj, ReadStatus status=FILE_LOADED):_status(status),_object(obj) {}
250               
251                ReadResult(const ReadResult& rr):_status(rr._status),_message(rr._message),_object(rr._object) {}
252                ReadResult& operator = (const ReadResult& rr) { if (this==&rr) return *this; _status=rr._status; _message=rr._message;_object=rr._object; return *this; }
253               
254                osg::Object* getObject();
255                osg::Image* getImage();
256                osg::HeightField* getHeightField();
257                osg::Node* getNode();
258                osgDB::Archive* getArchive();
259                osg::Shader* getShader();
260
261                bool validObject() { return _object.valid(); }
262                bool validImage() { return getImage()!=0; }
263                bool validHeightField() { return getHeightField()!=0; }
264                bool validNode() { return getNode()!=0; }
265                bool validArchive() { return getArchive()!=0; }
266                bool validShader() { return getShader()!=0; }
267
268                osg::Object* takeObject();
269                osg::Image* takeImage();
270                osg::HeightField* takeHeightField();
271                osg::Node* takeNode();
272                osgDB::Archive* takeArchive();
273                osg::Shader* takeShader();
274
275                std::string& message() { return _message; }
276                const std::string& message() const { return _message; }
277
278                ReadStatus status() const { return _status; }
279                bool success() const { return _status==FILE_LOADED || _status==FILE_LOADED_FROM_CACHE ; }
280                bool loadedFromCache() const { return _status==FILE_LOADED_FROM_CACHE; }
281                bool error() const { return _status==ERROR_IN_READING_FILE; }
282                bool notHandled() const { return _status==FILE_NOT_HANDLED || _status==NOT_IMPLEMENTED; }
283                bool notFound() const { return _status==FILE_NOT_FOUND; }
284
285            protected:
286           
287                ReadStatus                  _status;
288                std::string                 _message;
289                osg::ref_ptr<osg::Object>   _object;
290
291        };
292
293        class WriteResult
294        {
295            public:
296
297                enum WriteStatus
298                {
299                    NOT_IMPLEMENTED, //!< write*() method not implemented in concreate ReaderWriter.
300                    FILE_NOT_HANDLED,
301                    FILE_SAVED,
302                    ERROR_IN_WRITING_FILE
303                };
304
305                WriteResult(WriteStatus status=FILE_NOT_HANDLED):_status(status) {}
306                WriteResult(const std::string& m):_status(ERROR_IN_WRITING_FILE),_message(m) {}
307               
308                WriteResult(const WriteResult& rr):_status(rr._status),_message(rr._message) {}
309                WriteResult& operator = (const WriteResult& rr) { if (this==&rr) return *this; _status=rr._status; _message=rr._message; return *this; }
310               
311                std::string& message() { return _message; }
312                const std::string& message() const { return _message; }
313
314                WriteStatus status() const { return _status; }
315                bool success() const { return _status==FILE_SAVED; }
316                bool error() const { return _status==ERROR_IN_WRITING_FILE; }
317                bool notHandled() const { return _status==FILE_NOT_HANDLED || _status==NOT_IMPLEMENTED; }
318
319            protected:
320           
321                WriteStatus                 _status;
322                std::string                 _message;
323        };
324
325        enum ArchiveStatus
326        {
327            READ,
328            WRITE,
329            CREATE
330        };
331
332        /** open an archive for reading, writing, or to create an empty archive for writing to.*/
333        virtual ReadResult openArchive(const std::string& /*fileName*/,ArchiveStatus, unsigned int =4096, const Options* =NULL) const { return ReadResult(ReadResult::NOT_IMPLEMENTED); }
334
335        /** open an archive for reading.*/
336        virtual ReadResult openArchive(std::istream& /*fin*/,const Options* =NULL) const { return ReadResult(ReadResult::NOT_IMPLEMENTED); }
337
338        virtual ReadResult readObject(const std::string& /*fileName*/,const Options* =NULL) const { return ReadResult(ReadResult::NOT_IMPLEMENTED); }
339        virtual ReadResult readImage(const std::string& /*fileName*/,const Options* =NULL) const { return ReadResult(ReadResult::NOT_IMPLEMENTED); }
340        virtual ReadResult readHeightField(const std::string& /*fileName*/,const Options* =NULL) const { return ReadResult(ReadResult::NOT_IMPLEMENTED); }
341        virtual ReadResult readNode(const std::string& /*fileName*/,const Options* =NULL) const { return ReadResult(ReadResult::NOT_IMPLEMENTED); }
342        virtual ReadResult readShader(const std::string& /*fileName*/,const Options* =NULL) const { return ReadResult(ReadResult::NOT_IMPLEMENTED); }
343
344        virtual WriteResult writeObject(const osg::Object& /*obj*/,const std::string& /*fileName*/,const Options* =NULL) const {return WriteResult(WriteResult::NOT_IMPLEMENTED); }
345        virtual WriteResult writeImage(const osg::Image& /*image*/,const std::string& /*fileName*/,const Options* =NULL) const {return WriteResult(WriteResult::NOT_IMPLEMENTED); }
346        virtual WriteResult writeHeightField(const osg::HeightField& /*heightField*/,const std::string& /*fileName*/,const Options* =NULL) const {return WriteResult(WriteResult::NOT_IMPLEMENTED); }
347        virtual WriteResult writeNode(const osg::Node& /*node*/,const std::string& /*fileName*/,const Options* =NULL) const { return WriteResult(WriteResult::NOT_IMPLEMENTED); }
348        virtual WriteResult writeShader(const osg::Shader& /*shader*/,const std::string& /*fileName*/,const Options* =NULL) const {return WriteResult(WriteResult::NOT_IMPLEMENTED); }
349
350        virtual ReadResult readObject(std::istream& /*fin*/,const Options* =NULL) const { return ReadResult(ReadResult::NOT_IMPLEMENTED); }
351        virtual ReadResult readImage(std::istream& /*fin*/,const Options* =NULL) const { return ReadResult(ReadResult::NOT_IMPLEMENTED); }
352        virtual ReadResult readHeightField(std::istream& /*fin*/,const Options* =NULL) const { return ReadResult(ReadResult::NOT_IMPLEMENTED); }
353        virtual ReadResult readNode(std::istream& /*fin*/,const Options* =NULL) const { return ReadResult(ReadResult::NOT_IMPLEMENTED); }
354        virtual ReadResult readShader(std::istream& /*fin*/,const Options* =NULL) const { return ReadResult(ReadResult::NOT_IMPLEMENTED); }
355
356        virtual WriteResult writeObject(const osg::Object& /*obj*/,std::ostream& /*fout*/,const Options* =NULL) const { return WriteResult(WriteResult::NOT_IMPLEMENTED); }
357        virtual WriteResult writeImage(const osg::Image& /*image*/,std::ostream& /*fout*/,const Options* =NULL) const { return WriteResult(WriteResult::NOT_IMPLEMENTED); }
358        virtual WriteResult writeHeightField(const osg::HeightField& /*heightField*/,std::ostream& /*fout*/,const Options* =NULL) const { return WriteResult(WriteResult::NOT_IMPLEMENTED); }
359        virtual WriteResult writeNode(const osg::Node& /*node*/,std::ostream& /*fout*/,const Options* =NULL) const { return WriteResult(WriteResult::NOT_IMPLEMENTED); }
360        virtual WriteResult writeShader(const osg::Shader& /*shader*/,std::ostream& /*fout*/,const Options* =NULL) const { return WriteResult(WriteResult::NOT_IMPLEMENTED); }
361
362    protected:
363   
364        void supportsProtocol(const std::string& fmt, const std::string& description);
365        void supportsExtension(const std::string& fmt, const std::string& description);
366        void supportsOption(const std::string& fmt, const std::string& description);
367
368        FormatDescriptionMap _supportedProtocols;
369        FormatDescriptionMap _supportedExtensions;
370        FormatDescriptionMap _supportedOptions;
371};
372
373}
374
375#endif // OSGDB_READERWRITER
Note: See TracBrowser for help on using the browser.