CN108614687A - A kind of method that realizing reflection in C++ programs, storage medium and computing device - Google Patents

A kind of method that realizing reflection in C++ programs, storage medium and computing device Download PDF

Info

Publication number
CN108614687A
CN108614687A CN201810360561.0A CN201810360561A CN108614687A CN 108614687 A CN108614687 A CN 108614687A CN 201810360561 A CN201810360561 A CN 201810360561A CN 108614687 A CN108614687 A CN 108614687A
Authority
CN
China
Prior art keywords
class
reflection
function
programs
reflective information
Prior art date
Legal status (The legal status is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the status listed.)
Withdrawn
Application number
CN201810360561.0A
Other languages
Chinese (zh)
Inventor
孙金标
申云宾
王俊
Current Assignee (The listed assignees may be inaccurate. Google has not performed a legal analysis and makes no representation or warranty as to the accuracy of the list.)
Suzhou Snail Digital Technology Co Ltd
Original Assignee
Suzhou Snail Digital Technology Co Ltd
Priority date (The priority date is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the date listed.)
Filing date
Publication date
Application filed by Suzhou Snail Digital Technology Co Ltd filed Critical Suzhou Snail Digital Technology Co Ltd
Priority to CN201810360561.0A priority Critical patent/CN108614687A/en
Publication of CN108614687A publication Critical patent/CN108614687A/en
Withdrawn legal-status Critical Current

Links

Classifications

    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F8/00Arrangements for software engineering
    • G06F8/30Creation or generation of source code
    • G06F8/33Intelligent editors
    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F8/00Arrangements for software engineering
    • G06F8/30Creation or generation of source code
    • G06F8/35Creation or generation of source code model driven

Landscapes

  • Engineering & Computer Science (AREA)
  • Software Systems (AREA)
  • General Engineering & Computer Science (AREA)
  • Theoretical Computer Science (AREA)
  • Physics & Mathematics (AREA)
  • General Physics & Mathematics (AREA)
  • Stored Programmes (AREA)

Abstract

A kind of method that realizing reflection in C++ programs, storage medium and computing device, the method includes the steps:The reflection class of C++ class is created, the reflection class is preserving the reflective information of C++ class;The reflection class of C++ class is encapsulated with macro, and accesses the reflective information of the C++ class preserved in the reflection class of C++ class.Method, storage medium and the computing device that reflection is realized in C++ programs of the present invention can be such that the C++ project supports reflect, and realize the blueprint function of UE4, greatly simplify editing machine exploitation.

Description

