Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Detailed explanation of the implementation method of C++ reflection

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly introduces "detailed explanation of the implementation method of C++ reflection". In daily operation, I believe many people have doubts about the realization method of C++ reflection. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts about "detailed explanation of the implementation method of C++ reflection". Next, please follow the editor to study!

First, register auxiliary classes

ClassRegistry: template function for registration of data, module and contextdata, in which the functions are parsed:

Create_object: find the RegistryNode corresponding to the incoming name in RegistryMap (RegistryNode saves the name and constructor), and call the constructor to return.

Register_class: register RegistryMap with the name and constructor passed in, and only call it in the constructor of Register, which will be used later in ClassRegister DataRegister, ClassRegister ModuleRegister, and ClassRegister ContextDataRegister. The data in RegistryMap is inserted from the register_class method, which will be called later in IMPLEMENT_XXX.

Fill_name_array: find the name registered in RegistryMap and insert the passed parameters.

Second, the macro definition used

2.1 data

REGISTER_DATA: declare the function _ _ construct_##name##_data () that constructs data_class, in which the constructor of data_calss is called; declare the get_##name that gets class, and the get_data of the function body gets the corresponding IData from sign_data_map

# define REGISTER_DATA (data_class, name)\ inline:: wmf::IData* _ _ construct_##name##_data () {return new data_class;}\ namespace wmf {\ namespace internal () {return get_data (# name);}\}\} / / wmf::internal

IMPLEMENT_DATA: calls the constructor of DataRegister. Declare the variable _ _ # # name##_module_register, where the input name and constructor _ _ construct_##name##_data are registered with RegistryMap

# define IMPLEMENT_DATA (name)\:: wmf::internal::DataRegister _ _ # name##_module_register (\ # name, _ _ construct_##name##_data)

Use:

Call REGISTER_DATA at the location of the .h file of the .cpp file you need, declare the constructor and get the get_xxx function of the data.

Call IMPLEMENT_DATA in the line of sight of each service's cpp file to inject RegistryMap.

In the InitInjection of each service's cpp file, INJECT_DATA_MODULE_DEPENDENCY injects this dictionary into module.

2.2 module

REGISTER_MODULE: declare _ _ construct_#name##_module (), and return new module_class

Declare to get the get_##name of class, and return the object saved in ModuleMap in the function body (cast_module finds its corresponding object in ModuleMap, if it cannot, then finds its constructor in RegisterMap, inserts ModuleMap after calling create_object, and returns the newly created object (the data in RegisterMap comes from IMPLEMENT_XXX).

# define REGISTER_MODULE (module_class, name)\ inline:: wmf::IModule* _ construct_##name##_module () {\ return new module_class;\}\ namespace wmf {\ namespace internal {\ inline module_class* get_##name (:: wmf::Context& ctx) {\ return ctx.cast_module (# name);\} / / wmf::internal

IMPLEMENT_MODULE: declare the _ _ # # name##_module_register variable to insert RegistryMap.

# define IMPLEMENT_MODULE (name)\:: wmf::internal::ModuleRegister _ _ # name##_module_register (\ # name, _ _ construct_##name##_module)

Use:

Calling REGISTER_MODULE at the end of the .h file of the new module declares the constructor that will be used in IMPLEMENT_MODULE, as well as the get_xxx function that gets its object from ModuleMap.

IMPLEMENT_MODULE is called at the end of the service to register the module with RegistryMap.

2.3 context data

REGISTER_CONTEXT_DATA: declare _ _ construct_##name##_context_data (), create a new data_class

Declare to get the get_##name of class, and the function body finds the IContextData corresponding to the name and signature saved by ContextDataMap through name, which is converted to data_class.

# define REGISTER_CONTEXT_DATA (data_class, name)\ inline:: wmf::IContextData* _ construct_##name##_context_data () {\ return new data_class;\}\ namespace wmf {\ namespace internal {\ inline data_class* get_##name (const:: wmf::Context& ctx) {\ return ctx.cast_context_data (# name);\} / / wmf::internal

IMPLEMENT_CONTEXT_DATA: declare the _ _ # # name##_context_data variable, where the input name and constructor _ _ construct_##name##_context_data are registered with RegistryMap

# define IMPLEMENT_CONTEXT_DATA (name)\:: wmf::internal::ContextDataRegister _ _ # name##_context_data (\ # name, _ _ construct_##name##_context_data)

2.4 index_data

