Recent

Author Topic: Custom Attributes for dummieś example  (Read 449 times)

Thaddy

  • Hero Member
  • *****
  • Posts: 9645
Custom Attributes for dummieś example
« on: August 02, 2019, 08:57:34 am »
Here's a complete example on how you use custom attributes ( create, decorate and retrieve )
It is a bit clearer than current documentation.
Code: Pascal  [Select]
  1. program testattributes;
  2. {
  3.   This is a simple example on how to use custom attributes.
  4.   The class uses a custom attribute to retrieve a date at
  5.   program start-up. This is just for the purpose of demo.
  6.   This demo's:
  7.   - Creation
  8.   - Decoration
  9.   - Retrieval
  10.  
  11.  
  12.   Enjoy,
  13.   Thaddy de Koning
  14. }
  15.    
  16. {$mode delphi}{$H+}{$M+}
  17. {$warn 5079 off} { turn warning experimental off }
  18. uses
  19.   sysutils, typinfo, rtti, classes;
  20.  
  21. type
  22.    {A custom attribute to decorate a class with a certain date }
  23.    ADateTimeAttribute = class(TCustomAttribute)
  24.    private
  25.      FArg:TDateTime;
  26.    public
  27.    { Just to show a Custom Attribute can have mutiple constructors }
  28.      constructor Create(const aArg: TDateTime);overload;
  29.      { We can use the predefined %DATE% compiler include
  30.        In the context of a custom attribute we need a
  31.        constant expression for the default parameter }
  32.      constructor Create(const aArg: String = {$I %DATE%});overload;
  33.     published
  34.      property Date:TDateTime read Farg;
  35.    end;
  36.  
  37.    { A datetime class that is decorated with our custom attribute     }
  38.    { Note you can leave out 'Attribute', the compiler resolves it     }
  39.    { [ADateTime(21237.0)] uses first constructor,displays my birthday }
  40.  
  41.    {This calls the second constructor }
  42.    [ADateTime]
  43.    TMyDateTimeClass = class
  44.    private
  45.      FDateTime:TDateTime;
  46.    public
  47.      constructor create;
  48.    published
  49.      [ADateTime]
  50.      property Day:TDateTime read FDatetime write FdateTime;
  51.    end;
  52.  
  53.    constructor ADateTimeAttribute.Create(const aArg: TDateTime);
  54.    begin
  55.      FArg := aArg;
  56.    end;
  57.  
  58.    constructor ADateTimeAttribute.Create(const aArg: string );
  59.    var
  60.      MySettings:Tformatsettings;
  61.    begin
  62.      { set up the date format according to how
  63.        the compiler formats %DATE% include }
  64.      MySettings :=DefaultFormatSettings;
  65.      MySettings.ShortDateFormat:='yyyymmdd';
  66.      MySettings.DateSeparator :='/';
  67.      { Now convert }
  68.      FArg := StrToDateTime(aArg, MySettings);
  69.    end;
  70.    
  71.    { We query the rtti to set the value }
  72.    constructor TMyDateTimeClass.Create;
  73.    var
  74.     Context : TRttiContext;
  75.     AType : TRttiType;
  76.     Attribute : TCustomAttribute;
  77.    begin
  78.      inherited;  
  79.      Context := TRttiContext.Create;
  80.      try
  81.        AType := Context.GetType(typeinfo(TMyDateTimeClass));
  82.        for Attribute in  AType.GetAttributes do
  83.          if Attribute is ADateTimeAttribute then
  84.            FDateTime := ADateTimeAttribute(Attribute).Date;
  85.      finally
  86.        Context.Free
  87.      end;    
  88.    end;
  89.  
  90.  
  91. var
  92.  Test:TMyDateTimeClass;      
  93. begin
  94.    Test := TMyDateTimeClass.Create;
  95.    try
  96.      writeln('Date is :',DateTimeToStr(Test.Day));
  97.   finally
  98.     test.free;
  99.   end;
  100. end.
« Last Edit: August 09, 2019, 08:24:37 pm by Thaddy »
I am more like donkey than shrek

devEric69

  • Full Member
  • ***
  • Posts: 247
Re: Custom Attributes for dummieś example
« Reply #1 on: August 30, 2019, 01:29:01 pm »
If I understand correctly, with FPC 3.2.0, there is a rtti unit, that contains TRttiContext (which should allows us to introspect with object-oriented RTTI, via oRttiContext.FindType, etc) as since Delphi 2010?
use: Linux 64 bits (Ubuntu 18.04 LTS).
Lazarus version: 2.0.4 (svn revision: 62502M) compiled with fpc 3.0.4 - fpDebug \ Dwarf3.

Thaddy

  • Hero Member
  • *****
  • Posts: 9645
Re: Custom Attributes for dummieś example
« Reply #2 on: August 30, 2019, 01:55:39 pm »
If I understand correctly, with FPC 3.2.0, there is a rtti unit, that contains TRttiContext (which should allows us to introspect with object-oriented RTTI, via oRttiContext.FindType, etc) as since Delphi 2010?
It is more trunk. RTTI is still being worked on and not totally complete. TRttiContext.FindType is not yet implemented. GetType is.
I am more like donkey than shrek

devEric69

  • Full Member
  • ***
  • Posts: 247
Re: Custom Attributes for dummieś example
« Reply #3 on: August 30, 2019, 02:32:35 pm »
All right.
I stay with my FPC version, and still keep my compilation defines in place....
Code: Pascal  [Select]
  1. {$IfDef TRTTI_CONTEXT}
  2. .../...
  3. {$EndIf}
...around some "old" Delphi  bits of source code.
use: Linux 64 bits (Ubuntu 18.04 LTS).
Lazarus version: 2.0.4 (svn revision: 62502M) compiled with fpc 3.0.4 - fpDebug \ Dwarf3.