Method for implementing a binary library of function templates or class templates that can use any type of parameters

By designing Variant class and instantiation function templates or class templates as Variant types, a binary library that supports arbitrary types of data is generated, which solves the problem of the lack of backward compatibility of binary libraries in C++ template technology, and implements the same functions as the source code and Protected intellectual property rights.

CN118550516BActive Publication Date: 2024-11-15SHANXI UNIV
View PDF 0 Cites 0 Cited by

Patent Information

Application Number
CN202410673419.7
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2024-05-28
Publication Date
2024-11-15
Estimated Expiration
2044-05-28

AI Technical Summary

Technical Problem

C++'s function templates and class templates can only generate binary libraries of a single data type, and cannot generate binary libraries that can accept any type of data, resulting in the binary libraries lacking backward compatibility and cannot completely replace the function of the source code.

Method used

Design the Variant class, instantiate the function template or class template to Variant type, generate binary libraries, and convert data into Variant class objects when used by the user, to achieve support for any type of data, including compatibility of newly defined classes.

Benefits of technology

It realizes backward compatibility of binary libraries, can support any type of data like source code, protects the author's intellectual property rights, and improves programming efficiency and reliable performance.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN118550516B_ABST
    Figure CN118550516B_ABST
Patent Text Reader

Abstract

The present invention belongs to the field of software technology, and specifically relates to a method for implementing a binary library of a function template or class template that can use any type of parameter. In order to solve the problem that the binary library generated by the prior art does not support newly defined class data, the present invention redesigns the Variant class: it has the ability to express any type of data, and also supports data of newly defined classes after the binary library is generated, and it supports various operations; for the source code of the function template or class template, it only needs to be instantiated as a Variant type, and the source code does not need to be modified in any way to generate a binary library, which supports any type of data, and the library does not need to be modified to support new defined classes in the future; when the library is used, ordinary data is first converted into a Variant object, and then the object is passed to the binary library, and the result of the operation is equal to directly using ordinary data, and the conversion between ordinary data and Variant objects only requires an assignment statement.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present invention belongs to the field of software technology, and in particular relates to a method for realizing a function template or class template binary library that can use any type of parameters. Background Art

[0002] Since the birth of C++ template technology, as of September 11, 2023, C++ function templates and class templates can only generate binary libraries of a single data type, but cannot generate binary libraries that can accept any type of data.

[0003] If we want to let others use the function template or class template and keep the original function (that is, accept any type of data parameter), the only way is to share the source code. However, doing so means that the author has given up the intellectual property rights of these codes.

[0004] Therefore, in terms of C++ template technology, a method is needed to compile the source code of a function template or a class template into a binary library, but the binary library can accept any type of data parameters like the source code. In this way, the binary library can not only fully implement the template technology like the source code (that is, it can accept any type of data parameters), but also protect the intellectual property rights of the binary library author (because binary files can only be used but cannot be reversed to the source code, this is currently the most effective way to protect intellectual property rights of software products).

[0005] The data of any type mentioned here includes: (1) the data types supported by C++ when the binary library is generated, that is, the basic data types of C++ and the classes known at that time. (2) the new data types that appear after the binary library is generated, that is, the newly defined classes. For any of the above data types, the source code of the function template or class template is fully supported.

[0006] Patent 202311217917.2 (hereinafter referred to as "the prior art") solves the first problem, that is, the binary library can support any basic type of data and classes known at the time (when the binary library is generated). However, after the binary library is generated, the patent does not support newly defined classes, that is, the binary library of the prior art has no backward compatibility.

[0007] However, the source code of the function template or class template has the backward compatibility mentioned above. This is the gap between the binary library of the prior art and the source code of the function template or class template. Therefore, in terms of function, the binary library of the prior art cannot completely replace the source code of the function template or class template.

[0008] Therefore, in order to make the binary library have the same functions as the source code of the function template or class template, a technology is urgently needed, which can enable the binary library to support the newly defined classes after the binary library that supports any basic type data and known classes is generated, and without modifying the binary library, so as to make the binary library backward compatible. Summary of the invention

