OpenSceneGraph/src/osgIntrospection/Value.cpp
Robert Osfield c80313ccd0 From David Callu,
"
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.
"
2007-06-30 14:21:34 +00:00

231 lines
6.4 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/Value>
#include <osgIntrospection/Type>
#include <osgIntrospection/Exceptions>
#include <osgIntrospection/ReaderWriter>
#include <osgIntrospection/Comparator>
#include <osgIntrospection/Converter>
#include <osgIntrospection/Reflection>
#include <sstream>
#include <memory>
using namespace osgIntrospection;
Value Value::convertTo(const Type& outtype) const
{
Value v = tryConvertTo(outtype);
if (v.isEmpty())
throw TypeConversionException(_type->getExtendedTypeInfo(), outtype.getExtendedTypeInfo());
return v;
}
Value Value::tryConvertTo(const Type& outtype) const
{
check_empty();
if (_type == &outtype)
return *this;
if (_type->isConstPointer() && outtype.isNonConstPointer())
return Value();
// search custom converters
ConverterList conv;
if (Reflection::getConversionPath(*_type, outtype, conv))
{
std::auto_ptr<CompositeConverter> cvt(new CompositeConverter(conv));
return cvt->convert(*this);
}
std::auto_ptr<ReaderWriter::Options> wopt;
if (_type->isEnum() && (outtype.getQualifiedName() == "int" || outtype.getQualifiedName() == "unsigned int"))
{
wopt.reset(new ReaderWriter::Options);
wopt->setForceNumericOutput(true);
}
// ** never converte a pointer to another pointer with the ReaderWriter method
// ** in this case, the ReaderWriter method always work and
// ** using a pointer with a bad type cause a SEGFAULT
if (_type->isPointer() && outtype.isPointer()) return Value();
const ReaderWriter* src_rw = _type->getReaderWriter();
if (src_rw)
{
const ReaderWriter* dst_rw = outtype.getReaderWriter();
if (dst_rw)
{
std::stringstream ss;
if (src_rw->writeTextValue(ss, *this, wopt.get()))
{
Value v;
if (dst_rw->readTextValue(ss, v))
{
return v;
}
}
}
}
return Value();
}
std::string Value::toString() const
{
check_empty();
const ReaderWriter* rw = _type->getReaderWriter();
if (rw)
{
std::ostringstream oss;
if (!rw->writeTextValue(oss, *this))
throw StreamWriteErrorException();
return oss.str();
}
throw StreamingNotSupportedException(StreamingNotSupportedException::ANY, _type->getExtendedTypeInfo());
}
std::wstring Value::toWString() const
{
check_empty();
const ReaderWriter* rw = _type->getReaderWriter();
if (rw)
{
std::wostringstream woss;
if (!rw->writeTextValue(woss, *this))
throw StreamWriteErrorException();
return woss.str();
}
throw StreamingNotSupportedException(StreamingNotSupportedException::ANY, _type->getExtendedTypeInfo());
}
void Value::check_empty() const
{
if (!_type || !_inbox)
throw EmptyValueException();
}
void Value::swap(Value& v)
{
std::swap(_inbox, v._inbox);
std::swap(_type, v._type);
std::swap(_ptype, v._ptype);
}
bool Value::operator ==(const Value& other) const
{
if (isEmpty() && other.isEmpty())
return true;
if (isEmpty() ^ other.isEmpty())
return false;
const Comparator* cmp1 = _type->getComparator();
const Comparator* cmp2 = other._type->getComparator();
const Comparator* cmp = cmp1? cmp1: cmp2;
if (!cmp)
throw ComparisonNotPermittedException(_type->getExtendedTypeInfo());
if (cmp1 == cmp2)
return cmp->isEqualTo(*this, other);
if (cmp1)
return cmp1->isEqualTo(*this, other.convertTo(*_type));
return cmp2->isEqualTo(convertTo(*other._type), other);
}
bool Value::operator <=(const Value& other) const
{
const Comparator* cmp1 = _type->getComparator();
const Comparator* cmp2 = other._type->getComparator();
const Comparator* cmp = cmp1? cmp1: cmp2;
if (!cmp)
throw ComparisonNotPermittedException(_type->getExtendedTypeInfo());
if (cmp1 == cmp2)
return cmp->isLessThanOrEqualTo(*this, other);
if (cmp1)
return cmp1->isLessThanOrEqualTo(*this, other.convertTo(*_type));
return cmp2->isLessThanOrEqualTo(convertTo(*other._type), other);
}
bool Value::operator !=(const Value& other) const
{
return !operator==(other);
}
bool Value::operator >(const Value& other) const
{
return !operator<=(other);
}
bool Value::operator <(const Value& other) const
{
const Comparator* cmp1 = _type->getComparator();
const Comparator* cmp2 = other._type->getComparator();
const Comparator* cmp = cmp1? cmp1: cmp2;
if (!cmp)
throw ComparisonNotPermittedException(_type->getExtendedTypeInfo());
if (cmp1 == cmp2)
return cmp->isLessThanOrEqualTo(*this, other) && !cmp->isEqualTo(*this, other);
if (cmp1)
{
Value temp(other.convertTo(*_type));
return cmp1->isLessThanOrEqualTo(*this, temp) && !cmp1->isEqualTo(*this, temp);
}
Value temp(convertTo(*other._type));
return cmp2->isLessThanOrEqualTo(temp, other) && !cmp2->isEqualTo(temp, other);
}
bool Value::operator >=(const Value& other) const
{
const Comparator* cmp1 = _type->getComparator();
const Comparator* cmp2 = other._type->getComparator();
const Comparator* cmp = cmp1? cmp1: cmp2;
if (!cmp)
throw ComparisonNotPermittedException(_type->getExtendedTypeInfo());
if (cmp1 == cmp2)
return !cmp->isLessThanOrEqualTo(*this, other) || cmp->isEqualTo(*this, other);
if (cmp1)
{
Value temp(other.convertTo(*_type));
return !cmp1->isLessThanOrEqualTo(*this, temp) || cmp1->isEqualTo(*this, temp);
}
Value temp(convertTo(*other._type));
return !cmp2->isLessThanOrEqualTo(temp, other) || cmp2->isEqualTo(temp, other);
}