Delphi Atom World (1)

zhaozj2021-02-08  254

Delphi Atom World (1)

By jie

During using the Delphi development software, we are like a group of happy cattle and sheep, carefree enjoy the Object Pascal language, and the rich water grass provided by the sunshine and various VCL controls. Looking up, you are looking at the sky, you can taste the dense grass on the earth, who will want to think how big is the universe, what is more than the molecule and the atom? That is a philosopher. At this time, the philosopher is sitting on the top of the high mountain, looking up at the universe nebula transform, staring at the hilarious crawling, looked back, nodding the cattle and sheep grazing. Smile. If you pull up a grass, you gently contain it in your mouth, close your eyes, try it, don't know what this green grass is in the mouth of philosophers? Just, his face has been with satisfactory smile. Understand and understand Delphi's micro-world world, which allows us to completely understand the macro application structure of Delphi, and develop our software in a broader ideological space. This is like, Newton discovers the movement of the macro object, but because of why it is unclear why this is moving like this, Einstein experiences the happy life of relativism between basic particle regular and macro object! What is the first section of TOBJECT atom TOBJECT? It is the basic core of the Object Pascal language architecture and the origin of various VCL controls. We can think that TOBJECT is one of the atoms constituting the Delphi application, of course, they are constituted by a thinner particles such as basic Pascal syntax elements. Saying Tobject is the atom of the Delphi program because Tobject is supported by the Delphi compiler. All objects are derived from TOBJECT, even if you do not specify TOBJECT for ancestors. TOBJECT is defined at the System unit, which is part of the system. At the beginning of the System.Pas unit, there is such an comment text: {PREDEFINED Constants, Types, Procedures,} {and functions (Such as true, integer, or} {WriteLn) do not have it actual declarations.} {Instead they isy INTO The Compiler} {AND area Treated as if They were declared} {at the beginning, this unit contains predefined constants, types, procedures, and functions (such as: ture, integer or Writeln), they don't actually declare, but the compiler is built into, and the beginning of the compilation is considered to be defined. You can join other source program files such as Classes.Pas or Windows.PAS to compile and debug their source code, but you must not add the System.PAS source file to your project file to compile! Delphi will report the report repeatedly define the System's compilation error! Therefore, TOBJECT is the definition provided inside the compiler. For those we use the Delphi development program, Tobject is atomic things.

TObject defined in the System unit is such that: TObject = class constructor Create; procedure Free; class function InitInstance (Instance: Pointer): TObject; procedure CleanupInstance; function ClassType: TClass; class function ClassName: ShortString; class function ClassNameIs (const name: string): Boolean; class function ClassParent: TClass; class function ClassInfo: Pointer; class function instanceSize: Longint; class function InheritsFrom (AClass: TClass): Boolean; class function MethodAddress (const name: ShortString): Pointer; class function MethodName (Address: Pointer): ShortString; function FieldAddress (const Name: ShortString): Pointer; function GetInterface (const IID: TGUID; out Obj): Boolean; class function GetInterfaceEntry (const IID: TGUID): PInterfaceEntry; class function GetInterfaceTable: PinterFaceTable; Function SafeCallexception (ExceptObject: Tobject; Exceptaddr: Pointer): hResult; Virtual; ProcedureAfter Contrology: HRESULT; truction; virtual; procedure BeforeDestruction; virtual; procedure Dispatch (var Message); virtual; procedure DefaultHandler (var Message); virtual; class function NewInstance: TObject; virtual; procedure FreeInstance; virtual; destructor Destroy; virtual; end; Below, we Will gradually knock on the gate of the TOBJECT atom to see what is the structure. We know, TOBJECT is the basic class of all objects, then what is an object? Any object in Delphi is a pointer, which refers to a space occupied by the object in memory! Although the object is a pointer, we don't have to write like this code myObject ^ .GetName when we reference the object of the object, but can only be written as MyObject.getname, which is supported by the compiler. Friends using C Builder know the relationship between objects and pointers, because the objects of C Builder are defined as pointers.

The object pointer points to the object space for the object stores data, and we analyze the data structure of the memory space points to the object pointer. The head 4 bytes of the object space is the virtual method address table (VMT - Vritual Method Table). The next space is a space that stores the member data of the object itself, and is stored in the order of the data member of the object class from the data member of the object class, and the definition order of the data member in each class. The false method of the class (VMT) saves the process address of the original ancestor derived from the original ancestors of this class to all classes of the class. The virtual method of the class is to use the method of reserving the word Vritual declaration, the virtual method is the basic mechanism for achieving the object polymorphism. Although the dynamic method of reserving the word Dynamic declaration can also realize the polymorphism of the object, but such a method is not saved in the virtual method address table (VMT), it is only another kind of storage space provided by Object Pascal. Polymorphism implementation mechanism, but it is at the expense of call speed. Even if we do not define any class of false methods, the objects of such a class still exist pointers to the virtual method address table, just that the address item is zero. However, those false methods, such as DESTROY, FreeInstance, etc. in TOBJECT, and where is it stored? It turns out that their method address is stored in a space offset in the negative direction of the VMT pointer. In fact, the data space in the negative direction of the VMT table offsets 76 bytes is the system data structure of the object class, which is related to the compiler and may be changed in the future Delphi version. Therefore, you can think that VMT is a data structure starting from the negative offset address space, the negative offset data area is the system data area of ​​the VMT, and the positive offset data of the VMT is the user data area (custom imaginary address table. ). The functions and processes of class information or object runtime information defined in TOBJECT are generally related to system data of VMT. A VMT data represents a class, in fact, VMT is class! In Object Pascal, we use TOBJECT, TCOMPONENT, and the like, which are implemented as their respective VMT data internally in Delphi. The type of class defined by Class of reserves the word definition is actually pointing to the pointer to the associated VMT data. For our app, VMT data is static data. After the compiler compiles to complete our application, these data information has been identified and initialized. The program statement we wrote can access the VMT related information, obtain information such as the size of the object, class name, or run time, or call the name and address of the virtual method or reading method. When an object is generated, the system assigns a memory space for the object and linked the object with the related class, so that the head 4 bytes in the data space allocated for the object, becomes a point VMT data. Pointer. Let's take a look at how the object is born and destroyed. Looking at my three-year-old son is active on the grass, it is because of the birth process of my life, I can really realize the meaning and greatness of life. Only those who have experienced death will be more understanding and cherish life. So let us understand the process of the production and demise of the object! We all know that using the following statement can construct a simple object: anObject: = TOBJECT.CREATE; the compiler implements its compilation as: based on the VMT corresponding to TOBJECT, call the TOBJECT CREATE constructor.

In the CREATE constructor, the system's classcreate process is called, and the system's classcreate process is called the NewInstance virtual method in the class VMT. The purpose of calling the NewInstance method is to establish an instance space of the object, because we do not overload the method, so it is the newInstance of the Tobject class. The NewInstance method of the TOBJEC class will call the getMem process to assign memory for the object based on the object instance size (InstanceSize) initialized in the VMT table, and then call the INITINSTANCE method to initialize the assigned space. The InitInstance method first initializes the heads of the object space to point to the pointer to the object class corresponding to the VMT, and then clear the remaining space. After establishing an object instance, a virtual methodAfterconstruction is called. Finally, save the address pointer of the object instance data into the anobject variable, so that anobject object is born. Similarly, use the following statement to eliminate an object: anObject.destroy; TOBJECT's destructor DESTROY is declared as a false method, and it is also one of the intrinsic methods of the system. The Destory method first calls the BeforeDestruction virtual method, then call the system's ClassDestroy process. The ClassDestory process calls the FreeInstance virtual method by class VMT, and the FreeInstance method is called the FreeMem process to release the memory space of the object. In this way, an object disappears in the system. The sect of the object is simpler than the structure of the object, as if the birth of life is a long birth process, and death is relatively short, which seems to be an inevitable law. During the structure and destructuring of the object, NEWINSTANCE and FreeInstance are invoked to create and release the memory space of the object instance. The reason why these two functions declare as virtual functions, in order to allow users to prepare a special object class that requires users to manage memory (such as in some special industrial control programs), there is extended space. The Afterconstruction and BeForedStruction declared as virtual functions, but also to the future class after the object, have the opportunity to breathe the newly born object breathe the first fresh air, and the object can allow the object to complete the good event before the object is done, this is Reasonable things. In fact, the TFORM object and the TDATAMODULE object's oncreate events and ONDESTROY events are triggered by these two virtual functions overloaded in TForm and TDATAMODULE. In addition, TOBJEC also provides a Free method, it is not a virtual method, which is specially provided for those who can freely release the object if it is empty (NIL). In fact, I can't figure out if the object is empty, it is a problem that the program logic is unclear. However, anyone is not perfect, it may make mistakes, using free to avoid accidental mistakes. However, writing the correct program cannot relisten to such a solution, or should be the first goal of the program's logical correctness! Interested friends can read the original code of the SYSTEM unit, where a large number of code is written in assembly language. Careful friends can find that the Tobject's constructor crete and destructor Destory did not write any code. In fact, in the debug state through the debug's CPU window, it clearly reflects the assembly code of Create and Destory.

转载请注明原文地址:https://www.9cbs.com/read-1291.html

New Post(0)