[0009] The present invention provides a solution to the problem that the binary library in the prior art has no backward compatibility. With this method, after the binary library is generated, the binary library still supports the newly defined class without any modification to the binary library.

[0010] At this point, the problem of backward compatibility of binary libraries in the prior art has been solved. That is, the gap between the binary library in the prior art and the source code of the function template or class template has been completely eliminated. Therefore, in terms of use, the binary library has the same functions as the source code.

[0011] The basic idea of ​​the present invention is: because the function template and class template of C++ can only generate a binary library of a single data type, it is impossible to generate a binary library that can accept any type of data. In order to solve this problem, the solutions of the prior art (i.e., my existing invention) and the present invention are: the library author instantiates the function template or class template source code as a Variant type without modification, and then exports it as a binary library; when the user uses the library, the ordinary data is first converted into an object of the Variant class, and then the object is passed to the library. The result of the library operation is the same as the result of (function template or class template) directly using ordinary data. The Variant class is designed by the inventor. Because the class has the ability to represent any type of data, it is equivalent to the binary library being able to accept any type of data. Moreover, after the binary library is released, the library still supports the newly defined classes of the user without any modification to the library.

[0012] The main points of the present invention are:

[0013] 1. The present invention first designs a Variant class, which can represent any type of parameter. Then, a function template or a C++ class template is instantiated into a binary library of the Variant type. Because the Variant class can represent any type of parameter, it is equivalent to the above binary library being able to accept any type of parameter. When the user uses it, he only needs to generate an object of the Variant class from the data (including objects of the custom class), and then pass the object as a parameter to the binary library.

[0014] 2. Modify the Variant class. In addition to accepting any basic data type, this class can also accept data from a newly defined class. This class supports common mathematical and logical operations. However, if Variant represents a basic data type, the mathematical and logical operations are implemented inside the Variant class; if Variant represents a newly defined class, the mathematical and logical operations are implemented by the user's newly defined class. This is the fundamental reason why the newly defined class is still supported after the binary library is generated.

[0015] 3. This Variant class is transparent to the user. Transparency here means that the author of the binary library does not need to care about the existence of the Variant class and can still compile the code in the library in the original way; while the user of the library only needs to convert the data into a Variant class object and then pass the object as a parameter to the binary library. A simple assignment statement can solve the conversion problem between basic type data or newly defined class object data and Variant class object data.

[0016] 4. For library authors, in order to generate a binary library that can accept any parameters (including newly defined classes) from a function template or C++ class template, they only need to instantiate the function template or class template as a Variant class type and then export it, without making any changes to the source code in the library.

[0017] 5. All Variant classes (including Variant representing newly defined classes), in addition to implementing the functions of C++ function templates and class templates, are also fully supported by the STL library.

[0018] To achieve the above object, the present invention adopts the following technical solution, which includes the following five steps:

[0019] Step 1: Design the Variant class

[0020] This step 1 includes the following 5 parts:

[0021] 1. A struct class_data is added inside the prior art class Variant to store relevant data of the newly defined class, including the address of the newly defined class object and the addresses of relevant mathematical and logical operation functions.

[0022] 2. In the prior art class Variant, a Class type is added in the enum named Type to indicate that the data type stored in the class Variant is a newly defined class.

[0023] 3. Add a new constructor and a new operator= inside the prior art class Variant to construct the newly defined class object as a Variant object. The following two statements demonstrate how to construct a Variant object using the newly defined class object:

[0024] Person obj("ghi",12); / / Declare the newly defined object obj of class Person

[0025] Variant vobj = obj; / / Construct Variant object vobj

[0026] But there is one unique requirement for newly defined classes: the type must be defined in the newly defined class.

[0027] For example, using type = bool (equivalent statement: typedef bool type) statement in a newly defined class defines a type, which is actually of type bool.

[0028] After the object is constructed, the arithmetic and logical operation functions supported by the newly defined class need to be registered.

[0029] For example, the following statement:

[0030]

[0031]

[0032] Two logical operations supported by the newly defined class are registered: operator== and operator<.

