DELPHI the atomic world
DELPHI in the use of the software development process, we like cattle and sheep on the grassland a happy, carefree enjoyment of the Object Pascal language of the sun and bring us all kinds of VCL controls provided by the rich aquatic plants. Rise Wangwang immense blue sky, bow taste of the lush grass on the land, who will think about how the universe than the molecular and atomic smaller things? It is a matter philosopher. And philosopher at this time is sitting at the top of tall, look up nebula transform the universe, the ground staring bug crawl, suddenly back to our group of cattle and sheep grazing nodded smiling. Readily Cheqi a grass, gently with the mouth, eyes closed can taste, I do not know this in the grass roots of mouth is what philosopher taste? But his face has been with a satisfied smile.
DELPHI awareness and understanding of the micro-atomic world, we can make a thorough understanding of the macro-DELPHI application structure, and thus thinking in a broader space in the development of our software. It's like, Newton discovered the macroscopic objects movement, but because confuse objects Why is this campaign and distressed, on the contrary, is in the elementary particles of Einstein and macro-object movement between the relativistic experience the joy of life!
Atomic first quarter TObject
TObject?
Object Pascal language is the basic core architecture, but also the origin of VCL controls. We can believe that TObject DELPHI application constitutes one of the atom, of course, they turn Pascal basic syntax elements such as a more subtle particles.
DELPHI is said TObject procedures atoms, because TObject is DELPHI compiler internal support. All of the targets are derived from TObject, even if you did not specify TObject category for the ancestors. TObject is defined in the System Unit, which is part of the system. System.pas unit in the beginning, there is a footnote to the text:
(Predefined constants, types and procedures,)
(And functions (such as True, Integer, or)
(Writeln) do not have actual declarations.)
(Instead they are built into the compiler)
(And are treated as if they were declared)
(At the beginning of the System unit.)
It means that this unit contains pre-defined constants, type, process and function (such as: Ture, Integer or Writeln), they were not the actual statement, but the built-in compiler, and the compiler was the beginning of have already stated that the definition. You can Classes.pas Windows.pas or other source files you join in the project document and compile and debug source code, but you definitely can not System.pas source files will be added to your project documents to compile! DELPHI report will repeat the definition System compiler mistake! Therefore, TObject compiler is provided within the definition of the procedures we use DELPHI development for people who are TObject is something of atoms. TObject System modules in the definition is as follows:
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; procedure AfterConstruction; 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 knocked on the door of TObject atoms, in the end to see what is inside the structure.
We know that all objects TObject is the basic category, then an object in the end? DELPHI of any object is a guide, the object of this guideline specified in the memory space occupied by the one! Although the target is a guideline, but we use when not targeting members of the code written MyObject ^. GetName, but only written MyObject.GetName, this is the Object Pascal language syntax expansion is supported by the compiler . Using C + + Builder friends very clear targets and indicators, as in C + + Builder objects to the definition as a guide. Object pointer at the local target is the object of data storage space, and we have to take a look at the object pointer memory space data structure.
Space objects in the first four bytes refers to the object of the virtual Address Table (VMT - Vritual Method Table). Storage space is the next target members of their own data space, in accordance with the object from the most primitive ancestors of the data members to the object class data members of the order, and every one of the members of the definition of sequence data storage.
Class Virtual Address Table (VMT) from the preservation of such derivative of the original ancestors of all categories to such methods of the process virtual address. Kind of virtual method, and that is using the word vritual reservations statement, virtual object is to achieve polymorphism of the basic mechanism. Although the use of reserved words dynamic statement dynamic method can also be targeted to achieve polymorphism, but this method is not kept in virtual Address Table (VMT), it is only Object Pascal provides another type of a savings of more storage space State Implementation Mechanism, but at the expense of speed at the cost of calls.
Even if we do not own any kind of definition of virtual methods, but there are still the object of such false Address at the table pointer, but the length of the address zero. However, in TObject those defined in the virtual methods, such as Destroy, FreeInstance etc., stored in what? In fact, their approach addresses stored in the relative VMT pointer offset the negative direction in space. In fact, in the negative direction VMT Table offset 76 bytes of data space is the object class system data structure, the data structure is associated with the compiler, and in the future DELPHI version may be changing.
Therefore, you can believe that VMT is a negative offset from the beginning of the address space data structure, data migration zone is negative VMT system data, the VMT data is being offset user data area (the definition of virtual Address Table ). TObject in the definition of the type of information or object run time of the function and process information, and general system VMT data.
A VMT data on behalf of a class, in fact VMT is like! In the Object Pascal, we used TObject, TComponent identifier that category, and so on, they realize the internal DELPHI for their respective VMT data. Using class of reserved words of the definition of the type, is actually related to VMT data at the target.
Application of our procedures, the VMT data is static data, when we compile the completed application, these data have been identified and have been initialized. We can program statement prepared by the visit VMT related information, such as access to the object size, type or run time attribute data, and so forth, or call virtual method or methods of reading names and addresses, and so on operations.
When an object arise, the system will allocate a memory for the object space, and the object of the type associated with the link, therefore, for the distribution of data objects in space in the first four bytes on a point of VMT data pointer.
Let us look at what is the object of death and birth. I watched the three-year-old son on the lawn Guobengluandiao, it is precisely because life had witnessed the birth process, I can Zhenzhen understand the meaning of life and great. Only those who have experienced other dead people will be more understanding and cherish life. So, let us understand what objects and the formation of the demise of the process!
As we all know, with the following statement can be constructed one of the most simple object:
AnObject: = TObject.Create;
Compiler for the realization of its compiler:
TObject corresponding with the VMT basis, the call TObject Create constructor function. Create structures and function calls in the system ClassCreate processes, systems, the process through ClassCreate stored in the category called VMT NewInstance virtual methods. Call NewInstance method is to establish the purpose of the example object space, because we do not override this method, so it is kind of NewInstance TObject. TObjec NewInstance category will be under way in the VMT compiler Table examples of the object initialized in size (InstanceSize), called GetMem process for the allocation of memory object, and then call InitInstance methods will be allocated space initialization. InitInstance space methods will first target in the first four bytes at initialization to the object class counterparts VMT pointer, and then the remaining space will be cleared. After the establishment of instances, a virtual method call AfterConstruction. Finally, the instance data stored in the address pointer AnObject variables, so that AnObject object is created.
Similarly, the following statement can be used to destroy a target:
AnObject.Destroy;
TObject Analysis of the structure function Destroy statement to be false, it is inherent in the system of the virtual method. Destory BeforeDestruction method first calls the virtual method, then call system ClassDestroy process. ClassDestory through the process of VMT FreeInstance virtual method call from FreeInstance Method Invocation object FreeMem release process memory space. In this way, an object in the system disappeared.
Analysis of the object than the object structure of the structure is simple, like the birth of life is a long incubation process, and the death of the relatively short-term, it appears to be an inexorable law.
In the object structure and Analysis of the process of configuration, and call the NewInstance FreeInstance two virtual function, to create and release instances of memory space. The reason why these two functions statements virtual function is to allow users in the preparation requires users to manage its own memory when the special object class (as in some special procedures in the industrial control), the expansion of space.
The statement will be AfterConstruction and BeforeDestruction for virtual function, but also to the future of derivative produced in the target, the opportunity to breathe new birth of the first targets of mouth fresh air, and before the demise of the target object can be allowed to complete the aftermath, it is reasonable matter. In fact, TForm objects and object OnCreate TDataModule events and OnDestroy incident, and is in the TForm TDataModule override the virtual function of these two processes were triggered.
In addition, TObjec also offers a Free method, it is not imaginary, it is targeted to those who confuse it is empty (nil) of the object under the safe release specifically provided. In fact, the confusion is whether the air, logic itself will not clear the problem. However, no one is perfect, and may make mistakes, to avoid accidental use of Free mistakes is a good thing. However, the preparation of the correct procedure can not blindly rely on such a solution, it should be to ensure that the procedures for the correctness of the logic of programming the first target!
Interested friends can read the original code System modules, which use a lot of code is written in assembly language. Carefully friends can be found, TObject Create constructor function and structure Analysis of Destory was not writing any code, in fact, in the state through Debug debugging the CPU window, and clearly reflect Destory Create a compilation of the code. This is because, to create a master DELPHI doors do not want to get too many complicated things available to users, they want users to the concept of the preparation of simple applications, the work will be hidden in the complex system of internal commitment from them. Therefore, the release of the special unit System.pas function of the two code removed, enabling users TObject that is the source of all things, users derived entirely from the category of nothingness, there is nothing wrong with this in itself. Although the reading of these DELPHI the most essential needs of a small amount of code compilation of language knowledge, but reading this code, we can more deeply understand the world DELPHI origin and development of the basic law. Even if do not understand, can at least understand some basic things, we have prepared DELPHI procedures are of great help.
Section II TClass atoms in System.pas units, TClass this is the definition of:
TClass = class of TObject;
It means exactly what it says, is TObject TClass class. TObject itself as a class, so TClass category is the so-called category.
From the concept that TClass is the type of category, that is, categories and so on. However, we know of a class DELPHI, representing a VMT data. Therefore, the type of category can be considered for the VMT data type definition, in fact, it is a pointer at the VMT data type!
In the past the traditional C + + language, is not the definition of the type. Once the compiler on the target fixed, the structure of information has been translated into absolute machine code, will not exist in the memory of the integrity of the information. Some of the more advanced object-oriented language to support the type of dynamic information access and transfer, but often require interpretation of a complex internal mechanism and more system resources. And the Object Pascal language DELPHI absorb some high-level object-oriented language of the outstanding features, and retains procedures can be compiled into machine code directly traditional advantages, compared perfect solution to the advanced capabilities and efficiency of the procedure.
It is precisely because DELPHI in the application to retain the integrity of the type of information can be provided as and as such is the run-time conversion and discriminant senior category of object-oriented features, and the category in which the VMT data played a key role in the core . Interested friends can read AsClass System units and two IsClass compilation process, as they are operator and is the realization of the code to enhance the VMT data on the type and understanding. With the type of category, it can be used as variables to use. Class variables can be understood as a special kind of object, as you can visit visit as object type variable method. For example: We have to look at the procedures fragment below:
Type
TSampleClass = class of TSampleObject;
TSampleObject = class (TObject)
Public constructor Create;
Destructor Destroy; override;
Class function GetSampleObjectCount: Integer;
Procedure GetObjectIndex: Integer;
End;
Var
ASampleClass: TSampleClass;
AClass: TClass;
In this code, we define a category TSampleObject and related TSampleClass types of categories, including two of variables aSampleClass and aClass. In addition, we also TSampleObject definition of the category structure function, structure Analysis function, a class and a method GetSampleObjectCount GetObjectIndex object.
First, we have to understand what type variable aSampleClass and aClass meaning.
Obviously, you can TSampleObject and TObject as often money, and they will be assigned to aClass variable, as if money will be assigned to the 123 regular integer variables i like. Therefore, the class type, category and class variables is the relationship between the types, constants and variables, but in this kind of level, rather than the relationship between the object level. Of course, directly to aSampleClass TObject assignment is not legitimate, because aSampleClass TObject derived class is the class TSampleObject variable, and does not include TObject TSampleClass type compatible with all definitions. On the contrary, will be assigned to aClass TSampleObject variables it is legitimate, because TSampleObject TObject is the derived class, and TClass type compatible. This object variables and type of assignment match completely similar.
Then, let us look at what type of the methods.
The so-called method, refers to the level in the category called methods, such as the definition of GetSampleObjectCount above method, which is used to retain the word class statement method. Category is different from the call in the target level of the object, the object has been familiar to us, and always in the way of access and control all of the common object of this object and a centralized management level to use. TObject in the definition, we can find a lot of class methods, such as ClassName, ClassInfo and NewInstance etc.. Among them, NewInstance also for the virtual definition, the category that virtual methods. This means that you can be derived in the sub-category rewrite NewInstance, a method used special way to structure the object of such examples.
In the type of the methods that you can use this self identifier, but they represent the meaning of objects and methods of self is different. Class methods of self that is their own categories, namely at the VMT pointer, and the object of the self that is object itself, that is, at the Object Data space guidelines. Although the only method to use in the class hierarchy, but you can still object to the adoption of a method to call. For example, you can call statements aObject.ClassName object class TObject method ClassName, because the object pointer at the target data space in the first four bytes of VMT is at the target. On the contrary, you can not call on the object class hierarchy, as TObject.Free statements necessarily illegal. It is worth noting that the class constructor function, structure and Analysis of the object function!
What? Constructor function is the method, Structure Analysis of the object function! Make no mistake?
You see, when you create the object is clearly similar to the use of the following statements:
AObject: = TObject.Create;
He is called the Create method of TObject. And delete objects when used in the following sentence:
AObject.Destroy;
Even if the use of Free Methods release object, the object is called the indirect method Destroy.
The reason is very simple in structure object, object does not exist, existed only category, Create Object can only use methods like. On the contrary, delete certain object is to delete the object already exists, the object is to be released instead of being released.
Finally, the way virtual constructor function discuss the issue.
In traditional C + + language, we can achieve virtual configuration Analysis function, but achieving virtual constructor function it is a problem. This is because, in the traditional C + + language, there was no category type. The example is the global object was compiled data on the overall situation exists in space, the function is compiled at the local object in the stack space mapping example, even if the object is to create dynamic, but also with new operators at a fixed class structure in the heap Examples of space allocation, and only one pair of constructor function has been the object instance to initialize the object only. Traditional C + + there is no real method, even if the so-called static can be defined based on the type of approach has been to achieve its ultimate for a special kind of global function, not to mention the virtual method, virtual method can only be the object of specific examples effective. Therefore, the traditional C + + language that, in specific instances have before, but it has to have the object of the forthcoming structural object itself, which is impossible. Indeed it is impossible, because it would create a self-contradictory logic paradox!
However, it is precisely because in the DELPHI in the category of the type of dynamic information, real virtual method, and constructor function is based on the realization of these key concepts, and so on in order to achieve virtual constructor function. Produced by the object type, object as if the infant growth, and it is the type of mothers, babies themselves do not know what their future will be a kind of a man, but their mothers are the educational methods used to train different people , the reason is interlinked.
It is in the definition of TComponent category, Create constructor function is defined as virtual, can be of different types of controls to achieve their construction method. This is the type TClass create such a great concept, but also a great DELPHI.
Tags: Delphi








0 Comments to “DELPHI the atomic world”
No Comments. Send your comment.
Leave a Reply
You must be logged in to post a comment.