虽然C++11引入了RTTI、Metaprogramming 等技术,但C++在Reflection编程方面依旧功能有限。在社区上,RTTR则提供了一套C++编写的反射库,补充了C++在Reflection方面的缺陷。
零、环境
操作系统 | Windows 11 |
Visual Studio | Visual Studio Community 2022 |
CMake | CMake 3.24.2 |
Doxygen | Doxygen-1.9.8 |
一、下载源码
从GitHub拉取RTTR代码:
git clone https://github.com/rttrorg/rttr.git
二、编译
按照下表配置CMake,并完成构建与生成,
Where is the source code | Windows 11 |
Where to build the binaries | Visual Studio Community 2022 |
CMAKE_INSTALL_PREFIX | CMake 3.24.2 |
打开rttr.sln,构建"ALL_BUILD"完成RTTR编译;构建"INSTALL"完成RTTR安装。
三、RTTR源码分析
虽然各种反射机制的具体实现有所不同,但大体思路还是一致的:
- 生成类型元数据
类型元数据包括构造函数、属性、方法等,类型元数据可以在编译阶段自动生成,也可以在运行阶段手动注册;
- 执行反射
需要执行反射操作时,依据存储的反射信息访问对应的地址。
在RTTR中,类型元数据存储在rttr::detail::class_data、rttr::detail::type_data中,
struct RTTR_LOCAL class_data
{
class_data(get_derived_info_func func, std::vector<type> nested_types)
: m_derived_info_func(func),
m_nested_types(nested_types),
m_dtor(create_invalid_item<destructor>())
{}
get_derived_info_func m_derived_info_func;
std::vector<type> m_base_types;
std::vector<type> m_derived_types;
std::vector<rttr_cast_func> m_conversion_list;
std::vector<property> m_properties;
std::vector<method> m_methods;
std::vector<constructor> m_ctors;
std::vector<type> m_nested_types;
destructor m_dtor;
};
struct RTTR_LOCAL type_data
{
type_data* raw_type_data;
type_data* wrapped_type;
type_data* array_raw_type;
std::string name;
string_view type_name;
std::size_t get_sizeof;
std::size_t get_pointer_dimension;
impl::create_variant_func create_variant;
impl::get_base_types_func get_base_types; // FIXME: this info should not be stored, its just temporarily,
// thats why we store it as function pointer
enumeration_wrapper_base* enum_wrapper;
impl::get_metadata_func get_metadata;
impl::create_wrapper_func create_wrapper;
impl::get_class_data_func get_class_data;
bool is_valid;
RTTR_FORCE_INLINE bool type_trait_value(type_trait_infos type_trait) const RTTR_NOEXCEPT { return m_type_traits.test(static_cast<std::size_t>(type_trait)); }
type_traits m_type_traits;
};
3.1 生成rttr::detials::type_data
当第一次调用type::get()函数时,会生成对应的类型元数据,
template<typename T>
RTTR_INLINE type type::get() RTTR_NOEXCEPT
{
using non_ref_type = typename std::remove_cv<typename std::remove_reference<T>::type>::type;
return detail::create_or_get_type<non_ref_type>();
}
template<typename T>
RTTR_LOCAL RTTR_INLINE enable_if_t<is_complete_type<T>::value, type>
create_or_get_type() RTTR_NOEXCEPT
{
// when you get an error here, then the type was not completely defined
// (a forward declaration is not enough because base_classes will not be found)
using type_must_be_complete = char[ sizeof(T) ? 1: -1 ];
(void) sizeof(type_must_be_complete);
static const type val = create_type(get_registration_manager().add_item(make_type_data<T>()));
return val;
}
从中可以看出,rttr::type实际上时对类型数据的一种引用。
3.2 注册类型信息数据
rttr::registration实际上时借助于函数对象rttr::registration::bind完成构造函数的注册,
template<typename Class_Type>
template<typename F, typename acc_level, typename Tp>
registration::bind<detail::ctor_func, Class_Type, F, acc_level> registration::class_<Class_Type>::constructor(F func, acc_level level)
{
using namespace detail;
static_assert(is_functor<F>::value,
"No valid accessor for invoking the constructor provided!");
static_assert(std::is_same<return_func, typename method_type<F>::type>::value,
"For creating this 'class type', please provide a function pointer or std::function with a return value.");
return {create_if_empty(m_reg_exec), func};
}
template<typename Class_Type, typename F, typename acc_level>
class registration::bind<detail::ctor_func, Class_Type, F, acc_level> : public registration::class_<Class_Type>
{
//...
public:
bind(const std::shared_ptr<detail::registration_executer>& reg_exec, F func)
: registration::class_<Class_Type>(reg_exec), m_reg_exec(reg_exec), m_func(func)
{
m_reg_exec->add_registration_func(this);
}
template<typename... Args>
registration::class_<Class_Type> operator()(Args&&... args)
{
m_ctor = create_custom_constructor(m_func, std::forward<Args>(args)...);
return registration::class_<Class_Type>(m_reg_exec);
}
//...
}
registration_executer::~registration_executer()
{
for (auto&& item : m_list)
{
item.second();
}
}
至于,属性、方法等类型元数据的注册,原理类似,可参照对应代码。
3.3 生成对象
rttr::type调用存储的构造器,完成对象的实例化。
variant type::create(vector<argument> args) const
{
auto& ctors = m_type_data->get_class_data().m_ctors;
for (const auto& ctor : ctors)
{
if (detail::compare_with_arg_list::compare(ctor.get_parameter_infos(), args))
return ctor.invoke_variadic(std::move(args));
}
return variant();
}
四、扩展:Reflection的其他实现
4.1 MFC
在MFC中,DECLARE_DYNCREATE/IMPLEMENT_DYNCREATE宏用于声明、定义反射相关元数据,
#define DECLARE_DYNAMIC(class_name) \
public: \
static const CRuntimeClass class##class_name; \
virtual CRuntimeClass* GetRuntimeClass() const; \
#define DECLARE_DYNCREATE(class_name) \
DECLARE_DYNAMIC(class_name) \
static CObject* PASCAL CreateObject();
#define IMPLEMENT_RUNTIMECLASS(class_name, base_class_name, wSchema, pfnNew, class_init) \
AFX_COMDAT const CRuntimeClass class_name::class##class_name = { \
#class_name, sizeof(class class_name), wSchema, pfnNew, \
RUNTIME_CLASS(base_class_name), NULL, class_init }; \
CRuntimeClass* class_name::GetRuntimeClass() const \
{ return RUNTIME_CLASS(class_name); }
#define IMPLEMENT_DYNAMIC(class_name, base_class_name) \
IMPLEMENT_RUNTIMECLASS(class_name, base_class_name, 0xFFFF, NULL, NULL)
#define IMPLEMENT_DYNCREATE(class_name, base_class_name) \
CObject* PASCAL class_name::CreateObject() \
{ return new class_name; } \
IMPLEMENT_RUNTIMECLASS(class_name, base_class_name, 0xFFFF, \
class_name::CreateObject, NULL)
从中,可以看到,反射元数据实际上时存放到了CRuntimeClass静态变量中,
struct CRuntimeClass
{
// Attributes
LPCSTR m_lpszClassName;
int m_nObjectSize;
UINT m_wSchema; // schema number of the loaded class
CObject* (PASCAL* m_pfnCreateObject)(); // NULL => abstract class
#ifdef _AFXDLL
CRuntimeClass* (PASCAL* m_pfnGetBaseClass)();
#else
CRuntimeClass* m_pBaseClass;
#endif
// Operations
CObject* CreateObject();
BOOL IsDerivedFrom(const CRuntimeClass* pBaseClass) const;
// dynamic name lookup and creation
static CRuntimeClass* PASCAL FromName(LPCSTR lpszClassName);
static CRuntimeClass* PASCAL FromName(LPCWSTR lpszClassName);
static CObject* PASCAL CreateObject(LPCSTR lpszClassName);
static CObject* PASCAL CreateObject(LPCWSTR lpszClassName);
// Implementation
void Store(CArchive& ar) const;
static CRuntimeClass* PASCAL Load(CArchive& ar, UINT* pwSchemaNum);
// CRuntimeClass objects linked together in simple list
CRuntimeClass* m_pNextClass; // linked list of registered classes
const AFX_CLASSINIT* m_pClassInit;
};
4.2 Qt
在笔者<Qt源码分析:QMetaObject实现原理>一文中,已就Qt反射机制做了分析,可以看到Qt反射相关元数据时存放到了QMetaObject中了,
struct Q_CORE_EXPORT QMetaObject
{
//...
struct { // private data
SuperData superdata;
const QByteArrayData *stringdata;
const uint *data;
typedef void (*StaticMetacallFunction)(QObject *, QMetaObject::Call, int, void **);
StaticMetacallFunction static_metacall;
const SuperData *relatedMetaObjects;
void *extradata; //reserved for future use
} d;
//...
};
网络资料
Reflectionhttps://en.wikipedia.org/wiki/Reflection_%28computer_programming%29
The C++ Extensions for Reflection https://en.cppreference.com/w/cpp/experimental/reflect
RTTRhttps://www.rttr.org/QMetaObjecthttps://doc.qt.io/qt-5/qmetaobject.html
yazi-web https://github.com/kaifamiao/yazi-web.git
Boost Hana https://www.boost.org/doc/libs/1_80_0/libs/hana/doc/html/index.html
Boost PRF https://www.boost.org/doc/libs/master/doc/html/boost_pfr.html
An Introduction to Reflection in C++https://blog.csdn.net/qq_26221775/article/details/138768568?spm=1001.2014.3001.5501
FreeCAD: C++ Generic Factory Method inteface advicehttps://forum.freecad.org/viewtopic.php?p=24221&hilit=BaseClass#p24221