root/OpenSceneGraph/trunk/include/osgDB/Options @ 10171

Revision 10171, 10.3 kB (checked in by robert, 6 years ago)

Refactored the Registry::ReadFileCallback?, WriteFileCallback? and ReaderWriter::Options to they are now defined in their own header and in the osgDB namespace.

Introduced a new FindFileCallback? to Registry to compliement the existing ReadFileCallback? and WriteFileCallback?.

Added support for assign Find, Read and WriteFileCallbacks? to osdDB::Options to enable plugins/applications to override the callbacks just for that
read/write call and any nested file operations

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_OPTIONS
15#define OSGDB_OPTIONS 1
16
17#include <osgDB/AuthenticationMap>
18#include <osgDB/ReaderWriter>
19
20#include <deque>
21#include <list>
22#include <iosfwd>
23
24namespace osgDB {
25
26/** list of directories to search through which searching for files. */
27typedef std::deque<std::string> FilePathList;
28
29enum CaseSensitivity
30{
31    CASE_SENSITIVE,
32    CASE_INSENSITIVE
33};
34
35// forward decare
36class Options;
37
38class OSGDB_EXPORT FindFileCallback : public virtual osg::Referenced
39{
40    public:
41
42        virtual std::string findDataFile(const std::string& filename, const Options* options, CaseSensitivity caseSensitivity);
43
44        virtual std::string findLibraryFile(const std::string& filename, const Options* options, CaseSensitivity caseSensitivity);
45
46    protected:
47        virtual ~FindFileCallback() {}
48};
49
50
51class OSGDB_EXPORT ReadFileCallback : public virtual osg::Referenced
52{
53    public:
54
55        virtual ReaderWriter::ReadResult openArchive(const std::string& filename,ReaderWriter::ArchiveStatus status, unsigned int indexBlockSizeHint, const Options* useObjectCache);
56
57        virtual ReaderWriter::ReadResult readObject(const std::string& filename, const Options* options);
58
59        virtual ReaderWriter::ReadResult readImage(const std::string& filename, const Options* options);
60
61        virtual ReaderWriter::ReadResult readHeightField(const std::string& filename, const Options* options);
62
63        virtual ReaderWriter::ReadResult readNode(const std::string& filename, const Options* options);
64
65        virtual ReaderWriter::ReadResult readShader(const std::string& filename, const Options* options);
66
67    protected:
68        virtual ~ReadFileCallback() {}
69};
70
71class OSGDB_EXPORT WriteFileCallback : public virtual osg::Referenced
72{
73    public:
74
75        virtual ReaderWriter::WriteResult writeObject(const osg::Object& obj, const std::string& fileName,const Options* options);
76
77        virtual ReaderWriter::WriteResult writeImage(const osg::Image& obj, const std::string& fileName,const Options* options);
78
79        virtual ReaderWriter::WriteResult writeHeightField(const osg::HeightField& obj, const std::string& fileName,const Options* options);
80
81        virtual ReaderWriter::WriteResult writeNode(const osg::Node& obj, const std::string& fileName,const Options* options);
82
83        virtual ReaderWriter::WriteResult writeShader(const osg::Shader& obj, const std::string& fileName,const Options* options);
84
85    protected:
86        virtual ~WriteFileCallback() {}
87};
88
89/** Options base class used for passing options into plugins to control their operation.*/
90class Options : public osg::Object
91{
92    public:
93
94        /// bit mask for setting up which object types get cached by readObject/Image/HeightField/Node(filename) calls
95        enum CacheHintOptions
96        {   /// do not cache objects of any type
97            CACHE_NONE          = 0,
98
99            /// cache nodes loaded via readNode(filename)
100            CACHE_NODES         = 1<<0,
101
102            /// cache images loaded via readImage(filename)
103            CACHE_IMAGES        = 1<<1,
104
105            /// cache heightfield loaded via readHeightField(filename)
106            CACHE_HEIGHTFIELDS  = 1<<2,
107
108            /// cache heightfield loaded via readHeightField(filename)
109            CACHE_ARCHIVES      = 1<<3,
110
111            /// cache objects loaded via readObject(filename)
112            CACHE_OBJECTS       = 1<<4,
113
114            /// cache shaders loaded via readShader(filename)
115            CACHE_SHADERS       = 1<<5,
116
117            /// cache on all read*(filename) calls
118            CACHE_ALL           = CACHE_NODES |
119                                    CACHE_IMAGES |
120                                    CACHE_HEIGHTFIELDS |
121                                    CACHE_ARCHIVES |
122                                    CACHE_OBJECTS |
123                                    CACHE_SHADERS
124        };
125
126        /// range of options of whether to build kdtrees automatically on loading
127        enum BuildKdTreesHint
128        {
129            NO_PREFERENCE,
130            DO_NOT_BUILD_KDTREES,
131            BUILD_KDTREES
132        };
133
134
135        Options():
136            osg::Object(true),
137            _objectCacheHint(CACHE_ARCHIVES),
138            _buildKdTreesHint(NO_PREFERENCE) {}
139
140        Options(const std::string& str):
141            osg::Object(true),
142            _str(str),
143            _objectCacheHint(CACHE_ARCHIVES),
144            _buildKdTreesHint(NO_PREFERENCE) {}
145
146        Options(const Options& options,const osg::CopyOp& copyop=osg::CopyOp::SHALLOW_COPY):
147            osg::Object(options,copyop),
148            _str(options._str),
149            _databasePaths(options._databasePaths),
150            _objectCacheHint(options._objectCacheHint),
151            _buildKdTreesHint(options._buildKdTreesHint),
152            _pluginData(options._pluginData),
153            _pluginStringData(options._pluginStringData){}
154
155        META_Object(osgDB,Options);
156
157        /** Set the general Options string.*/
158        void setOptionString(const std::string& str) { _str = str; }
159
160        /** Get the general Options string.*/
161        const std::string& getOptionString() const { return _str; }
162
163        /** Set the database path to use a hint of where to look when loading models.*/
164        void setDatabasePath(const std::string& str) { _databasePaths.clear();  _databasePaths.push_back(str); }
165
166        /** Get the database path which is used a hint of where to look when loading models.*/
167        FilePathList& getDatabasePathList() { return _databasePaths; }
168
169        /** Get the const database path which is used a hint of where to look when loading models.*/
170        const FilePathList& getDatabasePathList() const { return _databasePaths; }
171
172
173        /** Set whether the Registry::ObjectCache should be used by default.*/
174        void setObjectCacheHint(CacheHintOptions useObjectCache) { _objectCacheHint = useObjectCache; }
175
176        /** Get whether the Registry::ObjectCache should be used by default.*/
177        CacheHintOptions getObjectCacheHint() const { return _objectCacheHint; }
178
179
180        /** Set whether the KdTrees should be built for geometry in the loader model. */
181        void setBuildKdTreesHint(BuildKdTreesHint hint) { _buildKdTreesHint = hint; }
182
183        /** Get whether the KdTrees should be built for geometry in the loader model. */
184        BuildKdTreesHint getBuildKdTreesHint() const { return _buildKdTreesHint; }
185
186
187        /** Set the password map to be used by plugins when access files from secure locations.*/
188        void setAuthenticationMap(AuthenticationMap* authenticationMap) { _authenticationMap = authenticationMap; }
189
190        /** Get the password map to be used by plugins when access files from secure locations.*/
191        const AuthenticationMap* getAuthenticationMap() const { return _authenticationMap.get(); }
192
193
194        /** Sets a plugindata value PluginData with a string */
195        void setPluginData(const std::string& s, void* v) const { _pluginData[s] = v; }
196
197        /** Get a value from the PluginData */
198        void* getPluginData(const std::string& s) { return _pluginData[s]; }
199
200        /** Get a value from the PluginData */
201        const void* getPluginData(const std::string& s) const
202        {
203            PluginDataMap::const_iterator itr = _pluginData.find(s);
204            return (itr == _pluginData.end()) ? 0 : itr->second;
205        }
206
207        /** Remove a value from the PluginData */
208        void removePluginData(const std::string& s) const { _pluginData.erase(s); }
209
210
211        /** Sets a plugindata value PluginData with a string */
212        void setPluginStringData(const std::string& s, const std::string& v) const { _pluginStringData[s] = v; }
213
214        /** Get a string from the PluginStrData */
215        std::string getPluginStringData(const std::string& s) { return _pluginStringData[s]; }
216
217        /** Get a value from the PluginData */
218        const std::string getPluginStringData(const std::string& s) const
219        {
220            PluginStringDataMap::const_iterator itr = _pluginStringData.find(s);
221            return (itr == _pluginStringData.end()) ? std::string("") : itr->second;
222        }
223
224        /** Remove a value from the PluginData */
225        void removePluginStringData(const std::string& s) const { _pluginStringData.erase(s); }
226
227
228
229        /** Set the find callback to use in place of the default findFile calls.*/
230        void setFindFileCallback( FindFileCallback* cb) { _findFileCallback = cb; }
231
232        /** Get the const findFile callback.*/
233        FindFileCallback* getFindFileCallback() const { return _findFileCallback.get(); }
234
235
236        /** Set the read callback to use in place of the default readFile calls.*/
237        void setReadFileCallback( ReadFileCallback* cb) { _readFileCallback = cb; }
238
239        /** Get the const readFile callback.*/
240        ReadFileCallback* getReadFileCallback() const { return _readFileCallback.get(); }
241
242
243        /** Set the Registry callback to use in place of the default writeFile calls.*/
244        void setWriteFileCallback( WriteFileCallback* cb) { _writeFileCallback = cb; }
245
246        /** Get the const writeFile callback.*/
247        WriteFileCallback* getWriteFileCallback() const { return _writeFileCallback.get(); }
248
249
250    protected:
251
252        virtual ~Options() {}
253
254        std::string                     _str;
255        FilePathList                    _databasePaths;
256        CacheHintOptions                _objectCacheHint;
257        BuildKdTreesHint                _buildKdTreesHint;
258        osg::ref_ptr<AuthenticationMap> _authenticationMap;
259
260        typedef std::map<std::string,void*> PluginDataMap;
261        mutable PluginDataMap _pluginData;
262        typedef std::map<std::string,std::string> PluginStringDataMap;
263        mutable PluginStringDataMap _pluginStringData;
264
265        osg::ref_ptr<FindFileCallback>      _findFileCallback;
266        osg::ref_ptr<ReadFileCallback>      _readFileCallback;
267        osg::ref_ptr<WriteFileCallback>     _writeFileCallback;
268};
269
270}
271
272#endif // OSGDB_OPTIONS
Note: See TracBrowser for help on using the browser.