2006-07-18 23:21:48 +08:00
|
|
|
/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield
|
2005-04-29 18:06:50 +08:00
|
|
|
*
|
|
|
|
* This library is open source and may be redistributed and/or modified under
|
|
|
|
* the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
|
|
|
|
* (at your option) any later version. The full license is in LICENSE file
|
|
|
|
* included with this distribution, and on the openscenegraph.org website.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* OpenSceneGraph Public License for more details.
|
|
|
|
*/
|
|
|
|
//osgIntrospection - Copyright (C) 2005 Marco Jez
|
|
|
|
|
2004-12-09 13:28:20 +08:00
|
|
|
#ifndef OSGINTROSPECTION_REFLECTION_
|
|
|
|
#define OSGINTROSPECTION_REFLECTION_
|
|
|
|
|
|
|
|
#include <osgIntrospection/Export>
|
2007-02-13 01:14:46 +08:00
|
|
|
#include <osgIntrospection/ExtendedTypeInfo>
|
2004-12-09 13:28:20 +08:00
|
|
|
|
|
|
|
#include <typeinfo>
|
|
|
|
#include <map>
|
2005-04-08 04:00:17 +08:00
|
|
|
#include <vector>
|
2004-12-09 13:28:20 +08:00
|
|
|
|
|
|
|
/// This macro emulates the behavior of the standard typeid operator,
|
|
|
|
/// returning the Type object associated to the type of the given
|
|
|
|
/// expression.
|
2007-02-15 23:03:30 +08:00
|
|
|
#define typeof(type) osgIntrospection::Reflection::getType(extended_typeid< type >())
|
2007-03-16 22:28:27 +08:00
|
|
|
#define typeofvalue(val) osgIntrospection::Reflection::getType(osgIntrospection::ExtendedTypeInfo(typeid(val), false, false))
|
2004-12-09 13:28:20 +08:00
|
|
|
|
|
|
|
namespace osgIntrospection
|
|
|
|
{
|
|
|
|
|
2005-03-14 17:28:31 +08:00
|
|
|
class Type;
|
2005-04-08 04:00:17 +08:00
|
|
|
struct Converter;
|
|
|
|
|
2005-04-29 19:19:58 +08:00
|
|
|
typedef std::vector<const Converter* > ConverterList;
|
2005-03-14 17:28:31 +08:00
|
|
|
|
2007-02-13 01:14:46 +08:00
|
|
|
/// A map of types, indexed by their associated ExtendedTypeInfo
|
|
|
|
/// structure.
|
|
|
|
typedef std::map<ExtendedTypeInfo, Type*> TypeMap;
|
2005-03-14 17:28:31 +08:00
|
|
|
|
|
|
|
|
|
|
|
/// This class provides basic reflection services such as registration
|
|
|
|
/// of new types and queries on the global type map.
|
|
|
|
class OSGINTROSPECTION_EXPORT Reflection
|
|
|
|
{
|
|
|
|
public:
|
2007-02-13 01:14:46 +08:00
|
|
|
/// Returns the Type object associated to the given
|
|
|
|
/// ExtendedTypeInfo structure. If the type hasn't been created
|
|
|
|
/// yet it is automatically created and added to the global type
|
|
|
|
/// map. Please note that such type will have the status of
|
2005-03-14 17:28:31 +08:00
|
|
|
/// "declared", you still need to give details about it through
|
|
|
|
/// a Reflector class before you can query it.
|
2007-02-13 01:14:46 +08:00
|
|
|
static const Type& getType(const ExtendedTypeInfo &ti);
|
2005-03-14 17:28:31 +08:00
|
|
|
|
|
|
|
/// Finds a Type object given its qualified name, which must
|
|
|
|
/// be identical to the qualified name returned by that Type's
|
|
|
|
/// getQualifiedName() method. If the type hasn't been created
|
|
|
|
/// yet, an exception is thrown.
|
2005-04-29 19:19:58 +08:00
|
|
|
static const Type& getType(const std::string& qname);
|
2005-03-14 17:28:31 +08:00
|
|
|
|
|
|
|
/// Returns the global map of types.
|
2005-04-29 19:19:58 +08:00
|
|
|
static const TypeMap& getTypes();
|
2005-03-14 17:28:31 +08:00
|
|
|
|
|
|
|
/// Return the Type object associated to the C++ type 'void'.
|
|
|
|
/// This is a shortcut for typeof(void), which may be slow if
|
|
|
|
/// the type map is large.
|
2005-04-29 19:19:58 +08:00
|
|
|
static const Type& type_void();
|
2005-04-08 04:00:17 +08:00
|
|
|
|
2005-04-29 19:19:58 +08:00
|
|
|
static const Converter* getConverter(const Type& source, const Type& dest);
|
|
|
|
static bool getConversionPath(const Type& source, const Type& dest, ConverterList& conv);
|
2005-03-14 17:28:31 +08:00
|
|
|
|
|
|
|
private:
|
|
|
|
template<typename C> friend class Reflector;
|
2005-04-04 21:50:07 +08:00
|
|
|
template<typename C> friend struct TypeNameAliasProxy;
|
2005-04-08 04:00:17 +08:00
|
|
|
friend struct ConverterProxy;
|
2005-03-14 17:28:31 +08:00
|
|
|
|
|
|
|
struct StaticData
|
|
|
|
{
|
|
|
|
TypeMap typemap;
|
2005-04-29 19:19:58 +08:00
|
|
|
const Type* type_void;
|
2005-04-08 04:00:17 +08:00
|
|
|
|
2005-04-29 19:19:58 +08:00
|
|
|
typedef std::map<const Type* , const Converter* > ConverterMap;
|
|
|
|
typedef std::map<const Type* , ConverterMap> ConverterMapMap;
|
2005-04-08 04:00:17 +08:00
|
|
|
ConverterMapMap convmap;
|
|
|
|
|
|
|
|
~StaticData();
|
2005-03-14 17:28:31 +08:00
|
|
|
};
|
|
|
|
|
2005-04-29 19:19:58 +08:00
|
|
|
static StaticData& getOrCreateStaticData();
|
2007-02-13 01:14:46 +08:00
|
|
|
static Type* registerType(const ExtendedTypeInfo &ti);
|
|
|
|
static Type* getOrRegisterType(const ExtendedTypeInfo &ti, bool replace_if_defined = false);
|
2005-04-29 19:19:58 +08:00
|
|
|
static void registerConverter(const Type& source, const Type& dest, const Converter* cvt);
|
2005-03-14 17:28:31 +08:00
|
|
|
|
|
|
|
private:
|
2005-04-29 19:19:58 +08:00
|
|
|
static bool accum_conv_path(const Type& source, const Type& dest, ConverterList& conv, std::vector<const Type* > &chain);
|
|
|
|
static StaticData* _static_data;
|
2005-03-14 17:28:31 +08:00
|
|
|
};
|
2004-12-09 13:28:20 +08:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|