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

Revision 9890, 18.6 kB (checked in by robert, 5 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
RevLine 
[5328]1/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield
[1529]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*/
[51]13
[8]14#ifndef OSGDB_READERWRITER
15#define OSGDB_READERWRITER 1
16
17#include <osg/Image>
[2398]18#include <osg/Shape>
[8]19#include <osg/Node>
20
[8620]21#include <osgDB/AuthenticationMap>
[1582]22
[3689]23#include <deque>
[8577]24#include <list>
[5375]25#include <iosfwd>
[3689]26
[8]27namespace osgDB {
28
[3580]29class Archive;
[8]30
[3689]31/** list of directories to search through which searching for files. */
32typedef std::deque<std::string> FilePathList;
33
[8]34/** pure virtual base class for reading and writing of non native formats. */
[3580]35class OSGDB_EXPORT ReaderWriter : public osg::Object
[8]36{
37    public:
[3580]38   
39   
[7455]40        ReaderWriter():
41            osg::Object(true) {}
42           
[6076]43        ReaderWriter(const ReaderWriter& rw,const osg::CopyOp& copyop=osg::CopyOp::SHALLOW_COPY):
[3721]44            osg::Object(rw,copyop) {}
[3580]45
46        virtual ~ReaderWriter();
47
48        META_Object(osgDB,ReaderWriter);
49
[8577]50        typedef std::map<std::string, std::string> FormatDescriptionMap;
[9887]51        typedef std::list<std::string> FeatureList;
[8]52
[8577]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. */
[8582]57        virtual const FormatDescriptionMap& supportedExtensions() const { return _supportedExtensions; }
[8577]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
[9887]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
[3471]96        /** Options base class used for passing options into plugins to control their operation.*/
[3687]97        class Options : public osg::Object
[93]98        {
99            public:
[3689]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,
[8]106
[3689]107                    /// cache nodes loaded via readNode(filename)
[8119]108                    CACHE_NODES         = 1<<0,
[3689]109
110                    /// cache images loaded via readImage(filename)
[8119]111                    CACHE_IMAGES        = 1<<1,
[3689]112
113                    /// cache heightfield loaded via readHeightField(filename)
[8119]114                    CACHE_HEIGHTFIELDS  = 1<<2,
[3689]115
116                    /// cache heightfield loaded via readHeightField(filename)
[8119]117                    CACHE_ARCHIVES      = 1<<3,
[3689]118
119                    /// cache objects loaded via readObject(filename)
[8119]120                    CACHE_OBJECTS       = 1<<4,
[3689]121
[7908]122                    /// cache shaders loaded via readShader(filename)
[8119]123                    CACHE_SHADERS       = 1<<5,
[7908]124
[3689]125                    /// cache on all read*(filename) calls
126                    CACHE_ALL           = CACHE_NODES |
127                                          CACHE_IMAGES |
128                                          CACHE_HEIGHTFIELDS |
129                                          CACHE_ARCHIVES |
[7908]130                                          CACHE_OBJECTS |
131                                          CACHE_SHADERS
[3689]132                };
[8535]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                };
[3689]141           
142
[7455]143                Options():
144                    osg::Object(true),
[8535]145                    _objectCacheHint(CACHE_ARCHIVES),
146                    _buildKdTreesHint(NO_PREFERENCE) {}
[8322]147                   
[7455]148                Options(const std::string& str):
149                    osg::Object(true),
150                    _str(str),
[8535]151                    _objectCacheHint(CACHE_ARCHIVES),
152                    _buildKdTreesHint(NO_PREFERENCE) {}
[93]153               
[6076]154                Options(const Options& options,const osg::CopyOp& copyop=osg::CopyOp::SHALLOW_COPY):
[3721]155                    osg::Object(options,copyop),
[3687]156                    _str(options._str),
[3689]157                    _databasePaths(options._databasePaths),
[8535]158                    _objectCacheHint(options._objectCacheHint),
[9200]159                    _buildKdTreesHint(options._buildKdTreesHint),
160                    _pluginData(options._pluginData){}
[3687]161
162                META_Object(osgDB,Options);
163
[3471]164                /** Set the general Options string.*/
[93]165                void setOptionString(const std::string& str) { _str = str; }
[3471]166
167                /** Get the general Options string.*/
[93]168                const std::string& getOptionString() const { return _str; }
169
[3471]170                /** Set the database path to use a hint of where to look when loading models.*/
[3689]171                void setDatabasePath(const std::string& str) { _databasePaths.clear();  _databasePaths.push_back(str); }
[3471]172
173                /** Get the database path which is used a hint of where to look when loading models.*/
[3689]174                FilePathList& getDatabasePathList() { return _databasePaths; }
[3471]175
[3689]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
[8322]179
[3689]180                /** Set whether the Registry::ObjectCache should be used by default.*/
[3694]181                void setObjectCacheHint(CacheHintOptions useObjectCache) { _objectCacheHint = useObjectCache; }
[3689]182
183                /** Get whether the Registry::ObjectCache should be used by default.*/
[3694]184                CacheHintOptions getObjectCacheHint() const { return _objectCacheHint; }
[3689]185
[8322]186
[8535]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
[8619]194                /** Set the password map to be used by plugins when access files from secure locations.*/
195                void setAuthenticationMap(AuthenticationMap* authenticationMap) { _authenticationMap = authenticationMap; }
[8535]196
[8619]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
[7501]201                /** Sets a plugindata value PluginData with a string */
[7504]202                void setPluginData(const std::string& s, void* v) const { _pluginData[s] = v; }
[3689]203
[7501]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 */
[7504]215                void removePluginData(const std::string& s) const { _pluginData.erase(s); }
[7501]216
[93]217            protected:
218
219                virtual ~Options() {}
220
[8619]221                std::string                     _str;
222                FilePathList                    _databasePaths;
223                CacheHintOptions                _objectCacheHint;
224                BuildKdTreesHint                _buildKdTreesHint;
225                osg::ref_ptr<AuthenticationMap> _authenticationMap;
[93]226
[7501]227                typedef std::map<std::string,void*> PluginDataMap;
[7504]228                mutable PluginDataMap _pluginData;
[93]229        };
230
231
[3583]232        class OSGDB_EXPORT ReadResult
[146]233        {
234            public:
[93]235
[172]236                enum ReadStatus
[146]237                {
[9890]238                    NOT_IMPLEMENTED, //!< raad*() method not implemented in concreate ReaderWriter.
[8322]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.
[8491]244                    FILE_REQUESTED //!< Asyncronous file read has been requested, but returning immediatiely, keep polling plugin till file read has been completed.
[146]245                };
246
[172]247                ReadResult(ReadStatus status=FILE_NOT_HANDLED):_status(status) {}
[146]248                ReadResult(const std::string& m):_status(ERROR_IN_READING_FILE),_message(m) {}
[2962]249                ReadResult(osg::Object* obj, ReadStatus status=FILE_LOADED):_status(status),_object(obj) {}
[146]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               
[3580]254                osg::Object* getObject();
255                osg::Image* getImage();
256                osg::HeightField* getHeightField();
257                osg::Node* getNode();
258                osgDB::Archive* getArchive();
[7908]259                osg::Shader* getShader();
260
[1133]261                bool validObject() { return _object.valid(); }
262                bool validImage() { return getImage()!=0; }
[2398]263                bool validHeightField() { return getHeightField()!=0; }
[1133]264                bool validNode() { return getNode()!=0; }
[3580]265                bool validArchive() { return getArchive()!=0; }
[7908]266                bool validShader() { return getShader()!=0; }
[146]267
[3580]268                osg::Object* takeObject();
269                osg::Image* takeImage();
270                osg::HeightField* takeHeightField();
271                osg::Node* takeNode();
272                osgDB::Archive* takeArchive();
[7908]273                osg::Shader* takeShader();
[146]274
[5697]275                std::string& message() { return _message; }
[146]276                const std::string& message() const { return _message; }
277
[1133]278                ReadStatus status() const { return _status; }
[2962]279                bool success() const { return _status==FILE_LOADED || _status==FILE_LOADED_FROM_CACHE ; }
280                bool loadedFromCache() const { return _status==FILE_LOADED_FROM_CACHE; }
[1133]281                bool error() const { return _status==ERROR_IN_READING_FILE; }
[9890]282                bool notHandled() const { return _status==FILE_NOT_HANDLED || _status==NOT_IMPLEMENTED; }
[2501]283                bool notFound() const { return _status==FILE_NOT_FOUND; }
[146]284
285            protected:
286           
[172]287                ReadStatus                  _status;
[146]288                std::string                 _message;
289                osg::ref_ptr<osg::Object>   _object;
[8619]290
[146]291        };
292
293        class WriteResult
294        {
295            public:
296
[172]297                enum WriteStatus
[146]298                {
[9890]299                    NOT_IMPLEMENTED, //!< write*() method not implemented in concreate ReaderWriter.
[146]300                    FILE_NOT_HANDLED,
301                    FILE_SAVED,
302                    ERROR_IN_WRITING_FILE
303                };
304
[172]305                WriteResult(WriteStatus status=FILE_NOT_HANDLED):_status(status) {}
[146]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               
[5697]311                std::string& message() { return _message; }
[146]312                const std::string& message() const { return _message; }
313
[1133]314                WriteStatus status() const { return _status; }
315                bool success() const { return _status==FILE_SAVED; }
316                bool error() const { return _status==ERROR_IN_WRITING_FILE; }
[9890]317                bool notHandled() const { return _status==FILE_NOT_HANDLED || _status==NOT_IMPLEMENTED; }
[146]318
319            protected:
320           
[172]321                WriteStatus                 _status;
[146]322                std::string                 _message;
323        };
324
[3580]325        enum ArchiveStatus
326        {
327            READ,
328            WRITE,
329            CREATE
330        };
331
[6076]332        /** open an archive for reading, writing, or to create an empty archive for writing to.*/
[9890]333        virtual ReadResult openArchive(const std::string& /*fileName*/,ArchiveStatus, unsigned int =4096, const Options* =NULL) const { return ReadResult(ReadResult::NOT_IMPLEMENTED); }
[3580]334
[3618]335        /** open an archive for reading.*/
[9890]336        virtual ReadResult openArchive(std::istream& /*fin*/,const Options* =NULL) const { return ReadResult(ReadResult::NOT_IMPLEMENTED); }
[3618]337
[9890]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); }
[146]343
[9890]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); }
[1973]349
[9890]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); }
[1973]355
[9890]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); }
[1973]361
[8577]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;
[8]371};
372
[349]373}
[8]374
[3523]375#endif // OSGDB_READERWRITER
Note: See TracBrowser for help on using the browser.