A kind of method that realizing reflection in C++ programs, storage medium and computing device
Technical field
The present invention relates to C++ technical fields, are situated between more particularly to a kind of method for realizing reflection in C++ programs, storage Matter and computing device.
Background technology
Reflection, which is program, can access, detects and change itself state or a kind of ability of behavior.Many language all prop up Reflection is held, there is reflection, it will be able to allow code that there is ability of examining oneself, code is allowed to know what oneself can do.Reflection is suitable for Following occasion:
1)Serializing and data binding;2)Data-driven can be created that class example by character string, pass through character string energy Enough access class method and generic attribute.
Can the high-level languages such as java, c# all supporting reflexs at present be had become by reflecting come dynamic access category information One of mark of modern software, and c++ is currently not supporting reflex function.In the prior art, the supporting reflex in C++ programs A kind of realization method be:Debugging message is parsed, for example, the program of visual studio exploitations, it can be by parsing Pdb texts Part obtains Debugging message, and the advantages of such realization method is that code need not be changed, and disadvantage is then to have to compiler phase The Debugging message of pass.
Invention content
In order to solve the shortcomings of the prior art, the purpose of the present invention is to provide one kind to realize instead in C++ programs Method, storage medium and the computing device penetrated can be such that the C++ project supports reflect, to realize the blueprint function of UE4, in turn Greatly simplify editing machine exploitation.
To achieve the above object, the method provided by the invention that reflection is realized in C++ programs, includes the following steps:
The reflection class of C++ class is created, the reflection class is preserving the reflective information of C++ class;
The reflection class of C++ class is encapsulated with macro, and accesses the reflective information of the C++ class preserved in the reflection class of C++ class.
Further, the reflective information of the C++ class includes:The attribute of C++ class and the reflective information of function.
Further, the step of reflection class for creating C++ class is that preset mark is added in the code of C++ class Position, obtains the reflective information of C++ class, creates the reflection class of C++ class.
The step of reflective information for obtaining C++ class, further comprises step:
Corresponding Get and Set functions are respectively set for each attribute of C++ class, in the reflective information of C++ class preserve Get and Set function pointers, wherein Get the and Set functions are respectively used to obtain and be arranged the attribute of C++ class.
The Get functions are further used for recording the name of the attribute of C++ class.
The step of reflective information for obtaining C++ class, further comprises step:
It is derived by template, records the reflective information of the function of C++ class, wherein the reflective information of the function of the C++ class, packet It includes:Function parameter number, the data type of each parameter, function whether be static function, the return value data type of function and Function pointer.
Further, the method that reflection is realized in C++ programs, further includes step:By by third party's class sequence Third party's class, is converted to the member variable and function parameter of C++ class by the mode of row chemical conversion character string.
The step of reflection class with macro to encapsulate C++ class, further comprises step:With macro in C++ class, C++ is registered The reflective information of the C++ class preserved in the reflection class of class and the reflection class of C++ class.
To achieve the above object, computer readable storage medium provided by the invention, is stored thereon with computer program, should The step of any one of present invention the method is realized when program is executed by processor.
To achieve the above object, computing device provided by the invention, including memory, processor and storage are on a memory And the computer program that can be run on a processor, the processor realize any one of present invention side when executing described program The step of method.
Method, storage medium and the computing device that reflection is realized in C++ programs of the present invention, can make C++ project branch Reflection is held, the blueprint function of UE4 is realized, greatly simplifies editing machine exploitation.
Other features and advantages of the present invention will be illustrated in the following description, also, partly becomes from specification It obtains it is clear that understand through the implementation of the invention.
Description of the drawings
Attached drawing is used to provide further understanding of the present invention, and a part for constitution instruction, and with the present invention's Embodiment together, for explaining the present invention, is not construed as limiting the invention.In the accompanying drawings:
Fig. 1 is the method flow diagram that reflection is realized in C++ programs according to the present invention.
Specific implementation mode
Hereinafter, preferred embodiments of the present invention will be described with reference to the accompanying drawings, it should be understood that preferred reality described herein Apply example only for the purpose of illustrating and explaining the present invention and is not intended to limit the present invention.
Fig. 1 is according to the method flow diagram for realizing reflection in C++ programs of the present invention, below with reference to Fig. 1, to this hair The bright method that reflection is realized in C++ programs is described in detail.
In step 110, the reflection class of C++ class is created, the reflection class of C++ class is preserving attribute, the function of C++ class(Side Method)Deng reflective information.
In the step, preset flag bit is added in the code of C++ class, obtains the reflective information of C++ class.For example, In the C++ class of Qt, UE4, the function that reflects in needs adds preset flag bit before method, obtains the reflection letter of C++ class Breath.
Specifically, corresponding Get and Set functions are respectively set in each attribute for C++ class, believe in the reflection of C++ class Get and Set function pointers are preserved in breath, wherein Get and Set functions are respectively used to obtain and be arranged the attribute of C++ class.Such as journey Shown in sequence 1-1, by function pointer, the name of the attribute of C++ class is recorded, obtains the setting function of the attribute of C++ class.
Program 1-1
class Test
{
public:
void Func(){}
int GetFlag(){return m_nFlag;}
void SetFlag(int nFlag){m_nFlag=nFlag;}
private:
int m_nFlag;
};
And derived by template, record the function of C++ class(Method)Reflective information, wherein the reflective information of record includes: Whether function parameter number, the data type of each parameter, function are static function, the return value data type of function and function Pointer etc..As shown in program 1-2, using template come the function parameter of automatic deduction C++ class, therefore registration function name is only needed, Such as code:Reg_Func(Test, Func) ;
Program 1-2(1-1 is continuous)
Reg(Test);
Reg_Field(int, Test, m_nFlag, GetFlag, SetFlag);
Reg_Func(Test, Func);
It is derived by template, obtains the function of C++ class(Method)Reflective information when, as shown in program 1-3, first with template Specialization, the function for making compiler Auto-matching parameter consistent.Program 1-3 is the example of the inclined specialization of template of a parameter, and It is unified to store data using aleatory variable Var in order to avoid void*.
Program 1-3
template<class __RT, class __C, class __P1>
inline MethodType* RTTIFuncTypeOf(__RT (__C::*f)(__P1)){
return new RTTIFuncType1<__RT, __C, __P1>(f);
}
template<class __RT, class __C, class __P1>
class RTTIFuncType1 : public MethodType
{
public:
typedef __RT (__C::*fptr)(__P1);
fptr f;
RTTIFuncType1(fptr f)
{
// record necessary reflective information:Function pointer returns to Value Types, number of parameters, the type of parameters
this->f = f;
m_nReturnType = TVarTraits<__RT>::Type;
int nParamType = TVarTraits<__P1>::Type;
m_paramsType.push_back(nParamType);
}
virtual void Invoke( void* obj, const IVarList& args, IVarList& res) {
// extraction function call parameter
__P1 p1 = TVarGetter<__P1>::Get(args, 0);
// by the function pointer that is recorded in reflective information come call function
TVarSetter<__RT>::Set(res, (((__C*)obj)->*f)(p1));
//res << (((__C*)obj)->*f)(p1);
}
};
Above-mentioned steps, the mode for using non-intrusion type obtain the reflective information of C++ class, so as to make existing C++ projects In third party library also supporting reflex.
For third party's class in existing C++ projects, third party's class sequence is melted into character string, you can realize third Square class is converted to C++ class member variable and function parameter, as shown in program 1-4.
Program 1-4
template<>
struct TVarGetter<const oc::FVector&>
{
static oc::FVector Get(const IVar& v)
{
oc::FVector temp;
temp.ParseFrom(v.StringVal());
return temp;
}
static oc::FVector Get(const IVarList& vl, size_t index)
{
return TVarGetter<oc::FVector>::Get(vl, index);
}
};
In step 120, the reflection class of C++ class is encapsulated with macro, and access the attribute of preservation C++ class in the reflection class of C++ class, Function(Method)Deng reflective information.
In the step, with macro in C++ class, the needs preserved in the reflection class of C++ class and the reflection class of C++ class are registered The attribute and function of the C++ class of reflection(Method)Deng reflective information.
Based on the present invention the method that reflection is realized in C++ programs, computer readable storage medium provided by the invention, It is stored thereon with computer program, the step of which realizes the method for the invention when being executed by processor.
Computer readable storage medium provided by the invention, can be located at one or more computing device, and the calculating is set Standby includes processor, the processor execute its computer in the computer readable storage medium on the computing device The step of program, realization the method for the invention.
Method, storage medium and the computing device that reflection is realized in C++ programs of the present invention, can make C++ project branch Reflection is held, the blueprint function of UE4 is realized, greatly simplifies editing machine exploitation.
One of ordinary skill in the art will appreciate that:The foregoing is only a preferred embodiment of the present invention, and does not have to In the limitation present invention, although the present invention is described in detail referring to the foregoing embodiments, for those skilled in the art For, still can to foregoing embodiments record technical solution modify, or to which part technical characteristic into Row equivalent replacement.All within the spirits and principles of the present invention, any modification, equivalent replacement, improvement and so on should all include Within protection scope of the present invention.

