Board index » delphi » can a funtion or procedure name be treated like an array?

can a funtion or procedure name be treated like an array?

is it possible to write a function or procedure that can be assigned a
value in an assignment statement? like the port[] array function does in
bpc/fpc??

I don't want it to do anything right now just a dummy entry in a unit
interface and implementation so the port[] code doesn't have to be
removed while new writeport() readport() functions can be added and
used. (there is so much port[] code that I have a hard time commenting
out the bad code and uncommenting the good code - and only want one copy
of the program.

portt can't be an actual array cause index could be $025D and it has to
have the behavior of a funtion.

The parameter passed will be meaningless in the dummy fuinction.

function portt(addre : word) : boolean;
begin
{ do nothing with portt(addr) }
portt := false;
end;

begin
portt(20) := 0;
end.

I tried to find the port[] code in fpc but only found the tport code.
is port[] 'trapped' in fpc and routed to tport??

 

Re:can a funtion or procedure name be treated like an array?


Quote
> is it possible to write a function or procedure that can be assigned a
> value in an assignment statement? like the port[] array function does in
> bpc/fpc??

   I don't really understand your intent here, but you can use @proc_name
as a value: in an array of names for a "computed goto" kind of thing.

Re:can a funtion or procedure name be treated like an array?


port[] array can be assigned a value like an array port[value] := $123;
or return a value as a function. a := port[value].
I can understand an array or a function but not both in one.
Quote
Mike Copeland wrote:

> > is it possible to write a function or procedure that can be assigned a
> > value in an assignment statement? like the port[] array function does in
> > bpc/fpc??

>    I don't really understand your intent here, but you can use @proc_name
> as a value: in an array of names for a "computed goto" kind of thing.

Re:can a funtion or procedure name be treated like an array?


lol yes edit.com has a search/replace

but without bugging the guys at fpc - I'd lvoe to how how they do it
with port[]
I think it is in the compiler - parsing the 'code text' and doing
different things depending on whether it is in 'function mode' or 'array
mode' (reading or writing the port).

It seems like a very interesting programming construct to say the least.

Quote
JKe7306087 wrote:
> I don't think this is possible in any version of TP that I know of... at least
> I've never seen it. If your text editor can do a search and replace using
> regular expressions it might not be to hard to comment these lines out

Re:can a funtion or procedure name be treated like an array?


Quote
>s it possible to write a function or procedure that can be assigned a
>value in an assignment statement? like the port[] array function does in
>bpc/fpc??

>I don't want it to do anything right now just a dummy entry in a unit
>interface and implementation so the port[] code doesn't have to be
>removed while new writeport() readport() functions can be added and
>used. (there is so much port[] code that I have a hard time commenting
>out the bad code and uncommenting the good code - and only want one copy
>of the program.

>portt can't be an actual array cause index could be $025D and it has to
>have the behavior of a funtion.

>The parameter passed will be meaningless in the dummy fuinction.

>function portt(addre : word) : boolean;
>begin
>{ do nothing with portt(addr) }
>portt := false;
>end;

>begin
>portt(20) := 0;
>end.

I don't think this is possible in any version of TP that I know of... at least
I've never seen it. If your text editor can do a search and replace using
regular expressions it might not be to hard to comment these lines out

James

Re:can a funtion or procedure name be treated like an array?


Quote
Mike Copeland wrote:

> > is it possible to write a function or procedure that can be assigned a
> > value in an assignment statement? like the port[] array function does in
> > bpc/fpc??

>    I don't really understand your intent here, but you can use @proc_name
> as a value: in an array of names for a "computed goto" kind of thing.

He could have looked at typed procedures. But I think that he had
better an array of goto labels, which he is familiar with ;-))

Regards,
--
Franz Glaser, Glasau 3, A-4191 Vorderweissenbach Austria +43-7219-7035-0
Muehlviertler Elektronik Glaser.  Industrial control and instrumentation
http://members.eunet.at/meg-glaser/    http://members.xoom.com/f_glaser/
http://www.geocities.com/~franzglaser/            http://start.at/bedarf

Re:can a funtion or procedure name be treated like an array?


  enztec <enz...@xmission.com> wrote:

Quote
> is it possible to write a function or procedure that can be assigned a
> value in an assignment statement? like the port[] array function does
in
> bpc/fpc??