DECLARE_INDEX_DATA:N for name, VT for VersionIndex type. Declare that type C is a VIAdaptor type assembled with type VT and path, name, and desc assembled with N.

# define DECLARE_INDEX_DATA (VT, C, N)\ extern const char _ _ index_##N##_path [];\ extern const char _ index_##N##_name [];\ extern const char _ _ index_##N##_desc [];\ typedef wmf::VIAdaptor\ C

DEFINE_INDEX_DATA:N for name, which declares a bunch of string variables for path, name, desc of data.

# define DEFINE_INDEX_DATA (N)\ const char _ _ index_##N##_path [] = # N "_ path";\ const char _ index_##N##_name [] = # N "_ name";\ const char _ index_##N##_desc [] = # N "_ desc";\ DEFINE_string (N##_path, "", "index" # N "path") \ DEFINE_string (N##_name, "", "index" # N "name");\ DEFINE_string (N##_desc, "index_" # N, "index" # N "desc")

2.5 injection

DEFINE_INJECTION: defines a function that sets the object_ref variable to an incoming variable of type class_type*.

# define DEFINE_INJECTION (injection_name, class_type, object_ref)\ void set_##injection_name (class_type* module) {object_ref = module;}

INJECT_OBJECT_OBJECT_DEPENDENCY: call the set_##injection_name method of the object object_to, with the passed-in parameter as a reference to object_from. To combine DEFINE_INJECTION is to set object_from into the object object_to.

# define INJECT_OBJECT_OBJECT_DEPENDENCY (injection_name, object_from,\ object_to)\ (object_to) .set _ # # injection_name (& (object_from))

INJECT_MODULE_DEPENDENCY: find the variable of module_from in the context context and inject it into the module_from of the same context.

# define INJECT_MODULE_DEPENDENCY (injection_point, context, module_from,\ module_to)\:: wmf::internal::get_##module_to (context)-> set_##injection_point (\:: wmf::internal::get_##module_from (context))

INJECT_DATA_MODULE_DEPENDENCY: injects data into the module_to obtained through the context context.

# define INJECT_DATA_MODULE_DEPENDENCY (injection_point, context, data,\ module_to)\:: wmf::internal::get_##module_to (context)-> set_##injection_point (\: wmf::internal::get_##data ())

INJECT_MODULE_OBJECT_DEPENDENCY: the module_from obtained through the context context is injected into the object_to.

# define INJECT_MODULE_OBJECT_DEPENDENCY (injection_point, context, module_from,\ object_to)\ (object_to) .set _ # # injection_point (\:: wmf::internal::get_##module_from (context))

INJECT_OBJECT_MODULE_DEPENDENCY: object_from is injected into the module_to obtained through the context.

# define INJECT_OBJECT_MODULE_DEPENDENCY (injection_point, context, object_from,\ module_to)\:: wmf::internal::get_##module_to (context)-> set_##injection_point (\ & (object_from))

Use:

Call INJECT_MODULE_DEPENDENCY, INJECT_DATA_MODULE_DEPENDENCY in context-sensitive session

INJECT_MODULE_DEPENDENCY is used to inject session-related information (such as session_docs, request, response) into module, and module means the name of the module required for the request.

INJECT_DATA_MODULE_DEPENDENCY is used to inject data into module.

III. Summary

3.1 add a module

Calling REGISTER_MODULE at the end of the .h file of the new module declares the constructor that will be used in IMPLEMENT_MODULE, as well as the get_xxx function that gets its object from ModuleMap.

IMPLEMENT_MODULE is called at the end of the service to register the module with RegistryMap.

Call INJECT_MODULE_DEPENDENCY, INJECT_DATA_MODULE_DEPENDENCY in context-sensitive session

INJECT_MODULE_DEPENDENCY is used to inject session-related information (such as session_docs, request, response) into module, and module means the name of the module required for the request.

INJECT_DATA_MODULE_DEPENDENCY is used to inject data into module.

3.2 Code Review

ClassRegistry is used for a bunch of macros for the second item. The key to mapping module to class is RegistryMap. When you add a module, the service will go to RegistryMap to find the constructor corresponding to the name. The data in the RegistryMap is the correspondence between the name and the class injected into the IMPLEMENT_MODULE. The configuration file is equipped with the chain of module. For example, if you need AModule,BModule, insert all module into the init, and then call the run function of each module in schedule_impl.

At this point, the study of "detailed explanation of the implementation method of C++ reflection" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report