[0033] The methods for converting between Variant objects and other types of data are summarized as follows:

[0034] 1) Between basic type data and Variant objects

[0035] Variant object = basic type data; / / Convert basic type data to Variant

[0036] Basic type data = Variant object; / / Convert Variant data to basic type data

[0037] 2) Newly defined class objects and Variant objects

[0038] Variant object = newly defined class object; / / Convert the newly defined class object to a Variant object

[0039] Newly defined class object pointer = Variant object; / / Variant object is converted to a pointer to a newly defined class object

[0040] 4. Computing power

[0041] If Variant represents basic type data, the operation is still the same as the prior art and is provided by the Variant class.

[0042] If the Variant represents a newly defined class, the operations (including mathematical and logical operations, etc.) are implemented by the newly defined class.

[0043] If Variant represents a newly defined class, it is necessary to register various operators of the newly defined class in the Variant object.

[0044] For example:

[0045] Variant vobj = obj; / / construct a Variant object, obj is a newly defined class object

[0046] vobj.set_fun_equ(&the_equ<MY_CLASS> ); / / Register the newly defined class operator==operator

[0047] This registers the operator== operation of the newly defined class. From now on, all == operations involving Variant objects (representing the newly defined class) will be implemented by the operator== operator of the newly defined class MY_CLASS.

[0048] 5. Stream Output

[0049] In order to make Variant objects output through streams like basic data types, overloaded << operators are also supported. For the right operand of the overloaded << operator, if the Variant object represents a basic data type, the right operand is the Variant object itself; if the Variant object represents a newly defined class, the right operand of the overloaded << operator should be the newly defined class object.

[0050] At this point, step - designing the Variant class is completed.

[0051] Step 2: Design the function template or class template to be exported;

[0052] Step 3. Copy the source code of the Variant class to the current project;

[0053] Step 4: Instantiate the template parameters of the designed function template or class template to be exported into Variant type, and the source code of the function template or class template to be exported does not need to be modified;

[0054] Step 5: Export the function template or class template instantiated as a Variant type to a binary library. In fact, it is to export a common (without template parameters) class or function to a binary library.

[0055] Compared with the prior art, the present invention has the following advantages:

[0056] (1) Since the birth of C++ template technology, as of September 11, 2023, C++ function templates and class templates can only generate binary libraries that accept a single data type, but cannot generate binary libraries that can accept data of any type.

[0057] If we want to let others use function templates or class templates while maintaining the original functionality (i.e. accepting data parameters of any type), the only way is to share the source code. However, doing so means that the author of the source code has given up the intellectual property rights of the code.

[0058] The prior art and this patent completely solve the above problems. The prior art can be used to compile the source code of a function template or a class template into a binary library that can accept any basic type data and object data of the known class at that time (before the binary library is generated). However, the prior art still has shortcomings, that is: after the binary library is generated, it cannot accept the data of the newly defined class (after the binary library is generated). This patent is specifically used to solve this problem. That is: after the binary library is generated, for the newly defined class data (after the binary library is generated), the binary library still supports it without any modification to the binary library.

[0059] because:

[0060] Any type of data = basic type data + known class (before binary library generation) data

[0061] +Data of newly defined classes (after binary library generation),

[0062] Combining the existing technology and this patent, the source code of a function template or a class template can be compiled into a binary library that can accept any type of data. In this way, the template technology can be fully implemented like the source code (that is, it can accept any type of data); on the other hand, because the binary library is ultimately provided to the user instead of the source code, the author's intellectual property rights can be effectively protected.

[0063] Therefore, the binary library generated by the present invention has good security and backward compatibility. Security is because users can use the binary library without knowing its source code, which is conducive to protecting the intellectual property rights of the library author; backward compatibility is because even after the binary library is released, the binary library still supports newly defined class data.

[0064] (2) Since the binary library of the present invention can accept parameters of any type, when the algorithm is the same, there is no need to increase the content of the library just because the data type is different. Therefore, when the capacity of the binary library is the same, its functions are more and stronger than before. In other words, when the functions of the binary library are the same, the required binary library capacity is less. Therefore, the computer resources can be effectively saved.

