Registry 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. //osgFX - Copyright (C) 2003 Marco Jez
  14. #ifndef OSGFX_REGISTRY_
  15. #define OSGFX_REGISTRY_
  16. #include <osgFX/Export>
  17. #include <osgFX/Effect>
  18. #include <osg/ref_ptr>
  19. #include <map>
  20. #include <string>
  21. namespace osgFX
  22. {
  23. class OSGFX_EXPORT Registry : public osg::Referenced
  24. {
  25. public:
  26. struct Proxy {
  27. Proxy(const Effect* effect): _effect(effect)
  28. {
  29. Registry::instance()->registerEffect(effect);
  30. }
  31. ~Proxy()
  32. {
  33. Registry::instance()->removeEffect(_effect.get());
  34. }
  35. private:
  36. osg::ref_ptr<const Effect> _effect;
  37. };
  38. typedef std::map<std::string, osg::ref_ptr<const Effect> > EffectMap;
  39. static Registry* instance();
  40. inline void registerEffect(const Effect* effect);
  41. inline void removeEffect(const Effect* effect);
  42. inline const EffectMap& getEffectMap() const;
  43. protected:
  44. // Registry is a singleton; constructor and destructor must be protected
  45. Registry();
  46. ~Registry() {}
  47. private:
  48. EffectMap _effects;
  49. };
  50. // INLINE METHODS
  51. inline const Registry::EffectMap& Registry::getEffectMap() const
  52. {
  53. return _effects;
  54. }
  55. inline void Registry::registerEffect(const Effect* effect)
  56. {
  57. _effects[effect->effectName()] = effect;
  58. }
  59. inline void Registry::removeEffect(const Effect* effect)
  60. {
  61. EffectMap::iterator itr = _effects.find(effect->effectName());
  62. if (itr != _effects.end())
  63. {
  64. _effects.erase(itr);
  65. }
  66. }
  67. }
  68. #endif