1/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2016 Robert Osfield
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.
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.
15#define OSG_VALUESTACK 1
17#include <osg/ValueMap>
21#define OSG_HAS_VALUESTACK
23class OSG_EXPORT ValueStack : public osg::Object
29 ValueStack(const ValueStack& ps, const osg::CopyOp& copyop=osg::CopyOp::SHALLOW_COPY);
31 META_Object(osg, ValueStack);
33 typedef std::vector< osg::ref_ptr<Object> > Values;
34 typedef std::map< osg::ref_ptr<const osg::Referenced>, Values> ValueStackMap;
36 void setValueStackMap(ValueStackMap& pm) { _valuesMap = pm; }
38 ValueStackMap& getValueStackMap() { return _valuesMap; }
40 const ValueStackMap& getValueStackMap() const { return _valuesMap; }
42 inline void push(const Referenced* key, Object* value)
44 _valuesMap[key].push_back(value);
48 void push(const osg::Referenced* key, const T& value)
50 typedef TemplateValueObject<T> UserValueObject;
51 _valuesMap[key].push_back(new UserValueObject(value));
54 inline void pop(const Referenced* key)
56 _valuesMap[key].pop_back();
59 inline void push(ValueMap* valueMap)
63 ValueMap::KeyValueMap& keyValueMap = valueMap->getKeyValueMap();
64 for(ValueMap::KeyValueMap::iterator itr = keyValueMap.begin();
65 itr != keyValueMap.end();
68 push(itr->first.get(), itr->second.get());
73 inline void pop(ValueMap* valueMap)
77 ValueMap::KeyValueMap& keyValueMap = valueMap->getKeyValueMap();
78 for(ValueMap::KeyValueMap::iterator itr = keyValueMap.begin();
79 itr != keyValueMap.end();
82 pop(itr->first.get());
87 inline osg::Object* getValue(const osg::Referenced* key)
89 ValueStackMap::iterator itr = _valuesMap.find(key);
90 if (itr==_valuesMap.end()) return 0;
92 Values& values = itr->second;
93 if (values.empty()) return 0;
95 return values.back().get();
98 inline const osg::Object* getValue(const osg::Referenced* key) const
100 ValueStackMap::const_iterator itr = _valuesMap.find(key);
101 if (itr==_valuesMap.end()) return 0;
103 const Values& values = itr->second;
104 if (values.empty()) return 0;
106 return values.back().get();
110 T* getValueOfType(const osg::Referenced* key)
112 Object* object = getValue(key);
113 return (object && typeid(*object)==typeid(T)) ? static_cast<T*>(object) : 0;
118 const T* getValueOfType(const osg::Referenced* key) const
120 const Object* object = getValue(key);
121 return (object && typeid(*object)==typeid(T)) ? static_cast<const T*>(object) : 0;
125 bool getValue(const osg::Referenced* key, T& value)
127 typedef TemplateValueObject<T> UserValueObject;
128 UserValueObject* uvo = getValueOfType<UserValueObject>(key);
131 value = uvo->getValue();
141 bool getValue(const osg::Referenced* key, T& value) const
143 typedef TemplateValueObject<T> UserValueObject;
144 const UserValueObject* uvo = getValueOfType<UserValueObject>(key);
147 value = uvo->getValue();
158 virtual ~ValueStack();
160 ValueStackMap _valuesMap;