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

Revision 10057, 19.7 kB (checked in by robert, 5 years ago)

From Neil Hughes, "please find attached a new version of the ReaderWriter? header file. This has additional functions to mimic the setPluginData functions for string data."

  • 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),
[10057]160                    _pluginData(options._pluginData),
161                    _pluginStringData(options._pluginStringData){}
[3687]162
163                META_Object(osgDB,Options);
164
[3471]165                /** Set the general Options string.*/
[93]166                void setOptionString(const std::string& str) { _str = str; }
[3471]167
168                /** Get the general Options string.*/
[93]169                const std::string& getOptionString() const { return _str; }
170
[3471]171                /** Set the database path to use a hint of where to look when loading models.*/
[3689]172                void setDatabasePath(const std::string& str) { _databasePaths.clear();  _databasePaths.push_back(str); }
[3471]173
174                /** Get the database path which is used a hint of where to look when loading models.*/
[3689]175                FilePathList& getDatabasePathList() { return _databasePaths; }
[3471]176
[3689]177                /** Get the const database path which is used a hint of where to look when loading models.*/
178                const FilePathList& getDatabasePathList() const { return _databasePaths; }
179
[8322]180
[3689]181                /** Set whether the Registry::ObjectCache should be used by default.*/
[3694]182                void setObjectCacheHint(CacheHintOptions useObjectCache) { _objectCacheHint = useObjectCache; }
[3689]183
184                /** Get whether the Registry::ObjectCache should be used by default.*/
[3694]185                CacheHintOptions getObjectCacheHint() const { return _objectCacheHint; }
[3689]186
[8322]187
[8535]188                /** Set whether the KdTrees should be built for geometry in the loader model. */
189                void setBuildKdTreesHint(BuildKdTreesHint hint) { _buildKdTreesHint = hint; }
190
191                /** Get whether the KdTrees should be built for geometry in the loader model. */
192                BuildKdTreesHint getBuildKdTreesHint() const { return _buildKdTreesHint; }
193
194
[8619]195                /** Set the password map to be used by plugins when access files from secure locations.*/
196                void setAuthenticationMap(AuthenticationMap* authenticationMap) { _authenticationMap = authenticationMap; }
[8535]197
[8619]198                /** Get the password map to be used by plugins when access files from secure locations.*/
199                const AuthenticationMap* getAuthenticationMap() const { return _authenticationMap.get(); }
200
201
[7501]202                /** Sets a plugindata value PluginData with a string */
[7504]203                void setPluginData(const std::string& s, void* v) const { _pluginData[s] = v; }
[3689]204
[7501]205                /** Get a value from the PluginData */
206                void* getPluginData(const std::string& s) { return _pluginData[s]; }
207
208                /** Get a value from the PluginData */
209                const void* getPluginData(const std::string& s) const
210                {
211                    PluginDataMap::const_iterator itr = _pluginData.find(s);
212                    return (itr == _pluginData.end()) ? 0 : itr->second;
213                }
214
215                /** Remove a value from the PluginData */
[7504]216                void removePluginData(const std::string& s) const { _pluginData.erase(s); }
[7501]217
[10057]218
219                /** Sets a plugindata value PluginData with a string */
220                void setPluginStringData(const std::string& s, const std::string& v) const { _pluginStringData[s] = v; }
221
222                /** Get a string from the PluginStrData */
223                std::string getPluginStringData(const std::string& s) { return _pluginStringData[s]; }
224
225                /** Get a value from the PluginData */
226                const std::string getPluginStringData(const std::string& s) const
227                {
228                    PluginStringDataMap::const_iterator itr = _pluginStringData.find(s);
229                    return (itr == _pluginStringData.end()) ? std::string("") : itr->second;
230                }
231
232                /** Remove a value from the PluginData */
233                void removePluginStringData(const std::string& s) const { _pluginStringData.erase(s); }
234
235
236
237
[93]238            protected:
239
240                virtual ~Options() {}
241
[8619]242                std::string                     _str;
243                FilePathList                    _databasePaths;
244                CacheHintOptions                _objectCacheHint;
245                BuildKdTreesHint                _buildKdTreesHint;
246                osg::ref_ptr<AuthenticationMap> _authenticationMap;
[93]247
[7501]248                typedef std::map<std::string,void*> PluginDataMap;
[7504]249                mutable PluginDataMap _pluginData;
[10057]250                typedef std::map<std::string,std::string> PluginStringDataMap;
251                mutable PluginStringDataMap _pluginStringData;
252
[93]253        };
254
255
[3583]256        class OSGDB_EXPORT ReadResult
[146]257        {
258            public:
[93]259
[172]260                enum ReadStatus
[146]261                {
[9890]262                    NOT_IMPLEMENTED, //!< raad*() method not implemented in concreate ReaderWriter.
[8322]263                    FILE_NOT_HANDLED, //!< File is not appropriate for this file reader, due to some incompatibility, but *not* a read error.
264                    FILE_NOT_FOUND, //!< File could not be found or could not be read.
265                    FILE_LOADED, //!< File successfully found, loaded, and converted into osg.
266                    FILE_LOADED_FROM_CACHE, //!< File found in cache and returned.
267                    ERROR_IN_READING_FILE, //!< File found, loaded, but an error was encountered during processing.
[8491]268                    FILE_REQUESTED //!< Asyncronous file read has been requested, but returning immediatiely, keep polling plugin till file read has been completed.
[146]269                };
270
[172]271                ReadResult(ReadStatus status=FILE_NOT_HANDLED):_status(status) {}
[146]272                ReadResult(const std::string& m):_status(ERROR_IN_READING_FILE),_message(m) {}
[2962]273                ReadResult(osg::Object* obj, ReadStatus status=FILE_LOADED):_status(status),_object(obj) {}
[146]274               
275                ReadResult(const ReadResult& rr):_status(rr._status),_message(rr._message),_object(rr._object) {}
276                ReadResult& operator = (const ReadResult& rr) { if (this==&rr) return *this; _status=rr._status; _message=rr._message;_object=rr._object; return *this; }
277               
[3580]278                osg::Object* getObject();
279                osg::Image* getImage();
280                osg::HeightField* getHeightField();
281                osg::Node* getNode();
282                osgDB::Archive* getArchive();
[7908]283                osg::Shader* getShader();
284
[1133]285                bool validObject() { return _object.valid(); }
286                bool validImage() { return getImage()!=0; }
[2398]287                bool validHeightField() { return getHeightField()!=0; }
[1133]288                bool validNode() { return getNode()!=0; }
[3580]289                bool validArchive() { return getArchive()!=0; }
[7908]290                bool validShader() { return getShader()!=0; }
[146]291
[3580]292                osg::Object* takeObject();
293                osg::Image* takeImage();
294                osg::HeightField* takeHeightField();
295                osg::Node* takeNode();
296                osgDB::Archive* takeArchive();
[7908]297                osg::Shader* takeShader();
[146]298
[5697]299                std::string& message() { return _message; }
[146]300                const std::string& message() const { return _message; }
301
[1133]302                ReadStatus status() const { return _status; }
[2962]303                bool success() const { return _status==FILE_LOADED || _status==FILE_LOADED_FROM_CACHE ; }
304                bool loadedFromCache() const { return _status==FILE_LOADED_FROM_CACHE; }
[1133]305                bool error() const { return _status==ERROR_IN_READING_FILE; }
[9890]306                bool notHandled() const { return _status==FILE_NOT_HANDLED || _status==NOT_IMPLEMENTED; }
[2501]307                bool notFound() const { return _status==FILE_NOT_FOUND; }
[146]308
309            protected:
310           
[172]311                ReadStatus                  _status;
[146]312                std::string                 _message;
313                osg::ref_ptr<osg::Object>   _object;
[8619]314
[146]315        };
316
317        class WriteResult
318        {
319            public:
320
[172]321                enum WriteStatus
[146]322                {
[9890]323                    NOT_IMPLEMENTED, //!< write*() method not implemented in concreate ReaderWriter.
[146]324                    FILE_NOT_HANDLED,
325                    FILE_SAVED,
326                    ERROR_IN_WRITING_FILE
327                };
328
[172]329                WriteResult(WriteStatus status=FILE_NOT_HANDLED):_status(status) {}
[146]330                WriteResult(const std::string& m):_status(ERROR_IN_WRITING_FILE),_message(m) {}
331               
332                WriteResult(const WriteResult& rr):_status(rr._status),_message(rr._message) {}
333                WriteResult& operator = (const WriteResult& rr) { if (this==&rr) return *this; _status=rr._status; _message=rr._message; return *this; }
334               
[5697]335                std::string& message() { return _message; }
[146]336                const std::string& message() const { return _message; }
337
[1133]338                WriteStatus status() const { return _status; }
339                bool success() const { return _status==FILE_SAVED; }
340                bool error() const { return _status==ERROR_IN_WRITING_FILE; }
[9890]341                bool notHandled() const { return _status==FILE_NOT_HANDLED || _status==NOT_IMPLEMENTED; }
[146]342
343            protected:
344           
[172]345                WriteStatus                 _status;
[146]346                std::string                 _message;
347        };
348
[3580]349        enum ArchiveStatus
350        {
351            READ,
352            WRITE,
353            CREATE
354        };
355
[6076]356        /** open an archive for reading, writing, or to create an empty archive for writing to.*/
[9890]357        virtual ReadResult openArchive(const std::string& /*fileName*/,ArchiveStatus, unsigned int =4096, const Options* =NULL) const { return ReadResult(ReadResult::NOT_IMPLEMENTED); }
[3580]358
[3618]359        /** open an archive for reading.*/
[9890]360        virtual ReadResult openArchive(std::istream& /*fin*/,const Options* =NULL) const { return ReadResult(ReadResult::NOT_IMPLEMENTED); }
[3618]361
[9890]362        virtual ReadResult readObject(const std::string& /*fileName*/,const Options* =NULL) const { return ReadResult(ReadResult::NOT_IMPLEMENTED); }
363        virtual ReadResult readImage(const std::string& /*fileName*/,const Options* =NULL) const { return ReadResult(ReadResult::NOT_IMPLEMENTED); }
364        virtual ReadResult readHeightField(const std::string& /*fileName*/,const Options* =NULL) const { return ReadResult(ReadResult::NOT_IMPLEMENTED); }
365        virtual ReadResult readNode(const std::string& /*fileName*/,const Options* =NULL) const { return ReadResult(ReadResult::NOT_IMPLEMENTED); }
366        virtual ReadResult readShader(const std::string& /*fileName*/,const Options* =NULL) const { return ReadResult(ReadResult::NOT_IMPLEMENTED); }
[146]367
[9890]368        virtual WriteResult writeObject(const osg::Object& /*obj*/,const std::string& /*fileName*/,const Options* =NULL) const {return WriteResult(WriteResult::NOT_IMPLEMENTED); }
369        virtual WriteResult writeImage(const osg::Image& /*image*/,const std::string& /*fileName*/,const Options* =NULL) const {return WriteResult(WriteResult::NOT_IMPLEMENTED); }
370        virtual WriteResult writeHeightField(const osg::HeightField& /*heightField*/,const std::string& /*fileName*/,const Options* =NULL) const {return WriteResult(WriteResult::NOT_IMPLEMENTED); }
371        virtual WriteResult writeNode(const osg::Node& /*node*/,const std::string& /*fileName*/,const Options* =NULL) const { return WriteResult(WriteResult::NOT_IMPLEMENTED); }
372        virtual WriteResult writeShader(const osg::Shader& /*shader*/,const std::string& /*fileName*/,const Options* =NULL) const {return WriteResult(WriteResult::NOT_IMPLEMENTED); }
[1973]373
[9890]374        virtual ReadResult readObject(std::istream& /*fin*/,const Options* =NULL) const { return ReadResult(ReadResult::NOT_IMPLEMENTED); }
375        virtual ReadResult readImage(std::istream& /*fin*/,const Options* =NULL) const { return ReadResult(ReadResult::NOT_IMPLEMENTED); }
376        virtual ReadResult readHeightField(std::istream& /*fin*/,const Options* =NULL) const { return ReadResult(ReadResult::NOT_IMPLEMENTED); }
377        virtual ReadResult readNode(std::istream& /*fin*/,const Options* =NULL) const { return ReadResult(ReadResult::NOT_IMPLEMENTED); }
378        virtual ReadResult readShader(std::istream& /*fin*/,const Options* =NULL) const { return ReadResult(ReadResult::NOT_IMPLEMENTED); }
[1973]379
[9890]380        virtual WriteResult writeObject(const osg::Object& /*obj*/,std::ostream& /*fout*/,const Options* =NULL) const { return WriteResult(WriteResult::NOT_IMPLEMENTED); }
381        virtual WriteResult writeImage(const osg::Image& /*image*/,std::ostream& /*fout*/,const Options* =NULL) const { return WriteResult(WriteResult::NOT_IMPLEMENTED); }
382        virtual WriteResult writeHeightField(const osg::HeightField& /*heightField*/,std::ostream& /*fout*/,const Options* =NULL) const { return WriteResult(WriteResult::NOT_IMPLEMENTED); }
383        virtual WriteResult writeNode(const osg::Node& /*node*/,std::ostream& /*fout*/,const Options* =NULL) const { return WriteResult(WriteResult::NOT_IMPLEMENTED); }
384        virtual WriteResult writeShader(const osg::Shader& /*shader*/,std::ostream& /*fout*/,const Options* =NULL) const { return WriteResult(WriteResult::NOT_IMPLEMENTED); }
[1973]385
[8577]386    protected:
387   
388        void supportsProtocol(const std::string& fmt, const std::string& description);
389        void supportsExtension(const std::string& fmt, const std::string& description);
390        void supportsOption(const std::string& fmt, const std::string& description);
391
392        FormatDescriptionMap _supportedProtocols;
393        FormatDescriptionMap _supportedExtensions;
394        FormatDescriptionMap _supportedOptions;
[8]395};
396
[349]397}
[8]398
[3523]399#endif // OSGDB_READERWRITER
Note: See TracBrowser for help on using the browser.