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

Revision 10057, 19.7 kB (checked in by robert, 4 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
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                    _pluginStringData(options._pluginStringData){}
162
163                META_Object(osgDB,Options);
164
165                /** Set the general Options string.*/
166                void setOptionString(const std::string& str) { _str = str; }
167
168                /** Get the general Options string.*/
169                const std::string& getOptionString() const { return _str; }
170
171                /** Set the database path to use a hint of where to look when loading models.*/
172                void setDatabasePath(const std::string& str) { _databasePaths.clear();  _databasePaths.push_back(str); }
173
174                /** Get the database path which is used a hint of where to look when loading models.*/
175                FilePathList& getDatabasePathList() { return _databasePaths; }
176
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
180
181                /** Set whether the Registry::ObjectCache should be used by default.*/
182                void setObjectCacheHint(CacheHintOptions useObjectCache) { _objectCacheHint = useObjectCache; }
183
184                /** Get whether the Registry::ObjectCache should be used by default.*/
185                CacheHintOptions getObjectCacheHint() const { return _objectCacheHint; }
186
187
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
195                /** Set the password map to be used by plugins when access files from secure locations.*/
196                void setAuthenticationMap(AuthenticationMap* authenticationMap) { _authenticationMap = authenticationMap; }
197
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
202                /** Sets a plugindata value PluginData with a string */
203                void setPluginData(const std::string& s, void* v) const { _pluginData[s] = v; }
204
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 */
216                void removePluginData(const std::string& s) const { _pluginData.erase(s); }
217
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
238            protected:
239
240                virtual ~Options() {}
241
242                std::string                     _str;
243                FilePathList                    _databasePaths;
244                CacheHintOptions                _objectCacheHint;
245                BuildKdTreesHint                _buildKdTreesHint;
246                osg::ref_ptr<AuthenticationMap> _authenticationMap;
247
248                typedef std::map<std::string,void*> PluginDataMap;
249                mutable PluginDataMap _pluginData;
250                typedef std::map<std::string,std::string> PluginStringDataMap;
251                mutable PluginStringDataMap _pluginStringData;
252
253        };
254
255
256        class OSGDB_EXPORT ReadResult
257        {
258            public:
259
260                enum ReadStatus
261                {
262                    NOT_IMPLEMENTED, //!< raad*() method not implemented in concreate ReaderWriter.
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.
268                    FILE_REQUESTED //!< Asyncronous file read has been requested, but returning immediatiely, keep polling plugin till file read has been completed.
269                };
270
271                ReadResult(ReadStatus status=FILE_NOT_HANDLED):_status(status) {}
272                ReadResult(const std::string& m):_status(ERROR_IN_READING_FILE),_message(m) {}
273                ReadResult(osg::Object* obj, ReadStatus status=FILE_LOADED):_status(status),_object(obj) {}
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               
278                osg::Object* getObject();
279                osg::Image* getImage();
280                osg::HeightField* getHeightField();
281                osg::Node* getNode();
282                osgDB::Archive* getArchive();
283                osg::Shader* getShader();
284
285                bool validObject() { return _object.valid(); }
286                bool validImage() { return getImage()!=0; }
287                bool validHeightField() { return getHeightField()!=0; }
288                bool validNode() { return getNode()!=0; }
289                bool validArchive() { return getArchive()!=0; }
290                bool validShader() { return getShader()!=0; }
291
292                osg::Object* takeObject();
293                osg::Image* takeImage();
294                osg::HeightField* takeHeightField();
295                osg::Node* takeNode();
296                osgDB::Archive* takeArchive();
297                osg::Shader* takeShader();
298
299                std::string& message() { return _message; }
300                const std::string& message() const { return _message; }
301
302                ReadStatus status() const { return _status; }
303                bool success() const { return _status==FILE_LOADED || _status==FILE_LOADED_FROM_CACHE ; }
304                bool loadedFromCache() const { return _status==FILE_LOADED_FROM_CACHE; }
305                bool error() const { return _status==ERROR_IN_READING_FILE; }
306                bool notHandled() const { return _status==FILE_NOT_HANDLED || _status==NOT_IMPLEMENTED; }
307                bool notFound() const { return _status==FILE_NOT_FOUND; }
308
309            protected:
310           
311                ReadStatus                  _status;
312                std::string                 _message;
313                osg::ref_ptr<osg::Object>   _object;
314
315        };
316
317        class WriteResult
318        {
319            public:
320
321                enum WriteStatus
322                {
323                    NOT_IMPLEMENTED, //!< write*() method not implemented in concreate ReaderWriter.
324                    FILE_NOT_HANDLED,
325                    FILE_SAVED,
326                    ERROR_IN_WRITING_FILE
327                };
328
329                WriteResult(WriteStatus status=FILE_NOT_HANDLED):_status(status) {}
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               
335                std::string& message() { return _message; }
336                const std::string& message() const { return _message; }
337
338                WriteStatus status() const { return _status; }
339                bool success() const { return _status==FILE_SAVED; }
340                bool error() const { return _status==ERROR_IN_WRITING_FILE; }
341                bool notHandled() const { return _status==FILE_NOT_HANDLED || _status==NOT_IMPLEMENTED; }
342
343            protected:
344           
345                WriteStatus                 _status;
346                std::string                 _message;
347        };
348
349        enum ArchiveStatus
350        {
351            READ,
352            WRITE,
353            CREATE
354        };
355
356        /** open an archive for reading, writing, or to create an empty archive for writing to.*/
357        virtual ReadResult openArchive(const std::string& /*fileName*/,ArchiveStatus, unsigned int =4096, const Options* =NULL) const { return ReadResult(ReadResult::NOT_IMPLEMENTED); }
358
359        /** open an archive for reading.*/
360        virtual ReadResult openArchive(std::istream& /*fin*/,const Options* =NULL) const { return ReadResult(ReadResult::NOT_IMPLEMENTED); }
361
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); }
367
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); }
373
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); }
379
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); }
385
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;
395};
396
397}
398
399#endif // OSGDB_READERWRITER
Note: See TracBrowser for help on using the browser.