A type conversion method, device and computer equipment
By determining the original type of the target object pointer and obtaining the type inheritance relationship tree during the running program, the type conversion of the target object pointer is achieved, solving the problem of difficulty in performing type conversion during the running program in the prior art, and improving the conversion efficiency and richness of the method.
Patent Information
- Application Number
- CN202110022599.9
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2021-01-08
- Publication Date
- 2025-05-16
- Estimated Expiration
- 2041-01-08
AI Technical Summary
The prior art is difficult to perform type conversion during running programs, resulting in the inability to realize code sharing and reduce code redundancy.
By determining the original type of the target object pointer during the running program, and obtaining the type inheritance relationship tree corresponding to the original type, obtaining the target type corresponding to the target type identifier in the type inheritance relationship tree, realizing the type conversion of the target object pointer.
It realizes the conversion of pointer types when the program is run, enriches the type conversion method. No type check is required whether it is uplink or downlink conversion, shortens the conversion process and improves the conversion efficiency.
Smart Images

Figure CN114741084B_ABST
Abstract
Description
Technical Field
[0001] The present application relates to the field of computer technology, and in particular to a type conversion method, device and computer equipment. Background Art
[0002] In programming languages, objects (or variables) can be converted between types to achieve code sharing and reduce code redundancy. Currently, most type conversions are implemented during compilation, such as static_cast, const_cast, and reinterpret_cast. These functions can implement type conversions during compilation. However, there is currently no method to perform type conversions during program execution. Therefore, how to perform type conversions at runtime has become an urgent problem to be solved. Summary of the invention
[0003] The embodiments of the present application provide a type conversion method, apparatus, and computer device, which can realize the conversion of pointer types during runtime and improve conversion efficiency.
[0004] The present application discloses a type conversion method, which includes:
[0005] During the running of a program, when a type conversion request for a target object pointer in the program is received, determining the original type of the target object pointer; the program includes N types of type information, any type information includes a type identifier, the type conversion request carries a target type identifier, and N is an integer greater than 1;
[0006] Acquire a type inheritance relationship tree corresponding to the original type, the type inheritance relationship tree includes type information of M types, the N type information includes M type information, and types corresponding to adjacent type information in the type inheritance relationship tree satisfy a type inheritance relationship;
[0007] Target type information corresponding to the target type identifier is obtained in the type inheritance relationship tree, and the type of the target object pointer is converted into a type corresponding to the target type information.
[0008] In one aspect, the present application discloses a type conversion device, which includes:
[0009] a determination unit, configured to determine the original type of the target object pointer when receiving a type conversion request for a target object pointer in the program during the running of the program; the program includes N types of type information, any type information includes a type identifier, the type conversion request carries a target type identifier, and N is an integer greater than 1;
[0010] An acquiring unit, configured to acquire a type inheritance relationship tree corresponding to the original type, wherein the type inheritance relationship tree includes type information of M types, the N type information includes M type information, and types corresponding to adjacent type information in the type inheritance relationship tree satisfy a type inheritance relationship;
[0011] The acquisition unit is further used to acquire the target type corresponding to the target type identifier in the type inheritance relationship tree;
[0012] A processing unit is used to convert the type of the target object pointer to the target type.
[0013] On one hand, an embodiment of the present application discloses a computer device, which includes a memory and a processor: the memory is used to store a computer program; the processor runs the computer program to implement the above-mentioned type conversion method.
[0014] On one hand, an embodiment of the present application discloses a computer-readable storage medium, wherein the computer-readable storage medium stores a computer program, and when the computer program is executed by a processor, the above-mentioned type conversion method is executed.
[0015] In one aspect, the present application discloses a computer program product or a computer program, wherein the computer program product or the computer program includes a computer instruction, and the computer instruction is stored in a computer-readable storage medium. A processor of a computer device reads the computer instruction from the computer-readable storage medium, and the processor executes the computer instruction, so that the computer device performs the above-mentioned type conversion method.
[0016] In an embodiment of the present application, during the running of a program, when a type conversion request for a target object pointer in the program is received, the computer device determines the original type of the target object pointer, obtains a type inheritance relationship tree corresponding to the original type, the types corresponding to the adjacent type information in the type inheritance relationship tree satisfy the type inheritance relationship, obtains the target type corresponding to the target type identifier in the type inheritance relationship tree, and converts the type of the target object pointer to the target type. Through this method, on the one hand, the conversion of the pointer type can be realized when the program is running, and during the conversion process, the target type is determined by the type identifier, and whether the target object pointer can be converted to the target type is determined by querying the type inheritance relationship tree, thereby enriching the type conversion method; on the other hand, no type check is required for either uplink conversion or downlink conversion, which can shorten the conversion process and improve conversion efficiency. BRIEF DESCRIPTION OF THE DRAWINGS
[0017] In order to more clearly illustrate the embodiments of the present invention or the technical solutions in the prior art, the drawings required for use in the embodiments or the description of the prior art will be briefly introduced below. Obviously, the drawings described below are only some embodiments of the present invention. For ordinary technicians in this field, other drawings can be obtained based on these drawings without paying creative work.
[0018] Figure 1 It is a flow chart of a type conversion method disclosed in an embodiment of the present application;
[0019] Figure 2 It is a type of pointer pointing schematic diagram disclosed in the embodiment of the present application;
[0020] Figure 3 It is a schematic diagram of a type of inheritance relationship tree disclosed in an embodiment of the present application;
[0021] Figure 4 It is a flowchart of a method for creating and deleting an instance provided by a type conversion system disclosed in an embodiment of the present application;
[0022] Figure 5 is a structural schematic diagram of a type conversion device disclosed in an embodiment of the present application;
[0023] Figure 6 It is a structural schematic diagram of a computer device disclosed in an embodiment of the present application. DETAILED DESCRIPTION
[0024] The following will be combined with the drawings in the embodiments of the present application to clearly and completely describe the technical solutions in the embodiments of the present application. Obviously, the described embodiments are only part of the embodiments of the present application, not all of the embodiments. Based on the embodiments in the present application, all other embodiments obtained by ordinary technicians in this field without creative work are within the scope of protection of this application.
[0025] Table 1
[0026]
[0027] Please refer to Table 1, which is a schematic diagram of a type conversion system disclosed in an embodiment of the present application. As shown in Table 1, the system includes a management class KFMetaManager for managing runtime type information, a user-defined class ClassX, a type conversion interface class IKFMeta, and a type information definition class ClassX::Meta matching the user-defined class ClassX, wherein there is an inheritance relationship between the type information definition class ClassX::Meta and the type conversion interface class IKFMeta, or the type information definition class ClassX::Meta is an implementation class of the type conversion interface class IKFMeta. The functions of the above-mentioned classes are introduced below:
[0028] The class KFMetaManager is mainly used to manage all runtime type information. It can provide type information registration function and two types of type information query function. It can include the following functions: registration function Register(IKFMeta*meta), which is used to register type information, query function IKFMeta*GetMeta(int type), which is used to obtain type information through type number, query function IKFMeta*GetMeta(string name), which is used to obtain type information through type name. Among them, type information includes type identification and type inheritance relationship. Type identification includes type name and type number. Type inheritance relationship will record the parent class and subclass of the class, which can be in the format of "(type A; type B; type C)", where the class before the semicolon represents the parent class of the class, and the class after the semicolon represents the subclass of the class, that is, the current type is type B, type A is the parent class of type B, and type C is the subclass of type B. Among them, the inheritance relationship between the three is that type B inherits from type A, and type C inherits from type B. The working principle of the registration function Register(IKFMeta*meta) is as follows: the type information meta (also called meta information) passed in will contain the type identifier and type inheritance relationship. By calling the registration function Register(IKFMeta*meta), the passed in type information is stored in the type information database. The type information database can specifically include a type information array and / or a type information mapping table. The storage process is as follows:
[0029] The storage process of the type information array is: call the registration function, obtain the type information, use the type number as the position number of the type information array (m_metas), store the type information in the type information array, and the type number in any type information is the same as the position of any type information in the type information array. Specifically, as shown in Table 2, if the position number is "1", the corresponding type number is also "1", and the type name of the type information corresponding to the position number "1" is "XX", and its inheritance relationship is (X1; XX; X3), indicating that "XX" inherits from "X1", and "X3" inherits from "XX".
[0030] Table 2
[0031]
[0032]
[0033] The storage process of the type information mapping table is: call the registration function, obtain the type information, and store the type information in the type information mapping table, wherein the type name of any type information is the same as the key value of any type information in the type information mapping table. The key value and the type number form a key-value pair stored in the type information mapping table. Specifically, as shown in Table 3, if the key value is "YY", the corresponding type name is also "YY", and the type number of the type information corresponding to the key value "YY" is "2", and its inheritance relationship is (Y1; YY; Y3), indicating that "YY" inherits from "Y1" and "Y3" inherits from "YY".
[0034] Table 3
[0035]
[0036] There are two functions for querying type information:
[0037] The first is the function IKFMeta*GetMeta(int type): get type information by type number. The working principle of the function IKFMeta*GetMeta(int type) is to query the type information array by the type number passed in. Since the position number of the type information array is consistent with the type number, the type information can be easily obtained in the type information array by the position number.
[0038] The second is the function IKFMeta*GetMeta(string name): get type information through the type name. The working principle of the function IKFMeta*GetMeta(int type) is to query the type information in the type information mapping table through the passed type name. Since the key value and type name of the type information mapping table are consistent, the type information can be easily queried through the key value.
[0039] After storing the type information in the type information array and type information mapping table, the type information is queried using the functions IKFMeta*GetMeta(int type) and IKFMeta*GetMeta(string name). The time complexity is O(1), and the query performance is very high.
[0040] The interface class IKFMeta is mainly used to encapsulate functions for obtaining type information, where runtime type information is called meta information. The interface class IKFMeta can mainly include the following functions:
[0041] Function int type() is used to obtain the type identifier.
[0042] For example, a unique type number can be generated by accumulating a global variable (such as KF_META_COUNT) in the constructor of the implementation class of the interface class IKFMeta (for example, the implementation class can be class ClassX::Meta), that is, it does not need to be defined by the user and is automatically generated.
[0043] The function string name() is mainly used to obtain the type name.
[0044] The function void*instantiate(int type) is used to instantiate the type corresponding to the type information and return a pointer to the function input type. In the implementation class of IKFMeta, if the input type number is , then a pointer to the type corresponding to the type information is returned. If the input type identifier is the subclass or parent class corresponding to the type, then the pointer of the instance is converted to a pointer to the corresponding subclass or parent class and returned.
[0045] The function void destroy(void*obj) is used to destroy an instance of this type.
[0046] Class ClassX::Meta is the implementation class of the interface class IKFMeta. Relevant information can be obtained through the static functions in class ClassX::Meta. The corresponding static functions in the interface class IKFMeta are defined: static int Type(), static string Name(), static ClassX*Instantiate(), static void Destroy(ClassX*obj). Since class ClassX::Metas inherits from the interface class IKFMeta, the functions of static int Type() and int type() are the same (i.e., obtaining the type identifier), static string Name() and string name() are the same (i.e., obtaining the type name), static ClassX*Instantiate() and void*instantiate(int type) are the same (i.e., used to create object instances), static void Destroy(ClassX*obj) and void destroy(void*obj) are the same (i.e., destroying the created object instance), so we will not go into details here.
[0047] Among them, the class ClassX::Meta also defines two attributes, int m_type and string m_name. The static function Static int Type() obtains the type identifier by reading the attribute value of the attribute int m_type; the static function Static string Name() obtains the type name by reading the attribute value of the attribute string m_name.
[0048] The class ClassX includes a static instance (m_smeta), which is an instance of the class ClassX::Meta after instantiation, a function static IKFMeta*smeta() for obtaining type information, and a function smeta() that can obtain type information through the method defined in the interface class IKFMeta. It also includes two virtual functions Virtual IKFMeta*meta() and Virtual void*As(int type). Virtual IKFMeta*meta() is used to obtain type information, and Virtual void*As(int type) is used to convert the type of an object. The object here specifically refers to a pointer, that is, the current type of pointer can be converted to a pointer of any specified type in the inheritance chain. Among them, virtual functions can mainly realize the polymorphism of programming languages (such as C++), improve code reuse and interface standardization, and are more in line with object-oriented design concepts.
[0049] Based on the type conversion system shown in Table 1, the type conversion of the pointer can be realized when the program is running. By creating a type information database, it is convenient to query type information, provide object instance creation methods and object instance destruction methods, enrich the type conversion methods, instance creation methods and instance destruction methods, and provide users with more choices.
[0050] See also Figure 1 , Figure 1 It is a flow chart of a type conversion method disclosed in an embodiment of the present application, which is mainly aimed at type conversion at runtime. The execution subject of the method can be a computer device, and the computer device runs a program. The method can mainly include the following steps:
[0051] S101. When a program is running and a type conversion request for a target object pointer in the program is received, the computer device determines the original type of the target object pointer, and the type conversion request carries a target type identifier.
[0052] Among them, the running program refers to the binary code compiled by the computer device after the program is compiled. Among them, correspondingly, the program includes N types, each type includes type information, and any type information includes a type identifier, and N is an integer greater than 1. More specifically, in the embodiment of the present application, there is the following peer relationship: type = class = type information + business function function (for example, the definition is about the class of sending messages, and the class will contain business function functions for sending and receiving messages), type information = type identifier + inheritance relationship (such as the static instance (m_smeta) in the object type X in Table 1 above contains the type information of type X), type identifier = type name + at least one of the type identifier.
[0053] In the embodiment of the present application, different positive integers are used to distinguish different type numbers, and the type name can be int, float, or the type name of a user-defined type (such as "X" in the above). The program here refers to a programming language, such as C++, Java, etc.
[0054] In a possible implementation, in the process of program running, in order to realize code sharing, it may be necessary to convert the pointer type to realize the fast calling of various functions at runtime and improve the running speed. Therefore, in the process of program running, it is first necessary to obtain a type conversion request for the target object pointer in the program, the type conversion request carries a target type identifier, which can be a type number or a type name, and then obtain the original type of the target object pointer. The target object pointer points to a memory space, which stores an object instance (the object instance is generated after the class is instantiated, and the type information contained in the class will also be contained in the object instance). At the same time, the memory space has a corresponding address information, and the object instance includes the original type information. Then the computer device determines the original type of the target object pointer by specifically querying the object instance from the memory space corresponding to the address pointed to by the target object pointer, and then extracting the original type information from the object instance, and determining the original type according to the original type information, wherein the original type information also includes the original type identifier, and the original type identifier is any one or two of the original type number and the original type name.
[0055] For example, when the following statement exists in the program: PtrC->As(int type), type represents the type identifier of the target type to be converted. Assuming that the target type corresponding to the target type identifier is type E, its purpose is to convert the type of the PtrC pointer to type E. Figure 2 As shown, PtrC points to object instance C, which is instantiated according to type C. Therefore, it can be determined that the original type of the PtrC pointer is type C. Correspondingly, the target type identifier is the same as the type identifier of type E.
[0056] Furthermore, after obtaining the type conversion request and the original type information of the object instance pointed to by the target object pointer, the computing technology equipment also needs to determine whether the target type identifier carried by the type conversion request is the same as the original type identifier. One way is to compare whether the type numbers are the same, and the other is to compare whether the type names are the same. If they are the same, it proves that type conversion is not required and the process ends. If they are equal, execute step S102, that is, execute the step of obtaining the type inheritance relationship tree corresponding to the original type.
[0057] S102: The computer device obtains a type inheritance relationship tree corresponding to the original type.
[0058] The type inheritance relationship tree includes M types of type information, N types of type information include M types of type information, and the types corresponding to the adjacent type information in the type inheritance relationship tree satisfy the type inheritance relationship, that is, the vertical type information always satisfies the type inheritance relationship. N is greater than or equal to M, N types of information may constitute multiple type inheritance relationship trees, and the type inheritance relationship tree constituted by M types of type information may be just one of the trees.
[0059] Since the program contains N types, one or more type inheritance trees can be formed. The purpose of this step is to find the type inheritance tree corresponding to the original pointer type from one or more type inheritance trees.
[0060] In a possible implementation, the type inheritance tree is automatically generated during the running of the program. When the original type pointed to by the target object pointer is known, the type inheritance tree corresponding to the original type can be obtained according to the original type. The type inheritance tree can be as follows: Figure 3 As shown. Type A, Type B, Type C, Type D and Type E together form a type inheritance tree, in which Type D and Type E are the parent classes of Type B, Type B and Type C are the parent classes of Type A, and correspondingly, Type A is the subclass of Type B and Type C, and Type B is the subclass of Type D and Type E.
[0061] S103: The computer device obtains a target type corresponding to the target type identifier in the type inheritance relationship tree, and converts the type of the target object pointer to the target type.
[0062] In a possible implementation, the computer device traverses the type inheritance tree according to the depth-first principle to find type information containing the target type identifier, and uses the type corresponding to the found type information as the target type. Figure 3 As shown, if you want to convert type C to type E, the computer device starts from type A and traverses from bottom to top until type E is found.
[0063] For example, Figure 3 To convert type C to type E, the definition format of type A to type E can be as shown in Table 4. A ClassX is defined, and ClassX includes a type information and a defined As(inttype) function, where X can be any one from A to E, but the type information corresponding to each class is different.
[0064] Table 4
[0065] ClassX Type Information As(int type) Function
[0066] Assume that the current requirement is to convert the target object pointer type C to type E, and the type identifier of type E is the target type identifier, corresponding to Figure 3 , the conversion steps are as follows:
[0067] 1. According to the depth traversal principle, the As(int type) function of type A is called;
[0068] 2. In the As(int type) function of type A, call the Int type() function to obtain the type identifier of type A, and compare the type identifier of type A with the target type identifier;
[0069] 3. If they are not equal, call the As function of the parent class of type A, that is, the As(int type) function of type B, in the As(int type) function of type A.
[0070] 4. In the As(int type) function of type B, call the Int type() function to obtain the type identifier of type B, and compare the type identifier of type B with the target type identifier;
[0071] 5. If they are not equal, call the As function of the parent class of type B, that is, the As(int type) function of type D, in the As(int type) function of type B.
[0072] 6. In the As(int type) function of type D, call the Int type() function to obtain the type identifier of type D, and compare the type identifier of type D with the target type identifier;
[0073] 7. It is found that they are still not equal, but at this time, type D has no parent class, so a null pointer is returned;
[0074] 8. Since the As(int type) function of the first parent class type D of type B returns a null pointer, the As(int type) function of the second parent class of type B, that is, the As(int type) function of type E, will be called;
[0075] 9. In the As(int type) function of type E, call the Int type() function to obtain the type identifier of type E, and compare the type identifier of type E with the target type identifier;
[0076] 10. If the target class identifier and the type identifier of type E are exactly the same, the type of the target object pointer is converted from type C to type E;
[0077] 11. Define a pointer PtrX to store the type E after type conversion, that is, the pointer of type E, that is, PtrX = PtrC->As(ClassE::Meta::Type).
[0078] Figure 3 This is just an example of a type inheritance tree, which has relatively few types. If there are more types of type inheritance trees, the conversion method is the same, that is, to achieve type conversion through layer-by-layer recursion through the principle of deep traversal.
[0079] In a possible implementation, the inheritance relationship between the original type and the target type can be: the original type inherits from the target type, or the target type inherits from the original type, that is, the original type can be a subclass of the target type, or the original type can be a superclass of the target type.
[0080] The inheritance relationship can be further subdivided into direct inheritance and indirect inheritance. Figure 3 As shown, assuming that the original type is type A, the target type is type B, and Figure 3 If there is no type C, it belongs to direct inheritance, and the conversion of type A to type B is a conversion from subtype to parent type; assuming that the original type is type D and the target type is type A, it belongs to indirect inheritance, and the conversion of type D to type A implements the conversion from parent type to subtype; assuming that the original type is type D and the target type is type B, and the inheritance relationship is multiple inheritance, the conversion of type D to type B implements the conversion from parent type to subtype in the multiple inheritance relationship.
[0081] Furthermore, assuming that the original type is type C and the target type is type E, type C is converted to type E. This shows that in the same type inheritance tree, type conversion can be achieved regardless of whether there is a direct inheritance or indirect inheritance relationship.
[0082] In other words, by using the present application, type conversion can be performed not only between a parent type and a child type with a direct inheritance relationship, but also between a parent type and a child type with an indirect inheritance relationship, between a parent type and a child type with multiple inheritance relationships, and between any two types belonging to the same type inheritance relationship tree.
[0083] You can also use the following method to implement type conversion between subtype to parent type and parent type to subtype. When converting a parent type to a subtype, this method needs to detect the object type to see if the two object types are consistent. If they are inconsistent, no conversion will be performed. Only when they are consistent will the type conversion be performed. As shown in Table 5, a type B and a type D are defined, where type D inherits from type B. Two objects are defined in the function. The pointer pb points to object D, which is of type B, and pb2 points to object B, which is also of type B, such as D*pd=dynamic_cast<D*> (pb) means converting the type pointed to by pb to type D. At this time, it will compare whether the object pointed to by pb is D. From the above, pb does point to object D, so type conversion can be performed; for example, D*pd2=dynamic_cast<D*> (pb2) means converting the type pointed to by pb2 to type D. It will also compare whether the object pointed to by pb2 is D. As mentioned above, if pb2 points to object B instead of object D, then no type conversion will be performed. At the same time, the premise of this method is to enable RTTI (Run-Time Type Information). If there is no RTTI support, it cannot be implemented. This method is used to convert from parent type to child type, but the conversion efficiency is low because the object type needs to be detected.
[0084] Table 5
[0085]
[0086] The type conversion method mentioned in Table 5, on the one hand, requires the support of RTTI when performing type conversion, but the performance overhead of RTTI is large and wastes resources. On the other hand, this method needs to check the two types when performing type conversion and convert them when they are consistent, so the conversion rate is slow and time-consuming.
[0087] The type conversion method provided in the embodiment of the present application does not need to check the types of both types, whether it is converting from a parent type to a child type, or from a child type to a parent type, and does not need to rely on third-party tools such as RTTI. Therefore, the conversion efficiency is relatively high and the overhead is relatively small.
[0088] In an embodiment of the present application, during the running of a program, when a type conversion request for a target object pointer in the program is received, the computer device determines the original type of the target object pointer, obtains a type inheritance relationship tree corresponding to the original type, the type corresponding to the adjacent type information in the type inheritance relationship tree satisfies the type inheritance relationship, obtains a target type corresponding to the target type identifier in the type inheritance relationship tree, and converts the type of the target object pointer to the target type. Through this method, the conversion of the pointer type during operation can be achieved without the help of a third-party tool. At the same time, this method does not need to check the original type and the target type, and is relatively efficient in conversion efficiency.
[0089] See also Figure 4 , is a flowchart of a method for creating and deleting an instance provided by a type conversion system disclosed in an embodiment of the application, which implements the instance creation function and instance deletion function of the type conversion system, and can mainly include the following steps:
[0090] S401. The computer device calls a type information acquisition method for each type to acquire type information of each type, and stores the type information in a type information database.
[0091] In a possible implementation, the type information database includes a type information array, that is, in this implementation, the type information is stored in the type information data. Among them, the above-mentioned program also includes N types and registration methods, each type includes type information and type information acquisition methods, and the type information of each type is a private attribute (corresponding to it is a public attribute, which can be directly obtained through the variable name, while the private attribute needs to be called through a function and cannot be called externally). The registration method mentioned here can be the aforementioned registration function Register (IKFMeta*meta). Then the computer device calls the type information acquisition method of each type to obtain the type information of each type in the N types. The type information includes a type identifier, and the type identifier may include a type number and a type name. In this implementation, at least the type number is included, and then the registration method is called, such as the registration function Register (IKFMeta*meta), and the type information of the N types is stored in the type information array, wherein the type number of any type information is the same as the position of any type information in the type information array, as shown in Table 2. Using this method to store type information can improve the efficiency of subsequent queries on type information.
[0092] In one possible implementation, the type information database includes a type information mapping table, that is, in this implementation, the type information is stored in the type information mapping table. Among them, the registration method can also be a registration function. After the computer device obtains the type information through each type of type information acquisition method, similarly, the type information here includes a type identifier, and the type identifier can include a type number and a type name. In this implementation, the type identifier at least includes a type name, and then calls the registration method to store N types of type information in the type information mapping table. The type name of any type information is the same as the key value of any type information in the type information mapping table, as shown in Table 3. Using this method to store type information can also improve the efficiency of subsequent queries on type information.
[0093] S402: When receiving an instance creation request, the computer device extracts a first type identifier carried in the instance creation request.
[0094] In a possible implementation, when a computer device receives an instance creation request during the execution of a program, the computer device extracts the first type identifier carried in the instance creation request from the instance creation request. The first type identifier includes at least one of a first type number and a first type name, and may also include both. The first type identifier is only used to distinguish different type identifiers and does not have a limiting function.
[0095] S403: The computer device searches for first type information corresponding to the first type identifier in a type information database.
[0096] In one possible implementation, if the type information database is a type information array, the computer device searches for the first type information corresponding to the first type number in the type information array according to the first type number in the first type identifier. Since the type number of any type information in the type information array is the same as the position of any type information in the type information array, the first type information can be quickly found according to the position information, that is, the position number, such as using IKFMeta*GetMeta(int type) to obtain the first type information. The first type information also includes a type inheritance relationship.
[0097] In one possible implementation, if the type information database is a type information mapping table, the computer device searches the type information mapping table for the first type information corresponding to the first type name according to the first type name in the first type identifier. Since the type name of any type information is the same as the key value of any type information in the type information mapping table, the first type information can be quickly found according to the key value, such as using IKFMeta*GetMeta(string name) to first obtain the type information. Similarly, the first type information also includes the type inheritance relationship.
[0098] In a possible implementation, if the type information database has both a type information array and a type information mapping table, the first type information can be obtained according to the first type number or the first type name in the first type identifier.
[0099] S404. The computer device creates a first object instance according to the first type information, where the type of the first object instance is the type corresponding to the first type information.
[0100] After the computer device obtains the first type information, it creates a first object instance according to the first type information. In the embodiments of the present application, specifically, the Static ClassX* Instantiate() in the class ClassX::Meta is used to create the object instance. This method mainly instantiates through the passed type identifier and returns a first object pointer, and the first object pointer points to the first object instance. When the first object instance is created, it is stored in the memory space, and the first object pointer points to the address of this memory space.
[0101] For example, in the embodiments of the present application, the Static ClassX* Instantiate() is used to create the object instance, and the definition can be as follows:
[0102] static ClassName* Instantiate() { return new ClassName();}
[0103] Different from the traditional method of creating an instance, the traditional method of creating an instance uses the system keyword "new" to create an instance. For example, new Person p1 = new Person("Wang XX", 25). The created instance has two attributes. One attribute is the name, which is "Wang XX", and the other attribute is the age, which is "25".
[0104] S405. When receiving an instance deletion request, the computer device extracts the first object pointer carried in the instance deletion request.
[0105] Corresponding to step S404, after the first object instance is successfully created, a first object pointer will be generated. Then when the computer device receives an instance deletion request, it will extract the first object pointer carried in the instance deletion request, and the first object pointer points to the address where the first object instance is stored.
[0106] S406. If the first object pointer is not null, delete the first object instance pointed to by the first object pointer.
[0107] In a possible implementation, after obtaining the first object pointer carried in the instance deletion request, the computer device queries the first object instance pointed to by the first object pointer, and determines whether the memory space pointed to by the pointer is empty. If it is empty, no relevant processing is performed, or a null pointer is returned. If the first object pointer is not empty, the first object instance pointed to by the first object pointer is deleted. The deletion method may specifically be to release the memory space pointed to by the first object pointer.
[0108] In the embodiments of the present application, the instance creation method and the instance deletion method provided by the type conversion system are explained. The method provided in the embodiments of the present application enriches the methods of creating object instances and deleting object instances, providing users with more choices.
[0109] See also Figure 5 , Figure 5 5 is a schematic diagram of a type conversion device disclosed in an embodiment of the present application. The device 50 includes: a determination unit 501, an acquisition unit 502, and a processing unit 503, which can be mainly used to execute:
[0110] The determining unit 501 is used to determine the original type of the target object pointer when receiving a type conversion request for a target object pointer in the program during the running of the program; the program includes N types of type information, any type information includes a type identifier, the type conversion request carries a target type identifier, and N is an integer greater than 1;
[0111] An acquiring unit 502 is configured to acquire a type inheritance relationship tree corresponding to the original type, wherein the type inheritance relationship tree includes type information of M types, the N type information includes M type information, and types corresponding to adjacent type information in the type inheritance relationship tree satisfy a type inheritance relationship;
[0112] The acquisition unit 502 is further configured to acquire a target type corresponding to the target type identifier in the type inheritance relationship tree;
[0113] The processing unit 503 is configured to convert the type of the target object pointer into the target type.
[0114] In a possible implementation, the object instance pointed to by the target object pointer includes original type information; the determining unit 501 determines the original type of the target object pointer to:
[0115] Extracting the original type information from the object instance pointed to by the target object pointer, and determining the original type according to the original type information;
[0116] The original type information includes an original type identifier, and the method further includes:
[0117] If the original type identifier and the target type identifier are different, the step of obtaining a type inheritance relationship tree corresponding to the original type is performed.
[0118] In a possible implementation, the acquiring unit 502 acquires the target type corresponding to the target type identifier in the type inheritance relationship tree, including:
[0119] Traversing the type inheritance relationship tree according to the depth-first principle to find type information containing the target type identifier;
[0120] The type corresponding to the found type information is used as the target type.
[0121] In a possible implementation, the original type inherits from the target type; or, the target type inherits from the original type.
[0122] In a possible implementation, the processing unit 503 is further configured to extract the first type identifier carried in the instance creation request when receiving the instance creation request;
[0123] The acquiring unit 502 is further configured to search a type information database for first type information corresponding to the first type identifier;
[0124] The processing unit 503 is further configured to create a first object instance according to the first type information, wherein the type of the first object instance is a type corresponding to the first type information.
[0125] In a possible implementation, the processing unit 503 is further configured to, when receiving an instance deletion request, extract a first object pointer carried in the instance deletion request; the first object pointer points to the first object instance, and the first object pointer is generated after the first object instance is created;
[0126] The processing unit 503 is further configured to delete the first object instance pointed to by the first object pointer if the first object pointer is not empty.
[0127] In a possible implementation, the type information database includes a type information array, the program includes N types and a registration method, each type includes type information and a type information acquisition method, and the type information of each type is a private attribute, and the type identifier includes a type number;
[0128] The processing unit 503 is further configured to:
[0129] Call the type information acquisition method of each type to obtain the type information of each type;
[0130] The registration method is called to store the N types of type information into a type information array, wherein the type number of any type information is the same as the position of any type information in the type information array.
[0131] In a possible implementation, the type information database further includes a type information mapping table, and the type identifier further includes a type name;
[0132] The processing unit 503 is further configured to:
[0133] The registration method is called to store the N types of type information into the type information mapping table, wherein the type name of any type information is the same as the key value of any type information in the type information mapping table.
[0134] In the embodiment of the present application, during the running of the program, when a type conversion request for a target object pointer in the program is received, the determination unit 501 determines the original type of the target object pointer, the acquisition unit 502 acquires the type inheritance relationship tree corresponding to the original type, the types corresponding to the adjacent type information in the type inheritance relationship tree satisfy the type inheritance relationship, the target type corresponding to the target type identifier is acquired in the type inheritance relationship tree, and the processing unit 503 converts the type of the target object pointer to the target type. Through this method, on the one hand, the conversion of the pointer type can be realized when the program is running, and during the conversion process, the target type is determined by the type identifier, and whether the target object pointer can be converted to the target type is determined by querying the type inheritance relationship tree, thereby enriching the type conversion method; on the other hand, no type check is required for either uplink conversion or downlink conversion, which can shorten the conversion process and improve conversion efficiency.
[0135] See also Figure 6 , Figure 660 is a schematic diagram of the structure of a computer device disclosed in an embodiment of the present application, wherein the computer device 60 at least includes a processor 601, a memory 602, and a communication device 603. The processor 601, the memory 602, and the communication device 603 may be connected via a bus or other means. The communication device 603 is used to send and receive data. The memory 602 may include a computer-readable storage medium, the memory 602 is used to store a computer program, the computer program includes computer instructions, and the processor 601 is used to execute the computer instructions stored in the memory 602. The processor 601 (or CPU (Central Processing Unit)) is the computing core and control core of the computer device 60, which is suitable for implementing one or more computer instructions, and is specifically suitable for loading and executing one or more computer instructions to implement the corresponding method flow or corresponding function.
[0136] The embodiment of the present application also discloses a computer-readable storage medium (Memory), which is a memory device in the computer device 60 for storing programs and data. It is understandable that the memory 602 here can include both the built-in storage medium in the computer device 60 and the extended storage medium supported by the computer device 60. The computer-readable storage medium provides a storage space, which stores the operating system of the computer device 60. In addition, one or more computer instructions suitable for being loaded and executed by the processor 601 are also stored in the storage space, and these computer instructions can be one or more computer programs (including program codes). It should be noted that the memory 602 here can be a high-speed RAM memory, or a non-volatile memory (Non-Volatile Memory), such as at least one disk memory; optionally, it can also be at least one computer-readable storage medium located away from the aforementioned processor 601.
[0137] In one implementation, the memory 602 stores a first computer instruction; the processor 601 loads and executes the first computer instruction stored in the memory 602 to implement Figure 2 , Figure 4 Corresponding steps in the method embodiment shown; in a specific implementation, the first computer instruction in the memory 602 is loaded by the processor 601 and executes the following steps:
[0138] During the running of a program, when a type conversion request for a target object pointer in the program is received, determining the original type of the target object pointer; the program includes N types of type information, any type information includes a type identifier, the type conversion request carries a target type identifier, and N is an integer greater than 1;
[0139] Acquire a type inheritance relationship tree corresponding to the original type, the type inheritance relationship tree includes type information of M types, the N type information includes M type information, and types corresponding to adjacent type information in the type inheritance relationship tree satisfy a type inheritance relationship;
[0140] A target type corresponding to the target type identifier is obtained in the type inheritance relationship tree, and the type of the target object pointer is converted into the target type.
[0141] In a possible implementation, the object instance pointed to by the target object pointer includes primitive type information; the processor 601 determines the primitive type of the target object pointer, for:
[0142] Extracting the original type information from the object instance pointed to by the target object pointer, and determining the original type according to the original type information;
[0143] The original type information includes an original type identifier, and the method further includes:
[0144] If the original type identifier and the target type identifier are different, the step of obtaining a type inheritance relationship tree corresponding to the original type is performed.
[0145] In a possible implementation manner, the processor 601 obtains the target type corresponding to the target type identifier in the type inheritance relationship tree, including:
[0146] Traversing the type inheritance relationship tree according to the depth-first principle to find type information containing the target type identifier;
[0147] The type corresponding to the found type information is used as the target type.
[0148] In a possible implementation, the original type inherits from the target type; or, the target type inherits from the original type.
[0149] In a possible implementation manner, the processor 601 is further configured to:
[0150] When receiving an instance creation request, extracting a first type identifier carried in the instance creation request;
[0151] Searching for first type information corresponding to the first type identifier in a type information database;
[0152] A first object instance is created according to the first type information, wherein the type of the first object instance is the type corresponding to the first type information.
[0153] In a possible implementation manner, the processor 601 is further configured to:
[0154] When receiving an instance deletion request, extracting a first object pointer carried in the instance deletion request; the first object pointer points to the first object instance, and the first object pointer is generated after the first object instance is created;
[0155] If the first object pointer is not empty, the first object instance pointed to by the first object pointer is deleted.
[0156] In a possible implementation, the type information database includes a type information array, the program includes N types and a registration method, each type includes type information and a type information acquisition method, and the type information of each type is a private attribute, and the type identifier includes a type number;
[0157] The processor 601 is further configured to:
[0158] Call the type information acquisition method of each type to obtain the type information of each type;
[0159] The registration method is called to store the N types of type information into a type information array, wherein the type number of any type information is the same as the position of any type information in the type information array.
[0160] In a possible implementation, the type information database further includes a type information mapping table, and the type identifier further includes a type name;
[0161] The processor 601 is further configured to:
[0162] The registration method is called to store the N types of type information into the type information mapping table, wherein the type name of any type information is the same as the key value of any type information in the type information mapping table.
[0163] In an embodiment of the present application, during the running of a program, when a type conversion request for a target object pointer in the program is received, the processor 601 determines the original type of the target object pointer, obtains a type inheritance relationship tree corresponding to the original type, the type corresponding to the adjacent type information in the type inheritance relationship tree satisfies the type inheritance relationship, obtains the target type corresponding to the target type identifier in the type inheritance relationship tree, and converts the type of the target object pointer to the target type. Through this method, on the one hand, the conversion of the pointer type can be realized when the program is running, and during the conversion process, the target type is determined by the type identifier, and whether the target object pointer can be converted to the target type is determined by querying the type inheritance relationship tree, thereby enriching the type conversion method; on the other hand, no type check is required for either uplink conversion or downlink conversion, which can shorten the conversion process and improve conversion efficiency.
[0164] According to one aspect of the present application, a computer program product or a computer program is also disclosed, the computer program product or the computer program comprising computer instructions, the computer instructions being stored in a computer-readable storage medium. A processor of a computer device reads the computer instructions from the computer-readable storage medium, and the processor executes the computer instructions, so that the computer device can perform the above Figure 2 , Figure 3 as well as Figure 4 The method in the embodiment corresponding to the flowchart will not be described in detail here.
[0165] It should be noted that, for the above-mentioned method embodiments, for the sake of simplicity, they are all expressed as a series of action combinations, but those skilled in the art should be aware that the present application is not limited by the order of the actions described, because according to the present application, some steps may be performed in other orders or simultaneously. Secondly, those skilled in the art should also be aware that the embodiments described in the specification are all preferred embodiments, and the actions and modules involved are not necessarily required by the present application.
[0166] In the several embodiments provided in this application, it should be understood that the disclosed device can be implemented in other ways. For example, the device embodiments described above are only schematic, and the division of the modules described above is only a logical function division. There may be other division methods in actual implementation, such as multiple modules or components can be combined or integrated into another system, or some features can be ignored or not executed.
[0167] The above is only a specific implementation of the present application, but the protection scope of the present application is not limited thereto. Any person skilled in the art who is familiar with the present technical field can easily think of changes or substitutions within the technical scope disclosed in the present application, which should be included in the protection scope of the present application. Therefore, the protection scope of the present application should be based on the protection scope of the claims.
Claims
1. A type conversion method, characterized in that: The method comprises: During the running of a program, when a type conversion request for a target object pointer in the program is received, the original type of the target object pointer is determined; the program includes type information of N types, any type information includes a type identifier, the type conversion request carries a target type identifier, and N is an integer greater than 1; the program includes N types and a registration method, each type includes type information and a type information acquisition method, and the type information of each type is a private attribute, and the type identifier includes a type number; Acquire a type inheritance relationship tree corresponding to the original type, the type inheritance relationship tree includes type information of M types, the N type information includes M type information, and types corresponding to adjacent type information in the type inheritance relationship tree satisfy a type inheritance relationship; Obtaining a target type corresponding to the target type identifier in the type inheritance relationship tree, and converting the type of the target object pointer to the target type; Call the type information acquisition method of each type to obtain the type information of each type; The registration method is called to store the N types of type information into a type information array included in a type information database, wherein the type number of any type information is the same as the position of any type information in the type information array.
2. The method according to claim 1, characterized in that The object instance pointed to by the target object pointer includes original type information; Determining the original type of the target object pointer includes: Extracting the original type information from the object instance pointed to by the target object pointer, and determining the original type according to the original type information; The original type information includes an original type identifier, and the method further includes: If the original type identifier and the target type identifier are different, the step of obtaining a type inheritance relationship tree corresponding to the original type is performed.
3. The method according to claim 1, characterized in that The acquiring the target type corresponding to the target type identifier in the type inheritance relationship tree includes: Traversing the type inheritance tree according to the depth-first principle to find type information containing the target type identifier; The type corresponding to the found type information is used as the target type.
4. The method according to any one of claims 1 to 3, characterized in that: The original type inherits from the target type; or, The target type inherits from the original type.
5. The method according to claim 1, characterized in that The method further comprises: When receiving an instance creation request, extracting a first type identifier carried in the instance creation request; Searching for first type information corresponding to the first type identifier in a type information database; A first object instance is created according to the first type information, wherein the type of the first object instance is the type corresponding to the first type information.
6. The method according to claim 5, characterized in that After creating the first object instance according to the first type information, the method further includes: When receiving an instance deletion request, extracting a first object pointer carried in the instance deletion request; the first object pointer points to the first object instance, and the first object pointer is generated after the first object instance is created; If the first object pointer is not empty, the first object instance pointed to by the first object pointer is deleted.
7. The method according to claim 5, characterized in that The type information database also includes a type information mapping table, and the type identifier also includes a type name; The method further comprises: The registration method is called to store the N types of type information into the type information mapping table, wherein the type name of any type information is the same as the key value of any type information in the type information mapping table.
8. A type conversion device, characterized in that: The device comprises: A determination unit, configured to determine the original type of a target object pointer when a type conversion request for a target object pointer in the program is received during the running of the program; the program includes type information of N types, any type information includes a type identifier, the type conversion request carries a target type identifier, and N is an integer greater than 1; the program includes N types and registration methods, each type includes type information and a type information acquisition method, and the type information of each type is a private attribute, and the type identifier includes a type number; An acquiring unit, configured to acquire a type inheritance relationship tree corresponding to the original type, wherein the type inheritance relationship tree includes type information of M types, the N type information includes M type information, and types corresponding to adjacent type information in the type inheritance relationship tree satisfy a type inheritance relationship; The acquisition unit is further used to acquire the target type corresponding to the target type identifier in the type inheritance relationship tree; A processing unit, configured to convert the type of the target object pointer to the target type; The acquisition unit is further used to call a type information acquisition method of each type to acquire type information of each type; The processing unit is further used to call the registration method to store the N types of type information into a type information array included in a type information database, wherein the type number of any type information is the same as the position of any type information in the type information array.
9. A computer device, characterized in that: The computer device comprises: Memory for storing computer programs; A processor, running the computer program; and implementing the type conversion method as described in any one of claims 1 to 7.
10. A computer-readable storage medium, characterized in that: The computer-readable storage medium stores one or more computer programs, and the one or more computer programs are suitable for being loaded by a processor and executing the type conversion method according to any one of claims 1 to 7.
11. A computer program product, characterized in that The computer program product includes a computer program, which is stored in a computer-readable storage medium. A processor of a computer device reads the computer program from the computer-readable storage medium, and the processor executes the computer program, so that the computer device executes the type conversion method according to any one of claims 1 to 7.
Citation Information
Patent Citations
Run-time type conversion
US20060150164A1