1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
|
/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2016 Robert Osfield
*
* This library is open source and may be redistributed and/or modified under
* the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
* (at your option) any later version. The full license is in LICENSE file
* included with this distribution, and on the openscenegraph.org website.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* OpenSceneGraph Public License for more details.
*/
#ifndef OSG_VALUEMAP
#define OSG_VALUEMAP 1
#include <osg/ValueObject>
#include <osg/Notify>
#include <map>
namespace osg {
#define OSG_HAS_VALUEMAP
class OSG_EXPORT ValueMap : public osg::Object
{
public:
ValueMap();
ValueMap(const ValueMap& vm, const osg::CopyOp& copyop=osg::CopyOp::SHALLOW_COPY);
META_Object(osg, ValueMap);
typedef std::map< osg::ref_ptr<const osg::Referenced>, osg::ref_ptr<osg::Object> > KeyValueMap;
void setKeyValueMap(KeyValueMap& properties) { _keyValueMap = properties; }
KeyValueMap& getKeyValueMap() { return _keyValueMap; }
const KeyValueMap& getKeyValueMap() const { return _keyValueMap; }
osg::Object* setValue(const osg::Referenced* key, osg::Object* object)
{
return (_keyValueMap[key] = object).get();
}
template<typename T>
osg::Object* setValue(const osg::Referenced* key, const T& value)
{
typedef TemplateValueObject<T> UserValueObject;
KeyValueMap::iterator itr = _keyValueMap.find(key);
if (itr!=_keyValueMap.end())
{
osg::Object* obj = itr->second.get();
if (typeid(*(obj))==typeid(UserValueObject))
{
UserValueObject* uvo = static_cast<UserValueObject*>(itr->second.get());
uvo->setValue(value);
return uvo;
}
}
return (_keyValueMap[key] = new UserValueObject(value)).get();
}
inline osg::Object* getValue(const osg::Referenced* key)
{
KeyValueMap::iterator itr = _keyValueMap.find(key);
return (itr!=_keyValueMap.end()) ? itr->second.get() : 0;
}
inline const osg::Object* getValue(const osg::Referenced* key) const
{
KeyValueMap::const_iterator itr = _keyValueMap.find(key);
return (itr!=_keyValueMap.end()) ? itr->second.get() : 0;
}
template<typename T>
T* getValueOfType(const osg::Referenced* key)
{
Object* object = getValue(key);
return (object && typeid(*object)==typeid(T)) ? static_cast<T*>(object) : 0;
}
template<typename T>
const T* getValueOfType(const osg::Referenced* key) const
{
const Object* object = getValue(key);
return (object && typeid(*object)==typeid(T)) ? static_cast<const T*>(object) : 0;
}
template<typename T>
bool getValue(const osg::Referenced* key, T& value)
{
typedef TemplateValueObject<T> UserValueObject;
UserValueObject* uvo = getValueOfType<UserValueObject>(key);
if (uvo)
{
value = uvo->getValue();
return true;
}
else
{
return false;
}
}
template<typename T>
bool getValue(const osg::Referenced* key, T& value) const
{
typedef TemplateValueObject<T> UserValueObject;
const UserValueObject* uvo = getValueOfType<UserValueObject>(key);
if (uvo)
{
value = uvo->getValue();
return true;
}
else
{
return false;
}
}
protected:
virtual ~ValueMap();
KeyValueMap _keyValueMap;
};
}
#endif
|