Delphi.net internal implementation analysis (5)
2.5 Others After understanding several important parts of Borland.Delphi.system, the rest is some zero fragmented sweeping work. 2.5.1 Type Alias is a unique type in Delphi, and a lot of types of alias are defined in the Borland.Delphi.system unit. If the TOBJECT we have analyzed earlier is the alias of System.Object. //----------------------------------------- ,land.delphi.system.pas --type TDateTime = type Double; Extended = type Double; // 80 bit reals are unique to the Intel architecture Comp = Int64 deprecated x86; TGUID = packed record D1: LongWord; D2: Word; D3: Word; D4: array [ 0..7] of Byte; end; // -------------------------------------- --- borland.delphi.system.pas - For the TDATETIME type of Delphi, it is implemented with a Double-8-byte floating point number, compatible OLE automation time format. Inheriting this storage mode in Delphi.NET, without direct use of the System.DateTime structure provided by BCL, but still can use DateTime.Fromoadate and DateTime.TooAdate methods to two-way conversions between System.DateTime and TDATETIME. The format storage description is implemented in the form of floating point numbers as follows (from MSDN) OLE Automation, which is the number of days from Midnight from December 30, 1899. For example, Midnight, December 31, 1899, is expressed as 1.0; 6 o'clock in the morning of January 1, 1999 is 2.25; December 29, 1899 is expressed as -1.0; December 29, 1899 is expressed as 6 o'clock in the morning 1.25. Only the DateTime object that is greater than or equal to the positive or negative 31241376000000000 can be represented as an OLE automation date. Unin-initialized DateTime (ie, an instance of the scale value 0) will convert to an equivalent unaptive OLE automation date (ie the date to 0.0, which means midnight on December 30, 1899). Extended and Comp are just a simple alias. Tguid is just a simple redefine. It is worth noting that the packed key here. In the CLR, the physical location of the class is meaningless, and the CLR may arbitrarily arrange the location of the field to perform byte alignment. In order to interact with existing code, the CLR provides the StructLayOutAttribute property to allow the internal physical structure of the type. In Delphi.NET, you can define this structure by packed keyword must be arranged in memory in the order of the defined order, namely, LayoutKind.sequential. In Delphi.NET, all Record is the subclass of valueetype, ie value type, and operate directly on the stack. 2.5.2 Excetion with TOBJECT, the root Exception of the exception class inheritance chain in delphi is only a alias of the BCL's exception class System.Exception, but only provides source code level compatibility with Class Helper.
//----------------------------------------- ,land.delphi.system.pas - Exception = System.Exception; ExceptionHelper = class helper for Exception private class function CreateMsg (const Msg: string): Exception; function GetHelpContext: Integer; procedure SetHelpContext (AHelpContext: Integer); public // Doc: The help context return zero (0) if exception's helplink property // can not be parsed into an integer property HelpContext: Integer read GetHelpContext write SetHelpContext; // constructor Create. (const Msg: string) is provided by the CLR class class function CreateFmt (const Msg: string; const Args: array of const): Exception; class function CreateHelp (const Msg: string; AHelpContext: Integer): Exception; class function CreateFmtHelp (const Msg: string; const Args: array of const; AHelpContext: Integer): Exception; end EXCEPTIONCLASS = Class of Exception; econvertEction; threadvar _exceptObject: Tobject; function exceptObject: Tobject; function ExceptObject : TOBJECT; / / ---------------------------------------- borland.delphi. System.PAS - Several class functions create * (...) are just packages for the System.Exception constructor, which is used to save the system.exception.Helplink property of the exception-related help file path, is used by Delphi.net Save HelpContext, because in VCL, all exceptions share a help file Tapplication.helpFile. The ExceptObject function is supported by the compiler and provides a means of accessing the abnormality currently thrown. This function is acquired through the SEH exception chain maintained by VCL in Delphi, and in delphi.net, the compiler has manually assigned the thread to the thread to store the variable _EXCEPTOBJECT, and then read it by the ExceptObject function, this is just a syntax One level is compatible with Delphi. However, this preview does not seem to provide Threadvar support, just put it in the global variable of the Borland.Delphi.system unit, as a static member variable that automatically generates the Unit class, is not a thread security! In Borland.Delphi.Classes, even directly of the definition of TTHREAD, not available. It is estimated that it is still developed ... Sigh 2.5.3 assertion (Assert) assertion is responsible for detecting whether a condition is established in debug mode, and failure is abnormal.
//----------------------------------------- ,land.delphi.system.pas --interface {debugging functions} procedure _Assert (const Message, Filename: AnsiString; LineNumber: Integer); type EAssertionFailed = class (Exception) public ShortMessage: string; Filename: string; LineNumber: Integer; end; resourcestring SAssertionFailed = '% s (% s at% d) '; implementationprocedure _Assert (const Message, Filename: AnsiString; LineNumber: Integer); var LException: EAssertionFailed; begin {TODO:? Should we be using System.Diagnostics.Debug.Assert / Fail} {$ MESSAGE WARN 'Assert doesn''t use CreateFmt because it returns the wrong type'} LException: = EAssertionFailed.Create (Format (SAssertionFailed, [Message, Filename, LineNumber])); LException.ShortMessage: = Message; LException.Filename: = Filename; lexception.LineNumber: = lineenumber; raise lexception; end; // ---------------------------------- ------- borland.delphi.system.pas - _ASSERT function is basically a simple packaging of EassertionFailed exceptions. Because Delphi does not provide a predefined macro similar to the __file __, __ line___, so it can only be compiled into the code when the compiler can be used by the compiler in the user, and the current file name, line number and other debug information are compiled into the code. The compiler level provides an assertion implementation.
//----------------------------------------- ,land.delphi.system.pas --function assigned (const agreementle: gchandle): Boolean; Begin Result: = agchandle.isallocated; end; // ------------------------------------------------------------------------------------------------ --------------- borland.delphi.system.pas - // ----------------------- ------------------ gchandle.cs - namespace system.Runtime.Interopservices {public struct gchandle {private INTPTR M_HANDLE; public bool isallocated {get {return m_handle! = INTPTR. Zero;}}}} // ---------------------------------------- Gchandle .cs - Assigned is a reference type variable, similar to Delphi, Delphi.NET directly by detecting whether the reference type value is empty (NULL) is valid, but the value type is compared to 0 . 2.5.4 Random number in delphi.net is basically a simple package for BCL-related classes, and BCL's random number algorithm is as mentally wise, simple features, and BCL's system.security.cryptography.randomnumbergenerator It is much better than the randomability, but it is necessary to pay the price. //----------------------------------------- ,land.delphi.system.pas --interface {random functions} var RandSeed: LongInt = 0; procedure Randomize; function Random (const ARange: Integer): Integer; overload; function Random: Extended; overload; implementationvar LastRandSeed: Integer = -1; RandomEngine: System.Random ; procedure InitRandom; begin if LastRandSeed <> RandSeed then begin if RandSeed = 0 then RandomEngine: = System.Random.Create else RandomEngine: = System.Random.Create (RandSeed); LastRandSeed: = RandSeed; end; end; procedure Randomize; begin LastRandSeed: = -1; RandSeed: = 0; end; function Random (const ARange: Integer): Integer; begin InitRandom; Result: = RandomEngine.Next (ARange); end; function Random: Extended; begin InitRandom; Result: = Randomengine.nextdouble; End; // ----------------------------------------- Borland.delphi.system.pas-
Delphi.net internal implementation analysis (6) I am embarrassed, I don't think there is anything worth mentioning. I have to end 2.5.5. Other borland.delphi.system units Although there are more than the SYSTEM unit in Delphi, but in which It is also full of functions that are commonly used but implements code boring. If the digital processing function set string processing function set command line information acquisition function set (cmdline / paramcount / paramstr) format the output function set (Format, etc.) text file (ie, the TEXT type, and the file type file does not provide support) On / Off / Read / write function set Dynamic array management (simple packaging of System.Array type) Current path and directory operation function set collection type (there is no collection concept in CLR, SET implementation is a simple package of byte array) Other miscellaneous packages Functions, etc. These zero code is basically simple packaging for BCL corresponding functions, which will not be detailed in detail here. 2.5.6 Summary, the introduction of the core unit borland.delphi.system unit in Delphi.NET will report a paragraph. Through the analysis of this unit, we roughly understand the implementation or imitation of some core concepts in delphi.net, but do not exclude that there is change in the official version. Exterior: First of all, thank you for your enthusiastic support. This is the biggest motivation to urge my lazy people (even if it is the end of the grass), I hope this article can understand the upcoming delphi.net, welcome .NET era Help. This series of articles is estimated here, it will be temporarily annihilated, because time is rush, it is not prepared, and the author is limited, and only Delphi.net is implemented different from Delphi. It is just a iceberg. One is already. I originally wanted to expand a point of analysis, but considering that most of the other units in delphi.NET is not very difficult to encapsulate the original Delphi code BCL package transplantation technology, it may be more than those familiar with Delphi directly read the source program. More easier. Therefore, after the analysis involves some Borland.delphi.system, it will hit it, although some tiger head snakes are suspected, but they will be separated from the rear-painted snake to add the name :) Application layer architecture VCL and The CLX that may be supported later, I don't have much effort to write an article. Because of the current VCL code, just the previous VCL code managed, implement the traditional API management window with Windows, and the system.windows.forms.form of BCL is not bound at all. In this way, there is a choice of vcl or clox or system.windows.forms.form ... Sigh, it is good to be evaluated. If there is an explanation in the article, you can follow it. Welcome to I discuss the problem of Delphi.net and CLR. Thank you again! :)