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
24{
25 public:
26
28
30
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
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>
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
161
162};
163
164
165}
166
167#endif
Definition CopyOp:41
@ SHALLOW_COPY
Definition CopyOp:47
Definition Object:61
Definition Referenced:44
Definition ValueObject:283
Definition ValueMap:26
KeyValueMap & getKeyValueMap()
Definition ValueMap:39
std::map< osg::ref_ptr< const osg::Referenced >, osg::ref_ptr< osg::Object > > KeyValueMap
Definition ValueMap:35
Definition ValueStack:24
void push(const Referenced *key, Object *value)
Definition ValueStack:42
ValueStackMap _valuesMap
Definition ValueStack:160
ValueStackMap & getValueStackMap()
Definition ValueStack:38
void pop(const Referenced *key)
Definition ValueStack:54
T * getValueOfType(const osg::Referenced *key)
Definition ValueStack:110
void push(const osg::Referenced *key, const T &value)
Definition ValueStack:48
void pop(ValueMap *valueMap)
Definition ValueStack:73
ValueStack(const ValueStack &ps, const osg::CopyOp &copyop=osg::CopyOp::SHALLOW_COPY)
bool getValue(const osg::Referenced *key, T &value) const
Definition ValueStack:141
std::map< osg::ref_ptr< const osg::Referenced >, Values > ValueStackMap
Definition ValueStack:34
virtual ~ValueStack()
bool getValue(const osg::Referenced *key, T &value)
Definition ValueStack:125
const T * getValueOfType(const osg::Referenced *key) const
Definition ValueStack:118
const ValueStackMap & getValueStackMap() const
Definition ValueStack:40
std::vector< osg::ref_ptr< Object > > Values
Definition ValueStack:33
void setValueStackMap(ValueStackMap &pm)
Definition ValueStack:36
void push(ValueMap *valueMap)
Definition ValueStack:59
const osg::Object * getValue(const osg::Referenced *key) const
Definition ValueStack:98
META_Object(osg, ValueStack)
osg::Object * getValue(const osg::Referenced *key)
Definition ValueStack:87
author: Julien Valentin 2017 (mp3butcher@hotmail.com)
Definition AlphaFunc:19
#define OSG_EXPORT
Definition osg/Export:39