| 1 | |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | |
|---|
| 13 | |
|---|
| 14 | |
|---|
| 15 | |
|---|
| 16 | |
|---|
| 17 | |
|---|
| 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 "Opcodes.h" |
|---|
| 27 | #include "Record.h" |
|---|
| 28 | |
|---|
| 29 | namespace flt { |
|---|
| 30 | |
|---|
| 31 | class Registry : public osg::Referenced |
|---|
| 32 | { |
|---|
| 33 | public: |
|---|
| 34 | |
|---|
| 35 | ~Registry(); |
|---|
| 36 | static Registry* instance(); |
|---|
| 37 | |
|---|
| 38 | |
|---|
| 39 | void addPrototype(int opcode, Record* prototype); |
|---|
| 40 | Record* getPrototype(int opcode); |
|---|
| 41 | |
|---|
| 42 | |
|---|
| 43 | typedef std::pair<std::string, osg::Group*> FilenameParentPair; |
|---|
| 44 | typedef std::queue<FilenameParentPair> ExternalQueue; |
|---|
| 45 | |
|---|
| 46 | inline ExternalQueue& getExternalReadQueue() { return _externalReadQueue; } |
|---|
| 47 | void addToExternalReadQueue(const std::string& filename, osg::Group* parent); |
|---|
| 48 | |
|---|
| 49 | |
|---|
| 50 | void addExternalToLocalCache(const std::string& filename, osg::Node* node); |
|---|
| 51 | osg::Node* getExternalFromLocalCache(const std::string& filename); |
|---|
| 52 | void addTextureToLocalCache(const std::string& filename, osg::StateSet* stateset); |
|---|
| 53 | osg::StateSet* getTextureFromLocalCache(const std::string& filename); |
|---|
| 54 | void clearLocalCache(); |
|---|
| 55 | |
|---|
| 56 | protected: |
|---|
| 57 | |
|---|
| 58 | Registry(); |
|---|
| 59 | |
|---|
| 60 | typedef std::map<int, osg::ref_ptr<Record> > RecordProtoMap; |
|---|
| 61 | RecordProtoMap _recordProtoMap; |
|---|
| 62 | |
|---|
| 63 | ExternalQueue _externalReadQueue; |
|---|
| 64 | |
|---|
| 65 | |
|---|
| 66 | typedef std::map<std::string, osg::ref_ptr<osg::Node> > ExternalCacheMap; |
|---|
| 67 | ExternalCacheMap _externalCacheMap; |
|---|
| 68 | |
|---|
| 69 | |
|---|
| 70 | typedef std::map<std::string, osg::ref_ptr<osg::StateSet> > TextureCacheMap; |
|---|
| 71 | TextureCacheMap _textureCacheMap; |
|---|
| 72 | }; |
|---|
| 73 | |
|---|
| 74 | inline void Registry::addToExternalReadQueue(const std::string& filename, osg::Group* parent) |
|---|
| 75 | { |
|---|
| 76 | _externalReadQueue.push( FilenameParentPair(filename,parent) ); |
|---|
| 77 | } |
|---|
| 78 | |
|---|
| 79 | inline void Registry::addExternalToLocalCache(const std::string& filename, osg::Node* node) |
|---|
| 80 | { |
|---|
| 81 | _externalCacheMap[filename] = node; |
|---|
| 82 | } |
|---|
| 83 | |
|---|
| 84 | inline osg::Node* Registry::getExternalFromLocalCache(const std::string& filename) |
|---|
| 85 | { |
|---|
| 86 | ExternalCacheMap::iterator itr = _externalCacheMap.find(filename); |
|---|
| 87 | if (itr != _externalCacheMap.end()) |
|---|
| 88 | return (*itr).second.get(); |
|---|
| 89 | return NULL; |
|---|
| 90 | } |
|---|
| 91 | |
|---|
| 92 | inline void Registry::addTextureToLocalCache(const std::string& filename, osg::StateSet* stateset) |
|---|
| 93 | { |
|---|
| 94 | _textureCacheMap[filename] = stateset; |
|---|
| 95 | } |
|---|
| 96 | |
|---|
| 97 | inline osg::StateSet* Registry::getTextureFromLocalCache(const std::string& filename) |
|---|
| 98 | { |
|---|
| 99 | TextureCacheMap::iterator itr = _textureCacheMap.find(filename); |
|---|
| 100 | if (itr != _textureCacheMap.end()) |
|---|
| 101 | return (*itr).second.get(); |
|---|
| 102 | return NULL; |
|---|
| 103 | } |
|---|
| 104 | |
|---|
| 105 | inline void Registry::clearLocalCache() |
|---|
| 106 | { |
|---|
| 107 | _externalCacheMap.clear(); |
|---|
| 108 | _textureCacheMap.clear(); |
|---|
| 109 | } |
|---|
| 110 | |
|---|
| 111 | |
|---|
| 112 | template<class T> |
|---|
| 113 | class RegisterRecordProxy |
|---|
| 114 | { |
|---|
| 115 | public: |
|---|
| 116 | |
|---|
| 117 | explicit RegisterRecordProxy(int opcode) |
|---|
| 118 | { |
|---|
| 119 | Registry::instance()->addPrototype(opcode,new T); |
|---|
| 120 | } |
|---|
| 121 | |
|---|
| 122 | ~RegisterRecordProxy() {} |
|---|
| 123 | }; |
|---|
| 124 | |
|---|
| 125 | |
|---|
| 126 | |
|---|
| 127 | extern "C" |
|---|
| 128 | { |
|---|
| 129 | typedef void (* CRecordFunction) (void); |
|---|
| 130 | } |
|---|
| 131 | |
|---|
| 132 | struct RecordFunctionProxy |
|---|
| 133 | { |
|---|
| 134 | RecordFunctionProxy(CRecordFunction function) { (function)(); } |
|---|
| 135 | }; |
|---|
| 136 | |
|---|
| 137 | #define USE_FLTRECORD(recname, opcode) \ |
|---|
| 138 | extern "C" void osgfltrec_##recname_##opcode(void); \ |
|---|
| 139 | static flt::RecordFunctionProxy proxy_fltrecord_##recname_##opcode(osgfltrec_##recname_##opcode); |
|---|
| 140 | |
|---|
| 141 | #define REGISTER_FLTRECORD(recname, opcode) \ |
|---|
| 142 | extern "C" void osgfltrec_##recname_##opcode(void) {} \ |
|---|
| 143 | static flt::RegisterRecordProxy<recname> g_proxy_fltrecord_##recname_##opcode(opcode); |
|---|
| 144 | |
|---|
| 145 | |
|---|
| 146 | } |
|---|
| 147 | |
|---|
| 148 | #endif |
|---|