[fpc-pascal] Intercept call to a property, method or attribute

Joao Morais jcmoraisjr at gmail.com
Sun Mar 3 18:03:09 CET 2013


Hello list. I need to implement lazy loading for an OPF so I need to
intercept the read call to a property. Lets take the following
classes:

    TClient = class
    private
      FName: string;
    published
      property Name: string read FName write FName;
    end;

    TInvoice = class
    private
      FClient: TClient;
    published
      property Client: TClient read FClient write FClient;
    end;

Tinvoice.Client need to be checked by the OPF before return its actual value.

Some approaches I have outlined so far:

1. A future aspect implementation: so I can verify the Client property
just before the value is returned to the caller.

2. A future (if possible) proxy (like Java proxy) implementation: so
the OPF can place a "fake" TClient instance in the client property,
and intercept calls to the Client instance. Just like Hibernate
actually do.

3. A proxy object between TInvoice and TClient is possible today, with
some effort from the user:

    TInvoice = class
    private
      FClient: specialize TProxy<TClient>;
    published
      property Client: TClient read GetClient write SetClient;
    end;

    GetClient: Result := FClient.GetValue;
    SetClient: FClient.SetInstance(AValue);

4. Finally a manual check is also possible and it's currently my
preferred approach, but also with some effort from the user:

    TInvoice = class
    private
      FClient: TClient;
    published
      property Client: TClient read GetClient write FClient;
    end;

    GetClient: somecheck(Self, @FClient); Result := FClient;

Is there some compiler magic I am missing?


Joao Morais



More information about the fpc-pascal mailing list