Added osg::Identifer, osg::ValueMap and osg::ValueStack classes to provide a general purpose means for storing and retrieving values from map or stack containers.
Typical use will be for storing and passing values between nodes during traversals.
This commit is contained in:
parent
90a0170d47
commit
041ab29d1f
68
include/osg/Identifier
Normal file
68
include/osg/Identifier
Normal file
@ -0,0 +1,68 @@
|
||||
/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2014 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_IDENTIFER_H
|
||||
#define OSG_IDENTIFER_H
|
||||
|
||||
#include <osg/Referenced>
|
||||
#include <osg/Observer>
|
||||
|
||||
#include <string>
|
||||
#include <cctype>
|
||||
|
||||
#define OSG_HAS_IDENTIFIER
|
||||
|
||||
namespace osg
|
||||
{
|
||||
|
||||
/** helper function for doing a case insenstive compare of two strings.*/
|
||||
inline bool iequals(const std::string& lhs, const std::string& rhs)
|
||||
{
|
||||
if (lhs.size()!=rhs.size()) return false;
|
||||
|
||||
for(std::string::size_type i=0; i<lhs.size(); ++i)
|
||||
{
|
||||
if (std::tolower(lhs[i])!=std::tolower(rhs[i])) return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
/** Unique Indentifier class to help with efficiently comparing
|
||||
* road classification or region via pointers.*/
|
||||
class Identifier : public osg::Referenced, public osg::Observer
|
||||
{
|
||||
public:
|
||||
static Identifier* get(const std::string& name, int number=0, osg::Referenced* first=0, osg::Referenced* second=0);
|
||||
static Identifier* get(int number, osg::Referenced* first=0, osg::Referenced* second=0);
|
||||
static Identifier* get(osg::Referenced* first, osg::Referenced* second=0);
|
||||
|
||||
const std::string& name() const { return _name; }
|
||||
const int& number() const { return _number; }
|
||||
|
||||
protected:
|
||||
Identifier(const std::string& name, int number, osg::Referenced* f, osg::Referenced* s);
|
||||
virtual ~Identifier();
|
||||
|
||||
virtual void objectDeleted(void* ptr);
|
||||
|
||||
std::string _name;
|
||||
int _number;
|
||||
osg::Referenced* _first;
|
||||
osg::Referenced* _second;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
#endif
|
138
include/osg/ValueMap
Normal file
138
include/osg/ValueMap
Normal file
@ -0,0 +1,138 @@
|
||||
/* -*-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() && typeid(*(itr->second))==typeid(UserValueObject))
|
||||
{
|
||||
UserValueObject* uvo = static_cast<UserValueObject*>(itr->second.get());
|
||||
uvo->setValue(value);
|
||||
return uvo;
|
||||
}
|
||||
else
|
||||
{
|
||||
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
|
167
include/osg/ValueStack
Normal file
167
include/osg/ValueStack
Normal file
@ -0,0 +1,167 @@
|
||||
/* -*-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_VALUESTACK
|
||||
#define OSG_VALUESTACK 1
|
||||
|
||||
#include <osg/ValueMap>
|
||||
|
||||
namespace osg {
|
||||
|
||||
#define OSG_HAS_VALUESTACK
|
||||
|
||||
class OSG_EXPORT ValueStack : public osg::Object
|
||||
{
|
||||
public:
|
||||
|
||||
ValueStack();
|
||||
|
||||
ValueStack(const ValueStack& ps, const osg::CopyOp& copyop=osg::CopyOp::SHALLOW_COPY);
|
||||
|
||||
META_Object(osg, ValueStack)
|
||||
|
||||
typedef std::vector< osg::ref_ptr<Object> > Values;
|
||||
typedef std::map< osg::ref_ptr<const osg::Referenced>, Values> ValueStackMap;
|
||||
|
||||
void setValueStackMap(ValueStackMap& pm) { _valuesMap = pm; }
|
||||
|
||||
ValueStackMap& getValueStackMap() { return _valuesMap; }
|
||||
|
||||
const ValueStackMap& getValueStackMap() const { return _valuesMap; }
|
||||
|
||||
inline void push(const Referenced* key, Object* value)
|
||||
{
|
||||
_valuesMap[key].push_back(value);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
void push(const osg::Referenced* key, const T& value)
|
||||
{
|
||||
typedef TemplateValueObject<T> UserValueObject;
|
||||
_valuesMap[key].push_back(new UserValueObject(value));
|
||||
}
|
||||
|
||||
inline void pop(const Referenced* key)
|
||||
{
|
||||
_valuesMap[key].pop_back();
|
||||
}
|
||||
|
||||
inline void push(ValueMap* valueMap)
|
||||
{
|
||||
if (valueMap)
|
||||
{
|
||||
ValueMap::KeyValueMap& keyValueMap = valueMap->getKeyValueMap();
|
||||
for(ValueMap::KeyValueMap::iterator itr = keyValueMap.begin();
|
||||
itr != keyValueMap.end();
|
||||
++itr)
|
||||
{
|
||||
push(itr->first.get(), itr->second.get());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
inline void pop(ValueMap* valueMap)
|
||||
{
|
||||
if (valueMap)
|
||||
{
|
||||
ValueMap::KeyValueMap& keyValueMap = valueMap->getKeyValueMap();
|
||||
for(ValueMap::KeyValueMap::iterator itr = keyValueMap.begin();
|
||||
itr != keyValueMap.end();
|
||||
++itr)
|
||||
{
|
||||
pop(itr->first.get());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
inline osg::Object* getValue(const osg::Referenced* key)
|
||||
{
|
||||
ValueStackMap::iterator itr = _valuesMap.find(key);
|
||||
if (itr==_valuesMap.end()) return 0;
|
||||
|
||||
Values& values = itr->second;
|
||||
if (values.empty()) return 0;
|
||||
|
||||
return values.back().get();
|
||||
}
|
||||
|
||||
inline const osg::Object* getValue(const osg::Referenced* key) const
|
||||
{
|
||||
ValueStackMap::const_iterator itr = _valuesMap.find(key);
|
||||
if (itr==_valuesMap.end()) return 0;
|
||||
|
||||
const Values& values = itr->second;
|
||||
if (values.empty()) return 0;
|
||||
|
||||
return values.back().get();
|
||||
}
|
||||
|
||||
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 ~ValueStack();
|
||||
|
||||
ValueStackMap _valuesMap;
|
||||
|
||||
};
|
||||
|
||||
|
||||
}
|
||||
|
||||
#endif
|
123
src/osg/Identifier.cpp
Normal file
123
src/osg/Identifier.cpp
Normal file
@ -0,0 +1,123 @@
|
||||
#include <osg/Identifier>
|
||||
|
||||
#include <osg/ref_ptr>
|
||||
#include <osg/Notify>
|
||||
|
||||
#include <OpenThreads/Mutex>
|
||||
|
||||
#include <map>
|
||||
|
||||
using namespace osg;
|
||||
|
||||
struct IdentifierKey
|
||||
{
|
||||
IdentifierKey(const std::string& str, int num, osg::Referenced* f, osg::Referenced* s):
|
||||
name(str),
|
||||
number(num),
|
||||
first(f),
|
||||
second(s)
|
||||
{
|
||||
}
|
||||
|
||||
const std::string name;
|
||||
const int number;
|
||||
const osg::Referenced* first;
|
||||
const osg::Referenced* second;
|
||||
|
||||
bool operator < (const IdentifierKey& rhs) const
|
||||
{
|
||||
if (name<rhs.name) return true;
|
||||
if (name>rhs.name) return false;
|
||||
|
||||
if (number<rhs.number) return true;
|
||||
if (number>rhs.number) return false;
|
||||
|
||||
if (first<rhs.first) return true;
|
||||
if (first>rhs.first) return false;
|
||||
|
||||
return (second<rhs.second);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
typedef std::map<IdentifierKey, osg::ref_ptr<Identifier> > IdentifierMap;
|
||||
|
||||
IdentifierMap s_IdentifierMap;
|
||||
OpenThreads::Mutex s_IdentifierMapMutex;
|
||||
|
||||
Identifier::Identifier(const std::string& name, int number, osg::Referenced* f, osg::Referenced* s):
|
||||
_name(name),
|
||||
_number(number),
|
||||
_first(f),
|
||||
_second(s)
|
||||
{
|
||||
if (_first) _first->addObserver(this);
|
||||
if (_second) _second->addObserver(this);
|
||||
}
|
||||
|
||||
Identifier::~Identifier()
|
||||
{
|
||||
if (_first) _first->removeObserver(this);
|
||||
if (_second) _second->removeObserver(this);
|
||||
}
|
||||
|
||||
void Identifier::objectDeleted(void* ptr)
|
||||
{
|
||||
if (_first==ptr || _second==ptr)
|
||||
{
|
||||
IdentifierKey key(_name, _number, _first, _second);
|
||||
|
||||
{
|
||||
OpenThreads::ScopedLock<OpenThreads::Mutex> lock(s_IdentifierMapMutex);
|
||||
IdentifierMap::iterator itr = s_IdentifierMap.find(key);
|
||||
if (itr!=s_IdentifierMap.end())
|
||||
{
|
||||
OSG_NOTICE<<"Warning : this = "<<this<<" Identifier::objectDeleted("<<ptr<<") found and deleted from s_IdentifierMap, s_IdentifierMap.size()="<<s_IdentifierMap.size()<<std::endl;
|
||||
s_IdentifierMap.erase(itr);
|
||||
}
|
||||
else
|
||||
{
|
||||
OSG_NOTICE<<"Warning : this = "<<this<<" Identifier::objectDeleted("<<ptr<<") not found in s_IdentifierMap."<<s_IdentifierMap.size()<<std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
if (_first==ptr && _second)
|
||||
{
|
||||
_second->removeObserver(this);
|
||||
}
|
||||
|
||||
if (_second==ptr && _first)
|
||||
{
|
||||
_first->removeObserver(this);
|
||||
}
|
||||
|
||||
_first = 0;
|
||||
_second = 0;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
Identifier* osg::Identifier::get(const std::string& name, int number, osg::Referenced* first, osg::Referenced* second)
|
||||
{
|
||||
IdentifierKey key(name, number, first, second);
|
||||
|
||||
OpenThreads::ScopedLock<OpenThreads::Mutex> lock(s_IdentifierMapMutex);
|
||||
|
||||
IdentifierMap::iterator itr = s_IdentifierMap.find(key);
|
||||
if (itr!=s_IdentifierMap.end()) return itr->second.get();
|
||||
|
||||
osg::ref_ptr<Identifier> identifier = new Identifier(name, number, first, second);
|
||||
s_IdentifierMap[key] = identifier;
|
||||
return identifier.get();
|
||||
}
|
||||
|
||||
Identifier* osg::Identifier::get(int number, osg::Referenced* first, osg::Referenced* second)
|
||||
{
|
||||
return get("", number, first, second);
|
||||
}
|
||||
|
||||
Identifier* osg::Identifier::get(osg::Referenced* first, osg::Referenced* second)
|
||||
{
|
||||
return get("", 0, first, second);
|
||||
}
|
36
src/osg/ValueMap.cpp
Normal file
36
src/osg/ValueMap.cpp
Normal file
@ -0,0 +1,36 @@
|
||||
/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 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.
|
||||
*/
|
||||
#include <osg/ValueStack>
|
||||
|
||||
#include <stdlib.h>
|
||||
|
||||
using namespace osg;
|
||||
|
||||
ValueMap::ValueMap()
|
||||
{
|
||||
}
|
||||
|
||||
ValueMap::ValueMap(const ValueMap& vm, const osg::CopyOp& copyop):
|
||||
osg::Object(vm, copyop)
|
||||
{
|
||||
for(KeyValueMap::const_iterator itr = vm._keyValueMap.begin();
|
||||
itr != vm._keyValueMap.end();
|
||||
++itr)
|
||||
{
|
||||
_keyValueMap[itr->first] = osg::clone(itr->second.get(), copyop);
|
||||
}
|
||||
}
|
||||
|
||||
ValueMap::~ValueMap()
|
||||
{
|
||||
}
|
30
src/osg/ValueStack.cpp
Normal file
30
src/osg/ValueStack.cpp
Normal file
@ -0,0 +1,30 @@
|
||||
/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 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.
|
||||
*/
|
||||
#include <osg/ValueStack>
|
||||
|
||||
#include <stdlib.h>
|
||||
|
||||
using namespace osg;
|
||||
|
||||
ValueStack::ValueStack()
|
||||
{
|
||||
}
|
||||
|
||||
ValueStack::ValueStack(const ValueStack& ps, const osg::CopyOp& copyop):
|
||||
osg::Object(ps, copyop)
|
||||
{
|
||||
}
|
||||
|
||||
ValueStack::~ValueStack()
|
||||
{
|
||||
}
|
Loading…
Reference in New Issue
Block a user