Claims (10)

1. a kind of method for realizing reflection in C++ programs, which is characterized in that include the following steps:
The reflection class of C++ class is created, the reflection class is preserving the reflective information of C++ class;
The reflection class of C++ class is encapsulated with macro, and accesses the reflective information of the C++ class preserved in the reflection class of C++ class.
2. the method according to claim 1 for realizing reflection in C++ programs, which is characterized in that the reflection of the C++ class Information includes:The attribute of C++ class and the reflective information of function.
3. the method according to claim 1 for realizing reflection in C++ programs, which is characterized in that the establishment C++ class The step of reflecting class is that preset flag bit is added in the code of C++ class, obtains the reflective information of C++ class, creates C++ class Reflection class.
4. the method according to claim 3 for realizing reflection in C++ programs, which is characterized in that the acquisition C++ class The step of reflective information, further comprises step:
Corresponding Get and Set functions are respectively set for each attribute of C++ class, in the reflective information of C++ class preserve Get and Set function pointers, wherein Get the and Set functions are respectively used to obtain and be arranged the attribute of C++ class.
5. the method according to claim 4 for realizing reflection in C++ programs, which is characterized in that the Get functions are into one Walk the name of the attribute for recording C++ class.
6. the method according to claim 3 for realizing reflection in C++ programs, which is characterized in that the acquisition C++ class The step of reflective information, further comprises step:
It is derived by template, records the reflective information of the function of C++ class, wherein the reflective information of the function of the C++ class, packet It includes:Function parameter number, the data type of each parameter, function whether be static function, the return value data type of function and Function pointer.
7. the method according to claim 1 for realizing reflection in C++ programs, which is characterized in that further include step:Pass through Third party's class sequence is melted into the mode of character string, third party's class is converted to the member variable and function parameter of C++ class.
8. the method according to claim 1 for realizing reflection in C++ programs, which is characterized in that described to encapsulate C with macro ++ the step of reflection class of class, further comprises step:With macro in C++ class, the reflection class and C++ class of C++ class are registered The reflective information of the C++ class preserved in reflection class.
9. a kind of computer readable storage medium, is stored thereon with computer program, which is characterized in that the program is held by processor The step of any one of claim 1-8 the methods are realized when row.
10. a kind of computing device, including memory, processor and storage are on a memory and the calculating that can run on a processor Machine program, which is characterized in that the processor realizes the step of any one of claim 1-8 the methods when executing described program Suddenly.
CN201810360561.0A 2018-04-20 2018-04-20 A kind of method that realizing reflection in C++ programs, storage medium and computing device Withdrawn CN108614687A (en)