[0065] (3) By using templates and STL technology, programming efficiency and reliability can be greatly improved. However, in the past, if these advanced technologies were used, it would not be possible to generate a binary library that can accept any type of parameters. Now, by using the present invention, these advanced technologies can also be used when compiling a binary library that can accept any type of parameters. Therefore, the technical advantages brought by using templates and STL technology are also applicable when compiling the binary library, such as greatly improving programming efficiency and reliability.

[0066] (4) Prior to the present invention, in the C++ software industry, software components that utilize advanced technologies such as templates and STL could only be provided by producers in the form of source code. For example, Microsoft in the United States also provides these software components in the form of source code. Providing source code means giving up the right to profit from the producers, which means that there is no profit from producing software components (that utilize advanced technologies such as templates and STL). This has severely dampened the enthusiasm of producers and constrained the development of the C++ software industry. It is also not conducive to the technical iteration of these advanced technologies.

[0067] The present invention can share the fruits of labor of these software components (which utilize advanced technologies such as templates and STL) while effectively protecting the rights and interests of library producers. Because this is the first new technology to solve this problem, it will greatly stimulate the enthusiasm of the world (C++ software industry) to produce this binary library, and effectively increase the software output value of the world.

[0068] (5) Based on the above advantages, the present invention may become part of the new standard of C++. If the invention is adopted by the new standard of ISOC++ (International Organization for Standardization C++ Standard Committee), it will be very meaningful to enhance my country's influence in C++ standards. BRIEF DESCRIPTION OF THE DRAWINGS

[0069] Figure 1A relationship diagram between operators, related functions, and registration functions of Variant and the newly defined class;

[0070] Figure 2 For the template function test_fun_variant <variant>Screenshot of the test output;

[0071] Figure 3 For the template class Ctest_variant <variant>Screenshot of the test output. DETAILED DESCRIPTION

[0072] In order to further illustrate the technical solution of the present invention, the present invention is further described below through embodiments.

[0073] A method for implementing a function template library or a class template library that can use any type of parameters in this embodiment specifically includes the following steps:

[0074] Step 1: Design the Variant class

[0075] This step includes the following 5 sub-steps:

[0076] 1. Modify the existing technical class Variant and add a struct class_data to store the relevant data of the newly defined class (the class defined after the binary library is generated is called the newly defined class, the same below), including the address of the newly defined class object and the address of related mathematical and logical operation functions.

[0077]

[0078] 2. Add a Class type to the enum named Type in the Variant class to indicate that the data type stored in the Variant class is a newly defined class.

[0079]

[0080] 3. Add a new constructor and a new operator= in the Variant class to construct the newly defined class object as a Variant object.

[0081]

[0082]

[0083] Because of the operator= operator mentioned above, you can construct a Variant object as follows:

[0084] Person obj("ghi",12); / / Declare the newly defined object obj of class Person

[0085] Variant vobj = obj; / / construct Variant object vobj

[0086] As for examples of using the constructor, they are omitted here.

[0087] 4. Computing power

[0088] If Variant represents basic type data, the operation is still the same as in the prior art and is provided by the Variant class, the details of which have been described in detail in the documentation of the prior art.

[0089] If Variant represents a newly defined class, operations (including mathematical and logical operations, etc.) are implemented by the newly defined class. This is achieved by adding struct class_data and related functions in the Variant class. These functions register various operators of the newly defined class into struct class_data.

[0090] In order to allow the operations of the binary library to be performed by the newly defined class outside (rather than inside the binary library), the binary library user needs to register the operators of the newly defined class with the binary library. Registration requires two steps:

[0091] 1) Register the operator in the relevant function

[0092] 2) Register this related function to the binary library.

[0093] Table 1 below provides the functions that need to be called when registering an operator:

[0094] Table 1 Functions that need to be called when registering an operator

[0095] Operators for newly defined classes Related functions (on the user side) Register function (Variant member function) operator< the_lt set_fun_lt operator> the_gt set_fun_gt operator== the_equ set_fun_equ Other operators …… ……

