OpenSceneGraph/include/osgIntrospection/Reflection

117 lines
4.5 KiB
Plaintext
Raw Normal View History

2006-07-18 23:21:48 +08:00
/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield
2005-04-29 18:06:50 +08:00
*
* 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
#ifndef OSGINTROSPECTION_REFLECTION_
#define OSGINTROSPECTION_REFLECTION_
#include <osgIntrospection/Export>
#include <osgIntrospection/ExtendedTypeInfo>
#include <typeinfo>
#include <map>
#include <vector>
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 22:21:34 +08:00
#include <list>
/// This macro emulates the behavior of the standard typeid operator,
/// returning the Type object associated to the type of the given
/// expression.
#define typeof(type) osgIntrospection::Reflection::getType(extended_typeid< type >())
#define typeofvalue(val) osgIntrospection::Reflection::getType(osgIntrospection::ExtendedTypeInfo(typeid(val), false, false))
namespace osgIntrospection
{
2005-03-14 17:28:31 +08:00
class Type;
struct Converter;
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 22:21:34 +08:00
typedef std::list<const Converter* > ConverterList;
2005-03-14 17:28:31 +08:00
/// A map of types, indexed by their associated ExtendedTypeInfo
/// structure.
typedef std::map<ExtendedTypeInfo, Type*> TypeMap;
2005-03-14 17:28:31 +08:00
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 22:21:34 +08:00
enum CastType
{
STATIC_CAST,
DYNAMIC_CAST,
REINTERPRET_CAST,
COMPOSITE_CAST
};
2005-03-14 17:28:31 +08:00
/// This class provides basic reflection services such as registration
/// of new types and queries on the global type map.
class OSGINTROSPECTION_EXPORT Reflection
{
public:
/// Returns the Type object associated to the given
/// ExtendedTypeInfo structure. If the type hasn't been created
/// yet it is automatically created and added to the global type
/// map. Please note that such type will have the status of
2005-03-14 17:28:31 +08:00
/// "declared", you still need to give details about it through
/// a Reflector class before you can query it.
static const Type& getType(const ExtendedTypeInfo &ti);
2005-03-14 17:28:31 +08:00
/// Finds a Type object given its qualified name, which must
/// be identical to the qualified name returned by that Type's
/// getQualifiedName() method. If the type hasn't been created
/// yet, an exception is thrown.
static const Type& getType(const std::string& qname);
2005-03-14 17:28:31 +08:00
/// Returns the global map of types.
static const TypeMap& getTypes();
2005-03-14 17:28:31 +08:00
/// Return the Type object associated to the C++ type 'void'.
/// This is a shortcut for typeof(void), which may be slow if
/// the type map is large.
static const Type& type_void();
static const Converter* getConverter(const Type& source, const Type& dest);
static bool getConversionPath(const Type& source, const Type& dest, ConverterList& conv);
2005-03-14 17:28:31 +08:00
// This function should be called (at least on windows platforms using Visual Studio 7.1 or 8 as compiler) to unregister
// all the known types before exiting your program: otherwise, you will get a lot of false positive memory leaks in debug builds.
// It might also be used to dynamically reload the description of the known types (?)
static void uninitialize();
2005-03-14 17:28:31 +08:00
private:
template<typename C> friend class Reflector;
template<typename C> friend struct TypeNameAliasProxy;
friend struct ConverterProxy;
2005-03-14 17:28:31 +08:00
struct StaticData
{
TypeMap typemap;
const Type* type_void;
typedef std::map<const Type* , const Converter* > ConverterMap;
typedef std::map<const Type* , ConverterMap> ConverterMapMap;
ConverterMapMap convmap;
~StaticData();
2005-03-14 17:28:31 +08:00
};
static StaticData& getOrCreateStaticData();
static Type* registerType(const ExtendedTypeInfo &ti);
static Type* getOrRegisterType(const ExtendedTypeInfo &ti, bool replace_if_defined = false);
static void registerConverter(const Type& source, const Type& dest, const Converter* cvt);
2005-03-14 17:28:31 +08:00
private:
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 22:21:34 +08:00
static bool accum_conv_path(const Type& source, const Type& dest, ConverterList& conv, std::vector<const Type* > &chain, CastType castType);
static StaticData* _static_data;
2005-03-14 17:28:31 +08:00
};
}
#endif