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