This application claims foreign priority of Chinese Patent Application No. 202410673419.7, filed on May 28, 2024 and Chinese Patent Application No. 202311217917.2, filed on Sep. 20, 2023 in the China National Intellectual Property Administration, the disclosures of all of which are hereby incorporated by reference.
The present disclosure relates to the technical field of software technology, in particular to a method for generating a binary library with function templates or class templates that support parameters of any type.
Since the advent of template technology in C and C plus plus (C++) (which has been around for over 20 years), if you want others to use function templates or class templates with template parameters while maintaining their original functionality (i.e., accepting any type of data parameters), the only way to achieve this is by sharing the source code. However, doing so means that the author relinquishes the intellectual property rights to that code. Why not generate a binary library instead? If a binary library is generated, others cannot access the source code. The reason is that a binary library can only accept a single type of data parameter, unlike the source code, which can accept any type of data parameter.
Therefore, in C and C++ template technology, there is a need for a method that allows compiling function templates or class templates source code modules into binary libraries, while the binary library being able to accept any type of data parameters just like the source code. Through this way, the binary library can fully implement template technology (i.e., accept any type of data parameters) while also protecting the intellectual property rights of the binary library author.
The “any type of data parameters” referred to here includes: (1) the data types supported by C and C++ at the time the binary library is generated, which means the basic data types and any classes known that defined before the generation of the binary library; (2) newly introduced data types, such as new-defined classes refer to classes defined after the binary library has been generated. The source code of function templates or class templates with template parameters fully supports all of these data types.
Therefore, in order to allow the binary library to have the same function as source code of the function templates or the class templates, the binary library needs to have the following two functions:
This binary library supports data parameters of any basic type and parameters of known classes; Moreover, after the binary library is generated, there is no need to modify it, and the binary library also supports the parameters of newly defined classes. To generate the binary library, the source code of the function template or class template does not need to be modified for this purpose.
The present disclosure completely achieved the two requirements that mentioned above:
The specific solution of the present disclosure is: the programmer instantiates the source code module of the function templates or the class templates to a Variant type (the Variant type is designed by the inventor) without modifying source code, and exporting the Variant type as a binary library. When the library is called by a user, as the binary library only includes two types of parameters: 1, Variant type; and 2, other types, include basic data parameters and data parameters of existing classes (existing classes refer to classes defined before creating the binary library), therefore, if it need to transfer parameters of the Variant type to the binary library, only need to convert the basic type data or the object of the new-defined classes (new-defined classes refer to classes defined after the binary library has been generated) into an object of the Variant class, and then transfer it. As for other types of parameters, they can be transferred directly. The result of running the binary library is the same as the result of directly using ordinary data (running the source code module of function templates or class templates containing template parameters). As the Variant type can represent any type of data, therefore the binary library can accept any type of data. Moreover, after the releasing the binary library, the binary library still supports the new-defined classes.
To realize the above objective, the present disclosure provides a method for generating a binary library with function templates or class templates that support parameters of any type, including:
For example, operator= is used to assign a Variant object (representing floating-point data), the instruction is: Variant vobj=(float) fdata, and the specific executing process is: firstly, the Variant sets the data type in the enum Type: vobj. Type=Float, and then stores the data at the corresponding position f of the union Data, that is, vobj.data.f=fdata.
If operator= is used to assign a Variant object (representing new-defined object), and then operate according to the following instructions:
The specific execution process is as follows: firstly, setting the data type in enum type, that is, vobj.type=Class, and then store the address of the new-defined class object in struct class_data, that is, vobj::class_data::ptr_class=& obj.
But there is only one requirement for the new-defined classes: a type must be defined in the new-defined classes. In addition, after the assignment of the Variant object (representing the new-defined class object) is completed, it is necessary to register the arithmetic and logical operation functions supported by the new-defined classes. For example, the following statement:
Thus, two logical operations that supported by the new-defined classes are registered: operator== and operator <.
Convention methods for the Variant object and other types of data are summarized below:
For example:
This will register the operator== for the new-defined classes. All the operator== of the Variant object (representing the new-defined classes) are implemented by the operator== Of MY_CLASS (the new-defined classes).
To make the Variant object be like with the basic data type, by the stream output, the present disclosure overload << operator: friend ostream & operator << (ostream & os, const Variant & var). Through the << operator, users can stream Variant object like basic data types and output them to standard output; it can also be output as a string object for further processing of the data.
For example, Variant obj=1.234; cout << obj; and the screen output: 1.234.
Here, the Variant type is generated, and the S1 ends.
Compared with the prior arts, the present invention has the following advantages:
The present disclosure creates a binary library for software modules (containing template parameters), and the library can accept any type of parameters. After the binary library is generated, objects of new-defined classes are still supported by the binary library without any modifications. In this way, this disclosure can effectively protect the rights and interests of library producers while also sharing these software components (including template parameters).
Because the present disclosure eliminates the obstacles that hinder the application of the aforementioned advanced technologies, it will effectively stimulate the enthusiasm of enterprises to use the aforementioned advanced technologies to produce binary library, and promote the development of the software industry worldwide.
As shown in
As shown in
The following is part of the addition operation code:
The following code is function prototype of the trigonometric, the logarithmic, and the exponential operations:
The following is part code of operator==:
In order to make the operations of the binary library to be executed by the new-defined classes which is outside, binary library users need to register the operators of the new-defined classes in the binary library. Registration requires two steps:
Table 1 provides the functions that need to be called when registering the operators.
For example, if we want to register the operator < for the new-defined class Person, firstly, the operator < should be defined inside Person. There are two statements: bool operator < (const Person & v) and bool operator <(const int & var)
And then, follow the S15, according to Table 1, the related function of the operator < is the_lt, so the operator < of Person needs to be registered in the function the_lt. The following is partial code of the function the_lt:
The relative of functions like the_lt are program frameworks that are relatively comprehensive. In general, the binary library users only need to clip the existing code without extensive programming, that is, deleting unsupported operations and leaving the supported ones. In case of special circumstances, if this existing code cannot meet the requirements, users can also write their own judgment branch code.
As the operator <inside the Person only support two operation of Person<Person and Person<int, so the_lt only remains two judgment branches:
And then follow S15 to register the related function the_lt, the example code is as follows:
In this way, for the Variant object Vobj (representing the new-defined class Person), when performing Vobj<Vobj and Vobj<int, the actual operations are the operator <(const Person & v) and operator <(const int & var) operations provided by Person, rather than operations within the Variant object. The reasons for this will be analyzed later.
To make the Variant object be like with the basic data type, the operator << is also supported. For the right operand of the overload operator <<must be the Variant object that represents the basic data type, otherwise an error message will be given. The function prototype is as follows:
Through the operator <<users can stream Variant object like basic data types and
In addition, introducing some other code related to the Variant class (some of which do not belong to the Variant class).
In the Variant class::struct class_data, there are multiple operator function definitions, as detailed below:
To have the binary library's operations executed by external new-defined classes, as mentioned in the preceding text: S15. setting operation ability, Operations->d->S2, it is necessary to register the related functions in the binary library using registration functions. Registration functions are member functions of the Variant class. Here, one of them is shown to illustrate its principle.
On the user side, by calling set_fun_lt(&the_lt<MY_CLASS>), the value of class_data:: ptr_lt will be assigned using Equation {circle around (2)}.
Below is the code for one of the two operator<functions within the Variant:
It can be seen that if both v1 and v2 are not Variant objects representing new defined class, the v1<v2 operation is executed by the internal code of the Variant class. If both v1 and v2 are Variant objects representing new defined class (the type is Variant::Class), the v1<v2 operation calls the class_data::ptr_lt function, which is the function on the binary library's user side, i.e., Equation {circle around (2)}, and Equation {circle around (2)} at least calls the operator<defined in the new defined classes.
Please refer to
Thus, the S1, design of the Variant class, is end.
This embodiment will design and export class templates Ctest_variant and function templates Test_Variant_Fun.
Please refers to
The definitions of the member functions are as follows:
Creating a function to instantiate the function templates as follows::
This will generate two files: Variant_use.lib and Variant_use.dll.
At this point, S2 of designing and exporting the binary library is complete.
This step primarily involves testing the binary library generated in S2.
It includes the following three sub-steps:
The following is the user-trimmed function the_lt, which is used to register the operator< for custom classes. Since there are only two types of operator< for the new-defined classes (Person<Person and Person<int), the_lt has only two branches remaining.
Testing the output of the template class Ctest_variant, as shown in test results of S3. Thus, S3 of testing the binary library end.
The member function Ctest_variant <T>::Variant_use primarily demonstrates the use of various STL functions, while the member function Ctest_variant<T>::computer mainly shows mathematical operations on Variant type data.
In the binary library, one output by the binary library is a class, and the other is a function. The class is Ctest_variant <Variant>, which is the instantiation of the Ctest_variant <T> with Variant type. The output function is Test_Variant_Fun <Variant> (Variant*p, int n, Variant*pout), which is the instantiation of the function template Test_Variant_Fun <T> with Variant type.
We tested the class Ctest_variant<Variant> output by the binary library in a console program. For brevity, the analysis of the function template Test_Variant_Fun is omitted here. The main testing tasks are: testing the operation of the binary library with Variant objects (representing basic type data) as parameters, and testing the operation of the binary library with Variant objects (representing the new-defined class Person) as parameters.
From the statement Variant data2 [sz2]={7, 6, 7.34, (double)5.01, 2, 1}, it is evident that Variant can indeed represent int, float, double, etc. The mathematical operations also yielded correct results. For instance: 3.1416+10=13.1416 and sin(3.1416)=−7.3*10−6.
In terms of using STL with the binary library, it correctly performed the following tasks on arrays of Variant objects: sorting, finding the maximum value, finding the minimum value, and counting elements that meet certain criteria. In summary, using Variant objects (representing basic type data) as parameters to the binary library consistently produced correct results.
The objective of this test is to determine whether the binary library supports the new-defined class Person. The criterion for support is: if the results from the binary library operations match those obtained from directly using Person objects, it indicates that the binary library supports the new-defined class. For example, if the original data is an array of Person objects, and the binary library can sort it; by contrast, you sort directly using Person objects (not use the binary library), if the results are identical, it proves that the binary library supports sorting operations on Person objects (operator <). Other operations on Person objects are validated similarly.
In practice, we use the Variant class to transfer Person objects to functions in the binary library. Since the parameters transferred to the binary library functions are Variant objects (representing Person objects), the process is as follows: if the results from the binary library when using Variant objects (representing Person objects) as parameters match the results from direct operations on Person objects, it proves that the binary library supports the new-defined class; otherwise, it indicates that the binary library does not support the new-defined class.
The binary library was tasked with the following operations on an array of Variant objects (representing Person class objects): sorting, finding the maximum value, finding the minimum value, and counting elements (that meet a certain condition). Based on the results shown below, when the parameters passed to the binary library functions are Variant objects (representing Person class objects), the binary library's results indeed match those obtained from directly using Person objects.
The following are test results of S3:
Testing Ctest_variant with Variant class (representing the new-defined class Person) ends. Press any key to continue . . . .
Since both tasks in the test produced correct results, it demonstrates that the binary library not only supports basic data types but also supports objects of new-defined classes. This confirms that the invention achieves its intended purpose.
The method for generating a binary library with function templates or class templates that support parameters of any type is processed by an equipment for generating a binary library with function templates or class templates that support parameters of any type.
As shown in
The memory 1005, as a non-volatile readable storage medium, may include an operating system, network communication module, application program module, and a program for generating a binary library with function templates or class templates that support parameters of any type. The network communication module is mainly used to connect to servers and communicate data with them; And processor 1001 is used to call the program to process the method stored in memory 1005, and execute all steps of the method for predicting thermal runaway in a lithium battery based on capacitive reactance analysis mentioned above.
The above are only some embodiments of the present disclosure, and neither the words nor the drawings can limit the protection scope of the present disclosure. Any equivalent structural transformation made by using the contents of the specification and the drawings of the present disclosure under the overall concept of the present disclosure, or directly/indirectly applied in other related technical fields are included in the protection scope of the present disclosure.
Number | Date | Country | Kind |
---|---|---|---|
202311217917.2 | Sep 2023 | CN | national |
202410673419.7 | May 2024 | CN | national |