[0096] For example, if we want to register the operator< operator of the newly defined class Person, first of all, the operator< operator should be defined inside Person. There are two: bool operator<(constPerson&v) and bool operator<(const int&var).

[0097] Then follow step 1), according to Table 1, the related function of operator< is the_lt, so operator< of Person needs to be registered in function the_lt.

[0098] The_lt and other related functions are program frameworks and are relatively complete. In general, binary library users only need to trim existing code without extensive programming, that is, to delete unsupported operations and leave supported ones. In special cases, when the existing code cannot meet the requirements, users can also write their own judgment branch code.

[0099] Because the operator< operator in Person only supports two operations: Person<Person and Person<int, the_lt only retains:

[0100] For Person<Person, if ((string(t1) == self_class) && (string(t2) == self_class)), and for Person<int, if ((string(t1) == self_class) && (is_integer <compile>(t2))) Two branches.

[0101] Then, according to step 2), register this related function the_lt. The example code is as follows:

[0102] Person obj; Variant vobj = obj;

[0103] / / According to Table 1, the registration function of the_lt is set_fun_lt

[0104] vobj.set_fun_lt(&the_lt<MY_CLASS>);

[0105] In this way, for the Variant object vobj (representing the newly defined class Person), when performing vobj < vobj and vobj < int operations, actually, the operator<(const Person& v) and operator<(const int& var) operations provided internally by Person are executed respectively. The reason will be analyzed later.

[0106] 5. Stream output:

[0107] To enable the Variant object to be output through the stream like a basic data type, overloading the << operator is also supported. For the right operand of the overloaded << operator, if the Variant object represents a basic data type, the right operand is the Variant object itself, otherwise an error message is given; if the Variant object represents a newly defined class, the right operand of the overloaded << operator should be the newly defined class object. The source code is as follows:

[0108] 6. Code related to the class Variant (part of which does not belong to the Variant class).

[0109] typedef bool(*relative_fun_prt)(unsigned char*, unsigned char*,

[0110] const char*, const char*) ①

[0111] template<typename T> bool the_lt(unsigned char* v1, unsigned char* v2,

[0112] const char* t1, const char* t2) ②

[0113] ① is the function type definition, which is used in two places: one is in the class Variant::struct class_data, and the other is on the user side, used to represent the related functions (binary operators of the newly defined class).

[0114] There are multiple definitions of operator functions in the class Variant::struct class_data, as follows:

[0115] struct class_data

[0116] {

[0117] unsigned char*ptr_class; / / Address of the newly defined class object

[0118] relative_fun_prt ptr_equ; / / operator == operator-related address

[0119] relative_fun_prt ptr_gt; / / operator<operator-related address

[0120] relative_fun_prt ptr_lt; / / operator> operator-related address

[0121] …

[0122] };

[0123] On the user side, as shown in Table 1, there may be multiple related functions, of which Formula ② is only one, and their types are all Formula ① (ie, relative_fun_prt).

[0124] In order to allow the binary library operations to be performed by the newly defined external class, as described in the previous article: "4. Operational Capability -> Step 2)", it is necessary to register the relevant functions with the binary library using a registration function. The registration functions are all member functions of the Variant class. Here is one of them to help you understand its principle.

[0125] void set_fun_lt(relative_fun_prtpv){class_data.ptr_equ=pv;}

[0126] The user side calls set_fun_lt(&the_lt<MY_CLASS> ), then set formula ② to class_data::ptr_lt.

[0127] The following is the code of operator< within Variant (one of the two in total):

[0128]

[0129] It can be seen that if neither v1 nor v2 is a Variant object representing a newly defined class, then like in the prior art, the v1<v2 operation is executed by the code inside the Variant class.

[0130] If both v1 and v2 are Variant objects representing newly defined classes (type Variant::Class), then the v1<v2 operation calls the class_data::ptr_lt function, which is a function on the user side of the binary library: Equation ②, and Equation ② ultimately calls the operator< operator within the newly defined class.

