ValueMap 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2016 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. #ifndef OSG_VALUEMAP
  14. #define OSG_VALUEMAP 1
  15. #include <osg/ValueObject>
  16. #include <osg/Notify>
  17. #include <map>
  18. namespace osg {
  19. #define OSG_HAS_VALUEMAP
  20. class OSG_EXPORT ValueMap : public osg::Object
  21. {
  22. public:
  23. ValueMap();
  24. ValueMap(const ValueMap& vm, const osg::CopyOp& copyop=osg::CopyOp::SHALLOW_COPY);
  25. META_Object(osg, ValueMap);
  26. typedef std::map< osg::ref_ptr<const osg::Referenced>, osg::ref_ptr<osg::Object> > KeyValueMap;
  27. void setKeyValueMap(KeyValueMap& properties) { _keyValueMap = properties; }
  28. KeyValueMap& getKeyValueMap() { return _keyValueMap; }
  29. const KeyValueMap& getKeyValueMap() const { return _keyValueMap; }
  30. osg::Object* setValue(const osg::Referenced* key, osg::Object* object)
  31. {
  32. return (_keyValueMap[key] = object).get();
  33. }
  34. template<typename T>
  35. osg::Object* setValue(const osg::Referenced* key, const T& value)
  36. {
  37. typedef TemplateValueObject<T> UserValueObject;
  38. KeyValueMap::iterator itr = _keyValueMap.find(key);
  39. if (itr!=_keyValueMap.end())
  40. {
  41. osg::Object* obj = itr->second.get();
  42. if (typeid(*(obj))==typeid(UserValueObject))
  43. {
  44. UserValueObject* uvo = static_cast<UserValueObject*>(itr->second.get());
  45. uvo->setValue(value);
  46. return uvo;
  47. }
  48. }
  49. return (_keyValueMap[key] = new UserValueObject(value)).get();
  50. }
  51. inline osg::Object* getValue(const osg::Referenced* key)
  52. {
  53. KeyValueMap::iterator itr = _keyValueMap.find(key);
  54. return (itr!=_keyValueMap.end()) ? itr->second.get() : 0;
  55. }
  56. inline const osg::Object* getValue(const osg::Referenced* key) const
  57. {
  58. KeyValueMap::const_iterator itr = _keyValueMap.find(key);
  59. return (itr!=_keyValueMap.end()) ? itr->second.get() : 0;
  60. }
  61. template<typename T>
  62. T* getValueOfType(const osg::Referenced* key)
  63. {
  64. Object* object = getValue(key);
  65. return (object && typeid(*object)==typeid(T)) ? static_cast<T*>(object) : 0;
  66. }
  67. template<typename T>
  68. const T* getValueOfType(const osg::Referenced* key) const
  69. {
  70. const Object* object = getValue(key);
  71. return (object && typeid(*object)==typeid(T)) ? static_cast<const T*>(object) : 0;
  72. }
  73. template<typename T>
  74. bool getValue(const osg::Referenced* key, T& value)
  75. {
  76. typedef TemplateValueObject<T> UserValueObject;
  77. UserValueObject* uvo = getValueOfType<UserValueObject>(key);
  78. if (uvo)
  79. {
  80. value = uvo->getValue();
  81. return true;
  82. }
  83. else
  84. {
  85. return false;
  86. }
  87. }
  88. template<typename T>
  89. bool getValue(const osg::Referenced* key, T& value) const
  90. {
  91. typedef TemplateValueObject<T> UserValueObject;
  92. const UserValueObject* uvo = getValueOfType<UserValueObject>(key);
  93. if (uvo)
  94. {
  95. value = uvo->getValue();
  96. return true;
  97. }
  98. else
  99. {
  100. return false;
  101. }
  102. }
  103. protected:
  104. virtual ~ValueMap();
  105. KeyValueMap _keyValueMap;
  106. };
  107. }
  108. #endif