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.
17#include <osg/ValueObject>
23#define OSG_HAS_VALUEMAP
25class OSG_EXPORT ValueMap : public osg::Object
31 ValueMap(const ValueMap& vm, const osg::CopyOp& copyop=osg::CopyOp::SHALLOW_COPY);
33 META_Object(osg, ValueMap);
35 typedef std::map< osg::ref_ptr<const osg::Referenced>, osg::ref_ptr<osg::Object> > KeyValueMap;
37 void setKeyValueMap(KeyValueMap& properties) { _keyValueMap = properties; }
39 KeyValueMap& getKeyValueMap() { return _keyValueMap; }
41 const KeyValueMap& getKeyValueMap() const { return _keyValueMap; }
43 osg::Object* setValue(const osg::Referenced* key, osg::Object* object)
45 return (_keyValueMap[key] = object).get();
49 osg::Object* setValue(const osg::Referenced* key, const T& value)
51 typedef TemplateValueObject<T> UserValueObject;
52 KeyValueMap::iterator itr = _keyValueMap.find(key);
53 if (itr!=_keyValueMap.end())
55 osg::Object* obj = itr->second.get();
56 if (typeid(*(obj))==typeid(UserValueObject))
58 UserValueObject* uvo = static_cast<UserValueObject*>(itr->second.get());
64 return (_keyValueMap[key] = new UserValueObject(value)).get();
68 inline osg::Object* getValue(const osg::Referenced* key)
70 KeyValueMap::iterator itr = _keyValueMap.find(key);
71 return (itr!=_keyValueMap.end()) ? itr->second.get() : 0;
74 inline const osg::Object* getValue(const osg::Referenced* key) const
76 KeyValueMap::const_iterator itr = _keyValueMap.find(key);
77 return (itr!=_keyValueMap.end()) ? itr->second.get() : 0;
82 T* getValueOfType(const osg::Referenced* key)
84 Object* object = getValue(key);
85 return (object && typeid(*object)==typeid(T)) ? static_cast<T*>(object) : 0;
90 const T* getValueOfType(const osg::Referenced* key) const
92 const Object* object = getValue(key);
93 return (object && typeid(*object)==typeid(T)) ? static_cast<const T*>(object) : 0;
98 bool getValue(const osg::Referenced* key, T& value)
100 typedef TemplateValueObject<T> UserValueObject;
101 UserValueObject* uvo = getValueOfType<UserValueObject>(key);
104 value = uvo->getValue();
114 bool getValue(const osg::Referenced* key, T& value) const
116 typedef TemplateValueObject<T> UserValueObject;
117 const UserValueObject* uvo = getValueOfType<UserValueObject>(key);
120 value = uvo->getValue();
134 KeyValueMap _keyValueMap;