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:
Robert Osfield
2016-02-04 13:04:40 +00:00
parent 90a0170d47
commit 041ab29d1f
6 changed files with 562 additions and 0 deletions

123
src/osg/Identifier.cpp Normal file
View 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
View 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
View 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()
{
}