openscenegraph
ValueMap
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_VALUEMAP
15#define OSG_VALUEMAP 1
16
17#include <osg/ValueObject>
18#include <osg/Notify>
19#include <map>
20
21namespace osg {
22
23#define OSG_HAS_VALUEMAP
24
25class OSG_EXPORT ValueMap : public osg::Object
26{
27 public:
28
29 ValueMap();
30
31 ValueMap(const ValueMap& vm, const osg::CopyOp& copyop=osg::CopyOp::SHALLOW_COPY);
32
33 META_Object(osg, ValueMap);
34
35 typedef std::map< osg::ref_ptr<const osg::Referenced>, osg::ref_ptr<osg::Object> > KeyValueMap;
36
37 void setKeyValueMap(KeyValueMap& properties) { _keyValueMap = properties; }
38
39 KeyValueMap& getKeyValueMap() { return _keyValueMap; }
40
41 const KeyValueMap& getKeyValueMap() const { return _keyValueMap; }
42
43 osg::Object* setValue(const osg::Referenced* key, osg::Object* object)
44 {
45 return (_keyValueMap[key] = object).get();
46 }
47
48 template<typename T>
49 osg::Object* setValue(const osg::Referenced* key, const T& value)
50 {
51 typedef TemplateValueObject<T> UserValueObject;
52 KeyValueMap::iterator itr = _keyValueMap.find(key);
53 if (itr!=_keyValueMap.end())
54 {
55 osg::Object* obj = itr->second.get();
56 if (typeid(*(obj))==typeid(UserValueObject))
57 {
58 UserValueObject* uvo = static_cast<UserValueObject*>(itr->second.get());
59 uvo->setValue(value);
60 return uvo;
61 }
62 }
63
64 return (_keyValueMap[key] = new UserValueObject(value)).get();
65 }
66
67
68 inline osg::Object* getValue(const osg::Referenced* key)
69 {
70 KeyValueMap::iterator itr = _keyValueMap.find(key);
71 return (itr!=_keyValueMap.end()) ? itr->second.get() : 0;
72 }
73
74 inline const osg::Object* getValue(const osg::Referenced* key) const
75 {
76 KeyValueMap::const_iterator itr = _keyValueMap.find(key);
77 return (itr!=_keyValueMap.end()) ? itr->second.get() : 0;
78 }
79
80
81 template<typename T>
82 T* getValueOfType(const osg::Referenced* key)
83 {
84 Object* object = getValue(key);
85 return (object && typeid(*object)==typeid(T)) ? static_cast<T*>(object) : 0;
86 }
87
88
89 template<typename T>
90 const T* getValueOfType(const osg::Referenced* key) const
91 {
92 const Object* object = getValue(key);
93 return (object && typeid(*object)==typeid(T)) ? static_cast<const T*>(object) : 0;
94 }
95
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
113 template<typename T>
114 bool getValue(const osg::Referenced* key, T& value) const
115 {
116 typedef TemplateValueObject<T> UserValueObject;
117 const UserValueObject* uvo = getValueOfType<UserValueObject>(key);
118 if (uvo)
119 {
120 value = uvo->getValue();
121 return true;
122 }
123 else
124 {
125 return false;
126 }
127 }
128
129
130 protected:
131
132 virtual ~ValueMap();
133
134 KeyValueMap _keyValueMap;
135
136};
137
138}
139
140#endif