Disclosure of Invention
The invention provides an object processing method, an object processing device, electronic equipment and a storage medium, which are used for solving the technical problem that an object created by a module in the prior art is released by other modules in the actual programming process.
The invention provides an object processing method, which comprises the following steps:
when an object creating instruction aiming at a preset registration implementation class is received, a dynamic extension module corresponding to the registration implementation class is obtained in a preset extensible object creating system;
creating an object of the registration implementation class through the dynamic extension module;
when a release instruction aiming at the object is received, the dynamic expansion module corresponding to the object is obtained in the expandable object creation system;
releasing the object through the dynamic expansion module.
Optionally, before the step of acquiring, in a preset extensible object creation system, a dynamic extension module corresponding to a preset registration implementation class when an object creation instruction for the preset registration implementation class is received, the method further includes:
creating an interface;
declaring the interface to a preset extensible object creation system;
writing an implementation class of the declared interface in the dynamic extension module;
and registering the implementation class to the extensible object creation system to obtain a registered implementation class.
Optionally, the step of creating, by the dynamic extension module, an object of the registration implementation class includes:
acquiring an identifier of the registration implementation class through the dynamic extension module;
obtaining a constructor of the registration implementation class based on the identifier;
and adopting the constructor to create an object of the registration implementation class.
Optionally, the step of releasing the object by the dynamic expansion module includes:
acquiring, by the dynamic extension module, a destructor of the registration implementation class based on the identifier;
and releasing the object by adopting the destructor.
The present invention also provides an object processing apparatus, comprising:
the first dynamic extension module acquisition module is used for acquiring a dynamic extension module corresponding to a registration implementation class in a preset extensible object creation system when an object creation instruction aiming at the preset registration implementation class is received;
the object creating module is used for creating the object of the registration implementation class through the dynamic extension module;
a second dynamic extension module obtaining module, configured to obtain, in the extensible object creation system, the dynamic extension module corresponding to the object when a release instruction for the object is received;
and the object releasing module is used for releasing the object through the dynamic expansion module.
Optionally, the method further comprises:
the interface creating module is used for creating an interface;
the interface declaration module is used for declaring the interface to a preset extensible object creation system;
the implementation class writing module is used for writing the implementation class of the declared interface in the dynamic extension module;
and the implementation class registration module is used for registering the implementation class to the extensible object creation system to obtain a registration implementation class.
Optionally, the object creation module includes:
the identifier acquisition submodule is used for acquiring the identifier of the registration implementation class through the dynamic expansion module;
a constructor obtaining sub-module, configured to obtain a constructor of the registration implementation class based on the identifier;
and the object creating submodule is used for creating the object of the registration implementation class by adopting the constructor.
Optionally, the object releasing module includes:
a destructor acquisition sub-module, configured to acquire, by the dynamic extension module, a destructor of the registration implementation class based on the identifier;
and the object release submodule is used for releasing the object by adopting the destructor.
The invention also provides an electronic device comprising a processor and a memory:
the memory is used for storing program codes and transmitting the program codes to the processor;
the processor is configured to execute the object processing method according to any one of the above instructions in the program code.
The present invention also provides a computer-readable storage medium for storing program code for executing the object processing method as described in any one of the above.
According to the technical scheme, the invention has the following advantages: according to the invention, when an object creating instruction aiming at a preset registration implementation class is received, a dynamic extension module corresponding to the registration implementation class is obtained in a preset extensible object creating system; creating an object of a registration implementation class through a dynamic extension module; when a release instruction aiming at an object is received, a dynamic expansion module corresponding to the object is obtained in an expandable object creating system; and releasing the object through the dynamic expansion module. The technical problem that in the prior art, objects created through modules are released through other modules in the actual programming process is solved.
Detailed Description
The embodiment of the invention provides an object processing method, an object processing device, electronic equipment and a storage medium, which are used for solving the technical problem that an object created by a module in the prior art is released by other modules in the actual programming process.
In order to make the objects, features and advantages of the present invention more obvious and understandable, the technical solutions in the embodiments of the present invention will be clearly and completely described below with reference to the accompanying drawings in the embodiments of the present invention, and it is obvious that the embodiments described below are only a part of the embodiments of the present invention, and not all of the embodiments. All other embodiments, which can be derived by a person skilled in the art from the embodiments given herein without making any creative effort, shall fall within the protection scope of the present invention.
Referring to fig. 1, fig. 1 is a flowchart illustrating steps of an object processing method according to an embodiment of the present invention.
The object processing method provided by the invention specifically comprises the following steps:
step 101, when an object creating instruction for a preset registration implementation class is received, acquiring a dynamic extension module corresponding to the registration implementation class in a preset extensible object creating system;
in the embodiment of the present invention, the dynamic extension module may be a dynamic link library or a component library.
Dynamic Link Library (DLL) is in the Windows operating system. One way to implement the shared library concept. The extensions of these functions ". dll", ". ocx" (library containing ActiveX controls), or ". drv" (legacy system driver). Dynamic linking provides a way that a process can call a function that does not belong to its executable code. The executable code for the functions is located in a DLL file that contains one or more functions that have been translated, linked and stored separately from the process in which they are used. DLLs also facilitate sharing of data and resources. Multiple applications can simultaneously access the contents of a single DLL copy in memory. Updates can be applied to individual modules more easily using a dynamically linked library without affecting other parts of the program. DLLs provide the role of a shared library as modular, allowing code and data in one DLL to be changed in common use by only a few applications without the need to change the applications themselves. Another benefit of modularity is the universal interface usage of the plug-ins. The single interface allows the old module to be seamlessly integrated with the previous application run as the new module does without requiring any changes to the application itself.
In the embodiment of the invention, an extensible object creation system can be constructed in advance, and a dynamic link library is deployed in the system. The number of the dynamic link libraries can be one or more.
In the embodiment of the invention, the extensible object creation system can be subjected to plug-in and componentization programming. Meanwhile, methods such as declaration of an interface, registration of implementation classes, creation and release of objects, loading and unloading of dynamic extension modules, acquisition of registration names of all registered implementation classes of a specified interface and the like can be provided.
In practical application, when an object creation instruction for a preset registration implementation class is received, a dynamic extension module corresponding to the registration implementation class can be acquired in a preset extensible object creation system to create an object.
102, creating an object of a registration implementation class through a dynamic extension module;
after the dynamic extension module for object creation is acquired, object creation of the registration implementation class can be performed through the method for creating the object provided by the extensible object creation system, and then the calling of the relevant method is completed through the interface corresponding to the registration implementation class. The registration implementation class and the interface thereof may be in the same dynamic extension module, or may not be in the same dynamic extension module.
103, when a release instruction for an object is received, acquiring a dynamic expansion module corresponding to the object in an expandable object creation system;
and 104, releasing the object through the dynamic expansion module.
In the embodiment of the present invention, after the object creation is completed, when a release instruction for the object is received, a dynamic extension module corresponding to a registration implementation class may be acquired in the extensible object creation system, so that the object is released through the dynamic extension module.
According to the invention, the dynamic extension module corresponding to the registration implementation class is acquired in the extensible object creation system, so that the object creation is carried out through the dynamic extension module. Meanwhile, according to the corresponding relation between the registration realization class and the dynamic extension module, when the object needs to be released, the same dynamic extension module can be obtained to perform the object release operation.
Referring to fig. 2, fig. 2 is a flowchart illustrating a method for processing an object according to another embodiment of the present invention. The method may specifically comprise the steps of:
step 201, creating an interface;
an interface is a collection of definitions of abstract methods and constant values, and essentially is a special abstract class which only contains the definitions of constants and methods, but no variable and method implementations. An interface generally defines how to use. Including how external programs are called and how calls between their internal programs.
In the embodiment of the invention, a user can write a required interface, such as an ICommand interface, in a general basic module/bottom module. The interface created by the embodiment of the invention needs to meet the following requirements: all methods must be purely virtual and contain no member variables, and the interface must be in a certain.
The pure virtual method is also called abstract function, generally, it only has function name, parameter and return value type, and does not need function body. This means that it has no function implementation and requires derivative classes to be implemented.
Pure virtual methods in C + + generally use 0 as a marker for such functions after signature of the function. In languages such as Java and C #, the function signature is decorated by directly using abstrat as a key word, which means that the method is a pure virtual method.
Step 202, creating a system declaration interface for a preset extensible object;
in embodiments of the present invention, after the interface creation is completed, the interface may be declared to the extensible object creation system.
In one example, declarations may be made by a declaration method provided by the EXTENSIBLE object creation system, such as EXTENSIBLE _ FACTORY _ DECLARE. A single interface only needs to be declared once. Meanwhile, the cpp file of the h file, where any include interface of the module where the interface is located, is defined by a declaration method EXTENSIBLE _ FACTORY _ DEFINE provided by the EXTENSIBLE object creation system.
Step 203, writing the implementation class of the declared interface in the dynamic extension module;
a class is a reflection in a computer of an entity in the real or thought world, which encapsulates data and operations on that data together. And the implementation class is used for realizing the functions of the interface.
In the embodiment of the present invention, the implementation class of the interface may be written in the dynamic extension module, and all pure virtual methods that inherit the interface may be implemented.
The technical requirements for realizing the classes are as follows: the constructor must satisfy the requirement of the extensible object creation system, the type of the first parameter must be const char, the type of the second parameter must be const void, and only two parameters can be contained.
Step 204, registering the implementation class to the extensible object creation system to obtain a registration implementation class;
after writing of the implementation class is completed, registration of the implementation class may be performed in a source file (CPP) of the implementation class of a dynamic extension module of the extensible authoring system.
Specifically, the registration implementation class can be obtained by registering through an EXTENSIBLE object creation system provided method of performing registration, namely, extensille _ factor _ REGISTER.
It should be noted that the specific implementation class can only register with the inherited interface; multiple names can be registered in the same implementation class, and only one registration is needed; the same interface may be registered by multiple implementation classes.
Step 205, when an object creating instruction for a preset registration implementation class is received, acquiring a dynamic extension module corresponding to the registration implementation class in a preset extensible object creating system;
in the embodiment of the present invention, after receiving an object creation instruction for a preset registration implementation class, the dynamic extension module may be loaded by a method provided by the extensible object creation system.
In one example, the method for loading the dynamic extension module may be extensible _ factor _ load _ dlls.
Step 206, creating an object of the registration implementation class through the dynamic extension module;
an object is an abstraction of an objective thing. Are variables having a class type. Classes and objects are the most basic concepts in object-oriented programming techniques. A class is an abstraction of an object, and an object is a concrete instance of a class. A class is a template that describes the behavior and state of a class of objects, and an object is an example of a class, having state and behavior. For example, a dog is a class, a dog is an object, and its state has: color, name, variety; the behaviors are as follows: rocking the tail, calling, eating, etc.
In the embodiment of the invention, after the loading operation of the dynamic extension module is completed, the object of the registration implementation class can be created through the dynamic extension module.
Specifically, the object is created through a method extensible _ factor _ get _ xxx provided by the extensible object creation system, where xxx is the name of the interface class.
In one example, step 206 may include the following sub-steps:
s61, acquiring the identifier of the registration implementation class through the dynamic extension module;
s62, acquiring a constructor of the registration implementation class based on the identifier;
s63, adopting the constructor to create the object of the register implementation class.
In a specific implementation, when creating an object, a constructor of an implementation class may be obtained through a global Map by using a specified identifier, and the constructor is called to create an object of an interface class.
The constructor is mainly used for initializing the object when the object is created, namely, the constructor gives an initial value to the object member variable and is always used in the statement for creating the object together with the new operator. A class may have multiple constructors that can be differentiated based on the number of parameters or the type of parameters.
Step 207, when a release instruction for the object is received, acquiring a dynamic extension module corresponding to the object in the extensible object creation system;
in the embodiment of the invention, when a release instruction for an object is received, the object can be released through a dynamic extension module for creating the object.
And step 208, releasing the object through the dynamic expansion module.
In the embodiment of the present invention, the object may be released through an extensible _ factor _ release _ xxx method for releasing the object provided by the extensible object creation system, where xxx is the name of the interface class.
In one example, step 208 may include the following sub-steps:
s81, acquiring a destructor of the registration implementation class based on the identifier through the dynamic expansion module;
and S82, releasing the object by using a destructor.
In specific implementation, when releasing an object, a destructor of an implementation class can be acquired through a designated identifier through a global Map, and the destructor is called to release the object of an interface class.
The destructor is opposite to the constructor, and when the object ends its life cycle, for example, when the function of the object is called, the destructor can be executed to release the object.
It should be noted that the constructors and destructors of all implementation classes must be in a fixed format, and have uniform parameter types, parameter orders, and parameter numbers.
According to the invention, the dynamic extension module corresponding to the registration implementation class is acquired in the extensible object creation system, so that the object creation is carried out through the dynamic extension module. Meanwhile, according to the corresponding relation between the registration realization class and the dynamic extension module, when the object needs to be released, the same dynamic extension module can be obtained to perform the object release operation.
For ease of understanding, embodiments of the present invention are described below by way of specific examples:
referring to fig. 3, fig. 3 is a process diagram of an object processing method according to an embodiment of the present invention.
As shown in FIG. 3, the extensible object creation system provides the declaration of the interface, the registration of the implementation classes, the creation and release of the object, and other operations, and also provides the methods of loading and unloading the dynamic extension module, obtaining the registered names of all the registered implementation classes of the specified interface, and the like. The realization principle is as follows:
1. the EXTENSIBLE _ FACTORY _ DECLARE is responsible for declaring a FACTORY method of a corresponding interface class, the declared method is EXTENSIBLE _ get _ xxx and the like, wherein xxx is the name of the interface class.
2. The EXTENSIBLE _ FACTORY _ DEFINE is responsible for implementing the declared FACTORY method, and the implementation principle is as follows: when the object is created, the constructor of the implementation class is obtained through the global Map through the appointed identifier, the constructor is called to create the object of the interface class, when the object is released, the destructor of the implementation class is obtained through the global Map through the appointed identifier, and the destructor is called to release the object of the interface class. Therefore, the constructors and destructors of all implementation classes must be in fixed format and have uniform parameter types, parameter orders and parameter numbers.
3. The EXTENSIBLE _ FACTORY _ REGISTER is responsible for registering the relevant information of the implementation class (class name, constructor, destructor, etc.) into the global Map.
4. The application program creates and releases the object through the extensible _ factor _ get _ xxx and the extensible _ factor _ release _ xxx methods of the extensible object creation system.
Examples of their use are as follows:
the/Common module needs to define DLL _ API _ SPEC __ declpsec (dllexport)
Command.h of/Common module
I/ICommand is an interface, all methods are pure virtual methods and do not contain member variables
class ICommand
{
virtual const char*getCommandName()=0;
virtual void setCommandName(const char*cmdName)=0;
};
#ifndef DLL_API_SPEC
#define DLL_API_SPEC__declspec(dllimport)
#endif
EXTENSIBLE_FACTORY_DECLARE(ICommand,DLL_API_SPEC);
Cpp of Common module
#include“Command.h”
EXTENSIBLE_FACTORY_DEFINE(ICommand);
v/CommandImp of component Module CommandSpective
class Command1
{
The constructor must be of the form, fixed type and number of parameters
Command1(const char*csaParam,const void*binParam)
{
}
const char*getCommandName()
{
return 0;
}
void setCommandName(const char*cmdName)
{
}
};
EXTENSIBLE _ FACTORY _ REGISTER (ICommand, Command1, "cmd 1", "Command 1");
class Command2
{
the constructor must be of the form, fixed type and number of parameters
Command2(const char*csaParam,const void*binParam)
{
}
const char*getCommandName()
{
return 0;
}
void setCommandName(const char*cmdName)
{
}
};
EXTENSIBLE _ FACTORY _ REGISTER (ICommand, Command2, "cmd 2", "Command 2");
use of/for ICommand interface
#include“Command.h”
// load first Command component
extensible_factory_load_dlls(“CommandSpectial”);
Creation of corresponding class objects through an extensible factory
ICommand*command1=extensible_factory_get_ICommand(“cmd1”,0);
// Release of corresponding class object through extensible factory
extensible_factory_release_ICommand(command1);
ICommand*command2=extensible_factory_get_ICommand(“cmd2”,0);
extensible_factory_release_ICommand(command2);
Referring to fig. 4, fig. 4 is a block diagram of an object processing apparatus according to an embodiment of the present invention.
An object processing apparatus provided in an embodiment of the present invention includes:
a first dynamic extension module obtaining module 401, configured to, when an object creating instruction for a preset registration implementation class is received, obtain, in a preset extensible object creating system, a dynamic extension module corresponding to the registration implementation class;
an object creating module 402, configured to create an object of the registration implementation class through the dynamic extension module;
a second dynamic extension module obtaining module 403, configured to, when a release instruction for an object is received, obtain, in the extensible object creation system, a dynamic extension module corresponding to the object;
an object release module 404, configured to release the object through the dynamic extension module.
In the embodiment of the present invention, the method further includes:
the interface creating module is used for creating an interface;
the interface declaration module is used for creating a system declaration interface for a preset extensible object;
the implementation class writing module is used for writing the implementation class of the declared interface in the dynamic extension module;
and the implementation class registration module is used for registering the implementation class to the extensible object creation system to obtain a registration implementation class.
In this embodiment of the present invention, the object creation module 402 includes:
the identifier acquisition submodule is used for acquiring the identifier of the registration implementation class through the dynamic expansion module;
a constructor obtaining submodule for obtaining a constructor of the registration implementation class based on the identifier;
and the object creating submodule is used for creating the object of the registration implementation class by adopting the constructor.
In this embodiment of the present invention, the object releasing module 404 includes:
a destructor acquisition sub-module for acquiring a destructor of the registration implementation class based on the identifier through the dynamic extension module;
and the object release submodule is used for releasing the object by adopting a destructor.
An embodiment of the present invention further provides an electronic device, where the device includes a processor and a memory:
the memory is used for storing the program codes and transmitting the program codes to the processor;
the processor is used for executing the object processing method of any embodiment of the invention according to instructions in the program code.
Embodiments of the present invention further provide a computer-readable storage medium, where the computer-readable storage medium is used to store a program code, and the program code is used to execute the object processing method according to any embodiment of the present invention.
It is clear to those skilled in the art that, for convenience and brevity of description, the specific working processes of the above-described systems, apparatuses and units may refer to the corresponding processes in the foregoing method embodiments, and are not described herein again.
The embodiments in the present specification are described in a progressive manner, each embodiment focuses on differences from other embodiments, and the same and similar parts among the embodiments are referred to each other.
As will be appreciated by one skilled in the art, embodiments of the present invention may be provided as a method, apparatus, or computer program product. Accordingly, embodiments of the present invention may take the form of an entirely hardware embodiment, an entirely software embodiment or an embodiment combining software and hardware aspects. Furthermore, embodiments of the present invention may take the form of a computer program product embodied on one or more computer-usable storage media (including, but not limited to, disk storage, CD-ROM, optical storage, and the like) having computer-usable program code embodied therein.
Embodiments of the present invention are described with reference to flowchart illustrations and/or block diagrams of methods, terminal devices (systems), and computer program products according to embodiments of the invention. It will be understood that each flow and/or block of the flow diagrams and/or block diagrams, and combinations of flows and/or blocks in the flow diagrams and/or block diagrams, can be implemented by computer program instructions. These computer program instructions may be provided to a processor of a general purpose computer, special purpose computer, embedded processor, or other programmable data processing terminal to produce a machine, such that the instructions, which execute via the processor of the computer or other programmable data processing terminal, create means for implementing the functions specified in the flowchart flow or flows and/or block diagram block or blocks.
These computer program instructions may also be stored in a computer-readable memory that can direct a computer or other programmable data processing terminal to function in a particular manner, such that the instructions stored in the computer-readable memory produce an article of manufacture including instruction means which implement the function specified in the flowchart flow or flows and/or block diagram block or blocks.
These computer program instructions may also be loaded onto a computer or other programmable data processing terminal to cause a series of operational steps to be performed on the computer or other programmable terminal to produce a computer implemented process such that the instructions which execute on the computer or other programmable terminal provide steps for implementing the functions specified in the flowchart flow or flows and/or block diagram block or blocks.
While preferred embodiments of the present invention have been described, additional variations and modifications of these embodiments may occur to those skilled in the art once they learn of the basic inventive concepts. Therefore, it is intended that the appended claims be interpreted as including preferred embodiments and all such alterations and modifications as fall within the scope of the embodiments of the invention.
Finally, it should also be noted that, herein, relational terms such as first and second, and the like may be used solely to distinguish one entity or action from another entity or action without necessarily requiring or implying any actual such relationship or order between such entities or actions. Also, the terms "comprises," "comprising," or any other variation thereof, are intended to cover a non-exclusive inclusion, such that a process, method, article, or terminal that comprises a list of elements does not include only those elements but may include other elements not expressly listed or inherent to such process, method, article, or terminal. Without further limitation, an element defined by the phrase "comprising an … …" does not exclude the presence of other like elements in a process, method, article, or terminal that comprises the element.
The above-mentioned embodiments are only used for illustrating the technical solutions of the present invention, and not for limiting the same; although the present invention has been described in detail with reference to the foregoing embodiments, it will be understood by those of ordinary skill in the art that: the technical solutions described in the foregoing embodiments may still be modified, or some technical features may be equivalently replaced; and such modifications or substitutions do not depart from the spirit and scope of the corresponding technical solutions of the embodiments of the present invention.