> I don't want it to do anything right now just a dummy entry in a unit
> interface and implementation so the port[] code doesn't have to be
> removed while new writeport() readport() functions can be added and
> used. (there is so much port[] code that I have a hard time commenting
> out the bad code and uncommenting the good code - and only want one
copy
> of the program.

> portt can't be an actual array cause index could be $025D and it has
to
> have the behavior of a funtion.

> The parameter passed will be meaningless in the dummy fuinction.

> function portt(addre : word) : boolean;
> begin
> { do nothing with portt(addr) }
> portt := false;
> end;

> begin
> portt(20) := 0;
> end.

> I tried to find the port[] code in fpc but only found the tport code.
> is port[] 'trapped' in fpc and routed to tport??

Impossible in TP/BP, AFAIK. In FPC it's done using classes - look at
documentation about classes, especially about default propertys. Good
example would be ports.pp (in older versions: go32.pp) port-handling
source code (don't know, maybe the port[] "array" doesn't exist in every
version). I've no idea about Delphi but I have understood that FPC team
has taken they classes stuff fully from Delphi.

About ports : if you want to keep your code portable I'd prefere to
write inport?() and outport?() for BP and use those functions/procedures
instead of port?[].

--
Ingmar

Sent via Deja.com http://www.deja.com/
Share what you know. Learn what you don't.

Re:can a funtion or procedure name be treated like an array?


Quote
enztec wrote:
> is it possible to write a function or procedure that can be assigned a
> value in an assignment statement? like the port[] array function does in
> bpc/fpc??

    I think you're looking for "indexed properties", aka "array properties".

Off the top of my head,

type
    TType = class(TObject)
      private
        function GetSomething(n: integer; s: string): TSomething;
        procedure SetSomething(n: integer; s: string; ASomething:
TSomething);
     public
       property Something[n: integer; s: string]: TSomething
         write SetSomething read GetSomething;
    end;

    Now if T is an instance of TSomething you can access or set

T.Something[n, s]

as though it were an array, what actually happens is those functions are
called. If you add "default" to the declaration of Something you can
abbreviate that to T[n, s].

    For example this is how the Strings property of TStrings works:
If S is a TStringlist you can get or set S[n]. That's an abbreviation
for S.Strings[n], and what actually happens is a few function calls;
saying S[0]:= 'Hello world' is not an actual array assignment.

    Hope that was entertaining.

Quote
> I don't want it to do anything right now just a dummy entry in a unit
> interface and implementation so the port[] code doesn't have to be
> removed while new writeport() readport() functions can be added and
> used. (there is so much port[] code that I have a hard time commenting
> out the bad code and uncommenting the good code - and only want one copy
> of the program.

> portt can't be an actual array cause index could be $025D and it has to
> have the behavior of a funtion.

> The parameter passed will be meaningless in the dummy fuinction.

> function portt(addre : word) : boolean;
> begin
> { do nothing with portt(addr) }
> portt := false;
> end;

> begin
> portt(20) := 0;
> end.

> I tried to find the port[] code in fpc but only found the tport code.
> is port[] 'trapped' in fpc and routed to tport??

Re:can a funtion or procedure name be treated like an array?


Quote
On Sat, 18 Sep 1999, enztec wrote:
> is it possible to write a function or procedure that can be assigned a
> value in an assignment statement? like the port[] array function does in
> bpc/fpc??

No, thsd is not possible with procedures/functions. TP handles the port
array using compiler magic (ie. it has special code to recognize the port
array and generates the correct code when it encounters it), FPC has
implemented the port array using Delphi style OOP (a class):

***
unit ports;

{ this unit uses classes so
  ObjFpc mode is required PM }
{$Mode ObjFpc}

interface

type
   tport = class
      procedure writeport(p : word;data : byte);
      function  readport(p : word) : byte;
      property pp[w : word] : byte read readport write writeport;default;
   end;

   tportw = class
      procedure writeport(p : word;data : word);
      function  readport(p : word) : word;
      property pp[w : word] : word read readport write writeport;default;
   end;

...

var port, portb: tport;

etc.
***

Because the property pp is declared as default, you can do port[value]
instead of port.pp[value]. A property can have a seperate function to read
from and write to it (readport and writeport in this case), so this
explains how you can have the behavior you encouter with port.

If you want the full implementation of this unit, it's in the file
ports.pp in the rtl/go32v2 directory of the RTL source package of FPC
(available in the development section of the website).

Note that Delphi styled OOP is quite a bit different from TP styled OOP
and that I can't really help you with that, since I don't really know it.

Jonas

Re:can a funtion or procedure name be treated like an array?


Im Artikel <37E41653.C8644...@xmission.com>, enztec <enz...@xmission.com>
schreibt:

Quote
>is port[] 'trapped' in fpc and routed to tport??

Many (32 bit) systems don't allow to access hardware (ports) directly. You'll
not know then, what really will happen when you access any port. This also
applies to your program, when it executes in a DOS box.

DoDi

Re:can a funtion or procedure name be treated like an array?


Quote
"Ing. Franz Glaser" wrote:
> He could have looked at typed procedures. But I think that he had
> better an array of goto labels, which he is familiar with ;-))

