From Marco Jez, updates to osgIntrospection.

This commit is contained in:
Robert Osfield
2005-04-04 13:50:07 +00:00
parent 21a69b5317
commit 5f75f765f0
30 changed files with 7591 additions and 3213 deletions

View File

@@ -95,6 +95,10 @@ LINK32=link.exe
# PROP Default_Filter "cpp;c;cxx;rc;def;r;odl;idl;hpj;bat"
# Begin Source File
SOURCE=..\..\src\osgIntrospection\ConstructorInfo.cpp
# End Source File
# Begin Source File
SOURCE=..\..\src\osgIntrospection\CustomAttributeProvider.cpp
# End Source File
# Begin Source File
@@ -119,6 +123,10 @@ SOURCE=..\..\src\osgIntrospection\Type.cpp
# End Source File
# Begin Source File
SOURCE=..\..\src\osgIntrospection\Utility.cpp
# End Source File
# Begin Source File
SOURCE=..\..\src\osgIntrospection\Value.cpp
# End Source File
# End Group
@@ -131,6 +139,14 @@ SOURCE=..\..\include\osgIntrospection\Attributes
# End Source File
# Begin Source File
SOURCE=..\..\include\osgIntrospection\Comparator
# End Source File
# Begin Source File
SOURCE=..\..\include\osgIntrospection\ConstructorInfo
# End Source File
# Begin Source File
SOURCE=..\..\include\osgIntrospection\CustomAttribute
# End Source File
# Begin Source File
@@ -143,6 +159,14 @@ SOURCE=..\..\include\osgIntrospection\Exceptions
# End Source File
# Begin Source File
SOURCE=..\..\include\osgIntrospection\Export
# End Source File
# Begin Source File
SOURCE=..\..\include\osgIntrospection\InstanceCreator
# End Source File
# Begin Source File
SOURCE=..\..\include\osgIntrospection\MethodInfo
# End Source File
# Begin Source File
@@ -171,14 +195,30 @@ SOURCE=..\..\include\osgIntrospection\Reflector
# End Source File
# Begin Source File
SOURCE=..\..\include\osgIntrospection\StaticMethodInfo
# End Source File
# Begin Source File
SOURCE=..\..\include\osgIntrospection\Type
# End Source File
# Begin Source File
SOURCE=..\..\include\osgIntrospection\TypedConstructorInfo
# End Source File
# Begin Source File
SOURCE=..\..\include\osgIntrospection\TypedMethodInfo
# End Source File
# Begin Source File
SOURCE=..\..\include\osgIntrospection\TypeNameAliasProxy
# End Source File
# Begin Source File
SOURCE=..\..\include\osgIntrospection\Utility
# End Source File
# Begin Source File
SOURCE=..\..\include\osgIntrospection\Value
# End Source File
# Begin Source File
@@ -192,3 +232,4 @@ SOURCE=..\..\include\osgIntrospection\variant_cast
# End Group
# End Target
# End Project

View File

@@ -4,6 +4,7 @@
#include <osgIntrospection/CustomAttribute>
#include <osgIntrospection/Value>
#include <osgIntrospection/Exceptions>
#include <osgIntrospection/Type>
namespace osgIntrospection
{
@@ -31,9 +32,12 @@ namespace osgIntrospection
/// last one is used with array properties.
struct PropertyGetter
{
virtual Value get(const Value &instance, const ValueList &indices) const { throw PropertyAccessException("[n/a inside a custom accessor]", PropertyAccessException::IGET); }
virtual Value get(const Value &instance) const { throw PropertyAccessException("[n/a inside a custom accessor]", PropertyAccessException::GET); }
virtual Value get(const Value &instance, int i) const { throw PropertyAccessException("[n/a inside a custom accessor]", PropertyAccessException::AGET); }
virtual Value get(Value & /*instance*/, const ValueList & /*indices*/) const { throw PropertyAccessException("[n/a inside a custom accessor]", PropertyAccessException::IGET); }
virtual Value get(Value & /*instance*/) const { throw PropertyAccessException("[n/a inside a custom accessor]", PropertyAccessException::GET); }
virtual Value get(Value & /*instance*/, int /*i*/) const { throw PropertyAccessException("[n/a inside a custom accessor]", PropertyAccessException::AGET); }
virtual Value get(const Value & /*instance*/, const ValueList & /*indices*/) const { throw PropertyAccessException("[n/a inside a custom accessor]", PropertyAccessException::IGET); }
virtual Value get(const Value & /*instance*/) const { throw PropertyAccessException("[n/a inside a custom accessor]", PropertyAccessException::GET); }
virtual Value get(const Value & /*instance*/, int /*i*/) const { throw PropertyAccessException("[n/a inside a custom accessor]", PropertyAccessException::AGET); }
virtual ~PropertyGetter() {}
};
@@ -64,9 +68,9 @@ namespace osgIntrospection
/// last one is used with array properties.
struct PropertySetter
{
virtual void set(Value &instance, ValueList &indices, const Value &value) const { throw PropertyAccessException("[n/a inside a custom accessor]", PropertyAccessException::ISET); }
virtual void set(Value &instance, const Value &value) const { throw PropertyAccessException("[n/a inside a custom accessor]", PropertyAccessException::SET); }
virtual void set(Value &instance, int i, const Value &value) const { throw PropertyAccessException("[n/a inside a custom accessor]", PropertyAccessException::ASET); }
virtual void set(Value & /*instance*/, ValueList & /*indices*/, const Value & /*value*/) const { throw PropertyAccessException("[n/a inside a custom accessor]", PropertyAccessException::ISET); }
virtual void set(Value & /*instance*/, const Value & /*value*/) const { throw PropertyAccessException("[n/a inside a custom accessor]", PropertyAccessException::SET); }
virtual void set(Value & /*instance*/, int /*i*/, const Value & /*value*/) const { throw PropertyAccessException("[n/a inside a custom accessor]", PropertyAccessException::ASET); }
virtual ~PropertySetter() {}
};
@@ -95,7 +99,7 @@ namespace osgIntrospection
/// in a chosen array property for the given instance.
struct PropertyCounter
{
virtual int count(const Value &instance) const { throw PropertyAccessException("[n/a inside a custom accessor]", PropertyAccessException::COUNT); }
virtual int count(const Value & /*instance*/) const { throw PropertyAccessException("[n/a inside a custom accessor]", PropertyAccessException::COUNT); }
virtual ~PropertyCounter() {}
};
@@ -145,6 +149,35 @@ namespace osgIntrospection
private:
const PropertyAdder *adder_;
};
/// Base struct for custom array property removers. Descendants should
/// override the remove() method whose purpose is to remove an item from
/// an array property.
struct PropertyRemover
{
virtual void remove(Value&, int) const { throw PropertyAccessException("[n/a inside a custom accessor]", PropertyAccessException::REMOVE); }
virtual ~PropertyRemover() {}
};
/// By setting an attribute of this class you can specify a custom object
/// that will be used to remove an item from an array property.
class CustomPropertyRemoveAttribute: public CustomAttribute
{
public:
CustomPropertyRemoveAttribute(const PropertyRemover *remover)
: CustomAttribute(), remover_(remover) {}
const PropertyRemover *getRemover() const { return remover_; }
~CustomPropertyRemoveAttribute()
{
delete remover_;
}
private:
const PropertyRemover *remover_;
};
/// This struct allows customization of an indexed property's index set.

View File

@@ -0,0 +1,48 @@
#ifndef OSGINTROSPECTION_COMPARATOR_
#define OSGINTROSPECTION_COMPARATOR_
namespace osgIntrospection
{
class Value;
struct Comparator
{
virtual bool isEqualTo(const Value &l, const Value &r) const = 0;
virtual bool isLessThanOrEqualTo(const Value &l, const Value &r) const = 0;
};
template<typename T>
struct TotalOrderComparator: Comparator
{
virtual bool isEqualTo(const Value &l, const Value &r) const
{
const T &vl = variant_cast<const T &>(l);
const T &vr = variant_cast<const T &>(r);
return vl <= vr && vr <= vl;
}
virtual bool isLessThanOrEqualTo(const Value &l, const Value &r) const
{
return variant_cast<const T &>(l) <= variant_cast<const T &>(r);
}
};
template<typename T>
struct PartialOrderComparator: Comparator
{
virtual bool isEqualTo(const Value &l, const Value &r) const
{
return variant_cast<const T &>(l) == variant_cast<const T &>(r);
}
virtual bool isLessThanOrEqualTo(const Value &, const Value &) const
{
throw ComparisonOperatorNotSupportedException(typeid(T), "less than or equal to");
}
};
}
#endif

View File

@@ -0,0 +1,49 @@
#ifndef OSGINTROSPECTION_CONSTRUCTORINFO_
#define OSGINTROSPECTION_CONSTRUCTORINFO_
#include <osgIntrospection/Export>
#include <osgIntrospection/Type>
#include <osgIntrospection/ParameterInfo>
#include <osgIntrospection/CustomAttributeProvider>
namespace osgIntrospection
{
class OSGINTROSPECTION_EXPORT ConstructorInfo: public CustomAttributeProvider
{
public:
ConstructorInfo(const Type &decltype, const ParameterInfoList &params)
: decltype_(decltype),
params_(params)
{
}
virtual ~ConstructorInfo()
{
for (ParameterInfoList::iterator i=params_.begin(); i!=params_.end(); ++i)
delete *i;
}
inline const Type &getDeclaringType() const
{
return decltype_;
}
inline const ParameterInfoList &getParameters() const
{
return params_;
}
virtual Value createInstance(ValueList &args) const = 0;
protected:
virtual void getInheritedProviders(CustomAttributeProviderList &providers) const;
private:
const Type &decltype_;
ParameterInfoList params_;
};
}
#endif

View File

@@ -37,6 +37,22 @@ namespace osgIntrospection
{
}
};
struct ConstructorNotFoundException: public ReflectionException
{
ConstructorNotFoundException(const std::type_info &ti)
: ReflectionException("could not find a suitable constructor in type `" + std::string(ti.name()) + "'")
{
}
};
struct InvokeNotImplementedException: public ReflectionException
{
InvokeNotImplementedException()
: ReflectionException("invoke() not implemented")
{
}
};
struct InvalidFunctionPointerException: public ReflectionException
{
@@ -148,6 +164,7 @@ namespace osgIntrospection
AGET,
ASET,
ADD,
REMOVE,
COUNT
};
@@ -169,6 +186,7 @@ namespace osgIntrospection
case AGET: msg = "retrieved with array index"; break;
case ASET: msg = "set with array index"; break;
case ADD: msg = "added"; break;
case REMOVE: msg = "removed"; break;
case COUNT: msg = "counted"; break;
default: msg = "?";
}
@@ -183,6 +201,22 @@ namespace osgIntrospection
{
}
};
struct ComparisonNotPermittedException: ReflectionException
{
ComparisonNotPermittedException(const std::type_info &ti)
: ReflectionException("comparison not permitted on type `" + std::string(ti.name()) + "'")
{
}
};
struct ComparisonOperatorNotSupportedException: ReflectionException
{
ComparisonOperatorNotSupportedException(const std::type_info &ti, const std::string &op)
: ReflectionException("comparison operator `" + op + "' is not supported on type `" + std::string(ti.name()) + "'")
{
}
};
}

View File

