diff --git a/include/osgIntrospection/Converter b/include/osgIntrospection/Converter index 53de2655e..c1d9526fd 100644 --- a/include/osgIntrospection/Converter +++ b/include/osgIntrospection/Converter @@ -16,18 +16,18 @@ #define OSGINTROSPECTION_CONVERTER_ #include -#include +#include namespace osgIntrospection { - struct Converter { + virtual CastType getCastType() const = 0; virtual Value convert(const Value& ) const = 0; virtual ~Converter() {} }; - typedef std::vector ConverterList; + typedef std::list ConverterList; class CompositeConverter: public Converter { @@ -44,6 +44,7 @@ namespace osgIntrospection return accum; } + virtual CastType getCastType() const { return COMPOSITE_CAST; } private: ConverterList cvt_; }; @@ -56,6 +57,8 @@ namespace osgIntrospection { return static_cast(variant_cast(src)); } + + virtual CastType getCastType() const { return STATIC_CAST; } }; template @@ -66,6 +69,8 @@ namespace osgIntrospection { return dynamic_cast(variant_cast(src)); } + + virtual CastType getCastType() const { return DYNAMIC_CAST; } }; template @@ -76,6 +81,8 @@ namespace osgIntrospection { return reinterpret_cast(variant_cast(src)); } + + virtual CastType getCastType() const { return REINTERPRET_CAST; } }; } diff --git a/include/osgIntrospection/Reflection b/include/osgIntrospection/Reflection index c4253a6ea..91a6bf8f7 100644 --- a/include/osgIntrospection/Reflection +++ b/include/osgIntrospection/Reflection @@ -21,6 +21,7 @@ #include #include #include +#include /// This macro emulates the behavior of the standard typeid operator, /// returning the Type object associated to the type of the given @@ -34,12 +35,19 @@ namespace osgIntrospection class Type; struct Converter; - typedef std::vector ConverterList; + typedef std::list ConverterList; /// A map of types, indexed by their associated ExtendedTypeInfo /// structure. typedef std::map TypeMap; + enum CastType + { + STATIC_CAST, + DYNAMIC_CAST, + REINTERPRET_CAST, + COMPOSITE_CAST + }; /// This class provides basic reflection services such as registration /// of new types and queries on the global type map. @@ -94,7 +102,7 @@ namespace osgIntrospection static void registerConverter(const Type& source, const Type& dest, const Converter* cvt); private: - static bool accum_conv_path(const Type& source, const Type& dest, ConverterList& conv, std::vector &chain); + static bool accum_conv_path(const Type& source, const Type& dest, ConverterList& conv, std::vector &chain, CastType castType); static StaticData* _static_data; }; diff --git a/src/osgIntrospection/Reflection.cpp b/src/osgIntrospection/Reflection.cpp index 5cb198ee4..72f752f7f 100644 --- a/src/osgIntrospection/Reflection.cpp +++ b/src/osgIntrospection/Reflection.cpp @@ -1,3 +1,17 @@ +/* -*-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 #include #include @@ -36,12 +50,12 @@ Reflection::StaticData& Reflection::getOrCreateStaticData() static OpenThreads::Mutex access_mtx; OpenThreads::ScopedLock lock(access_mtx); - if (!_static_data) - { + if (!_static_data) + { _static_data = new StaticData; std::auto_ptr tvoid(new Type(extended_typeid())); _static_data->typemap.insert(std::make_pair(extended_typeid(), tvoid.get())); - _static_data->type_void = tvoid.release(); + _static_data->type_void = tvoid.release(); } return *_static_data; } @@ -126,15 +140,28 @@ bool Reflection::getConversionPath(const Type& source, const Type& dest, Convert { ConverterList temp; std::vector chain; - if (accum_conv_path(source, dest, temp, 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 &chain) +bool Reflection::accum_conv_path(const Type& source, const Type& dest, ConverterList& conv, std::vector &chain, CastType castType) { // break unwanted loops if (std::find(chain.begin(), chain.end(), &source) != chain.end()) @@ -143,22 +170,28 @@ bool Reflection::accum_conv_path(const Type& source, const Type& dest, Converter // 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()) + 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()) + 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 (accum_conv_path(*j->first, dest, conv, chain)) + if ((j->second->getCastType() == castType) && accum_conv_path(*j->first, dest, conv, chain, castType)) + { + conv.push_front(j->second); return true; + } } return false; diff --git a/src/osgIntrospection/Reflector.cpp b/src/osgIntrospection/Reflector.cpp index a133d9423..7dbfbbb06 100644 --- a/src/osgIntrospection/Reflector.cpp +++ b/src/osgIntrospection/Reflector.cpp @@ -1,3 +1,16 @@ +/* -*-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 namespace osgIntrospection diff --git a/src/osgIntrospection/Value.cpp b/src/osgIntrospection/Value.cpp index 2f0f289fd..6305163aa 100644 --- a/src/osgIntrospection/Value.cpp +++ b/src/osgIntrospection/Value.cpp @@ -59,6 +59,12 @@ Value Value::tryConvertTo(const Type& outtype) const 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) {