Hi jose


2006/10/17, Jose Luis Hidalgo <[EMAIL PROTECTED]>:
The problem I have here is   that the viewer is not a referenced
object and the variant cast returns it as a valid pointer:
vec3 = 0
viewer(variant_cast) = 0xbfb3aba4
viewer(dynamic_cast) = 0
group (variant_cast)= 0x82149b8
group (dynamic_cast) = 0x82149b8

the viewer gets converted to a Referenced through variant_cast... and
is not correct.


Effectively, they are a dangerous problem!!!

So first, why osgIntrospection do this? :
Use your example to answer:


osgProducer::Viewer viewer
osgIntrospection::Value val(&viewer);
 ref = variant_cast<osg::Referenced*>(val);


this call the following function

    osg::Referenced* variant_cast(const Value& v)
    {
        // This dynamic_cast return not NULL pointer only if type of v._inbox->inst_ is a Value::Instance<osg::Referenced*>
        Value::Instance<osg::Referenced*> *i = dynamic_cast<Value::Instance<osg::Referenced*> *>(v._inbox->inst_);
        if (i) return i->_data;

        // This dynamic_cast return not NULL pointer only if type of v._inbox->inst_ is a Value::Instance<osg::Referenced &>
        i = dynamic_cast<Value::Instance<osg::Referenced*> *>(v._inbox->_ref_inst);
        if (i) return i->_data;

        // This dynamic_cast return not NULL pointer only if type of v._inbox->inst_ is a Value::Instance<const osg::Referenced &>
        i = dynamic_cast<Value::Instance<osg::Referenced*> *>(v._inbox->_const_ref_inst);
        if (i) return i->_data;

// ** all this call failed and we must convert the Type of the variable in Value in the request Type e.g. osg::Referenced*

        // try to convert v in type of osg::Referenced*
        return variant_cast<osg::Referenced*>( v.convertTo(typeof(osg::Referenced*)));
    }


  v.convertTo(typeof(osg::Referenced*)) call the following function

Value Value::convertTo(const Type& outtype) const
{
    Value v = tryConvertTo(outtype);
    if (v.isEmpty())
        throw TypeConversionException(_type->getStdTypeInfo(), outtype.getStdTypeInfo());
    return v;
}

which call the following function

Value Value::tryConvertTo(const Type& outtype) const
{

// ** all this is not use in own example

    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);
    }

// ** ok we use the rest of the function
// ** so here we try to change the osgProducer::Viewer pointer in string representation
// ** and change this string representation in osg::Referenced pointer
// ** there are any probleme to do this and this work correctly
// ** but there are any type compatibility test

    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();
}


So the variant_cast is more a static_cast than a dynamic_cast
I don't no if this is the good behavious of variant_cast.
The fact is we can determine if a Type is derived from another type so we could check this before to
use a variant_cast. But it's could be practical to have a dynamic_variant_cast and a static_variant_cast or something like this

I would start an evolution of osgIntrospection to allow the reflection of global Property and Method and another little thing that don't work very well. This is another thing to push in the osgIntrospection evolution list.

For now, the only thing a can say to you is "check the Type compatibility before use variant_cast"

cheers
David


_______________________________________________
osg-users mailing list
[email protected]
http://openscenegraph.net/mailman/listinfo/osg-users
http://www.openscenegraph.org/

Reply via email to