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 //
15 // OpenFlight� loader for OpenSceneGraph
16 //
17 // Copyright (C) 2005-2007 Brede Johansen
18 //
19
20 #ifndef FLT_REGISTRY_H
21 #define FLT_REGISTRY_H 1
22
23 #include <queue>
24 #include <map>
25 #include <osg/ref_ptr>
26 #include <osgDB/ObjectCache>
27 #include <osgDB/Registry>
28 #include "Opcodes.h"
29 #include "Record.h"
30
31 namespace flt {
32
33 class Registry : public osg::Referenced
34 {
35 public:
36
37 ~Registry();
38 static Registry* instance();
39
40 // Record prototypes
41 void addPrototype(int opcode, Record* prototype);
42 Record* getPrototype(int opcode);
43
44 // External read queue
45 typedef std::pair<std::string, osg::Group*> FilenameParentPair; // ExtNameNodePair;
46 typedef std::queue<FilenameParentPair> ExternalQueue;
47
getExternalReadQueue()48 inline ExternalQueue& getExternalReadQueue() { return _externalReadQueue; }
49 void addToExternalReadQueue(const std::string& filename, osg::Group* parent);
50
51 // Local cache
52 void addExternalToLocalCache(const std::string& filename, osg::Node* node);
53 osg::Node* getExternalFromLocalCache(const std::string& filename);
54 void addTextureToLocalCache(const std::string& filename, osg::StateSet* stateset);
55 osg::StateSet* getTextureFromLocalCache(const std::string& filename);
56
57 protected:
58
59 Registry();
60
61 typedef std::map<int, osg::ref_ptr<Record> > RecordProtoMap;
62 RecordProtoMap _recordProtoMap;
63
64 ExternalQueue _externalReadQueue;
65 };
66
addToExternalReadQueue(const std::string & filename,osg::Group * parent)67 inline void Registry::addToExternalReadQueue(const std::string& filename, osg::Group* parent)
68 {
69 _externalReadQueue.push( FilenameParentPair(filename,parent) );
70 }
71
addExternalToLocalCache(const std::string & filename,osg::Node * node)72 inline void Registry::addExternalToLocalCache(const std::string& filename, osg::Node* node)
73 {
74 osgDB::Registry::instance()->addEntryToObjectCache(filename, node);
75 }
76
getExternalFromLocalCache(const std::string & filename)77 inline osg::Node* Registry::getExternalFromLocalCache(const std::string& filename)
78 {
79 return dynamic_cast<osg::Node*>(osgDB::Registry::instance()->getFromObjectCache(filename));
80 }
81
addTextureToLocalCache(const std::string & filename,osg::StateSet * stateset)82 inline void Registry::addTextureToLocalCache(const std::string& filename, osg::StateSet* stateset)
83 {
84 osgDB::Registry::instance()->addEntryToObjectCache(filename, stateset);
85 }
86
getTextureFromLocalCache(const std::string & filename)87 inline osg::StateSet* Registry::getTextureFromLocalCache(const std::string& filename)
88 {
89 return dynamic_cast<osg::StateSet*>(osgDB::Registry::instance()->getFromObjectCache(filename));
90 }
91
92 /** Proxy class for automatic registration of reader/writers with the Registry.*/
93 template<class T>
94 class RegisterRecordProxy
95 {
96 public:
97
RegisterRecordProxy(int opcode)98 explicit RegisterRecordProxy(int opcode)
99 {
100 Registry::instance()->addPrototype(opcode,new T);
101 }
102
~RegisterRecordProxy()103 ~RegisterRecordProxy() {}
104 };
105
106 //////////////////////////////////////////////////////////////////////////
107
108 extern "C"
109 {
110 typedef void (* CRecordFunction) (void);
111 }
112
113 struct RecordFunctionProxy
114 {
RecordFunctionProxyRecordFunctionProxy115 RecordFunctionProxy(CRecordFunction function) { (function)(); }
116 };
117
118 #define USE_FLTRECORD(recname, opcode) \
119 extern "C" void osgfltrec_##recname_##opcode(void); \
120 static flt::RecordFunctionProxy proxy_fltrecord_##recname_##opcode(osgfltrec_##recname_##opcode);
121
122 #define REGISTER_FLTRECORD(recname, opcode) \
123 extern "C" void osgfltrec_##recname_##opcode(void) {} \
124 static flt::RegisterRecordProxy<recname> g_proxy_fltrecord_##recname_##opcode(opcode);
125
126
127 } // end namespace
128
129 #endif
130