@@ -0,0 +1,335 @@
#ifndef OSGINTROSPECTION_INSTANCECREATOR_
#define OSGINTROSPECTION_INSTANCECREATOR_
#include <osgIntrospection/Value>
#include <osgIntrospection/variant_cast>
namespace osgIntrospection
{
/// The ObjectInstanceCreator struct template is a collection of
/// static methods that provide the means for creating instances
/// of object types dynamically. Such methods are usually called
/// from within TypedConstructorInfo{n}::createInstance().
template<typename T>
struct ObjectInstanceCreator
{
template<typename P0, typename P1, typename P2, typename P3, typename P4, typename P5, typename P6, typename P7, typename P8, typename P9, typename P10, typename P11, typename P12, typename P13, typename P14, typename P15>
static Value create(Value &a0, Value &a1, Value &a2, Value &a3, Value &a4, Value &a5, Value &a6, Value &a7, Value &a8, Value &a9, Value &a10, Value &a11, Value &a12, Value &a13, Value &a14, Value &a15)
{
return new T(variant_cast<P0>(a0), variant_cast<P1>(a1), variant_cast<P2>(a2), variant_cast<P3>(a3), variant_cast<P4>(a4), variant_cast<P5>(a5), variant_cast<P6>(a6), variant_cast<P7>(a7), variant_cast<P8>(a8), variant_cast<P9>(a9), variant_cast<P10>(a10), variant_cast<P11>(a11), variant_cast<P12>(a12), variant_cast<P13>(a13), variant_cast<P14>(a14), variant_cast<P15>(a15));
}
template<typename P0, typename P1, typename P2, typename P3, typename P4, typename P5, typename P6, typename P7, typename P8, typename P9, typename P10, typename P11, typename P12, typename P13, typename P14>
static Value create(Value &a0, Value &a1, Value &a2, Value &a3, Value &a4, Value &a5, Value &a6, Value &a7, Value &a8, Value &a9, Value &a10, Value &a11, Value &a12, Value &a13, Value &a14)
{
return new T(variant_cast<P0>(a0), variant_cast<P1>(a1), variant_cast<P2>(a2), variant_cast<P3>(a3), variant_cast<P4>(a4), variant_cast<P5>(a5), variant_cast<P6>(a6), variant_cast<P7>(a7), variant_cast<P8>(a8), variant_cast<P9>(a9), variant_cast<P10>(a10), variant_cast<P11>(a11), variant_cast<P12>(a12), variant_cast<P13>(a13), variant_cast<P14>(a14));
}
template<typename P0, typename P1, typename P2, typename P3, typename P4, typename P5, typename P6, typename P7, typename P8, typename P9, typename P10, typename P11, typename P12, typename P13>
static Value create(Value &a0, Value &a1, Value &a2, Value &a3, Value &a4, Value &a5, Value &a6, Value &a7, Value &a8, Value &a9, Value &a10, Value &a11, Value &a12, Value &a13)
{
return new T(variant_cast<P0>(a0), variant_cast<P1>(a1), variant_cast<P2>(a2), variant_cast<P3>(a3), variant_cast<P4>(a4), variant_cast<P5>(a5), variant_cast<P6>(a6), variant_cast<P7>(a7), variant_cast<P8>(a8), variant_cast<P9>(a9), variant_cast<P10>(a10), variant_cast<P11>(a11), variant_cast<P12>(a12), variant_cast<P13>(a13));
}
template<typename P0, typename P1, typename P2, typename P3, typename P4, typename P5, typename P6, typename P7, typename P8, typename P9, typename P10, typename P11, typename P12>
static Value create(Value &a0, Value &a1, Value &a2, Value &a3, Value &a4, Value &a5, Value &a6, Value &a7, Value &a8, Value &a9, Value &a10, Value &a11, Value &a12)
{
return new T(variant_cast<P0>(a0), variant_cast<P1>(a1), variant_cast<P2>(a2), variant_cast<P3>(a3), variant_cast<P4>(a4), variant_cast<P5>(a5), variant_cast<P6>(a6), variant_cast<P7>(a7), variant_cast<P8>(a8), variant_cast<P9>(a9), variant_cast<P10>(a10), variant_cast<P11>(a11), variant_cast<P12>(a12));
}
template<typename P0, typename P1, typename P2, typename P3, typename P4, typename P5, typename P6, typename P7, typename P8, typename P9, typename P10, typename P11>
static Value create(Value &a0, Value &a1, Value &a2, Value &a3, Value &a4, Value &a5, Value &a6, Value &a7, Value &a8, Value &a9, Value &a10, Value &a11)
{
return new T(variant_cast<P0>(a0), variant_cast<P1>(a1), variant_cast<P2>(a2), variant_cast<P3>(a3), variant_cast<P4>(a4), variant_cast<P5>(a5), variant_cast<P6>(a6), variant_cast<P7>(a7), variant_cast<P8>(a8), variant_cast<P9>(a9), variant_cast<P10>(a10), variant_cast<P11>(a11));
}
template<typename P0, typename P1, typename P2, typename P3, typename P4, typename P5, typename P6, typename P7, typename P8, typename P9, typename P10>
static Value create(Value &a0, Value &a1, Value &a2, Value &a3, Value &a4, Value &a5, Value &a6, Value &a7, Value &a8, Value &a9, Value &a10)
{
return new T(variant_cast<P0>(a0), variant_cast<P1>(a1), variant_cast<P2>(a2), variant_cast<P3>(a3), variant_cast<P4>(a4), variant_cast<P5>(a5), variant_cast<P6>(a6), variant_cast<P7>(a7), variant_cast<P8>(a8), variant_cast<P9>(a9), variant_cast<P10>(a10));
}
template<typename P0, typename P1, typename P2, typename P3, typename P4, typename P5, typename P6, typename P7, typename P8, typename P9>
static Value create(Value &a0, Value &a1, Value &a2, Value &a3, Value &a4, Value &a5, Value &a6, Value &a7, Value &a8, Value &a9)
{
return new T(variant_cast<P0>(a0), variant_cast<P1>(a1), variant_cast<P2>(a2), variant_cast<P3>(a3), variant_cast<P4>(a4), variant_cast<P5>(a5), variant_cast<P6>(a6), variant_cast<P7>(a7), variant_cast<P8>(a8), variant_cast<P9>(a9));
}
template<typename P0, typename P1, typename P2, typename P3, typename P4, typename P5, typename P6, typename P7, typename P8>
static Value create(Value &a0, Value &a1, Value &a2, Value &a3, Value &a4, Value &a5, Value &a6, Value &a7, Value &a8)
{
return new T(variant_cast<P0>(a0), variant_cast<P1>(a1), variant_cast<P2>(a2), variant_cast<P3>(a3), variant_cast<P4>(a4), variant_cast<P5>(a5), variant_cast<P6>(a6), variant_cast<P7>(a7), variant_cast<P8>(a8));
}
template<typename P0, typename P1, typename P2, typename P3, typename P4, typename P5, typename P6, typename P7>
static Value create(Value &a0, Value &a1, Value &a2, Value &a3, Value &a4, Value &a5, Value &a6, Value &a7)
{
return new T(variant_cast<P0>(a0), variant_cast<P1>(a1), variant_cast<P2>(a2), variant_cast<P3>(a3), variant_cast<P4>(a4), variant_cast<P5>(a5), variant_cast<P6>(a6), variant_cast<P7>(a7));
}
template<typename P0, typename P1, typename P2, typename P3, typename P4, typename P5, typename P6>
static Value create(Value &a0, Value &a1, Value &a2, Value &a3, Value &a4, Value &a5, Value &a6)
{
return new T(variant_cast<P0>(a0), variant_cast<P1>(a1), variant_cast<P2>(a2), variant_cast<P3>(a3), variant_cast<P4>(a4), variant_cast<P5>(a5), variant_cast<P6>(a6));
}
template<typename P0, typename P1, typename P2, typename P3, typename P4, typename P5>
static Value create(Value &a0, Value &a1, Value &a2, Value &a3, Value &a4, Value &a5)
{
return new T(variant_cast<P0>(a0), variant_cast<P1>(a1), variant_cast<P2>(a2), variant_cast<P3>(a3), variant_cast<P4>(a4), variant_cast<P5>(a5));
}
template<typename P0, typename P1, typename P2, typename P3, typename P4>
static Value create(Value &a0, Value &a1, Value &a2, Value &a3, Value &a4)
{
return new T(variant_cast<P0>(a0), variant_cast<P1>(a1), variant_cast<P2>(a2), variant_cast<P3>(a3), variant_cast<P4>(a4));
}
template<typename P0, typename P1, typename P2, typename P3>
static Value create(Value &a0, Value &a1, Value &a2, Value &a3)
{
return new T(variant_cast<P0>(a0), variant_cast<P1>(a1), variant_cast<P2>(a2), variant_cast<P3>(a3));
}
template<typename P0, typename P1, typename P2>
static Value create(Value &a0, Value &a1, Value &a2)
{
return new T(variant_cast<P0>(a0), variant_cast<P1>(a1), variant_cast<P2>(a2));
}
template<typename P0, typename P1>
static Value create(Value &a0, Value &a1)
{
return new T(variant_cast<P0>(a0), variant_cast<P1>(a1));
}
template<typename P0>
static Value create(Value &a0)
{
return new T(variant_cast<P0>(a0));
}
static Value create()
{
return new T();
}
};
template<typename T>
struct ValueInstanceCreator
{
template<typename P0, typename P1, typename P2, typename P3, typename P4, typename P5, typename P6, typename P7, typename P8, typename P9, typename P10, typename P11, typename P12, typename P13, typename P14, typename P15>
static Value create(Value &a0, Value &a1, Value &a2, Value &a3, Value &a4, Value &a5, Value &a6, Value &a7, Value &a8, Value &a9, Value &a10, Value &a11, Value &a12, Value &a13, Value &a14, Value &a15)
{
return T(variant_cast<P0>(a0), variant_cast<P1>(a1), variant_cast<P2>(a2), variant_cast<P3>(a3), variant_cast<P4>(a4), variant_cast<P5>(a5), variant_cast<P6>(a6), variant_cast<P7>(a7), variant_cast<P8>(a8), variant_cast<P9>(a9), variant_cast<P10>(a10), variant_cast<P11>(a11), variant_cast<P12>(a12), variant_cast<P13>(a13), variant_cast<P14>(a14), variant_cast<P15>(a15));
}
template<typename P0, typename P1, typename P2, typename P3, typename P4, typename P5, typename P6, typename P7, typename P8, typename P9, typename P10, typename P11, typename P12, typename P13, typename P14>
static Value create(Value &a0, Value &a1, Value &a2, Value &a3, Value &a4, Value &a5, Value &a6, Value &a7, Value &a8, Value &a9, Value &a10, Value &a11, Value &a12, Value &a13, Value &a14)
{
return T(variant_cast<P0>(a0), variant_cast<P1>(a1), variant_cast<P2>(a2), variant_cast<P3>(a3), variant_cast<P4>(a4), variant_cast<P5>(a5), variant_cast<P6>(a6), variant_cast<P7>(a7), variant_cast<P8>(a8), variant_cast<P9>(a9), variant_cast<P10>(a10), variant_cast<P11>(a11), variant_cast<P12>(a12), variant_cast<P13>(a13), variant_cast<P14>(a14));
}
template<typename P0, typename P1, typename P2, typename P3, typename P4, typename P5, typename P6, typename P7, typename P8, typename P9, typename P10, typename P11, typename P12, typename P13>
static Value create(Value &a0, Value &a1, Value &a2, Value &a3, Value &a4, Value &a5, Value &a6, Value &a7, Value &a8, Value &a9, Value &a10, Value &a11, Value &a12, Value &a13)
{
return T(variant_cast<P0>(a0), variant_cast<P1>(a1), variant_cast<P2>(a2), variant_cast<P3>(a3), variant_cast<P4>(a4), variant_cast<P5>(a5), variant_cast<P6>(a6), variant_cast<P7>(a7), variant_cast<P8>(a8), variant_cast<P9>(a9), variant_cast<P10>(a10), variant_cast<P11>(a11), variant_cast<P12>(a12), variant_cast<P13>(a13));
}
template<typename P0, typename P1, typename P2, typename P3, typename P4, typename P5, typename P6, typename P7, typename P8, typename P9, typename P10, typename P11, typename P12>
static Value create(Value &a0, Value &a1, Value &a2, Value &a3, Value &a4, Value &a5, Value &a6, Value &a7, Value &a8, Value &a9, Value &a10, Value &a11, Value &a12)
{
return T(variant_cast<P0>(a0), variant_cast<P1>(a1), variant_cast<P2>(a2), variant_cast<P3>(a3), variant_cast<P4>(a4), variant_cast<P5>(a5), variant_cast<P6>(a6), variant_cast<P7>(a7), variant_cast<P8>(a8), variant_cast<P9>(a9), variant_cast<P10>(a10), variant_cast<P11>(a11), variant_cast<P12>(a12));
}
template<typename P0, typename P1, typename P2, typename P3, typename P4, typename P5, typename P6, typename P7, typename P8, typename P9, typename P10, typename P11>
static Value create(Value &a0, Value &a1, Value &a2, Value &a3, Value &a4, Value &a5, Value &a6, Value &a7, Value &a8, Value &a9, Value &a10, Value &a11)
{
return T(variant_cast<P0>(a0), variant_cast<P1>(a1), variant_cast<P2>(a2), variant_cast<P3>(a3), variant_cast<P4>(a4), variant_cast<P5>(a5), variant_cast<P6>(a6), variant_cast<P7>(a7), variant_cast<P8>(a8), variant_cast<P9>(a9), variant_cast<P10>(a10), variant_cast<P11>(a11));
}
template<typename P0, typename P1, typename P2, typename P3, typename P4, typename P5, typename P6, typename P7, typename P8, typename P9, typename P10>
static Value create(Value &a0, Value &a1, Value &a2, Value &a3, Value &a4, Value &a5, Value &a6, Value &a7, Value &a8, Value &a9, Value &a10)
{
return T(variant_cast<P0>(a0), variant_cast<P1>(a1), variant_cast<P2>(a2), variant_cast<P3>(a3), variant_cast<P4>(a4), variant_cast<P5>(a5), variant_cast<P6>(a6), variant_cast<P7>(a7), variant_cast<P8>(a8), variant_cast<P9>(a9), variant_cast<P10>(a10));
}
template<typename P0, typename P1, typename P2, typename P3, typename P4, typename P5, typename P6, typename P7, typename P8, typename P9>
static Value create(Value &a0, Value &a1, Value &a2, Value &a3, Value &a4, Value &a5, Value &a6, Value &a7, Value &a8, Value &a9)
{
return T(variant_cast<P0>(a0), variant_cast<P1>(a1), variant_cast<P2>(a2), variant_cast<P3>(a3), variant_cast<P4>(a4), variant_cast<P5>(a5), variant_cast<P6>(a6), variant_cast<P7>(a7), variant_cast<P8>(a8), variant_cast<P9>(a9));
}
template<typename P0, typename P1, typename P2, typename P3, typename P4, typename P5, typename P6, typename P7, typename P8>
static Value create(Value &a0, Value &a1, Value &a2, Value &a3, Value &a4, Value &a5, Value &a6, Value &a7, Value &a8)
{
return T(variant_cast<P0>(a0), variant_cast<P1>(a1), variant_cast<P2>(a2), variant_cast<P3>(a3), variant_cast<P4>(a4), variant_cast<P5>(a5), variant_cast<P6>(a6), variant_cast<P7>(a7), variant_cast<P8>(a8));
}
template<typename P0, typename P1, typename P2, typename P3, typename P4, typename P5, typename P6, typename P7>
static Value create(Value &a0, Value &a1, Value &a2, Value &a3, Value &a4, Value &a5, Value &a6, Value &a7)
{
return T(variant_cast<P0>(a0), variant_cast<P1>(a1), variant_cast<P2>(a2), variant_cast<P3>(a3), variant_cast<P4>(a4), variant_cast<P5>(a5), variant_cast<P6>(a6), variant_cast<P7>(a7));
}
template<typename P0, typename P1, typename P2, typename P3, typename P4, typename P5, typename P6>
static Value create(Value &a0, Value &a1, Value &a2, Value &a3, Value &a4, Value &a5, Value &a6)
{
return T(variant_cast<P0>(a0), variant_cast<P1>(a1), variant_cast<P2>(a2), variant_cast<P3>(a3), variant_cast<P4>(a4), variant_cast<P5>(a5), variant_cast<P6>(a6));
}
template<typename P0, typename P1, typename P2, typename P3, typename P4, typename P5>
static Value create(Value &a0, Value &a1, Value &a2, Value &a3, Value &a4, Value &a5)
{
return T(variant_cast<P0>(a0), variant_cast<P1>(a1), variant_cast<P2>(a2), variant_cast<P3>(a3), variant_cast<P4>(a4), variant_cast<P5>(a5));
}
template<typename P0, typename P1, typename P2, typename P3, typename P4>
static Value create(Value &a0, Value &a1, Value &a2, Value &a3, Value &a4)
{
return T(variant_cast<P0>(a0), variant_cast<P1>(a1), variant_cast<P2>(a2), variant_cast<P3>(a3), variant_cast<P4>(a4));
}
template<typename P0, typename P1, typename P2, typename P3>
static Value create(Value &a0, Value &a1, Value &a2, Value &a3)
{
return T(variant_cast<P0>(a0), variant_cast<P1>(a1), variant_cast<P2>(a2), variant_cast<P3>(a3));
}
template<typename P0, typename P1, typename P2>
static Value create(Value &a0, Value &a1, Value &a2)
{
return T(variant_cast<P0>(a0), variant_cast<P1>(a1), variant_cast<P2>(a2));
}
template<typename P0, typename P1>
static Value create(Value &a0, Value &a1)
{
return T(variant_cast<P0>(a0), variant_cast<P1>(a1));
}
template<typename P0>
static Value create(Value &a0)
{
return T(variant_cast<P0>(a0));
}
static Value create()
{
return T();
}
};
template<typename T>
struct DummyInstanceCreator
{
template<typename P0, typename P1, typename P2, typename P3, typename P4, typename P5, typename P6, typename P7, typename P8, typename P9, typename P10, typename P11, typename P12, typename P13, typename P14, typename P15>
static Value create(Value &, Value &, Value &, Value &, Value &, Value &, Value &, Value &, Value &, Value &, Value &, Value &, Value &, Value &, Value &, Value &)
{
return Value();
}
template<typename P0, typename P1, typename P2, typename P3, typename P4, typename P5, typename P6, typename P7, typename P8, typename P9, typename P10, typename P11, typename P12, typename P13, typename P14>
static Value create(Value &, Value &, Value &, Value &, Value &, Value &, Value &, Value &, Value &, Value &, Value &, Value &, Value &, Value &, Value &)
{
return Value();
}
template<typename P0, typename P1, typename P2, typename P3, typename P4, typename P5, typename P6, typename P7, typename P8, typename P9, typename P10, typename P11, typename P12, typename P13>
static Value create(Value &, Value &, Value &, Value &, Value &, Value &, Value &, Value &, Value &, Value &, Value &, Value &, Value &, Value &)
{
return Value();
}
template<typename P0, typename P1, typename P2, typename P3, typename P4, typename P5, typename P6, typename P7, typename P8, typename P9, typename P10, typename P11, typename P12>
static Value create(Value &, Value &, Value &, Value &, Value &, Value &, Value &, Value &, Value &, Value &, Value &, Value &, Value &)
{
return Value();
}
template<typename P0, typename P1, typename P2, typename P3, typename P4, typename P5, typename P6, typename P7, typename P8, typename P9, typename P10, typename P11>
static Value create(Value &, Value &, Value &, Value &, Value &, Value &, Value &, Value &, Value &, Value &, Value &, Value &)
{
return Value();
}
template<typename P0, typename P1, typename P2, typename P3, typename P4, typename P5, typename P6, typename P7, typename P8, typename P9, typename P10>
static Value create(Value &, Value &, Value &, Value &, Value &, Value &, Value &, Value &, Value &, Value &, Value &)
{
return Value();
}
template<typename P0, typename P1, typename P2, typename P3, typename P4, typename P5, typename P6, typename P7, typename P8, typename P9>
static Value create(Value &, Value &, Value &, Value &, Value &, Value &, Value &, Value &, Value &, Value &)
{
return Value();
}
template<typename P0, typename P1, typename P2, typename P3, typename P4, typename P5, typename P6, typename P7, typename P8>
static Value create(Value &, Value &, Value &, Value &, Value &, Value &, Value &, Value &, Value &)
{
return Value();
}
template<typename P0, typename P1, typename P2, typename P3, typename P4, typename P5, typename P6, typename P7>
static Value create(Value &, Value &, Value &, Value &, Value &, Value &, Value &, Value &)
{
return Value();
}
template<typename P0, typename P1, typename P2, typename P3, typename P4, typename P5, typename P6>
static Value create(Value &, Value &, Value &, Value &, Value &, Value &, Value &)
{
return Value();
}
template<typename P0, typename P1, typename P2, typename P3, typename P4, typename P5>
static Value create(Value &, Value &, Value &, Value &, Value &, Value &)
{
return Value();
}
template<typename P0, typename P1, typename P2, typename P3, typename P4>
static Value create(Value &, Value &, Value &, Value &, Value &)
{
return Value();
}
template<typename P0, typename P1, typename P2, typename P3>
static Value create(Value &, Value &, Value &, Value &)
{
return Value();
}
template<typename P0, typename P1, typename P2>
static Value create(Value &, Value &, Value &)
{
return Value();
}
template<typename P0, typename P1>
static Value create(Value &, Value &)
{
return Value();
}
template<typename P0>
static Value create(Value &)
{
return Value();
}
static Value create()
{
return Value();
}
};
}
#endif