[0131] Please refer to the appendix Figure 1 which details the relationships among the operators of Variant, the operators of the newly defined class, the related functions (on the user side), and the registration functions (Variant member functions).

[0132] So far, Step 1 - Designing the Variant class ends.

[0133] Step 2 - Design and export the binary library

[0134] This embodiment will design and export a class template Ctest_variant and a function template Test_Variant_Fun.

[0135] This step further includes the following 6 sub - steps:

[0136] 1. Use vs2019 to create a new Regular DLL project (RegularDLL).

[0137] 2. Copy Variant.h and Variant.cpp to the current directory.

[0138] 3. Create a class template to be exported, and its source code is as follows:

[0139]

[0140] The definitions of member functions are as follows:

[0141]

[0142]

[0143] 4. Create another function template to be exported, and its source code is as follows:

[0144]

[0145] 5. Create a global function to instantiate the class template: Ctest_variant <variant>, and export the class at the same time.

[0146] Ctest_variant <variant>*GetTheCon(void)

[0147] {return new Ctest_variant <variant>;}

[0148] Create another full function to instantiate the function template: Test_Variant_Fun <variant>, and export at the same time.

[0149] void test_variant_fun(Variant*p,int n,Variant*pout)

[0150] {return Test_Variant_Fun(p,n,pout);}

[0151] 6. Set up the def file and export the above GetTheCon and test_fun_variant functions.

[0152] This will generate two files: Variant_use.lib and Variant_use.DLL.

[0153] At this point, step 2 of designing and exporting the binary library is completed.

[0154] 7. Program analysis:

[0155] Class member function Ctest_variant <t>::Variant_use and function template Test_Variant_Fun mainly demonstrate the use of various STL functions; class member function Ctest_variant <t>::computer mainly demonstrates mathematical operations on Variant type data.

[0156] We export one class and one function in the binary. The class is: Ctest_variant <variant>, which is the class template Ctest_variant <t>The class instantiated as Variant; the output function is: Test_Variant_Fun <variant>(Variant* p, int n, Variant* pout). It is the function obtained by instantiating the function template Test_Variant_Fun with Variant.

[0157] Step 3: Test the binary library

[0158] This step mainly tests the binary library generated in Step 2.

[0159] This step further includes the following 4 steps:

[0160] 1. Create a new console project (the version of VC in this example is VS2015)

[0161] 2. Copy the 2 files Variant_use.lib and Variant_use.DLL of the binary library generated in the previous test Step 2, and the 2 source files Variant.h and Variant.cpp, a total of 4 files, to the current directory.

[0162] 3a. Test the function template test_fun_variant. The complete test code is as follows:

[0163]

[0164]

[0165]

[0166] The following is the relevant function the_lt after being trimmed by the user, which is used to register the operator< operator of the custom class. Since there are only two types of operator< operators for the custom class (Person<Person and Person<int)), there are only two branches left in the_lt.

[0167]

[0168]

[0169]

[0170]

[0171] Test the template function test_fun_variant <variant>The output is as follows: Figure 2 shown.

[0172] 3b. Test template class Ctest_variant <variant>, the code is as follows:

[0173] Since the code outside the main() function is the same as 3a (template function test_fun_variant), only the code of the main() function is shown here:

[0174]

[0175]

[0176]

[0177] The output of the test template class Ctest_variant is as follows: Figure 3 shown.

[0178] At this point, step 3 of testing the binary library is completed.

[0179] 4. Test results and program analysis:

[0180] Class member function Ctest_variant <t>::Variant_use and function template Test_Variant_Fun mainly demonstrate the use of various STL functions; function Ctest_variant <t>::computer mainly demonstrates mathematical operations on Variant type data.

[0181] We export one class and one function in this binary. The class is:

[0182] Ctest_variant <variant>, which is Ctest_variant <t>Instantiated as a template class of type Variant; the output function is: Test_Variant_Fun <variant>(Variant*p, int n, Variant*pout). It is a template function of the Variant type instantiated by the function template Test_Variant_Fun.

