OpenSceneGraph 3.6.5
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
26{
27 public:
28
30
32
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
40
41 const KeyValueMap& getKeyValueMap() const { return _keyValueMap; }
42
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
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>
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
135
136};
137
138}
139
140#endif
The core osg library provides the basic scene graph classes such as Nodes, State and Drawables,...
Definition AlphaFunc:19
Copy Op(erator) used to control whether shallow or deep copy is used during copy construction and clo...
Definition CopyOp:41
@ SHALLOW_COPY
Definition CopyOp:47
Base class/standard interface for objects which require IO support, cloning and reference counting.
Definition Object:61
Object()
Construct an object.
Definition Object:69
Smart pointer for handling referenced counted objects.
Definition ref_ptr:32
Base class for providing reference counted objects.
Definition Referenced:44
ValueMap(const ValueMap &vm, const osg::CopyOp &copyop=osg::CopyOp::SHALLOW_COPY)
void setKeyValueMap(KeyValueMap &properties)
Definition ValueMap:37
KeyValueMap & getKeyValueMap()
Definition ValueMap:39
bool getValue(const osg::Referenced *key, T &value)
Definition ValueMap:98
META_Object(osg, ValueMap)
T * getValueOfType(const osg::Referenced *key)
Definition ValueMap:82
const KeyValueMap & getKeyValueMap() const
Definition ValueMap:41
KeyValueMap _keyValueMap
Definition ValueMap:134
std::map< osg::ref_ptr< const osg::Referenced >, osg::ref_ptr< osg::Object > > KeyValueMap
Definition ValueMap:35
virtual ~ValueMap()
osg::Object * getValue(const osg::Referenced *key)
Definition ValueMap:68
osg::Object * setValue(const osg::Referenced *key, osg::Object *object)
Definition ValueMap:43
bool getValue(const osg::Referenced *key, T &value) const
Definition ValueMap:114
const osg::Object * getValue(const osg::Referenced *key) const
Definition ValueMap:74
const T * getValueOfType(const osg::Referenced *key) const
Definition ValueMap:90
osg::Object * setValue(const osg::Referenced *key, const T &value)
Definition ValueMap:49
Definition ValueObject:283
#define OSG_EXPORT
Definition Export:39

osg logo
Generated at Wed Jul 23 2025 00:00:00 for the OpenSceneGraph by doxygen 1.14.0.