View File

@@ -25,6 +25,9 @@ namespace osgIntrospection
public:
/// Direct initialization constructor.
inline MethodInfo(const std::string &qname, const Type &decltype, const Type &rtype, const ParameterInfoList &plist);
/// Destructor
inline ~MethodInfo();
/// Returns the Type object associated to the type that
/// declares the reflected method.
@@ -42,14 +45,25 @@ namespace osgIntrospection
/// Returns whether the reflected method is const or not.
virtual bool isConst() const = 0;
/// Returns whether the reflected method is static or not.
virtual bool isStatic() const = 0;
/// Returns whether this method would override the given
/// method.
bool overrides(const MethodInfo *other) const;
/// Invokes the reflected method dynamically on the given const
/// instance, passing it the arguments as a list of Value objects.
virtual Value invoke(const Value &instance, ValueList &args) const = 0;
inline virtual Value invoke(const Value &instance, ValueList &args) const;
/// Invokes the reflected method dynamically on the given instance,
/// passing it the arguments as a list of Value objects.
virtual Value invoke(Value &instance, ValueList &args) const = 0;
inline virtual Value invoke(Value &instance, ValueList &args) const;
/// Invokes the reflected static method dynamically passing it the
/// arguments as a list of Value objects.
inline virtual Value invoke(ValueList &args) const;
/// Invokes the reflected method dynamically on the given const
/// instance, without arguments.
@@ -58,6 +72,9 @@ namespace osgIntrospection
/// Invokes the reflected method dynamically on the given
/// instance, without arguments.
inline Value invoke(Value &instance) const;
/// Invokes the reflected static method without arguments.
inline Value invoke() const;
private:
inline std::string strip_namespace(const std::string &s) const;
@@ -73,7 +90,7 @@ namespace osgIntrospection
// INLINE METHODS
inline MethodInfo::MethodInfo(const std::string &qname, const Type &decltype, const Type &rtype, const ParameterInfoList &plist)
: CustomAttributeProvider(),
: CustomAttributeProvider(),
decltype_(decltype),
rtype_(rtype),
params_(plist)
@@ -109,6 +126,21 @@ namespace osgIntrospection
return params_;
}
inline Value MethodInfo::invoke(const Value &, ValueList &) const
{
throw InvokeNotImplementedException();
}
inline Value MethodInfo::invoke(Value &, ValueList &) const
{
throw InvokeNotImplementedException();
}
inline Value MethodInfo::invoke(ValueList &) const
{
throw InvokeNotImplementedException();
}
inline Value MethodInfo::invoke(const Value &instance) const
{
ValueList args;
@@ -120,6 +152,18 @@ namespace osgIntrospection
ValueList args;
return invoke(instance, args);
}
inline Value MethodInfo::invoke() const
{
ValueList args;
return invoke(args);
}
inline MethodInfo::~MethodInfo()
{
for (ParameterInfoList::iterator i=params_.begin(); i!=params_.end(); ++i)
delete *i;
}
}

View File

@@ -22,27 +22,26 @@ namespace osgIntrospection
{
NONE = 0,
IN = 1, // parameter is used to pass data to the function
IN = 1, // parameter is used to pass data to the function
OUT = 2, // parameter is used to return data from the function
INOUT = IN | OUT
};
/// Direct initialization constructor.
inline ParameterInfo(const std::string &name, const Type &type, int position, int attribs);
inline ParameterInfo(const std::string &name, const Type &type, int attribs, const Value &defval = Value());
/// Returns the parameter's name.
inline const std::string &getName() const;
/// Return the parameter's position within the function's parameter
/// list. This position is zero-based.
inline int getPosition() const;
/// Returns the parameter's type.
inline const Type &getParameterType() const;
/// Returns the parameter's attributes.
inline int getAttributes() const;
/// Returns the default value.
inline const Value &getDefaultValue() const;
/// Returns whether the parameter has the IN attribute.
inline bool isIn() const { return (attribs_ & IN) != 0; }
@@ -57,17 +56,17 @@ namespace osgIntrospection
private:
std::string name_;
const Type &type_;
int position_;
int attribs_;
Value default_;
};
// INLINE METHODS
inline ParameterInfo::ParameterInfo(const std::string &name, const Type &type, int position, int attribs)
: name_(name),
inline ParameterInfo::ParameterInfo(const std::string &name, const Type &type, int attribs, const Value &defval)
: name_(name),
type_(type),
position_(position),
attribs_(attribs)
attribs_(attribs),
default_(defval)
{
}
@@ -76,11 +75,6 @@ namespace osgIntrospection
return name_;
}
inline int ParameterInfo::getPosition() const
{
return position_;
}
inline const Type &ParameterInfo::getParameterType() const
{
return type_;
@@ -90,6 +84,11 @@ namespace osgIntrospection
{
return attribs_;
}
inline const Value &ParameterInfo::getDefaultValue() const
{
return default_;
}
}

View File

@@ -47,7 +47,7 @@ namespace osgIntrospection
/// to be indexed. The same is true if the getter is null and the
/// setter has more than one parameter.
PropertyInfo(const Type &decltype, const Type &ptype, const std::string &name, const MethodInfo *getm, const MethodInfo *setm)
: CustomAttributeProvider(),
: CustomAttributeProvider(),
decltype_(decltype),
ptype_(ptype),
name_(name),
@@ -55,6 +55,7 @@ namespace osgIntrospection
setm_(setm),
numm_(0),
addm_(0),
remm_(0),
is_array_(false)
{
if (getm_)
@@ -76,9 +77,9 @@ namespace osgIntrospection
/// You must pass the Type object associated to the type that
/// declares the property, the Type object that describes the
/// type of the property's value, the property name and the
/// getter/setter/counter/adder methods.
PropertyInfo(const Type &decltype, const Type &ptype, const std::string &name, const MethodInfo *getm, const MethodInfo *setm, const MethodInfo *numm, const MethodInfo *addm)
: CustomAttributeProvider(),
/// getter/setter/counter/adder/remover methods.
PropertyInfo(const Type &decltype, const Type &ptype, const std::string &name, const MethodInfo *getm, const MethodInfo *setm, const MethodInfo *numm, const MethodInfo *addm, const MethodInfo *remm)
: CustomAttributeProvider(),
decltype_(decltype),
ptype_(ptype),
name_(name),
@@ -86,6 +87,7 @@ namespace osgIntrospection
setm_(setm),
numm_(numm),
addm_(addm),
remm_(remm),
is_array_(true)
{
}
@@ -139,6 +141,12 @@ namespace osgIntrospection
{
return addm_;
}
/// Returns the remover method.
inline const MethodInfo *getRemoveMethod() const
{
return remm_;
}
/// Returns whether the property's value can be retrieved.
inline bool canGet() const
@@ -163,6 +171,13 @@ namespace osgIntrospection
{
return addm_ != 0 || isDefined<CustomPropertyAddAttribute>(false);
}
/// Returns whether items can be removed from the array property.
inline bool canRemove() const
{
return remm_ != 0 || isDefined<CustomPropertyRemoveAttribute>(false);
}
/// Returns whether the property is simple.
inline bool isSimple() const
@@ -195,16 +210,26 @@ namespace osgIntrospection
/// values will be returned.
void getIndexValueSet(int whichindex, const Value &instance, ValueList &values) const;
/// Invokes the getter method on the given instance and
/// Invokes the getter method on the given const instance and
/// returns the property's value. If a custom getter attribute
/// is defined, it will be invoked instead.
Value getValue(const Value &instance) const;
/// Invokes the getter method on the given instance and
/// returns the property's value. If a custom getter attribute
/// is defined, it will be invoked instead.
Value getValue(Value &instance) const;
/// Invokes the setter method on the given instance and
/// sets the property's value. If a custom setter attribute
/// is defined, it will be invoked instead.
void setValue(Value &instance, const Value &value) const;
/// Invokes the getter method on the given const instance passing a
/// list of indices and returns the indexed property's value. If a
/// custom getter attribute is defined, it will be invoked instead.
Value getIndexedValue(Value &instance, ValueList &indices) const;
/// Invokes the getter method on the given instance passing a list
/// of indices and returns the indexed property's value. If a custom
/// getter attribute is defined, it will be invoked instead.
@@ -220,11 +245,16 @@ namespace osgIntrospection
/// attribute is defined, it will be invoked instead.
int getNumArrayItems(const Value &instance) const;
/// Invokes the getter method on the given instance and returns
/// Invokes the getter method on the given const instance and returns
/// the i-th item of the array property. If a custom getter attribute
/// us defined, it will be invoked instead.
Value getArrayItem(const Value &instance, int i) const;
/// Invokes the getter method on the given instance and returns
/// the i-th item of the array property. If a custom getter attribute
/// us defined, it will be invoked instead.
Value getArrayItem(Value &instance, int i) const;
/// Invokes the setter method on the given instance and sets
/// the i-th item of the array property. If a custom setter attribute
/// is defined, it will be invoked instead.
@@ -234,6 +264,11 @@ namespace osgIntrospection
/// an item to the array property. If a custom adder attribute is
/// defined, it will be invoked instead.
void addArrayItem(Value &instance, const Value &value) const;
/// Invokes the remover method on the given instance and removes
/// an item from the array property. If a custom remover attribute
/// is defined, it will be invoked instead.
void removeArrayItem(Value &instance, int i) const;
/// Returns the default value associated to the reflected property.
/// If no default value has been specified, this method tries to
@@ -253,6 +288,7 @@ namespace osgIntrospection
const MethodInfo *setm_;
const MethodInfo *numm_;
const MethodInfo *addm_;
const MethodInfo *remm_;
ParameterInfoList indices_;
bool is_array_;
};

View File

