ValueStack 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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_VALUESTACK
  14. #define OSG_VALUESTACK 1
  15. #include <osg/ValueMap>
  16. namespace osg {
  17. #define OSG_HAS_VALUESTACK
  18. class OSG_EXPORT ValueStack : public osg::Object
  19. {
  20. public:
  21. ValueStack();
  22. ValueStack(const ValueStack& ps, const osg::CopyOp& copyop=osg::CopyOp::SHALLOW_COPY);
  23. META_Object(osg, ValueStack);
  24. typedef std::vector< osg::ref_ptr<Object> > Values;
  25. typedef std::map< osg::ref_ptr<const osg::Referenced>, Values> ValueStackMap;
  26. void setValueStackMap(ValueStackMap& pm) { _valuesMap = pm; }
  27. ValueStackMap& getValueStackMap() { return _valuesMap; }
  28. const ValueStackMap& getValueStackMap() const { return _valuesMap; }
  29. inline void push(const Referenced* key, Object* value)
  30. {
  31. _valuesMap[key].push_back(value);
  32. }
  33. template<typename T>
  34. void push(const osg::Referenced* key, const T& value)
  35. {
  36. typedef TemplateValueObject<T> UserValueObject;
  37. _valuesMap[key].push_back(new UserValueObject(value));
  38. }
  39. inline void pop(const Referenced* key)
  40. {
  41. _valuesMap[key].pop_back();
  42. }
  43. inline void push(ValueMap* valueMap)
  44. {
  45. if (valueMap)
  46. {
  47. ValueMap::KeyValueMap& keyValueMap = valueMap->getKeyValueMap();
  48. for(ValueMap::KeyValueMap::iterator itr = keyValueMap.begin();
  49. itr != keyValueMap.end();
  50. ++itr)
  51. {
  52. push(itr->first.get(), itr->second.get());
  53. }
  54. }
  55. }
  56. inline void pop(ValueMap* valueMap)
  57. {
  58. if (valueMap)
  59. {
  60. ValueMap::KeyValueMap& keyValueMap = valueMap->getKeyValueMap();
  61. for(ValueMap::KeyValueMap::iterator itr = keyValueMap.begin();
  62. itr != keyValueMap.end();
  63. ++itr)
  64. {
  65. pop(itr->first.get());
  66. }
  67. }
  68. }
  69. inline osg::Object* getValue(const osg::Referenced* key)
  70. {
  71. ValueStackMap::iterator itr = _valuesMap.find(key);
  72. if (itr==_valuesMap.end()) return 0;
  73. Values& values = itr->second;
  74. if (values.empty()) return 0;
  75. return values.back().get();
  76. }
  77. inline const osg::Object* getValue(const osg::Referenced* key) const
  78. {
  79. ValueStackMap::const_iterator itr = _valuesMap.find(key);
  80. if (itr==_valuesMap.end()) return 0;
  81. const Values& values = itr->second;
  82. if (values.empty()) return 0;
  83. return values.back().get();
  84. }
  85. template<typename T>
  86. T* getValueOfType(const osg::Referenced* key)
  87. {
  88. Object* object = getValue(key);
  89. return (object && typeid(*object)==typeid(T)) ? static_cast<T*>(object) : 0;
  90. }
  91. template<typename T>
  92. const T* getValueOfType(const osg::Referenced* key) const
  93. {
  94. const Object* object = getValue(key);
  95. return (object && typeid(*object)==typeid(T)) ? static_cast<const T*>(object) : 0;
  96. }
  97. template<typename T>
  98. bool getValue(const osg::Referenced* key, T& value)
  99. {
  100. typedef TemplateValueObject<T> UserValueObject;
  101. UserValueObject* uvo = getValueOfType<UserValueObject>(key);
  102. if (uvo)
  103. {
  104. value = uvo->getValue();
  105. return true;
  106. }
  107. else
  108. {
  109. return false;
  110. }
  111. }
  112. template<typename T>
  113. bool getValue(const osg::Referenced* key, T& value) const
  114. {
  115. typedef TemplateValueObject<T> UserValueObject;
  116. const UserValueObject* uvo = getValueOfType<UserValueObject>(key);
  117. if (uvo)
  118. {
  119. value = uvo->getValue();
  120. return true;
  121. }
  122. else
  123. {
  124. return false;
  125. }
  126. }
  127. protected:
  128. virtual ~ValueStack();
  129. ValueStackMap _valuesMap;
  130. };
  131. }
  132. #endif