BorlandTalk.com Forum Index BorlandTalk.com
Borland discussion newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Using TMethod to create a list of procedures/functions (of O

 
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> Delphi OO design
View previous topic :: View next topic  
Author Message
R. Hoek
Guest





PostPosted: Mon Oct 06, 2003 8:55 am    Post subject: Using TMethod to create a list of procedures/functions (of O Reply with quote



I'm creating a callback manager. Basicly you can register a callback
procedure/function with the manager and when the callbackmanager's main
callback
procedure is called, all registered callbacks will be called sequetially.

Now when creating a list of these pointers to object-procedures/functions I
typecast these to TMethod.
When comparing 2 methods, do I have to compare the Code or Data member of
the TMethod record? Or do I have to compare both?

I need this to UnRegister the procedure/function, when an Object that
registered a procedure/function with the manager is being destroyed.

--
Greatings,

Ronald Hoek

Applicationdevelopper
ComponentAgro B.V.
Barendrecht

Phone. +31-180-621306
Fax. +31-180-611797

E-mail: [email]hoek (AT) componentagro (DOT) nl[/email] <mailto:Amailhoek (AT) componentagro (DOT) nl>
http://www.componentagro.nl




--
Greatings,

Ronald Hoek

Applicationdevelopper
ComponentAgro B.V.
Barendrecht

Phone. +31-180-621306
Fax. +31-180-611797

E-mail: [email]hoek (AT) componentagro (DOT) nl[/email] <mailto:Amailhoek (AT) componentagro (DOT) nl>
http://www.componentagro.nl



Back to top
Bryan Crotaz
Guest





PostPosted: Mon Oct 06, 2003 5:40 pm    Post subject: Re: Using TMethod to create a list of procedures/functions ( Reply with quote



Personally, I would do it with interfaces, and then TInterfaceList does all
the work for you.

If not, you need to compare both Code and Data of TMethod.

Bryan


"R. Hoek" <nospam*hoek (AT) componentagro (DOT) nl> wrote

Quote:
I'm creating a callback manager. Basicly you can register a callback
procedure/function with the manager and when the callbackmanager's main
callback
procedure is called, all registered callbacks will be called sequetially.

Now when creating a list of these pointers to object-procedures/functions
I
typecast these to TMethod.
When comparing 2 methods, do I have to compare the Code or Data member of
the TMethod record? Or do I have to compare both?

I need this to UnRegister the procedure/function, when an Object that
registered a procedure/function with the manager is being destroyed.

--
Greatings,

Ronald Hoek

Applicationdevelopper
ComponentAgro B.V.
Barendrecht

Phone. +31-180-621306
Fax. +31-180-611797

E-mail: [email]hoek (AT) componentagro (DOT) nl[/email] http://www.componentagro.nl




--
Greatings,

Ronald Hoek

Applicationdevelopper
ComponentAgro B.V.
Barendrecht

Phone. +31-180-621306
Fax. +31-180-611797

E-mail: [email]hoek (AT) componentagro (DOT) nl[/email] http://www.componentagro.nl






Back to top
Mauricio I. Magni
Guest





PostPosted: Mon Oct 06, 2003 6:27 pm    Post subject: Re: Using TMethod to create a list of procedures/functions ( Reply with quote



I compare either Code as Data. This comparison works ok!.
A little question, do you use a TMethod array or another structure?
Thanks.
Mauricio.


"R. Hoek" <nospam*hoek (AT) componentagro (DOT) nl> wrote

Quote:
I'm creating a callback manager. Basicly you can register a callback
procedure/function with the manager and when the callbackmanager's main
callback
procedure is called, all registered callbacks will be called sequetially.

Now when creating a list of these pointers to object-procedures/functions
I
typecast these to TMethod.
When comparing 2 methods, do I have to compare the Code or Data member of
the TMethod record? Or do I have to compare both?

I need this to UnRegister the procedure/function, when an Object that
registered a procedure/function with the manager is being destroyed.

--
Greatings,

Ronald Hoek

Applicationdevelopper
ComponentAgro B.V.
Barendrecht

Phone. +31-180-621306
Fax. +31-180-611797

E-mail: [email]hoek (AT) componentagro (DOT) nl[/email] http://www.componentagro.nl




--
Greatings,

Ronald Hoek

Applicationdevelopper
ComponentAgro B.V.
Barendrecht

Phone. +31-180-621306
Fax. +31-180-611797

E-mail: [email]hoek (AT) componentagro (DOT) nl[/email] http://www.componentagro.nl






Back to top
R. Hoek
Guest





PostPosted: Tue Oct 07, 2003 10:10 am    Post subject: Re: Using TMethod to create a list of procedures/functions ( Reply with quote

Quote:
A little question, do you use a TMethod array or another structure?

Well I use TMethode (but it's a record type)

--
Greatings,

Ronald Hoek



Back to top
John Elrick
Guest





PostPosted: Tue Oct 07, 2003 2:31 pm    Post subject: Re: Using TMethod to create a list of procedures/functions ( Reply with quote

"R. Hoek" <nospam*hoek (AT) componentagro (DOT) nl> wrote

Quote:
I'm creating a callback manager. Basicly you can register a callback
procedure/function with the manager and when the callbackmanager's main
callback
procedure is called, all registered callbacks will be called sequetially.

Now when creating a list of these pointers to object-procedures/functions
I
typecast these to TMethod.
When comparing 2 methods, do I have to compare the Code or Data member of
the TMethod record? Or do I have to compare both?

Well...if the callbacks follow a specific signature:

TMyMethodCallback = procedure(const aParam: string) of object;


IMethodAdapter = interface
procedure execute(const aParam: string);
function equals(const aMethodCallback: TMyMethodCallback): boolean;
end;

TMethodAdapter = class(TInterfacedObject, IMethodAdapter)
private
FMyMethodCallback: TMyMethodCallback;
public
constructor create(const aMethodCallback: TMyMethodCallback);
procedure execute(const aParam: string);
function equals(const aMethodCallback: TMyMethodCallback): boolean;
end;

ICallbackRegistry = interface
procedure register(const aMethodCallback: TMyMethodCallback);
procedure unregister(const aMethodCallback: TMyMethodCallback);
procedure execute(const aParam: string);
end;

TCallbackRegistry = class(TInterfacedObject, ICallbackRegistry)
....stuff ommitted, but trivial
procedure register(const aMethodCallback: TMyMethodCallback);
procedure unregister(const aMethodCallback: TMyMethodCallback);
procedure execute(const aParam: string);
end;


function TMethodAdapter.equals(const aMethodCallback: TMyMethodCallback):
boolean;
begin
result := aMethodCallback = FMyMethodCallback;
end;


....and if they don't:

TMyMethodCallback1 = procedure(const aParam: string) of object;
TMyMethodCallback2 = procedure(const aParam: string; const aInt: integer) of
object;

IExecuteVisitor = interface
procedure callback(const aMethodCallback: TMyMethodCallback1); overload;
procedure callback(const aMethodCallback: TMyMethodCallback2); overload;
end;


IMethodAdapter = interface
procedure execute(const aExecuteVisitor: IExecuteVisitor);
function equals(const aMethodCallback: TMyMethodCallback1): boolean;
overload;
function equals(const aMethodCallback: TMyMethodCallback2): boolean;
overload;
end;



HTH

John



Back to top
Ritchie Annand
Guest





PostPosted: Fri Oct 10, 2003 1:42 am    Post subject: Re: Using TMethod to create a list of procedures/functions ( Reply with quote

In article <3f81b311 (AT) newsgroups (DOT) borland.com>, [email]mmagni (AT) belatrixsf (DOT) com[/email]
says...
Quote:
I compare either Code as Data. This comparison works ok!.
A little question, do you use a TMethod array or another structure?
Thanks.

You really, really should compare BOTH :)

Code refers to the method. Data refers to the object.

If you compare Code but not Data, you won't be able to tell the
difference between the same OnXXX in two different instances of the same
class.

var
AX1, AX2 : TMyClass;
begin
AX1 := TMyClass.Create;
AX2 := TMyClass.Create;
CompareCode(AX1.OnMyEvent, AX2.OnMyEvent);

If you compare Data but not Code, you won't be able to tell the
difference between OnXXX and OnYYY from the same object.

var
AX1 : TMyClass;
begin
AX1 := TMyClass.Create;
CompareData(AX1.OnMyEvent, AX1.OnMyOtherEvent);

You can, with some rather esoteric programming, actually "bind" Code and
Data together manually:

NOTE: This will NOT work on .NET! (Or at least I'd be really really
really surprised if it did ;)

procedure CallMe(ACodePointer : Pointer; AMethodObject : TObject);
type
TPlainMethod = procedure of object;
var
AMethod : TMethod;
ACall : TPlainMethod absolute AMethod; // Makes ACall an alias for
// AMethod
begin
// Bind the method to the object that should run it
AMethod.Code := ACodePointer;
AMethod.Data := AMethodObject;
// Call the method!
ACall;
end;

CallMe(@TMyClass.MyMethod,SomeInstanceOfMyClass);

-- Ritchie Annand
Senior Software Architect
http://www.malibugroup.com
http://nimble.nimblebrain.net
http://wiki.nimblebrain.net

Back to top
Bryan Crotaz
Guest





PostPosted: Fri Oct 10, 2003 4:17 am    Post subject: Re: Using TMethod to create a list of procedures/functions ( Reply with quote

Quote:
NOTE: This will NOT work on .NET! (Or at least I'd be really really
really surprised if it did ;)

procedure CallMe(ACodePointer : Pointer; AMethodObject : TObject);
type
TPlainMethod = procedure of object;
var
AMethod : TMethod;
ACall : TPlainMethod absolute AMethod; // Makes ACall an alias for
// AMethod
begin
// Bind the method to the object that should run it
AMethod.Code := ACodePointer;
AMethod.Data := AMethodObject;
// Call the method!
ACall;
end;

CallMe(@TMyClass.MyMethod,SomeInstanceOfMyClass);


Ye Gods!

I would be horrified if this worked in .NET. How many unsafe things are you
doing in there. absolute for a start!! I love the idea of absolute, but
I've only found one use for it so far, and you've just found another...

Bryan



Back to top
Display posts from previous:   
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> Delphi OO design All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.