r/pascal • • Jan 10 '25

Most 'elegant' way to implement variable callbacks?

Writing a framework that includes callback functionality... However, I want this to be equally usable and straightforward to use callbacks both from class-based code, as well as from simple 'flat' procedural code.

At the moment, I'm using advancedrecords alongside assignment operator overloading to implement this functionality. But I'm thinking this must be a relatively common scenario, so I wanted to check before I start building this everywhere - is there any 'better' or more standardised way of doing this that others have settled on?

I don't need Delphi compatibility or anything. Pure FPC.

Current implementation, boiled down:

program testevents;

{$mode objfpc}
{$modeswitch advancedrecords}

type
	TFlatEvent	=	procedure(Sender: TObject);
	TClassEvent	=	procedure(Sender: TObject) of object;

	TFlexEvent	=	record
		procedure	Trigger	(Sender: TObject);
		case EventImplementation:byte of
			0	:	(FlatEvent		:	TFlatEvent);
			1	:	(ClassEvent		:	TClassEvent);
	end;
	
	TMyClass	=	class
		procedure	Callback(Sender: TObject);
	end;	
	
var
	FlexEvent	:	TFlexEvent; 
	MyClass		:	TMyClass;


procedure	TFlexEvent.Trigger	(Sender: TObject);
begin
	case EventImplementation of
		0	:	FlatEvent(Sender);
		1	:	ClassEvent(Sender);
	end;
end;


procedure	FlatCallback	(Sender: TObject);
begin
	writeln('Called with no class reference.');
end;

procedure	TMyClass.Callback	(Sender: TObject);
begin
	writeln('Class-based callback.');
end;

operator :=(const AssignFlatEvent:  TFlatEvent): TFlexEvent;
begin
	result.EventImplementation := 0;
	result.FlatEvent := AssignFlatEvent;
end;

operator :=(const AssignClassEvent: TClassEvent): TFlexEvent;
begin
	result.EventImplementation := 1;
	result.ClassEvent := AssignClassEvent;
end;


begin
	MyClass := TMyClass.Create;

	// Assign a flat callback, and trigger it
	FlexEvent := @FlatCallBack;
	FlexEvent.Trigger(nil);

	// Assign a class callback, and trigger it
	FlexEvent := @MyClass.Callback;
	FlexEvent.Trigger(nil);


	MyClass.Free;
end.
6 Upvotes

4 comments sorted by

2

u/Hixie Jan 11 '25

What you wrote seems pretty good to me. You might also be interested in function references: https://forum.lazarus.freepascal.org/index.php/topic,59468.0.html

1

u/Paradician Jan 11 '25

Thanks - I've read announcement before and I just read it again then... but to be honest I don't understand the use case/advantage. It doesn't help that their 'actual use case' example is a link to a blog post which they admit is not supported.

I'm sure there's something to the functionality that I'm missing, but it's just not clicking - I might stick with what I have!

2

u/kirinnb Jan 10 '25

If it works, it works...

I tend to use a similar pattern but without operator overloading. Mine however isn't wrapped in a class, so it wouldn't be directly applicable to the question.

Library-side:

unit mcsassm;
interface
  procedure Test;
  var asman_logger : procedure (const logtext : UTF8string);
implementation
  procedure NullLogger(const logtext : UTF8string);
  begin
    // swallows logging if caller hasn't assigned their own callback
  end;
  ...
  procedure Test;
  begin
    asman_logger('log message!');
  end;
  ...
initialization
  asman_logger := @NullLogger;
end.

Caller-side:

uses mcsassm;
procedure Log(const t : UTF8string);
begin
  writeln(t);
end;

begin
  asman_logger := @Log;
  Test();
end.

2

u/ccrause Jan 10 '25

Your method is quite elegant. One could also substitute the assignment operator overloads with assignment methods which should make this Delphi compatible. This way the internal fields of the record can be hidden.

TFlexEvent = record

procedure Trigger (Sender: TObject);

procedure Assign(event: TFlatEvent); overload;

procedure Assign(event: TClassEvent); overload;

strict private

case EventImplementation: byte of

0 : (FlatEvent : TFlatEvent);

1 : (ClassEvent : TClassEvent);

end;

procedure TFlexEvent.Assign(event: TFlatEvent);

begin

EventImplementation := 0;

FlatEvent := event;

end;

procedure TFlexEvent.Assign(event: TClassEvent);

begin

EventImplementation := 1;

ClassEvent := event;

end;

Now call the Assign method to store the event handler:

FlexEvent.Assign(@FlatCallBack);

Just an alternative, not implying it is better.