"
Found in the join file the fix for the bug found by Rafa.
Problem :
osgIntrospection::Value grp(new osg::Group);
osgIntrospection::ValueList vlcall;
vlcall.push_back(osgIntrospection::Value("toto"));
const osgIntrospection::MethodInfo *m =
grp->getType.getCompatibleMethod("setName", vlcall, true);
if (m)
{
m->invoke(grp, vlcall); // ** SEGFAULT here
}
Algorithm explanation :
The "invoke" method try to convert "grp", which reflect an
"osg::Group*", in a
"osgIntrospection::Value", which reflect a "osg::Node*".
This because
the "setName(const char *)" method found by
"grp->getType.getCompatibleMethod"
is an "osg::Object" type method.
When osgIntrospection do this conversion it try :
- to found a "osgIntrospection::Converter" to convert
from "osg::Group*" to "osg::Node*"
- to found a chain of "osgIntrospection::Converter" to convert
from "osg::Group*" to "one or many type" to "osg::Node*"
- to converte an Enum to int or unsigned int
- to convert the value in its "value string representation",
then converte this string in the destination value
Else it throw a "TypeConversionException".
Bug :
1)
When osgIntrospection try to found a chain of
"osgIntrospection::Converter"
It could do any downcast or (Type to SuperType) or upcast
(SuperType to Type).
This mean the the chain could be :
osg::Group to osg::Transform to osg::Camera to
osg::CullSettings to osg::CullStack to
osg::CollectOccludersVisitor to
osg::NodeVisitor to osg::Referenced to osg::Object
During the convertion with this chain, A METTRE failed and
the pointer in
"grp" is set NULL. But the "grp" is always a valid
"osgIntrospection::Value"
and so, osgIntrospection accept the conversion. Then it try
to use this pointer
to call the "setName" function. And Bing SEGFAULT.
2)
In "bool Reflection::accum_conv_path( ... )"
the convection path isn't accumulate in the recursive loop.
this cause multi request of a conversion path, and a
slowdown in the
conversion algorithm.
3)
Use of the last conversion way in a conversion from
pointer to pointer
this mean you can do this :
"osg::Node*" to " value string representation" to "osg::Material*"
What a bad thing !!!
Solution :
1)
Introduce the concept of dynamic_cast and static_cast.
now, to do a conversion, osgIntrospection does this :
- to found a "osgIntrospection::Converter" to convert
from "osg::Group*" to "osg::Node*"
- to found a chain of "osgIntrospection::Converter" to convert
from "osg::Group*" to "one or many type" to "osg::Node*"
only with static_cast, downcast (Type to SuperType)
- to found, if the source and the destination are two pointer,
a chain of "osgIntrospection::Converter" to convert
from "osg::Group*" to "one or many type" to "osg::Node*"
only with dynamic_cast, upcast (SuperType to Type)
- to convert an Enum to int or to unsigned int
- to convert the value in its "value string representation",
then convert this string in the destination value
Else it throw a "TypeConversionException".
Add the "enum CastType" to distinguish the static_cast or
dynamic_cast converter.
Add file OpenSceneGraph/include/osgIntrospection/CastType
2)
add a line to accumulate converter in converter Path.
3)
add a line to check if source and destination are pointer.
"
199 lines
5.7 KiB
C++
199 lines
5.7 KiB
C++
/* -*-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.
|
|
*/
|
|
//osgIntrospection - Copyright (C) 2005 Marco Jez
|
|
|
|
#include <osgIntrospection/Reflection>
|
|
#include <osgIntrospection/Exceptions>
|
|
#include <osgIntrospection/Type>
|
|
#include <osgIntrospection/Converter>
|
|
|
|
#include <OpenThreads/Mutex>
|
|
#include <OpenThreads/ScopedLock>
|
|
|
|
#include <memory>
|
|
|
|
using namespace osgIntrospection;
|
|
|
|
Reflection::StaticData* Reflection::_static_data = 0;
|
|
|
|
Reflection::StaticData::~StaticData()
|
|
{
|
|
for (TypeMap::iterator i=typemap.begin(); i!=typemap.end(); ++i)
|
|
delete i->second;
|
|
|
|
for (ConverterMapMap::iterator i=convmap.begin(); i!=convmap.end(); ++i)
|
|
{
|
|
for (ConverterMap::iterator j=i->second.begin(); j!=i->second.end(); ++j)
|
|
{
|
|
delete j->second;
|
|
}
|
|
}
|
|
}
|
|
|
|
const TypeMap& Reflection::getTypes()
|
|
{
|
|
return getOrCreateStaticData().typemap;
|
|
}
|
|
|
|
Reflection::StaticData& Reflection::getOrCreateStaticData()
|
|
{
|
|
static OpenThreads::Mutex access_mtx;
|
|
OpenThreads::ScopedLock<OpenThreads::Mutex> lock(access_mtx);
|
|
|
|
if (!_static_data)
|
|
{
|
|
_static_data = new StaticData;
|
|
std::auto_ptr<Type> tvoid(new Type(extended_typeid<void>()));
|
|
_static_data->typemap.insert(std::make_pair(extended_typeid<void>(), tvoid.get()));
|
|
_static_data->type_void = tvoid.release();
|
|
}
|
|
return *_static_data;
|
|
}
|
|
|
|
const Type& Reflection::getType(const ExtendedTypeInfo &ti)
|
|
{
|
|
const TypeMap& types = getTypes();
|
|
|
|
TypeMap::const_iterator i = types.find(ti);
|
|
if (i == types.end())
|
|
{
|
|
return *registerType(ti);
|
|
}
|
|
return *i->second;
|
|
}
|
|
|
|
const Type& Reflection::getType(const std::string& qname)
|
|
{
|
|
const TypeMap& types = getTypes();
|
|
|
|
for (TypeMap::const_iterator i=types.begin(); i!=types.end(); ++i)
|
|
{
|
|
if (i->second->isDefined() && i->second->getQualifiedName().compare(qname) == 0)
|
|
return *i->second;
|
|
for (int j=0; j<i->second->getNumAliases(); ++j)
|
|
if (i->second->getAlias(j).compare(qname) == 0)
|
|
return *i->second;
|
|
}
|
|
|
|
throw TypeNotFoundException(qname);
|
|
}
|
|
|
|
const Type& Reflection::type_void()
|
|
{
|
|
return *getOrCreateStaticData().type_void;
|
|
}
|
|
|
|
Type* Reflection::registerType(const ExtendedTypeInfo &ti)
|
|
{
|
|
std::auto_ptr<Type> type(new Type(ti));
|
|
getOrCreateStaticData().typemap.insert(std::make_pair(ti, type.get()));
|
|
return type.release();
|
|
}
|
|
|
|
Type* Reflection::getOrRegisterType(const ExtendedTypeInfo &ti, bool replace_if_defined)
|
|
{
|
|
TypeMap& tm = getOrCreateStaticData().typemap;
|
|
TypeMap::iterator i = tm.find(ti);
|
|
|
|
if (i != tm.end())
|
|
{
|
|
if (replace_if_defined && i->second->isDefined())
|
|
{
|
|
std::string old_name = i->second->getName();
|
|
std::string old_namespace = i->second->getNamespace();
|
|
std::vector<std::string> old_aliases = i->second->_aliases;
|
|
|
|
Type* newtype = new (i->second) Type(ti);
|
|
newtype->_name = old_name;
|
|
newtype->_namespace = old_namespace;
|
|
newtype->_aliases.swap(old_aliases);
|
|
|
|
return newtype;
|
|
}
|
|
return i->second;
|
|
}
|
|
|
|
return registerType(ti);
|
|
}
|
|
|
|
void Reflection::registerConverter(const Type& source, const Type& dest, const Converter* cvt)
|
|
{
|
|
getOrCreateStaticData().convmap[&source][&dest] = cvt;
|
|
}
|
|
|
|
const Converter* Reflection::getConverter(const Type& source, const Type& dest)
|
|
{
|
|
return getOrCreateStaticData().convmap[&source][&dest];
|
|
}
|
|
|
|
bool Reflection::getConversionPath(const Type& source, const Type& dest, ConverterList& conv)
|
|
{
|
|
ConverterList temp;
|
|
std::vector<const Type* > chain;
|
|
|
|
if (accum_conv_path(source, dest, temp, chain, STATIC_CAST))
|
|
{
|
|
conv.swap(temp);
|
|
return true;
|
|
}
|
|
|
|
if (source.isPointer() && dest.isPointer())
|
|
{
|
|
chain.clear();
|
|
temp.clear();
|
|
if (accum_conv_path(source, dest, temp, chain, DYNAMIC_CAST))
|
|
{
|
|
conv.swap(temp);
|
|
return true;
|
|
}
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
bool Reflection::accum_conv_path(const Type& source, const Type& dest, ConverterList& conv, std::vector<const Type* > &chain, CastType castType)
|
|
{
|
|
// break unwanted loops
|
|
if (std::find(chain.begin(), chain.end(), &source) != chain.end())
|
|
return false;
|
|
|
|
// store the type being processed to avoid loops
|
|
chain.push_back(&source);
|
|
|
|
// search a converter from "source"
|
|
StaticData::ConverterMapMap::const_iterator i = getOrCreateStaticData().convmap.find(&source);
|
|
if (i == getOrCreateStaticData().convmap.end())
|
|
return false;
|
|
|
|
// search a converter to "dest"
|
|
const StaticData::ConverterMap& cmap = i->second;
|
|
StaticData::ConverterMap::const_iterator j = cmap.find(&dest);
|
|
if (j != cmap.end() && (j->second->getCastType() == castType))
|
|
{
|
|
conv.push_back(j->second);
|
|
return true;
|
|
}
|
|
|
|
// search a undirect converter from "source" to ... to "dest"
|
|
for (j=cmap.begin(); j!=cmap.end(); ++j)
|
|
{
|
|
if ((j->second->getCastType() == castType) && accum_conv_path(*j->first, dest, conv, chain, castType))
|
|
{
|
|
conv.push_front(j->second);
|
|
return true;
|
|
}
|
|
}
|
|
|
|
return false;
|
|
}
|