It is a typical beginners question, so I can answer it too.

Type TinFunc = Function : Byte;
     Toutproc= Procedure(n : Byte);
{$F+}
Function InPort3F8 : Byte;
Begin
  asm or whatever to read port 3F8
End;
Function InPort3F9 : Byte;
Begin
  asm or whatever to read port 3F9
End;
...
Const InFuncs = Array[$3F8..$3FF] of TinFunc = (Inport3F8,Inport3F9....);

  CharFromCOM1 := InFuncs[$3F8];
--
Franz Glaser, Glasau 3, A-4191 Vorderweissenbach Austria +43-7219-7035-0
Muehlviertler Elektronik Glaser.  Industrial control and instrumentation
http://members.eunet.at/meg-glaser/           mailto:meg-gla...@eunet.at
http://www.geocities.com/~franzglaser/ http://members.xoom.com/f_glaser/

Re:can a funtion or procedure name be treated like an array?


Quote
>Type TinFunc = Function : Byte;
>     Toutproc= Procedure(n : Byte);
>{$F+}
>Function InPort3F8 : Byte;
>Begin
>  asm or whatever to read port 3F8
>End;
>Function InPort3F9 : Byte;
>Begin
>  asm or whatever to read port 3F9
>End;
>...
>Const InFuncs = Array[$3F8..$3FF] of TinFunc = (Inport3F8,Inport3F9....);

>  CharFromCOM1 := InFuncs[$3F8];

Nice for Pascal ... how about Properties as used in Delphi?

--
  [ http://home.t-online.de/~Matthias.Buechse/ ] - [ Matthias.Buec...@T-Online.de ]
  [ Homepage contents: About Me - My Projects - ... ]  -  [ Last update: 12.09.99 ]
  [ Vote for an English version of my hp via my E-Mail ]  -  [ currently no votes ]

Re:can a funtion or procedure name be treated like an array?


Im Artikel <37E6B522.D4513...@eunet.at>, "Ing. Franz Glaser"
<meg-gla...@eunet.at> schreibt:

Quote
> asm or whatever to read port 3F8

The IN A*,DX instruction allows to read from any port. Make the port address an
argument of the function, and load it into DX prior to the IN instruction. I'm
not familiar with the assembler, so I cannot provide working code.

DoDi

Re:can a funtion or procedure name be treated like an array?


Quote
VBDis wrote:

> Im Artikel <37E6B522.D4513...@eunet.at>, "Ing. Franz Glaser"
> <meg-gla...@eunet.at> schreibt:

> > asm or whatever to read port 3F8

> The IN A*,DX instruction allows to read from any port. Make the port address an
> argument of the function, and load it into DX prior to the IN instruction. I'm
> not familiar with the assembler, so I cannot provide working code.

Oh yes, but the original posting had something to do with
protected mode issues. Else it would not have been necessary
to do all the array of procedure tricks.

Or, assume that you have a special application where you want to
write a protocol in parallel, whenever an I/O access is performed
or something similar. This is done within the many procedures.

Or you could do some redirection in the procedures. But you need
not worry about different procedure names wherever you call it.

:-)
--
Franz Glaser, Glasau 3, A-4191 Vorderweissenbach Austria +43-7219-7035-0
Muehlviertler Elektronik Glaser.  Industrial control and instrumentation
http://members.eunet.at/meg-glaser/    http://members.xoom.com/f_glaser/
http://www.geocities.com/~franzglaser/            http://start.at/bedarf

Re:can a funtion or procedure name be treated like an array?


Quote
"Matthias Bchse" wrote:

> >  CharFromCOM1 := InFuncs[$3F8];
> Nice for Pascal ... how about Properties as used in Delphi?

If you could give me an advice how to implement the properties in
TP 6 or TP 7 you would get a special place in my hall of honours!

:-)
--
Franz Glaser, Glasau 3, A-4191 Vorderweissenbach Austria +43-7219-7035-0
Muehlviertler Elektronik Glaser.  Industrial control and instrumentation
http://members.eunet.at/meg-glaser/    http://members.xoom.com/f_glaser/
http://www.geocities.com/~franzglaser/            http://start.at/bedarf

Go to page: [1] [2]

Other Threads