[0183] Then we output the class of the binary library in the console program: Ctest_variant <variant>and the function test_variant_fun <variant>Tested.

[0184] The class Person is defined after the binary library is generated, so it is a newly defined class. Whether this binary library supports the newly defined class Person is the main purpose of this test. The standard for support is: if the result of the binary library operation is the same as the result of the direct operation using the Person object, then it proves that this binary library supports the newly defined class. For example, if the original data is an array of Person objects, it can be sorted using the binary library; as a comparison, without using the binary library, directly using the Person object (operator) to sort, if the results of the two sorts are exactly the same, then it proves that the binary library supports the sorting operation (operator<) of the Person object. Other operations of the Person object are also verified in a similar way.

[0185] In fact, we use the Variant class to pass the Person object to the binary library function. Since the parameter passed to the binary library function is a Variant object (representing the Person object), the judgment process is: pass the Variant object (representing the Person object) as a parameter to the binary library. If the result of running this binary library is the same as the result of directly using the Variant object, then it proves that this binary library supports the newly defined class; otherwise, it shows that this binary library does not support the newly defined class.

[0186] Output from the test Figure 2 , Figure 3 From the above, we can see that if the parameter passed to this binary library function is a Variant object (representing the Person object), the operation result of this binary library is indeed the same as the result of directly using the Variant object. Because by analyzing the operation result of the binary library, we can know that this binary library correctly performs the following tasks on the Person object array: sorting, finding the maximum value, finding the minimum value, and counting the number of elements (meeting a certain condition). The correctness here means that the operation result of the above binary library and the direct operation with the Person object have the same result.

[0187] If the parameter passed to the binary library function is a Variant object (representing basic type data), the operation of the binary library is performed internally by the Variant class as in the prior art.

[0188] In summary, this proves that the binary library supports not only basic type data but also object data of newly defined classes, which means that the present invention has achieved the intended purpose.< / variant> < / variant> < / variant> < / t> < / variant> < / t> < / t> < / variant> < / variant> < / variant> < / t> < / variant> < / t> < / t> < / variant> < / variant> < / variant> < / variant> < / compile> < / variant> < / variant>

Claims

1. A method for implementing a binary library of function templates or class templates that can use any type of parameters, characterized in that: Including designing Variant classes, specifically: (1) The Variant class is transparent to users, that is, the author of the library and the user of the library do not need to know the details of the Variant class. For the author of the library, it is only necessary to instantiate the template parameters of the function template or class template as the Variant type; for the user of the library, when using the binary library, it is only necessary to pass the parameters of the Variant type. (2) By instantiating a function template or class template as a Variant type, you can generate a binary library that can accept parameters of any type without making any changes to the source code; (3) Variant type data supports comprehensive mathematical operations; (4) Variant type data fully supports STL; (5) The Variant class is generic, that is, it can be applied to any function template or class template without modification; (6) The Variant class supports the user's newly defined classes. After a binary library of function templates or class templates that can use any type of parameters is generated, there is no need to modify the binary library. The binary library still supports the user's newly defined classes, but the type must be defined in the newly defined class. (7) Add a struct class_data inside the Variant class to store the relevant data of the newly defined class, including the address of the newly defined class object and the addresses of related mathematical and logical operation functions. Add a Class type in the enum named Type to indicate that the data type stored in the class Variant is the newly defined class. Add a new constructor and a new operator= operator to construct the newly defined class object into a Variant object. The method further comprises: Design the function template or class template to be exported; Copy the source code of the Variant class to the current project; Instantiate the template parameters of the designed function template or class template to be exported into Variant type, and the source code of the function template or class template to be exported does not need to be modified; Export a function template or class template instantiated as a Variant type as a binary library; If you want the binary library to process basic type data, you only need to use an assignment statement to convert the basic type data into a Variant object, and then pass the Variant object as a parameter to the binary library; If you want the binary library to process data of a custom class type, you need to follow three steps: (1) Use an assignment statement to convert a custom class object to a Variant object; (2) Register the operator of the custom class object with the binary library; (3) Pass the Variant object as a parameter to the binary library.