@@ -219,12 +219,12 @@ namespace osgIntrospection
return is;
}
virtual std::ostream &writeBinaryValue(std::ostream &os, const Value &v, const Options *options = 0) const
virtual std::ostream &writeBinaryValue(std::ostream &os, const Value &v, const Options * = 0) const
{
return os.write(reinterpret_cast<const char *>(extract_raw_data<T>(v)), sizeof(T));
}
virtual std::istream &readBinaryValue(std::istream &is, Value &v, const Options *options = 0) const
virtual std::istream &readBinaryValue(std::istream &is, Value &v, const Options * = 0) const
{
if (v.isEmpty())
v = Value(T());

View File

@@ -61,6 +61,7 @@ namespace osgIntrospection
private:
template<typename C> friend class Reflector;
template<typename C> friend struct TypeNameAliasProxy;
struct StaticData
{
@@ -70,7 +71,7 @@ namespace osgIntrospection
static StaticData &getOrCreateStaticData();
static Type *registerType(const std::type_info &ti);
static Type *registerOrReplaceType(const std::type_info &ti);
static Type *getOrRegisterType(const std::type_info &ti, bool replace_if_defined = false);
private:
static StaticData *staticdata__;

File diff suppressed because it is too large Load Diff

View File

@@ -3,17 +3,23 @@
#include <osgIntrospection/Reflection>
#include <osgIntrospection/Type>
#include <osgIntrospection/Value>
#include <osgIntrospection/PropertyInfo>
#include <osgIntrospection/MethodInfo>
#include <osgIntrospection/ConstructorInfo>
#include <osgIntrospection/InstanceCreator>
#include <osgIntrospection/ReaderWriter>
#include <osgIntrospection/TypedConstructorInfo>
#include <osgIntrospection/Comparator>
#include <string>
#include <sstream>
#include <iterator>
namespace osgIntrospection
{
class CustomAttribute;
class CustomAttributeProvider;
class ReaderWriter;
/// A Reflector is a proxy class that is used to create a new description
@@ -42,15 +48,13 @@ namespace osgIntrospection
protected:
/// Direct initialization constructor. Parameter 'name' is the name
/// of the type being reflected, 'ns' is its namespace and 'rw' is
/// the ReaderWriter object associated to the type.
Reflector(const std::string &name, const std::string &ns, const ReaderWriter *rw);
/// of the type being reflected and 'ns' is its namespace.
Reflector(const std::string &name, const std::string &ns, bool abstract = false);
/// Direct initialization constructor. Parameter 'qname' is the
/// fully-qualified name of the type being reflected, i.e. containing
/// both the namespace and the name (separated by "::"). Parameter
/// 'rw' is the ReaderWriter object associated to the type.
Reflector(const std::string &qname, const ReaderWriter *rw);
/// both the namespace and the name (separated by "::").
Reflector(const std::string &qname, bool abstract = false);
protected:
@@ -59,6 +63,9 @@ namespace osgIntrospection
/// Declares a new base type for the current type.
void addBaseType(const Type &type);
/// Sets the comparator object for the current type.
void setComparator(const Comparator *cmp);
/// Adds a property description to the current type.
PropertyInfo *addProperty(PropertyInfo *pi);
@@ -69,8 +76,11 @@ namespace osgIntrospection
/// Adds an enumeration label to the current type.
void addEnumLabel(int v, const std::string &label, bool strip_namespace = true);
/// Sets the instance creator for the current type.
void setInstanceCreator(const InstanceCreatorBase *icb);
/// Adds a constructor description to the current type.
/// As soon as a constructor is added through this method,
/// the automatically-generated default constructor is
/// removed.
ConstructorInfo *addConstructor(ConstructorInfo *ci);
/// Returns a string containing the qualified version of 'name'.
std::string qualifyName(const std::string name) const;
@@ -82,8 +92,32 @@ namespace osgIntrospection
void setReaderWriter(const ReaderWriter *rw);
private:
void init();
struct PtrConstructor: ConstructorInfo
{
PtrConstructor(const Type *pt)
: ConstructorInfo(*pt, ParameterInfoList())
{
}
Value createInstance(ValueList &) const { T* x = 0; return x; }
};
struct ConstPtrConstructor: ConstructorInfo
{
ConstPtrConstructor(const Type *pt)
: ConstructorInfo(*pt, ParameterInfoList())
{
}
Value createInstance(ValueList &) const { const T *x = 0; return x; }
};
void init();
static std::string purify(const std::string &s);
static void split_qualified_name(const std::string &q, std::string &n, std::string &ns);
typedef std::vector<MethodInfo *> TempMethodList;
TempMethodList temp_methods_;
Type *type_;
};
@@ -93,38 +127,38 @@ namespace osgIntrospection
/// classes. The instance creator associated to types created through
/// this reflector will create Value objects whose internal type is T.
template<typename T>
struct ValueReflector: public Reflector<T>
struct ValueReflector: Reflector<T>
{
typedef ValueReflector<T> inherited;
typedef ValueInstanceCreator<typename Reflector<T>::reflected_type> instance_creator_type;
ValueReflector(const std::string &name, const std::string &ns, const ReaderWriter *rw = 0)
: Reflector<T>(name, ns, rw)
ValueReflector(const std::string &name, const std::string &ns)
: Reflector<T>(name, ns, false)
{
setInstanceCreator(new ValueInstanceCreator<T>);
}
ValueReflector(const std::string &qname, const ReaderWriter *rw = 0)
: Reflector<T>(qname, rw)
ValueReflector(const std::string &qname)
: Reflector<T>(qname, false)
{
setInstanceCreator(new ValueInstanceCreator<T>);
}
};
/// This reflector is to be used to describe abstract types that can't
/// be created directly, and therefore can't have an InstanceCreator
/// object associated to them.
/// be instantiated. For this reason a DummyInstanceCreator is used in
/// order to avoid compiler errors.
template<typename T>
struct AbstractObjectReflector: public Reflector<T>
struct AbstractObjectReflector: Reflector<T>
{
typedef AbstractObjectReflector<T> inherited;
typedef DummyInstanceCreator<typename Reflector<T>::reflected_type> instance_creator_type;
AbstractObjectReflector(const std::string &name, const std::string &ns)
: Reflector<T>(name, ns, 0)
: Reflector<T>(name, ns, true)
{
}
AbstractObjectReflector(const std::string &qname)
: Reflector<T>(qname, 0)
: Reflector<T>(qname, true)
{
}
};
@@ -135,20 +169,19 @@ namespace osgIntrospection
/// created through this reflector will create Value objects whose
/// internal type is T*.
template<typename T>
struct ObjectReflector: public Reflector<T>
struct ObjectReflector: Reflector<T>
{
typedef ObjectReflector<T> inherited;
typedef ObjectInstanceCreator<typename Reflector<T>::reflected_type> instance_creator_type;
ObjectReflector(const std::string &name, const std::string &ns)
: Reflector<T>(name, ns, 0)
: Reflector<T>(name, ns, false)
{
setInstanceCreator(new InstanceCreator<T>);
}
ObjectReflector(const std::string &qname)
: Reflector<T>(qname, 0)
: Reflector<T>(qname, false)
{
setInstanceCreator(new InstanceCreator<T>);
}
};
@@ -157,16 +190,24 @@ namespace osgIntrospection
/// types that can be read and written from/to streams using the <<
/// and >> operators. A StdReaderWriter is assigned by default.
template<typename T>
struct StdValueReflector: public ValueReflector<T>
struct AtomicValueReflector: ValueReflector<T>
{
StdValueReflector(const std::string &name, const std::string &ns)
: ValueReflector<T>(name, ns, new StdReaderWriter<T>)
typedef typename ValueReflector<T>::instance_creator_type instance_creator_type;
AtomicValueReflector(const std::string &name, const std::string &ns)
: ValueReflector<T>(name, ns)
{
setReaderWriter(new StdReaderWriter<T>);
setComparator(new PartialOrderComparator<T>);
addConstructor(new TypedConstructorInfo0<T, instance_creator_type>(ParameterInfoList()));
}
StdValueReflector(const std::string &qname)
: ValueReflector<T>(qname, new StdReaderWriter<T>)
AtomicValueReflector(const std::string &qname)
: ValueReflector<T>(qname)
{
setReaderWriter(new StdReaderWriter<T>);
setComparator(new PartialOrderComparator<T>);
addConstructor(new TypedConstructorInfo0<T, instance_creator_type>(ParameterInfoList()));
}
};
@@ -174,29 +215,44 @@ namespace osgIntrospection
/// This reflector is a ValueReflector that should be used to define
/// enumerations. It assigns an EnumReaderWriter by default.
template<typename T>
struct EnumReflector: public ValueReflector<T>
struct EnumReflector: ValueReflector<T>
{
typedef EnumReflector<T> inherited;
typedef typename ValueReflector<T>::instance_creator_type instance_creator_type;
EnumReflector(const std::string &name, const std::string &ns)
: ValueReflector<T>(name, ns, new EnumReaderWriter<T>)
: ValueReflector<T>(name, ns)
{
setReaderWriter(new EnumReaderWriter<T>);
setComparator(new TotalOrderComparator<T>);
addConstructor(new TypedConstructorInfo0<T, instance_creator_type>(ParameterInfoList()));
}
EnumReflector(const std::string &qname)
: ValueReflector<T>(qname, new EnumReaderWriter<T>)
: ValueReflector<T>(qname)
{
}
setReaderWriter(new EnumReaderWriter<T>);
setComparator(new TotalOrderComparator<T>);
addConstructor(new TypedConstructorInfo0<T, instance_creator_type>(ParameterInfoList()));
}
};
/// This class allows to define the means for reflecting STL containers
/// such as std::deque and std::vector.
template<typename T, typename VT>
struct StdContainerReflector: ValueReflector<T>
struct StdVectorReflector: ValueReflector<T>
{
typedef typename ValueReflector<T>::instance_creator_type instance_creator_type;
struct Getter: PropertyGetter
{
virtual Value get(Value &instance, int i) const
{
T &ctr = variant_cast<T &>(instance);
return ctr.at(i);
}
virtual Value get(const Value &instance, int i) const
{
const T &ctr = variant_cast<const T &>(instance);
@@ -231,13 +287,189 @@ namespace osgIntrospection
}
};
StdContainerReflector(const std::string &name): ValueReflector<T>(name)
struct Remover: PropertyRemover
{
PropertyInfo *pi = new PropertyInfo(typeof(T), typeof(typename T::value_type), "Items", 0, 0, 0, 0);
virtual void remove(Value &instance, int i) const
{
T &ctr = variant_cast<T &>(instance);
typename T::iterator j=ctr.begin();
std::advance(j, i);
ctr.erase(j);
}
};
StdVectorReflector(const std::string &name): ValueReflector<T>(name)
{
addConstructor(new TypedConstructorInfo0<T, instance_creator_type>(ParameterInfoList()));
PropertyInfo *pi = new PropertyInfo(typeof(T), typeof(typename T::value_type), "Items", 0, 0, 0, 0, 0);
pi->addAttribute(new CustomPropertyGetAttribute(new Getter));
pi->addAttribute(new CustomPropertySetAttribute(new Setter));
pi->addAttribute(new CustomPropertyCountAttribute(new Counter));
pi->addAttribute(new CustomPropertyAddAttribute(new Adder));
pi->addAttribute(new CustomPropertyRemoveAttribute(new Remover));
if (typeid(VT).before(typeid(typename T::value_type)) ||
typeid(typename T::value_type).before(typeid(VT)))
{
pi->addAttribute(new PropertyTypeAttribute(typeof(VT)));
}
this->addProperty(pi);
}
};
/// This class allows to define the means for reflecting STL containers
/// such as std::set and std::multiset.
template<typename T, typename VT>
struct StdSetReflector: ValueReflector<T>
{
typedef typename ValueReflector<T>::instance_creator_type instance_creator_type;
struct Getter: PropertyGetter
{
virtual Value get(Value &instance, int i) const
{
T &ctr = variant_cast<T &>(instance);
typename T::iterator j=ctr.begin();
std::advance(j, i);
return *j;
}
virtual Value get(const Value &instance, int i) const
{
const T &ctr = variant_cast<const T &>(instance);
typename T::const_iterator j=ctr.begin();
std::advance(j, i);
return *j;
}
};
struct Counter: PropertyCounter
{
virtual int count(const Value &instance) const
{
const T &ctr = variant_cast<const T &>(instance);
return static_cast<int>(ctr.size());
}
};
struct Adder: PropertyAdder
{
virtual void add(Value &instance, const Value &v) const
{
T &ctr = variant_cast<T &>(instance);
ctr.insert(variant_cast<const typename T::value_type &>(v));
}
};
struct Remover: PropertyRemover
{
virtual void remove(Value &instance, int i) const
{
T &ctr = variant_cast<T &>(instance);
typename T::iterator j=ctr.begin();
std::advance(j, i);
ctr.erase(j);
}
};
StdSetReflector(const std::string &name): ValueReflector<T>(name)
{
addConstructor(new TypedConstructorInfo0<T, instance_creator_type>(ParameterInfoList()));
PropertyInfo *pi = new PropertyInfo(typeof(T), typeof(typename T::value_type), "Items", 0, 0, 0, 0, 0);
pi->addAttribute(new CustomPropertyGetAttribute(new Getter));
pi->addAttribute(new CustomPropertyCountAttribute(new Counter));
pi->addAttribute(new CustomPropertyAddAttribute(new Adder));
pi->addAttribute(new CustomPropertyRemoveAttribute(new Remover));
if (typeid(VT).before(typeid(typename T::value_type)) ||
typeid(typename T::value_type).before(typeid(VT)))
{
pi->addAttribute(new PropertyTypeAttribute(typeof(VT)));
}
this->addProperty(pi);
}
};
/// This class allows to define the means for reflecting STL containers
/// that cannot be indexed directly, such as std::list.
template<typename T, typename VT>
struct StdListReflector: ValueReflector<T>
{
typedef typename ValueReflector<T>::instance_creator_type instance_creator_type;
struct Getter: PropertyGetter
{
virtual Value get(Value &instance, int i) const
{
T &ctr = variant_cast<T &>(instance);
typename T::iterator j=ctr.begin();
std::advance(j, i);
return *j;
}
virtual Value get(const Value &instance, int i) const
{
const T &ctr = variant_cast<const T &>(instance);
typename T::const_iterator j=ctr.begin();
std::advance(j, i);
return *j;
}
};
struct Setter: PropertySetter
{
virtual void set(Value &instance, int i, const Value &v) const
{
T &ctr = variant_cast<T &>(instance);
typename T::iterator j=ctr.begin();
std::advance(j, i);
*j = variant_cast<const typename T::value_type &>(v);
}
};
struct Counter: PropertyCounter
{
virtual int count(const Value &instance) const
{
const T &ctr = variant_cast<const T &>(instance);
return static_cast<int>(ctr.size());
}
};
struct Adder: PropertyAdder
{
virtual void add(Value &instance, const Value &v) const
{
T &ctr = variant_cast<T &>(instance);
ctr.push_back(variant_cast<const typename T::value_type &>(v));
}
};
struct Remover: PropertyRemover
{
virtual void remove(Value &instance, int i) const
{
T &ctr = variant_cast<T &>(instance);
typename T::iterator j=ctr.begin();
std::advance(j, i);
ctr.erase(j);
}
};
StdListReflector(const std::string &name): ValueReflector<T>(name)
{
addConstructor(new TypedConstructorInfo0<T, instance_creator_type>(ParameterInfoList()));
PropertyInfo *pi = new PropertyInfo(typeof(T), typeof(typename T::value_type), "Items", 0, 0, 0, 0, 0);
pi->addAttribute(new CustomPropertyGetAttribute(new Getter));
pi->addAttribute(new CustomPropertySetAttribute(new Setter));
pi->addAttribute(new CustomPropertyCountAttribute(new Counter));
pi->addAttribute(new CustomPropertyAddAttribute(new Adder));
pi->addAttribute(new CustomPropertyRemoveAttribute(new Remover));
if (typeid(VT).before(typeid(typename T::value_type)) ||
typeid(typename T::value_type).before(typeid(VT)))
@@ -254,12 +486,25 @@ namespace osgIntrospection
template<typename T, typename IT, typename VT>
struct StdMapReflector: ValueReflector<T>
{
typedef typename ValueReflector<T>::instance_creator_type instance_creator_type;
typedef typename T::iterator iterator;
typedef typename T::const_iterator const_iterator;
typedef typename T::key_type key_type;
typedef typename T::mapped_type mapped_type;
struct Getter: PropertyGetter
{
virtual Value get(Value &instance, const ValueList &indices) const
{
T& ctr = variant_cast<T &>(instance);
const key_type& key = variant_cast<const key_type &>(indices.front());
iterator i = ctr.find(key);
if (i == ctr.end()) return Value();
return i->second;
}
virtual Value get(const Value &instance, const ValueList &indices) const
{
const T& ctr = variant_cast<const T &>(instance);
@@ -301,7 +546,7 @@ namespace osgIntrospection
return params_;
}
virtual void getIndexValueSet(int whichindex, const Value &instance, ValueList &values) const
virtual void getIndexValueSet(int /*whichindex*/, const Value &instance, ValueList &values) const
{
const T &ctr = variant_cast<const T &>(instance);
for (const_iterator i=ctr.begin();
@@ -315,7 +560,9 @@ namespace osgIntrospection
StdMapReflector(const std::string &name): ValueReflector<T>(name)
{
PropertyInfo *pi = new PropertyInfo(typeof(T), typeof(typename T::value_type), "Items", 0, 0);
addConstructor(new TypedConstructorInfo0<T, instance_creator_type>(ParameterInfoList()));
PropertyInfo *pi = new PropertyInfo(typeof(T), typeof(typename T::mapped_type), "Items", 0, 0);
pi->addAttribute(new CustomPropertyGetAttribute(new Getter));
pi->addAttribute(new CustomPropertySetAttribute(new Setter));
pi->addAttribute(new CustomIndexAttribute(new Indexer));
@@ -333,11 +580,23 @@ namespace osgIntrospection
template<typename T, typename PT1, typename PT2>
struct StdPairReflector: ValueReflector<T>
{
typedef typename ValueReflector<T>::instance_creator_type instance_creator_type;
struct Accessor: PropertyGetter, PropertySetter
{
Accessor(int i): i_(i) {}
virtual Value get(const Value &instance) const
{
switch (i_)
{
case 0: return variant_cast<T &>(instance).first;
case 1: return variant_cast<T &>(instance).second;
default: return Value();
}
}
virtual Value get(Value &instance) const
{
switch (i_)
{
@@ -363,6 +622,8 @@ namespace osgIntrospection
StdPairReflector(const std::string &name): ValueReflector<T>(name)
{
addConstructor(new TypedConstructorInfo0<T, instance_creator_type>(ParameterInfoList()));
PropertyInfo *pi1 = new PropertyInfo(typeof(T), typeof(typename T::first_type), "first", 0, 0);
pi1->addAttribute(new CustomPropertyGetAttribute(new Accessor(0)));
pi1->addAttribute(new CustomPropertySetAttribute(new Accessor(0)));
@@ -389,30 +650,31 @@ namespace osgIntrospection
// TEMPLATE METHODS
template<typename T>
Reflector<T>::Reflector(const std::string &name, const std::string &ns, const ReaderWriter *rw)
: type_(Reflection::registerOrReplaceType(typeid(T)))
Reflector<T>::Reflector(const std::string &name, const std::string &ns, bool abstract)
: type_(Reflection::getOrRegisterType(typeid(T), true))
{
type_->name_ = name;
type_->namespace_ = ns;
type_->rw_ = rw;
init();
if (!type_->name_.empty())
type_->aliases_.push_back(ns.empty()? purify(name): purify(ns+"::"+name));
else
{
type_->name_ = purify(name);
type_->namespace_ = purify(ns);
}
type_->is_abstract_ = abstract;
init();
}
template<typename T>
Reflector<T>::Reflector(const std::string &qname, const ReaderWriter *rw)
: type_(Reflection::registerOrReplaceType(typeid(T)))
Reflector<T>::Reflector(const std::string &qname, bool abstract)
: type_(Reflection::getOrRegisterType(typeid(T), true))
{
std::string::size_type p = qname.rfind("::");
if (p != std::string::npos)
{
type_->namespace_ = qname.substr(0, p);
type_->name_ = qname.substr(p+2);
}
else
{
type_->name_ = qname;
}
type_->rw_ = rw;
if (!type_->name_.empty())
type_->aliases_.push_back(purify(qname));
else
{
split_qualified_name(purify(qname), type_->name_, type_->namespace_);
}
type_->is_abstract_ = abstract;
init();
}
@@ -422,30 +684,73 @@ namespace osgIntrospection
// pointer type
if (!type_->pointed_type_)
{
Type *ptype = Reflection::registerOrReplaceType(typeid(T*));
Type *ptype = Reflection::getOrRegisterType(typeid(T*), true);
ptype->name_ = type_->name_;
ptype->namespace_ = type_->namespace_;
ptype->pointed_type_ = type_;
ptype->is_defined_ = true;
ptype->set_instance_creator(new ValueInstanceCreator<T*>);
ptype->cons_.push_back(new PtrConstructor(ptype));
ptype->rw_ = new PtrReaderWriter<T*>();
ptype->cmp_ = new TotalOrderComparator<T*>();
}
// const pointer type
if (!type_->pointed_type_ || !type_->is_const_)
{
Type *cptype = Reflection::registerOrReplaceType(typeid(const T*));
Type *cptype = Reflection::getOrRegisterType(typeid(const T*), true);
cptype->name_ = type_->name_;
cptype->namespace_ = type_->namespace_;
cptype->is_const_ = true;
cptype->pointed_type_ = type_;
cptype->is_defined_ = true;
cptype->set_instance_creator(new ValueInstanceCreator<const T*>);
cptype->cons_.push_back(new ConstPtrConstructor(cptype));
cptype->rw_ = new PtrReaderWriter<const T*>();
cptype->cmp_ = new TotalOrderComparator<const T*>();
}
type_->is_defined_ = true;
}
template<typename T>
std::string Reflector<T>::purify(const std::string &s)
{
std::string r(s);
while (true)
{
std::string::size_type p = r.find(" COMMA ");
if (p == std::string::npos) break;
r.replace(p, 7, ", ");
};
return r;
}
template<typename T>
void Reflector<T>::split_qualified_name(const std::string &q, std::string &n, std::string &ns)
{
int templ = 0;
std::string::size_type split_point = std::string::npos;
std::string::size_type j = 0;
for (std::string::const_iterator i=q.begin(); i!=q.end(); ++i, ++j)
{
if (*i == '<') ++templ;
if (*i == '>') --templ;
if (templ == 0)
{
if (*i == ':' && (i+1)!=q.end() && *(i+1) == ':')
split_point = j;
}
}
if (split_point == std::string::npos)
{
ns.clear();
n = q;
}
else
{
n = q.substr(split_point+2);
ns = q.substr(0, split_point);
}
}
template<typename T>
void Reflector<T>::addBaseType(const Type &type)
@@ -463,6 +768,13 @@ namespace osgIntrospection
template<typename T>
MethodInfo *Reflector<T>::addMethod(MethodInfo *mi)
{
for (TempMethodList::iterator i=temp_methods_.begin(); i!=temp_methods_.end(); ++i)
{
if (mi->overrides(*i))
return *i;
}
temp_methods_.push_back(mi);
type_->methods_.push_back(mi);
return mi;
}
@@ -483,9 +795,10 @@ namespace osgIntrospection
}
template<typename T>
void Reflector<T>::setInstanceCreator(const InstanceCreatorBase *icb)
ConstructorInfo *Reflector<T>::addConstructor(ConstructorInfo *ci)
{
type_->set_instance_creator(icb);
type_->cons_.push_back(ci);
return ci;
}
template<typename T>
@@ -517,6 +830,12 @@ namespace osgIntrospection
{
type_->rw_ = rw;
}
template<typename T>
void Reflector<T>::setComparator(const Comparator *cmp)
{
type_->cmp_ = cmp;
}
}

File diff suppressed because it is too large Load Diff

View File

@@ -10,6 +10,7 @@
#include <typeinfo>
#include <vector>
#include <map>
#include <algorithm>
namespace osgIntrospection
{
@@ -19,49 +20,18 @@ namespace osgIntrospection
class PropertyInfo;
class ParameterInfo;
class ReaderWriter;
class ConstructorInfo;
struct Comparator;
// typedefs for member info lists
typedef std::vector<const MethodInfo *> MethodInfoList;
typedef std::vector<const PropertyInfo *> PropertyInfoList;
typedef std::vector<const ParameterInfo *> ParameterInfoList;
typedef std::vector<const ConstructorInfo *> ConstructorInfoList;
// typedef for enum label map
typedef std::map<int, std::string> EnumLabelMap;
/// Base class for instance creators. An instance creator is
/// a lightweight object that creates a Value containing an
/// instance of some type. Every non-abstract Type object has
/// an instance creator that creates instances of that type.
/// This is an abstract interface, it must be derived to
/// provide the actual implementation of createInstance().
struct InstanceCreatorBase
{
virtual Value createInstance() const = 0;
};
/// This is an instance creator to be used with types that ought to
/// be created on the heap, for example all classes derived from
/// osg::Referenced.
template<typename T>
struct InstanceCreator: public InstanceCreatorBase
{
Value createInstance() const
{
return new T();
}
};
/// This is an instance creator to be used with types that can be
/// created on the stack (for example: int, std::string, or other
/// possibly small user-defined structs or classes).
template<typename T>
struct ValueInstanceCreator: public InstanceCreatorBase
{
Value createInstance() const
{
return T();
}
};
/// Objects of class Type are used to maintain information about
/// reflected types. They also provide a number of services, like
@@ -96,6 +66,10 @@ namespace osgIntrospection
/// name is formed by the namespace, if present, plus other modifiers
/// like 'const' and/or '*' (pointer) where applicable.
inline std::string getQualifiedName() const;
/// Returns true if either the fully-qualified name or one of the
/// name aliases match the given argument
inline bool matchesName(const std::string &name) const;
/// Returns the number of base types.
/// This number is zero if the type is not derived from any other
@@ -104,6 +78,12 @@ namespace osgIntrospection
/// Returns the i-th base type.
inline const Type &getBaseType(int i) const;
/// Returns the number of type name aliases.
inline int getNumAliases() const;
/// Returns the i-th name alias
const std::string &getAlias(int i) const;
/// Returns whether the reflected type is abstract.
inline bool isAbstract() const;
@@ -140,6 +120,9 @@ namespace osgIntrospection
/// Fills a list of properties that are either defined in this Type
/// or in inherited types.
void getAllProperties(PropertyInfoList &props) const;
/// Returns the list of constructors defined for this type.
inline const ConstructorInfoList &getConstructors() const;
/// Returns the list of methods defined for this type. The list
/// does not include methods inherited from base types.
@@ -153,6 +136,16 @@ namespace osgIntrospection
/// enumeration, an empty map is returned.
inline const EnumLabelMap &getEnumLabels() const;
/// Searches for a constructor that can be called with the given list
/// of arguments without raising type conversion errors. If more than
/// one constructors are suitable for calling, the best match is
/// returned.
const ConstructorInfo *getCompatibleConstructor(const ValueList &values) const;
/// Searches for a constructor whose parameters match exactly the given
/// list of parameter descriptions.
const ConstructorInfo *getConstructor(const ParameterInfoList &params) const;
/// Searches for a method that can be called with the given list of
/// arguments without raising type conversion errors. If more than
/// one method are suitable for calling, the best match is returned.
@@ -180,20 +173,26 @@ namespace osgIntrospection
/// Returns the instance of the reader/writer object assigned to
/// this type, if any. Otherwise it returns the null pointer.
inline const ReaderWriter *getReaderWriter() const;
/// Returns the instance of the comparator object assigned to
/// this type, if any. Otherwise it returns the null pointer.
inline const Comparator *getComparator() const;
/// Creates an instance of the reflected type. The returned Value
/// can be casted to T*, where T is the reflected type. If the type
/// is abstract, an exception is thrown.
Value createInstance(ValueList &args) const;
inline Value createInstance() const;
protected:
Type(const std::type_info &ti)
: ti_(ti),
is_const_(false),
is_abstract_(false),
pointed_type_(0),
is_defined_(false),
icb_(0),
rw_(0)
rw_(0),
cmp_(0)
{
}
@@ -202,14 +201,9 @@ namespace osgIntrospection
virtual void getInheritedProviders(CustomAttributeProviderList &providers) const;
void set_instance_creator(const InstanceCreatorBase *icb)
{
delete icb_;
icb_ = icb;
}
private:
template<typename C> friend class Reflector;
template<typename C> friend struct TypeNameAliasProxy;
friend class Reflection;
Type(const Type &copy): CustomAttributeProvider(copy), ti_(copy.ti_) {}
@@ -223,16 +217,21 @@ namespace osgIntrospection
TypeList base_;
bool is_const_;
bool is_abstract_;
const Type *pointed_type_;
ConstructorInfoList cons_;
PropertyInfoList props_;
MethodInfoList methods_;
EnumLabelMap labels_;
bool is_defined_;
const InstanceCreatorBase *icb_;
const ReaderWriter *rw_;
const Comparator *cmp_;
typedef std::vector<std::string> AliasList;
AliasList aliases_;
};
// OPERATORS
@@ -326,7 +325,7 @@ namespace osgIntrospection
inline bool Type::isAbstract() const
{
check_defined();
return icb_ == 0;
return is_abstract_;
}
inline bool Type::isAtomic() const
@@ -340,6 +339,12 @@ namespace osgIntrospection
check_defined();
return props_;
}
inline const ConstructorInfoList &Type::getConstructors() const
{
check_defined();
return cons_;
}
inline const MethodInfoList &Type::getMethods() const
{
@@ -389,6 +394,12 @@ namespace osgIntrospection
return rw_;
}
inline const Comparator *Type::getComparator() const
{
check_defined();
return cmp_;
}
inline const Type &Type::getBaseType(int i) const
{
check_defined();
@@ -397,10 +408,27 @@ namespace osgIntrospection
inline Value Type::createInstance() const
{
check_defined();
if (!icb_)
throw TypeIsAbstractException(ti_);
return icb_->createInstance();
ValueList args;
return createInstance(args);
}
inline int Type::getNumAliases() const
{
return static_cast<int>(aliases_.size());
}
inline const std::string &Type::getAlias(int i) const
{
return aliases_[i];
}
inline bool Type::matchesName(const std::string &name) const
{
if (getQualifiedName() == name)
return true;
if (std::find(aliases_.begin(), aliases_.end(), name) != aliases_.end())
return true;
return false;
}
}

View File

@@ -0,0 +1,26 @@
#ifndef OSGINTROSPECTION_TYPENAMEALIASPROXY_
#define OSGINTROSPECTION_TYPENAMEALIASPROXY_
#include <osgIntrospection/Type>
#include <osgIntrospection/Reflection>
#include <string>
#include <algorithm>
namespace osgIntrospection
{
template<typename C>
struct TypeNameAliasProxy
{
TypeNameAliasProxy(const std::string &name)
{
Type *type = Reflection::getOrRegisterType(typeid(C));
if (std::find(type->aliases_.begin(), type->aliases_.end(), name) == type->aliases_.end())
type->aliases_.push_back(name);
}
};
}
#endif

View File

@@ -0,0 +1,454 @@
#ifndef OSGINTROSPECTION_TYPEDCONSTRUCTORINFO_
#define OSGINTROSPECTION_TYPEDCONSTRUCTORINFO_
#include <osgIntrospection/ConstructorInfo>
#include <osgIntrospection/InstanceCreator>
#include <osgIntrospection/Utility>
namespace osgIntrospection
{
template<typename C, typename IC>
struct TypedConstructorInfo0: ConstructorInfo
{
TypedConstructorInfo0(const ParameterInfoList &plist)
: ConstructorInfo(typeof(C), plist)
{
}
Value createInstance(ValueList & /*args*/) const
{
return IC::create();
}
};
template<typename C, typename IC, typename P0>
struct TypedConstructorInfo1: ConstructorInfo
{
TypedConstructorInfo1(const ParameterInfoList &plist)
: ConstructorInfo(typeof(C), plist)
{
}
Value createInstance(ValueList &args) const
{
ValueList newargs(1);
convertArgument<P0>(args, newargs, getParameters(), 0);
return IC::template create<P0>(newargs[0]);
}
};
template<typename C, typename IC, typename P0, typename P1>
struct TypedConstructorInfo2: ConstructorInfo
{
TypedConstructorInfo2(const ParameterInfoList &plist)
: ConstructorInfo(typeof(C), plist)
{
}
Value createInstance(ValueList &args) const
{
ValueList newargs(2);
convertArgument<P0>(args, newargs, getParameters(), 0);
convertArgument<P1>(args, newargs, getParameters(), 1);
return IC::template create<P0, P1>(newargs[0], newargs[1]);
}
};
template<typename C, typename IC, typename P0, typename P1, typename P2>
struct TypedConstructorInfo3: ConstructorInfo
{
TypedConstructorInfo3(const ParameterInfoList &plist)
: ConstructorInfo(typeof(C), plist)
{
}
Value createInstance(ValueList &args) const
{
ValueList newargs(3);
convertArgument<P0>(args, newargs, getParameters(), 0);
convertArgument<P1>(args, newargs, getParameters(), 1);
convertArgument<P2>(args, newargs, getParameters(), 2);
return IC::template create<P0, P1, P2>(newargs[0], newargs[1], newargs[2]);
}
};
template<typename C, typename IC, typename P0, typename P1, typename P2, typename P3>
struct TypedConstructorInfo4: ConstructorInfo
{
TypedConstructorInfo4(const ParameterInfoList &plist)
: ConstructorInfo(typeof(C), plist)
{
}
Value createInstance(ValueList &args) const
{
ValueList newargs(4);
convertArgument<P0>(args, newargs, getParameters(), 0);
convertArgument<P1>(args, newargs, getParameters(), 1);
convertArgument<P2>(args, newargs, getParameters(), 2);
convertArgument<P3>(args, newargs, getParameters(), 3);
return IC::template create<P0, P1, P2, P3>(newargs[0], newargs[1], newargs[2], newargs[3]);
}
};
template<typename C, typename IC, typename P0, typename P1, typename P2, typename P3, typename P4>
struct TypedConstructorInfo5: ConstructorInfo
{
TypedConstructorInfo5(const ParameterInfoList &plist)
: ConstructorInfo(typeof(C), plist)
{
}
Value createInstance(ValueList &args) const
{
ValueList newargs(5);
convertArgument<P0>(args, newargs, getParameters(), 0);
convertArgument<P1>(args, newargs, getParameters(), 1);
convertArgument<P2>(args, newargs, getParameters(), 2);
convertArgument<P3>(args, newargs, getParameters(), 3);
convertArgument<P4>(args, newargs, getParameters(), 4);
return IC::template create<P0, P1, P2, P3, P4>(newargs[0], newargs[1], newargs[2], newargs[3], newargs[4]);
}
};
template<typename C, typename IC, typename P0, typename P1, typename P2, typename P3, typename P4, typename P5>
struct TypedConstructorInfo6: ConstructorInfo
{
TypedConstructorInfo6(const ParameterInfoList &plist)
: ConstructorInfo(typeof(C), plist)
{
}
Value createInstance(ValueList &args) const
{
ValueList newargs(6);
convertArgument<P0>(args, newargs, getParameters(), 0);
convertArgument<P1>(args, newargs, getParameters(), 1);
convertArgument<P2>(args, newargs, getParameters(), 2);
convertArgument<P3>(args, newargs, getParameters(), 3);
convertArgument<P4>(args, newargs, getParameters(), 4);
convertArgument<P5>(args, newargs, getParameters(), 5);
return IC::template create<P0, P1, P2, P3, P4, P5>(newargs[0], newargs[1], newargs[2], newargs[3], newargs[4], newargs[5]);
}
};
template<typename C, typename IC, typename P0, typename P1, typename P2, typename P3, typename P4, typename P5, typename P6>
struct TypedConstructorInfo7: ConstructorInfo
{
TypedConstructorInfo7(const ParameterInfoList &plist)
: ConstructorInfo(typeof(C), plist)
{
}
Value createInstance(ValueList &args) const
{
ValueList newargs(7);
convertArgument<P0>(args, newargs, getParameters(), 0);
convertArgument<P1>(args, newargs, getParameters(), 1);
convertArgument<P2>(args, newargs, getParameters(), 2);
convertArgument<P3>(args, newargs, getParameters(), 3);
convertArgument<P4>(args, newargs, getParameters(), 4);
convertArgument<P5>(args, newargs, getParameters(), 5);
convertArgument<P6>(args, newargs, getParameters(), 6);
return IC::template create<P0, P1, P2, P3, P4, P5, P6>(newargs[0], newargs[1], newargs[2], newargs[3], newargs[4], newargs[5], newargs[6]);
}
};
template<typename C, typename IC, typename P0, typename P1, typename P2, typename P3, typename P4, typename P5, typename P6, typename P7>
struct TypedConstructorInfo8: ConstructorInfo
{
TypedConstructorInfo8(const ParameterInfoList &plist)
: ConstructorInfo(typeof(C), plist)
{
}
Value createInstance(ValueList &args) const
{
ValueList newargs(8);
convertArgument<P0>(args, newargs, getParameters(), 0);
convertArgument<P1>(args, newargs, getParameters(), 1);
convertArgument<P2>(args, newargs, getParameters(), 2);
convertArgument<P3>(args, newargs, getParameters(), 3);
convertArgument<P4>(args, newargs, getParameters(), 4);
convertArgument<P5>(args, newargs, getParameters(), 5);
convertArgument<P6>(args, newargs, getParameters(), 6);
convertArgument<P7>(args, newargs, getParameters(), 7);
return IC::template create<P0, P1, P2, P3, P4, P5, P6, P7>(newargs[0], newargs[1], newargs[2], newargs[3], newargs[4], newargs[5], newargs[6], newargs[7]);
}
};
template<typename C, typename IC, typename P0, typename P1, typename P2, typename P3, typename P4, typename P5, typename P6, typename P7, typename P8>
struct TypedConstructorInfo9: ConstructorInfo
{
TypedConstructorInfo9(const ParameterInfoList &plist)
: ConstructorInfo(typeof(C), plist)
{
}
Value createInstance(ValueList &args) const
{
ValueList newargs(9);
convertArgument<P0>(args, newargs, getParameters(), 0);
convertArgument<P1>(args, newargs, getParameters(), 1);
convertArgument<P2>(args, newargs, getParameters(), 2);
convertArgument<P3>(args, newargs, getParameters(), 3);
convertArgument<P4>(args, newargs, getParameters(), 4);
convertArgument<P5>(args, newargs, getParameters(), 5);
convertArgument<P6>(args, newargs, getParameters(), 6);
convertArgument<P7>(args, newargs, getParameters(), 7);
convertArgument<P8>(args, newargs, getParameters(), 8);
return IC::template create<P0, P1, P2, P3, P4, P5, P6, P7, P8>(newargs[0], newargs[1], newargs[2], newargs[3], newargs[4], newargs[5], newargs[6], newargs[7], newargs[8]);
}
};
template<typename C, typename IC, typename P0, typename P1, typename P2, typename P3, typename P4, typename P5, typename P6, typename P7, typename P8, typename P9>
struct TypedConstructorInfo10: ConstructorInfo
{
TypedConstructorInfo10(const ParameterInfoList &plist)
: ConstructorInfo(typeof(C), plist)
{
}
Value createInstance(ValueList &args) const
{
ValueList newargs(10);
convertArgument<P0>(args, newargs, getParameters(), 0);
convertArgument<P1>(args, newargs, getParameters(), 1);
convertArgument<P2>(args, newargs, getParameters(), 2);
convertArgument<P3>(args, newargs, getParameters(), 3);
convertArgument<P4>(args, newargs, getParameters(), 4);
convertArgument<P5>(args, newargs, getParameters(), 5);
convertArgument<P6>(args, newargs, getParameters(), 6);
convertArgument<P7>(args, newargs, getParameters(), 7);
convertArgument<P8>(args, newargs, getParameters(), 8);
convertArgument<P9>(args, newargs, getParameters(), 9);
return IC::template create<P0, P1, P2, P3, P4, P5, P6, P7, P8, P9>(newargs[0], newargs[1], newargs[2], newargs[3], newargs[4], newargs[5], newargs[6], newargs[7], newargs[8], newargs[9]);
}
};
template<typename C, typename IC, typename P0, typename P1, typename P2, typename P3, typename P4, typename P5, typename P6, typename P7, typename P8, typename P9, typename P10>
struct TypedConstructorInfo11: ConstructorInfo
{
TypedConstructorInfo11(const ParameterInfoList &plist)
: ConstructorInfo(typeof(C), plist)
{
}
Value createInstance(ValueList &args) const
{
ValueList newargs(11);
convertArgument<P0>(args, newargs, getParameters(), 0);
convertArgument<P1>(args, newargs, getParameters(), 1);
convertArgument<P2>(args, newargs, getParameters(), 2);
convertArgument<P3>(args, newargs, getParameters(), 3);
convertArgument<P4>(args, newargs, getParameters(), 4);
convertArgument<P5>(args, newargs, getParameters(), 5);
convertArgument<P6>(args, newargs, getParameters(), 6);
convertArgument<P7>(args, newargs, getParameters(), 7);
convertArgument<P8>(args, newargs, getParameters(), 8);
convertArgument<P9>(args, newargs, getParameters(), 9);
convertArgument<P10>(args, newargs, getParameters(), 10);
return IC::template create<P0, P1, P2, P3, P4, P5, P6, P7, P8, P9, P10>(newargs[0], newargs[1], newargs[2], newargs[3], newargs[4], newargs[5], newargs[6], newargs[7], newargs[8], newargs[9], newargs[10]);
}
};
template<typename C, typename IC, typename P0, typename P1, typename P2, typename P3, typename P4, typename P5, typename P6, typename P7, typename P8, typename P9, typename P10, typename P11>
struct TypedConstructorInfo12: ConstructorInfo
{
TypedConstructorInfo12(const ParameterInfoList &plist)
: ConstructorInfo(typeof(C), plist)
{
}
Value createInstance(ValueList &args) const
{
ValueList newargs(12);
convertArgument<P0>(args, newargs, getParameters(), 0);
convertArgument<P1>(args, newargs, getParameters(), 1);
convertArgument<P2>(args, newargs, getParameters(), 2);
convertArgument<P3>(args, newargs, getParameters(), 3);
convertArgument<P4>(args, newargs, getParameters(), 4);
convertArgument<P5>(args, newargs, getParameters(), 5);
convertArgument<P6>(args, newargs, getParameters(), 6);
convertArgument<P7>(args, newargs, getParameters(), 7);
convertArgument<P8>(args, newargs, getParameters(), 8);
convertArgument<P9>(args, newargs, getParameters(), 9);
convertArgument<P10>(args, newargs, getParameters(), 10);
convertArgument<P11>(args, newargs, getParameters(), 11);
return IC::template create<P0, P1, P2, P3, P4, P5, P6, P7, P8, P9, P10, P11>(newargs[0], newargs[1], newargs[2], newargs[3], newargs[4], newargs[5], newargs[6], newargs[7], newargs[8], newargs[9], newargs[10], newargs[11]);
}
};
template<typename C, typename IC, typename P0, typename P1, typename P2, typename P3, typename P4, typename P5, typename P6, typename P7, typename P8, typename P9, typename P10, typename P11, typename P12>
struct TypedConstructorInfo13: ConstructorInfo
{
TypedConstructorInfo13(const ParameterInfoList &plist)
: ConstructorInfo(typeof(C), plist)
{
}
Value createInstance(ValueList &args) const
{
ValueList newargs(13);
convertArgument<P0>(args, newargs, getParameters(), 0);
convertArgument<P1>(args, newargs, getParameters(), 1);
convertArgument<P2>(args, newargs, getParameters(), 2);
convertArgument<P3>(args, newargs, getParameters(), 3);
convertArgument<P4>(args, newargs, getParameters(), 4);
convertArgument<P5>(args, newargs, getParameters(), 5);
convertArgument<P6>(args, newargs, getParameters(), 6);
convertArgument<P7>(args, newargs, getParameters(), 7);
convertArgument<P8>(args, newargs, getParameters(), 8);
convertArgument<P9>(args, newargs, getParameters(), 9);
convertArgument<P10>(args, newargs, getParameters(), 10);
convertArgument<P11>(args, newargs, getParameters(), 11);
convertArgument<P12>(args, newargs, getParameters(), 12);
return IC::template create<P0, P1, P2, P3, P4, P5, P6, P7, P8, P9, P10, P11, P12>(newargs[0], newargs[1], newargs[2], newargs[3], newargs[4], newargs[5], newargs[6], newargs[7], newargs[8], newargs[9], newargs[10], newargs[11], newargs[12]);
}
};
template<typename C, typename IC, typename P0, typename P1, typename P2, typename P3, typename P4, typename P5, typename P6, typename P7, typename P8, typename P9, typename P10, typename P11, typename P12, typename P13>
struct TypedConstructorInfo14: ConstructorInfo
{
TypedConstructorInfo14(const ParameterInfoList &plist)
: ConstructorInfo(typeof(C), plist)
{
}
Value createInstance(ValueList &args) const
{
ValueList newargs(14);
convertArgument<P0>(args, newargs, getParameters(), 0);
convertArgument<P1>(args, newargs, getParameters(), 1);
convertArgument<P2>(args, newargs, getParameters(), 2);
convertArgument<P3>(args, newargs, getParameters(), 3);
convertArgument<P4>(args, newargs, getParameters(), 4);
convertArgument<P5>(args, newargs, getParameters(), 5);
convertArgument<P6>(args, newargs, getParameters(), 6);
convertArgument<P7>(args, newargs, getParameters(), 7);
convertArgument<P8>(args, newargs, getParameters(), 8);
convertArgument<P9>(args, newargs, getParameters(), 9);
convertArgument<P10>(args, newargs, getParameters(), 10);
convertArgument<P11>(args, newargs, getParameters(), 11);
convertArgument<P12>(args, newargs, getParameters(), 12);
convertArgument<P13>(args, newargs, getParameters(), 13);
return IC::template create<P0, P1, P2, P3, P4, P5, P6, P7, P8, P9, P10, P11, P12, P13>(newargs[0], newargs[1], newargs[2], newargs[3], newargs[4], newargs[5], newargs[6], newargs[7], newargs[8], newargs[9], newargs[10], newargs[11], newargs[12], newargs[13]);
}
};
template<typename C, typename IC, typename P0, typename P1, typename P2, typename P3, typename P4, typename P5, typename P6, typename P7, typename P8, typename P9, typename P10, typename P11, typename P12, typename P13, typename P14>
struct TypedConstructorInfo15: ConstructorInfo
{
TypedConstructorInfo15(const ParameterInfoList &plist)
: ConstructorInfo(typeof(C), plist)
{
}
Value createInstance(ValueList &args) const
{
ValueList newargs(15);
convertArgument<P0>(args, newargs, getParameters(), 0);
convertArgument<P1>(args, newargs, getParameters(), 1);
convertArgument<P2>(args, newargs, getParameters(), 2);
convertArgument<P3>(args, newargs, getParameters(), 3);
convertArgument<P4>(args, newargs, getParameters(), 4);
convertArgument<P5>(args, newargs, getParameters(), 5);
convertArgument<P6>(args, newargs, getParameters(), 6);
convertArgument<P7>(args, newargs, getParameters(), 7);
convertArgument<P8>(args, newargs, getParameters(), 8);
convertArgument<P9>(args, newargs, getParameters(), 9);
convertArgument<P10>(args, newargs, getParameters(), 10);
convertArgument<P11>(args, newargs, getParameters(), 11);
convertArgument<P12>(args, newargs, getParameters(), 12);
convertArgument<P13>(args, newargs, getParameters(), 13);
convertArgument<P14>(args, newargs, getParameters(), 14);
return IC::template create<P0, P1, P2, P3, P4, P5, P6, P7, P8, P9, P10, P11, P12, P13, P14>(newargs[0], newargs[1], newargs[2], newargs[3], newargs[4], newargs[5], newargs[6], newargs[7], newargs[8], newargs[9], newargs[10], newargs[11], newargs[12], newargs[13], newargs[14]);
}
};
template<typename C, typename IC, typename P0, typename P1, typename P2, typename P3, typename P4, typename P5, typename P6, typename P7, typename P8, typename P9, typename P10, typename P11, typename P12, typename P13, typename P14, typename P15>
struct TypedConstructorInfo16: ConstructorInfo
{
TypedConstructorInfo16(const ParameterInfoList &plist)
: ConstructorInfo(typeof(C), plist)
{
}
Value createInstance(ValueList &args) const
{
ValueList newargs(16);
convertArgument<P0>(args, newargs, getParameters(), 0);
convertArgument<P1>(args, newargs, getParameters(), 1);
convertArgument<P2>(args, newargs, getParameters(), 2);
convertArgument<P3>(args, newargs, getParameters(), 3);
convertArgument<P4>(args, newargs, getParameters(), 4);
convertArgument<P5>(args, newargs, getParameters(), 5);
convertArgument<P6>(args, newargs, getParameters(), 6);
convertArgument<P7>(args, newargs, getParameters(), 7);
convertArgument<P8>(args, newargs, getParameters(), 8);
convertArgument<P9>(args, newargs, getParameters(), 9);
convertArgument<P10>(args, newargs, getParameters(), 10);
convertArgument<P11>(args, newargs, getParameters(), 11);
convertArgument<P12>(args, newargs, getParameters(), 12);
convertArgument<P13>(args, newargs, getParameters(), 13);
convertArgument<P14>(args, newargs, getParameters(), 14);
convertArgument<P15>(args, newargs, getParameters(), 15);
return IC::template create<P0, P1, P2, P3, P4, P5, P6, P7, P8, P9, P10, P11, P12, P13, P14, P15>(newargs[0], newargs[1], newargs[2], newargs[3], newargs[4], newargs[5], newargs[6], newargs[7], newargs[8], newargs[9], newargs[10], newargs[11], newargs[12], newargs[13], newargs[14], newargs[15]);
}
};
}
#endif

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,37 @@
#ifndef OSGINTROSPECTION_UTILITY_
#define OSGINTROSPECTION_UTILITY_
#include <osgIntrospection/Export>
#include <osgIntrospection/Value>
#include <osgIntrospection/ParameterInfo>
#include <osgIntrospection/MethodInfo>
#include <osgIntrospection/variant_cast>
#include <vector>
namespace osgIntrospection
{
bool OSGINTROSPECTION_EXPORT areParametersCompatible(const ParameterInfoList &pl1, const ParameterInfoList &pl2);
bool OSGINTROSPECTION_EXPORT areArgumentsCompatible(const ValueList &vl, const ParameterInfoList &pl, float &match);
template<typename T>
void convertArgument(ValueList &src, ValueList &dest, const ParameterInfoList &pl, int index)
{
if (index >= static_cast<int>(src.size()))
{
dest[index] = pl[index]->getDefaultValue();
}
else
{
Value &sv = src[index];
if (requires_conversion<T>(sv))
dest[index] = sv.convertTo(pl[index]->getParameterType());
else
dest[index].swap(sv);
}
}
}
#endif

View File

@@ -90,18 +90,24 @@ namespace osgIntrospection
/// getType().
inline const Type &getInstanceType() const;
/// Equality test operator. Returns true if the value passed
/// as parameter is equal to this instance. The compare() method
/// is used to perform the actual comparison.
inline bool operator==(const Value &other) const;
/// Equal to operator.
bool operator==(const Value &other) const;
/// Inequality test operator. Returns !operator==(other).
inline bool operator!=(const Value &other) const;
/// Conversion to bool operator. Returns true if the value is
/// not empty, false otherwise.
inline operator bool() const;
/// Less than or equal to operator.
bool operator<=(const Value &other) const;
/// Inequality test operator. Returns !operator==(other).
bool operator!=(const Value &other) const;
/// Greater than operator. Returns !operator<=(other).
bool operator>(const Value &other) const;
/// Less than operator. Returns !operator==(other) && operator<=(other).
bool operator<(const Value &other) const;
/// Greater than or equal to operator. Returns operator==(other) || !operator<=(other)
bool operator>=(const Value &other) const;
/// Tries to convert this instance to a Value of the given type.
/// The conversion is performed by rendering to a temporary stream
/// in the source format and trying to read back from the stream
@@ -126,15 +132,15 @@ namespace osgIntrospection
/// associated to it. If the conversion can't be completed, an
/// exception is thrown.
std::string toString() const;
/// Swaps the content of this Value with another Value
void swap(Value &v);
/// Compares two values for equality. Two empty values are considered
/// equal. If the two values' types are different, a conversion is
/// attempted and then the equality test is performed again.
static bool compare(const Value &v1, const Value &v2);
private:
// It's good to have friends!
template<typename T> friend T variant_cast(const Value &v);
template<typename T> friend bool requires_conversion(const Value &v);
template<typename T> friend T *extract_raw_data(Value &v);
template<typename T> friend const T *extract_raw_data(const Value &v);
@@ -188,8 +194,6 @@ namespace osgIntrospection
virtual const Type *type() const = 0;
// returns the actual pointed type if applicable
virtual const Type *ptype() const { return 0; }
// tests for equality
virtual bool equal(const Value &v) const = 0;
// returns whether the data is a null pointer
virtual bool nullptr() const = 0;
@@ -234,12 +238,6 @@ namespace osgIntrospection
return &typeof(static_cast<Instance<T> *>(inst_)->data_);
}
virtual bool equal(const Value &v) const
{
return static_cast<Instance<T> *>(static_cast<Instance_box<T> *>(v.inbox_)->inst_)->data_ ==
static_cast<Instance<T> *>(inst_)->data_;
}
virtual bool nullptr() const
{
return nullptr_;
@@ -292,12 +290,6 @@ namespace osgIntrospection
return &typeof(*static_cast<Instance<T> *>(inst_)->data_);
}
virtual bool equal(const Value &v) const
{
return static_cast<Instance<T> *>(static_cast<Instance_box<T> *>(v.inbox_)->inst_)->data_ ==
static_cast<Instance<T> *>(inst_)->data_;
}
virtual bool nullptr() const
{
return static_cast<Instance<T> *>(inst_)->data_ == 0;
@@ -312,8 +304,8 @@ namespace osgIntrospection
/// A vector of values.
typedef std::vector<Value> ValueList;
// INLINE METHODS
inline Value::Value()
@@ -375,21 +367,6 @@ namespace osgIntrospection
return *this;
}
inline bool Value::operator==(const Value &other) const
{
return compare(*this, other);
}
inline bool Value::operator!=(const Value &other) const
{
return !compare(*this, other);
}
inline Value::operator bool() const
{
return !isEmpty();
}
inline Value::~Value()
{
delete inbox_;

View File

@@ -37,6 +37,27 @@ namespace osgIntrospection
// try to convert v to type T and restart
return variant_cast<T>(v.convertTo(typeof(T)));
}
/// Returns true if the Value passed as parameter can't be casted to
/// the specified type without a (potentially slow) conversion.
/// Returns false otherwise.
template<typename T> bool requires_conversion(const Value &v)
{
// direct value
Value::Instance<T> *i = dynamic_cast<Value::Instance<T> *>(v.inbox_->inst_);
if (i) return false;
// reference to value
i = dynamic_cast<Value::Instance<T> *>(v.inbox_->ref_inst_);
if (i) return false;
// const reference to value
i = dynamic_cast<Value::Instance<T> *>(v.inbox_->const_ref_inst_);
if (i) return false;
return true;
}
/// Returns a typed pointer to the data contained in a Value
/// instance. If the value's type is not identical to type T,

View File

@@ -0,0 +1,15 @@
#include <osgIntrospection/ConstructorInfo>
using namespace osgIntrospection;
void ConstructorInfo::getInheritedProviders(CustomAttributeProviderList &providers) const
{
for (int i=0; i<decltype_.getNumBaseTypes(); ++i)
{
const ConstructorInfo *ci = decltype_.getBaseType(i).getConstructor(params_);
if (ci)
{
providers.push_back(ci);
}
}
}

View File

@@ -7,27 +7,28 @@
ABSTRACT_OBJECT_REFLECTOR(void)
STD_VALUE_REFLECTOR(char)
STD_VALUE_REFLECTOR(signed char)
STD_VALUE_REFLECTOR(unsigned char)
ATOMIC_VALUE_REFLECTOR(char)
ATOMIC_VALUE_REFLECTOR(signed char)
ATOMIC_VALUE_REFLECTOR(unsigned char)
STD_VALUE_REFLECTOR(int)
STD_VALUE_REFLECTOR(unsigned int)
STD_VALUE_REFLECTOR(long int)
STD_VALUE_REFLECTOR(long long int)
STD_VALUE_REFLECTOR(unsigned long int)
STD_VALUE_REFLECTOR(short int)
STD_VALUE_REFLECTOR(unsigned short int)
ATOMIC_VALUE_REFLECTOR(int)
ATOMIC_VALUE_REFLECTOR(unsigned int)
ATOMIC_VALUE_REFLECTOR(long int)
ATOMIC_VALUE_REFLECTOR(long long int)
ATOMIC_VALUE_REFLECTOR(unsigned long int)
ATOMIC_VALUE_REFLECTOR(unsigned long long int)
ATOMIC_VALUE_REFLECTOR(short int)
ATOMIC_VALUE_REFLECTOR(unsigned short int)
STD_VALUE_REFLECTOR(bool)
ATOMIC_VALUE_REFLECTOR(bool)
STD_VALUE_REFLECTOR(float)
ATOMIC_VALUE_REFLECTOR(float)
STD_VALUE_REFLECTOR(double)
STD_VALUE_REFLECTOR(long double)
ATOMIC_VALUE_REFLECTOR(double)
ATOMIC_VALUE_REFLECTOR(long double)
// STL types
STD_VALUE_REFLECTOR(std::string)
ATOMIC_VALUE_REFLECTOR(std::string)

View File

@@ -2,12 +2,14 @@ TOPDIR = ../..
include $(TOPDIR)/Make/makedefs
CXXFILES =\
ConstructorInfo.cpp\
CustomAttributeProvider.cpp\
DefaultReflectors.cpp\
MethodInfo.cpp\
PropertyInfo.cpp\
Reflection.cpp\
Type.cpp\
Utility.cpp\
Value.cpp\

View File

@@ -13,3 +13,48 @@ void MethodInfo::getInheritedProviders(CustomAttributeProviderList &providers) c
}
}
}
bool MethodInfo::overrides(const MethodInfo *other) const
{
if (isConst() != other->isConst()) return false;
if (decltype_ != other->decltype_) return false;
if (rtype_ != other->rtype_) return false;
if (name_ != other->name_) return false;
if (params_.size() != other->params_.size()) return false;
ParameterInfoList::const_iterator i=params_.begin();
ParameterInfoList::const_iterator j=other->params_.begin();
for (; i!=params_.end(); ++i, ++j)
{
if (&(*i)->getParameterType() != &(*j)->getParameterType())
return false;
}
return true;
/*
std::size_t num_fixed_1 = 0;
std::size_t num_optional_1 = 0;
for (ParameterInfoList::const_iterator i=params_.begin(); i!=params_.end(); ++i)
{
if ((*i)->getDefaultValue().isEmpty())
++num_fixed_1;
else
++num_optional_1;
}
std::size_t num_fixed_2 = 0;
std::size_t num_optional_2 = 0;
for (ParameterInfoList::const_iterator i=other->params_.begin(); i!=other->params_.end(); ++i)
{
if ((*i)->getDefaultValue().isEmpty())
++num_fixed_2;
else
++num_optional_2;
}
if (num_fixed_1 > num_fixed_2)
{
}
*/
}

View File

@@ -36,6 +36,26 @@ Value PropertyInfo::getValue(const Value &instance) const
return getm_->invoke(instance);
}
Value PropertyInfo::getValue(Value &instance) const
{
const PropertyTypeAttribute *pta = getAttribute<PropertyTypeAttribute>(false);
const CustomPropertyGetAttribute *cget = getAttribute<CustomPropertyGetAttribute>(false);
if (cget)
{
if (pta)
return cget->getGetter()->get(instance).convertTo(pta->getPropertyType());
return cget->getGetter()->get(instance);
}
if (!getm_)
throw PropertyAccessException(decltype_.getQualifiedName() + "::" + name_, PropertyAccessException::GET);
if (pta)
return getm_->invoke(instance).convertTo(pta->getPropertyType());
return getm_->invoke(instance);
}
void PropertyInfo::setValue(Value &instance, const Value &value) const
{
const CustomPropertySetAttribute *cset = getAttribute<CustomPropertySetAttribute>(false);
@@ -74,6 +94,26 @@ Value PropertyInfo::getIndexedValue(const Value &instance, ValueList &args) cons
return getm_->invoke(instance, args);
}
Value PropertyInfo::getIndexedValue(Value &instance, ValueList &args) const
{
const PropertyTypeAttribute *pta = getAttribute<PropertyTypeAttribute>(false);
const CustomPropertyGetAttribute *cget = getAttribute<CustomPropertyGetAttribute>(false);
if (cget)
{
if (pta)
return cget->getGetter()->get(instance, args).convertTo(pta->getPropertyType());
return cget->getGetter()->get(instance, args);
}
if (!getm_)
throw PropertyAccessException(decltype_.getQualifiedName() + "::" + name_, PropertyAccessException::IGET);
if (pta)
return getm_->invoke(instance, args).convertTo(pta->getPropertyType());
return getm_->invoke(instance, args);
}
void PropertyInfo::setIndexedValue(Value &instance, ValueList &args, const Value &value) const
{
const CustomPropertySetAttribute *cset = getAttribute<CustomPropertySetAttribute>(false);
@@ -125,6 +165,29 @@ Value PropertyInfo::getArrayItem(const Value &instance, int i) const
return getm_->invoke(instance, args);
}
Value PropertyInfo::getArrayItem(Value &instance, int i) const
{
const PropertyTypeAttribute *pta = getAttribute<PropertyTypeAttribute>(false);
const CustomPropertyGetAttribute *cget = getAttribute<CustomPropertyGetAttribute>(false);
if (cget)
{
if (pta)
return cget->getGetter()->get(instance, i).convertTo(pta->getPropertyType());
return cget->getGetter()->get(instance, i);
}
if (!getm_)
throw PropertyAccessException(decltype_.getQualifiedName() + "::" + name_, PropertyAccessException::AGET);
ValueList args;
args.push_back(i);
if (pta)
return getm_->invoke(instance, args).convertTo(pta->getPropertyType());
return getm_->invoke(instance, args);
}
void PropertyInfo::setArrayItem(Value &instance, int i, const Value &value) const
{
const CustomPropertySetAttribute *cset = getAttribute<CustomPropertySetAttribute>(false);
@@ -160,6 +223,23 @@ void PropertyInfo::addArrayItem(Value &instance, const Value &value) const
addm_->invoke(instance, args);
}
void PropertyInfo::removeArrayItem(Value &instance, int i) const
{
const CustomPropertyRemoveAttribute *crem = getAttribute<CustomPropertyRemoveAttribute>(false);
if (crem)
{
crem->getRemover()->remove(instance, i);
return;
}
if (!remm_)
throw PropertyAccessException(decltype_.getQualifiedName() + "::" + name_, PropertyAccessException::REMOVE);
ValueList args;
args.push_back(i);
remm_->invoke(instance, args);
}
Value PropertyInfo::getDefaultValue() const
{
if (isArray() || isIndexed()) return Value();

View File

@@ -48,8 +48,13 @@ 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);
}
@@ -66,13 +71,28 @@ Type *Reflection::registerType(const std::type_info &ti)
return type.release();
}
Type *Reflection::registerOrReplaceType(const std::type_info &ti)
Type *Reflection::getOrRegisterType(const std::type_info &ti, bool replace_if_defined)
{
TypeMap &tm = getOrCreateStaticData().typemap;
TypeMap::iterator i = tm.find(&ti);
if (i != tm.end())
return new (i->second) Type(ti);
{
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);
}

View File

@@ -4,6 +4,9 @@
#include <osgIntrospection/PropertyInfo>
#include <osgIntrospection/MethodInfo>
#include <osgIntrospection/ReaderWriter>
#include <osgIntrospection/Utility>
#include <osgIntrospection/ConstructorInfo>
#include <osgIntrospection/Comparator>
#include <iterator>
#include <algorithm>
@@ -13,21 +16,25 @@ using namespace osgIntrospection;
namespace
{
struct MethodMatch
template<typename T>
struct ObjectMatch
{
int list_pos;
int exact_args;
const MethodInfo *method;
float match;
const T *object;
bool operator < (const MethodMatch &m) const
bool operator < (const ObjectMatch &m) const
{
if (exact_args > m.exact_args) return true;
if (exact_args < m.exact_args) return false;
if (match > m.match) return true;
if (match < m.match) return false;
if (list_pos < m.list_pos) return true;
return false;
}
};
typedef ObjectMatch<MethodInfo> MethodMatch;
typedef ObjectMatch<ConstructorInfo> ConstructorMatch;
}
Type::~Type()
@@ -36,8 +43,11 @@ Type::~Type()
delete *i;
for (MethodInfoList::const_iterator i=methods_.begin(); i!=methods_.end(); ++i)
delete *i;
delete icb_;
for (ConstructorInfoList::const_iterator i=cons_.begin(); i!=cons_.end(); ++i)
delete *i;
delete rw_;
delete cmp_;
}
bool Type::isSubclassOf(const Type &type) const
@@ -76,44 +86,22 @@ const MethodInfo *Type::getCompatibleMethod(const std::string &name, const Value
const MethodInfo *mi = *j;
if (mi->getName().compare(name) == 0)
{
const ParameterInfoList &other_params = mi->getParameters();
if (values.size() == other_params.size())
{
if (values.empty())
return mi;
ParameterInfoList::const_iterator i1 = other_params.begin();
ValueList::const_iterator i2 = values.begin();
bool candidate = true;
int exact_args = 0;
for (; i1<other_params.end(); ++i1, ++i2)
{
if ((*i1)->getParameterType() != i2->getType())
{
if (i2->tryConvertTo((*i1)->getParameterType()).isEmpty())
{
candidate = false;
break;
}
}
else
++exact_args;
}
if (candidate)
{
MethodMatch mm;
mm.list_pos = pos;
mm.exact_args = exact_args;
mm.method = mi;
matches.push_back(mm);
}
}
float match;
if (areArgumentsCompatible(values, mi->getParameters(), match))
{
MethodMatch mm;
mm.list_pos = pos;
mm.match = match;
mm.object = mi;
matches.push_back(mm);
}
}
}
if (!matches.empty())
{
std::sort(matches.begin(), matches.end());
return matches.front().method;
return matches.front().object;
}
return 0;
@@ -124,28 +112,13 @@ const MethodInfo *Type::getMethod(const std::string &name, const ParameterInfoLi
check_defined();
for (MethodInfoList::const_iterator j=methods_.begin(); j!=methods_.end(); ++j)
{
const MethodInfo &mi = **j;
if (mi.getName().compare(name) == 0)
const MethodInfo *mi = *j;
if (mi->getName().compare(name) == 0)
{
const ParameterInfoList &other_params = mi.getParameters();
if (params.size() == other_params.size())
{
if (params.empty())
return &mi;
ParameterInfoList::const_iterator i1 = params.begin();
ParameterInfoList::const_iterator i2 = other_params.begin();
for (; i1<params.end(); ++i1, ++i2)
{
const ParameterInfo &p1 = **i1;
const ParameterInfo &p2 = **i2;
if (p1.getParameterType() == p2.getParameterType() &&
p1.getAttributes() == p2.getAttributes() &&
p1.getPosition() == p2.getPosition())
{
return &mi;
}
}
}
if (areParametersCompatible(params, mi->getParameters()))
{
return mi;
}
}
}
@@ -172,28 +145,13 @@ const PropertyInfo *Type::getProperty(const std::string &name, const Type &ptype
check_defined();
for (PropertyInfoList::const_iterator i=props_.begin(); i!=props_.end(); ++i)
{
const PropertyInfo &pi = **i;
if (pi.getName() == name && pi.getPropertyType() == ptype)
const PropertyInfo *pi = *i;
if (pi->getName() == name && pi->getPropertyType() == ptype)
{
const ParameterInfoList &other_indices = pi.getIndexParameters();
if (indices.size() == other_indices.size())
{
if (indices.empty())
return &pi;
ParameterInfoList::const_iterator i1 = indices.begin();
ParameterInfoList::const_iterator i2 = other_indices.begin();
for (; i1<indices.end(); ++i1, ++i2)
{
const ParameterInfo &p1 = **i1;
const ParameterInfo &p2 = **i2;
if (p1.getParameterType() == p2.getParameterType() &&
p1.getAttributes() == p2.getAttributes() &&
p1.getPosition() == p2.getPosition())
{
return &pi;
}
}
}
if (areParametersCompatible(indices, pi->getIndexParameters()))
{
return pi;
}
}
}
@@ -225,7 +183,6 @@ Value Type::invokeMethod(const std::string &name, Value &instance, ValueList &ar
return mi->invoke(instance, args);
}
void Type::getAllProperties(PropertyInfoList &props) const
{
check_defined();
@@ -245,3 +202,58 @@ void Type::getAllMethods(MethodInfoList &methods) const
(*i)->getAllMethods(methods);
}
}
Value Type::createInstance(ValueList &args) const
{
if (isAbstract())
throw TypeIsAbstractException(ti_);
const ConstructorInfo *ci = getCompatibleConstructor(args);
if (!ci)
throw ConstructorNotFoundException(ti_);
return ci->createInstance(args);
}
const ConstructorInfo *Type::getCompatibleConstructor(const ValueList &values) const
{
check_defined();
typedef std::vector<ConstructorMatch> MatchList;
MatchList matches;
int pos = 0;
for (ConstructorInfoList::const_iterator j=cons_.begin(); j!=cons_.end(); ++j, ++pos)
{
float match;
if (areArgumentsCompatible(values, (*j)->getParameters(), match))
{
ConstructorMatch mm;
mm.list_pos = pos;
mm.match = match;
mm.object = *j;
matches.push_back(mm);
}
}
if (!matches.empty())
{
std::sort(matches.begin(), matches.end());
return matches.front().object;
}
return 0;
}
const ConstructorInfo *Type::getConstructor(const ParameterInfoList &params) const
{
check_defined();
for (ConstructorInfoList::const_iterator j=cons_.begin(); j!=cons_.end(); ++j)
{
if (areParametersCompatible(params, (*j)->getParameters()))
return *j;
}
return 0;
}

View File

@@ -0,0 +1,71 @@
#include <osgIntrospection/Utility>
using namespace osgIntrospection;
bool osgIntrospection::areParametersCompatible(const ParameterInfoList &pl1, const ParameterInfoList &pl2)
{
if (pl1.size() == pl2.size())
{
if (pl1.empty())
return true;
ParameterInfoList::const_iterator i1 = pl1.begin();
ParameterInfoList::const_iterator i2 = pl2.begin();
for (; i1<pl1.end(); ++i1, ++i2)
{
const ParameterInfo &p1 = **i1;
const ParameterInfo &p2 = **i2;
if (p1.getParameterType() == p2.getParameterType() &&
p1.getAttributes() == p2.getAttributes())
{
return true;
}
}
}
return false;
}
bool osgIntrospection::areArgumentsCompatible(const ValueList &vl, const ParameterInfoList &pl, float &match)
{
if (pl.empty())
{
if (vl.empty())
{
match = 1.0f;
return true;
}
return false;
}
ParameterInfoList::const_iterator i1 = pl.begin();
ValueList::const_iterator i2 = vl.begin();
int exact_args = 0;
for (; i1<pl.end(); ++i1)
{
if (i2 == vl.end())
{
if ((*i1)->getDefaultValue().isEmpty())
return false;
continue;
}
if ((*i1)->getParameterType() != i2->getType())
{
if (i2->tryConvertTo((*i1)->getParameterType()).isEmpty())
{
return false;
}
}
else
++exact_args;
++i2;
}
match = static_cast<float>(exact_args) / pl.size();
return true;
}

View File

@@ -2,6 +2,7 @@
#include <osgIntrospection/Type>
#include <osgIntrospection/Exceptions>
#include <osgIntrospection/ReaderWriter>
#include <osgIntrospection/Comparator>
#include <sstream>
#include <memory>
@@ -71,17 +72,115 @@ std::string Value::toString() const
throw StreamingNotSupportedException(StreamingNotSupportedException::ANY, type_->getStdTypeInfo());
}
bool Value::compare(const Value &v1, const Value &v2)
{
if (v1.isEmpty() && v2.isEmpty()) return true;
if (v1.isEmpty() || v2.isEmpty()) return false;
if (v1.type_ == v2.type_) return v1.inbox_->equal(v2);
Value temp(v2.convertTo(v1.getType()));
return compare(v1, temp);
}
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_->getStdTypeInfo());
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_->getStdTypeInfo());
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_->getStdTypeInfo());
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_->getStdTypeInfo());
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);
}