Delphi structure extension, can be automatically initialized, anti-initialization, custom copy function.

Source: Internet
Author: User

Reprint: Http://www.raysoftware.cn/?p=518&utm_source=tuicool

Congratulations on the advent of Delphi XE7, Delphi XE7 integrates the dynamic array extensions I implemented earlier in the compiler. http://www.raysoftware.cn/?p=278
Now there is a long history of demand, the QC of the Delphi plate on the top for many years. The official has not been fulfilled.
http://qc.embarcadero.com/wc/qcmain.aspx?d=21729
It is well known that Delphi's record and CPP's struct are distinguished. The struct of CPP can be regarded as the alias of class, can have construct, destructor, can inherit.

Delphi's record began a few years ago to support the method, support operator overloading, the drawback is that you can not write the default constructor. Delphi's help document says it can write its own constructor but cannot write constructor/destructor without parameters. But in practice, unless there is a reference type variable, the struct is not initialized.

This creates a number of problems,

For example, I'm going to implement a string record by using the record operator overload, overloading the +,:=, and so on. But this record must be initialized manually after each declaration, otherwise the contents are not initialized and are random values.

tstring = record</pre>   len:integer;//This is not automatically initialized   Data:pchar;//This is not initialized automatically   

Implement a structure initialization, anti-initialization, copy of the automatic call unit.
As long as your struct references a variable of type Autorecord, the struct can automatically invoke initialization, initialization, and copying.
Like what:

  TTest = Record  _: Autorecord;  Procedure Operator_initialize (); Initialize  procedure operaor_finalize ();//anti-Initialize  procedure operator_assign (const source:ttest); overload;//copy: =  procedure operator_assign (const source:ttest; defaultassign:tdefaultassign (* function that calls default assignment *)); overload;//Copy: =  end;

This will automatically be called when the ttest is used.

var  test:ttest;  Testarray:array[0..1] of Ttest;begin//test.operator_initialize (); Testarray[0]. Operator_initialize (); TESTARRAY[1]. Operator_initialize ();  ..... Test: = Testarray[0]; Test. Operator_assign (Testarray[0]); End;//test.operator_finalize (); Testarray[0]. Operator_finalize (); TESTARRAY[1]. Operator_finalize ();

The code that is called automatically is in the comment. It's fun.
With these features, smart pointers can also be made.

However, there are a few limitations that are limited by the Delphi compiler. It can't be achieved.
1. Cannot be a global variable. The global variable is the compiler's responsibility to initialize the data section of the PE file directly.
2. Cannot be a member variable of a class. The tobject.initinstance of Delphi is simply Fillchar (instance^, instancesize, 0) and no longer cares for the members inside.
3. Cannot be a dynamic array of this type, the initialization of a dynamic array is just a fillchar of 0

In short, the compiler does not support the words will be subject to the above restrictions, just to play for everyone.

Uses  autorecords;type  TTest = Record    _: Autorecord;    I, J, K:integer;    Len:integer;    P:pbyte;    Procedure Operator_initialize (); Initialize    procedure operaor_finalize ();//anti-Initialize    procedure operator_assign (const source:ttest); overload;//Copy: =
   end;  {TTest}procedure ttest.operaor_finalize;begin  freemem (P); end;procedure ttest.operator_assign (const Source: TTest); Begin  Move (source. p^, p^, Len);  I: = source.i + +;  J: = SOURCE.J * 5;  K: = Source.k;end;procedure ttest.operator_initialize;begin  i: = 0;  J: = 1;  K: = 2;  Len: = +;  Getmem (P, Len); End;procedure Tform5.button1click (Sender:tobject); var  a:array [0:1] of ttest;begin//Will call TTEST.O Perator_initialize two times  a[0]: = a[1];//Will call operator_assignend;//Will call Ttest.operaor_finalize two times

Code link
Source

Delphi structure extension, can be automatically initialized, anti-initialization, custom copy function.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.