Priority Applications (1)

Application Number Priority Date Filing Date Title
CN201810360561.0A CN108614687A (en) 2018-04-20 2018-04-20 A kind of method that realizing reflection in C++ programs, storage medium and computing device

Applications Claiming Priority (1)

Application Number Priority Date Filing Date Title
CN201810360561.0A CN108614687A (en) 2018-04-20 2018-04-20 A kind of method that realizing reflection in C++ programs, storage medium and computing device

Publications (1)

Publication Number Publication Date
CN108614687A true CN108614687A (en) 2018-10-02

Family

ID=63660538

Family Applications (1)

Application Number Title Priority Date Filing Date
CN201810360561.0A Withdrawn CN108614687A (en) 2018-04-20 2018-04-20 A kind of method that realizing reflection in C++ programs, storage medium and computing device

Country Status (1)

Country Link
CN (1) CN108614687A (en)

Cited By (1)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN110413266A (en) * 2019-08-05 2019-11-05 广东三维家信息科技有限公司 A kind of improved method and system of the reflection mechanism of haxe

Citations (3)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
WO2000007120A1 (en) * 1998-07-29 2000-02-10 American Management Systems, Incorporated Object to relational database mapping infrastructure in a customer care and billing system
CN1916842A (en) * 2006-09-06 2007-02-21 上海科泰世纪科技有限公司 Method of packaging component for implementing reflection mechanism in structurized software system
CN107480537A (en) * 2017-06-28 2017-12-15 北京小度信息科技有限公司 Authority the Resources list automatic generation method and device

Patent Citations (3)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
WO2000007120A1 (en) * 1998-07-29 2000-02-10 American Management Systems, Incorporated Object to relational database mapping infrastructure in a customer care and billing system
CN1916842A (en) * 2006-09-06 2007-02-21 上海科泰世纪科技有限公司 Method of packaging component for implementing reflection mechanism in structurized software system
CN107480537A (en) * 2017-06-28 2017-12-15 北京小度信息科技有限公司 Authority the Resources list automatic generation method and device

Non-Patent Citations (1)

* Cited by examiner, † Cited by third party
Title
鲍亮 等: "C++反射机制的一种简单实现", 《计算机工程》 *

Cited By (1)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN110413266A (en) * 2019-08-05 2019-11-05 广东三维家信息科技有限公司 A kind of improved method and system of the reflection mechanism of haxe

Similar Documents

Publication Publication Date Title
US20070288853A1 (en) Software, methods and apparatus facilitating presentation of a wireless communication device user interface with multi-language support
RU2459238C2 (en) Managed execution environment for software application interfacing
CN110297999A (en) A kind of web page template generation method and device, webpage loading method and front-end server and Website server
CN109388766A (en) The method and apparatus of page load
US7730492B1 (en) Method and system for running multiple virtual machines in a single process
KR20050039551A (en) Programming interface for a computer platform
KR20140067018A (en) Describing native application programming interfaces of an operating system with metadata
GB2406938A (en) Method and apparatus for handling text and binary markup languages in a computing device
CN1316782C (en) Method for realizing tree-shaped structure in B/S structure
US20080168430A1 (en) Open controls
CN110045956A (en) Configuration method, device, equipment and the readable storage medium storing program for executing of component
CN116974620A (en) Application program generation method, operation method and corresponding device
CN109032936A (en) A kind of desktop automatic test development approach and system
US6842905B2 (en) Method and system for implementing collection program interface for accessing a collection of data associated with a legacy enumeration application interface
CN108614687A (en) A kind of method that realizing reflection in C++ programs, storage medium and computing device
CA3203793C (en) System and method for real-time, dynamic creation, delivery, and use of customizable web applications
Yoo et al. Snapshot-based migration of es6 javascript
CN112596734A (en) Static library generation method and device
CN114546434A (en) Application updating method and device, electronic equipment and storage medium
CN113608726B (en) Code generation method, device, electronic equipment and storage medium
Fraze-Frazenko et al. Panorama tours management application development
Flatscher The augsburg version of BSF4Rexx
CN116755767A (en) Code document generation method, device, nonvolatile storage medium and electronic equipment
CN117971350A (en) Applet operating method, device and computer readable storage medium
Gagniuc Data Types and Statements

Legal Events

Date Code Title Description
PB01 Publication
PB01 Publication
SE01 Entry into force of request for substantive examination
SE01 Entry into force of request for substantive examination
WW01 Invention patent application withdrawn after publication

Application publication date: 20181002

WW01 Invention patent application withdrawn after publication