Recent

Author Topic: Generic way to get back enumeration value as string i/o integer  (Read 25454 times)

Goodman H__

  • Full Member
  • ***
  • Posts: 130
Greeting!

I am seeking a generic way which can retrieve an enumeration value as string representation instead of its integer value.

Given below code:
Code: [Select]
...
const
  genArr:array [0..1] of string=('male','female');
type
  Gender=(male,female);
  function genStr(gen:Gender):string;
  begin
    result:=genArr[integer(gen)];//==>is there a generic function to do so?
  end;

begin
  writeln('He is ',genStr(male),'.');//output 'He is male.'
  writeln('She is ',genStr(female),'.');//output 'She is female.'
  readln;
end.       
It works as expected,properly print the string representations of emumeration value;however if there is a more generic (templated) way to archieve this ,it will be great.

Any idea would be appreciated.

Regards,
Sam       
« Last Edit: August 29, 2012, 10:19:01 am by Goodman H__ »
fpc:2.6.1 Lazarus:1.1 SVN39277
OS:win 7

taazz

  • Hero Member
  • *****
  • Posts: 5368
Re: Generic way to get back enumeration value as string i/o integer
« Reply #1 on: August 29, 2012, 10:28:09 am »
Code: [Select]
var
  t:string;
begin
  WriteStr(t,brNew); //same of t:='brNew';
end;[code]

I haven't used my self at all so I can't comment on it but it seems what you are looking for.
« Last Edit: August 29, 2012, 10:30:59 am by taazz »
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

Goodman H__

  • Full Member
  • ***
  • Posts: 130
Re: Generic way to get back enumeration value as string i/o integer
« Reply #2 on: August 29, 2012, 11:12:04 am »
Thanks a lot.It definitely helps!
I learnt the WriteStr take a string parm and args parm as Arguments type.When I tried to write a function which returns a string representation of the parm 'args',the compiler does not recognize 'Arguments' even if I added 'system' unit.
Code: [Select]
...
function enum2str(gen:Gender):string;
var
str:string;
begin
    writeStr(str,gen);
    result:=str;
end;
The above works,but not below:
Code: [Select]
function enum2str(args:Arguments):string;//===>Compiler does not recognize 'Arguments'!!
var
  str:string;
begin
   WriteStr(str,args);
    result:=str;
end;
fpc:2.6.1 Lazarus:1.1 SVN39277
OS:win 7

KpjComp

  • Hero Member
  • *****
  • Posts: 680
Re: Generic way to get back enumeration value as string i/o integer
« Reply #3 on: August 29, 2012, 11:39:11 am »
I believe WriteStr is an internal compiler magic function.
IOW: it's not a real function, but more like a macro.

The reason for this is that WriteStr will internally be using TypeInfo, and this only gets computed at compile time too.

eg.
WriteStr internally will be doing something like ->
Code: [Select]
s := GetEnumName(typeinfo(SomeType),ord(SomeValue));

ludob

  • Hero Member
  • *****
  • Posts: 1173
Re: Generic way to get back enumeration value as string i/o integer
« Reply #4 on: August 29, 2012, 11:46:14 am »
WriteStr is like Write one of the few pascal functions that accept a variable number of arguments. There is some compiler magic needed to support this and you can't declare yourself functions that support a variable number of arguments. (Exception being the combination of cdecl and Array of Const as described here http://www.freepascal.org/docs-html/ref/refsu60.html). So "Arguments" is not a real type but just an awkward indication that a variable number of arguments can be passed. You can't use it for your own functions.

An alternative for WriteStr is the use of typeinfo:
Code: [Select]
uses ...,typinfo;
writeln(GetEnumName(TypeInfo(Gender), ord(male)));

Goodman H__

  • Full Member
  • ***
  • Posts: 130
Re: Generic way to get back enumeration value as string i/o integer
« Reply #5 on: August 29, 2012, 11:58:04 am »
Thanks you all!

One more stupid question:why I can not make use of unit sysutils,typinfo in a pure console app?
Say I would like to make use of function Format,GetEnumName etc,but the compiler can't recognize either of them even if I've already imported sysutils and Typinfo into the current project.

Appreciated.

Sam
fpc:2.6.1 Lazarus:1.1 SVN39277
OS:win 7

gulyone

  • Guest
Re: Generic way to get back enumeration value as string i/o integer
« Reply #6 on: August 29, 2012, 11:59:53 am »
variable number of arguments was added to pascal to be able to call C functions declared with (...) and using va_arg va_start, va_end macros to access them.
as many applications use libraries created from other languages , pascal needed to be extended that way... the same concept applies to generics, wich approaches are same as c++ templates. Pascal needed that
There is no magic or trick or whatever you want in all that ! fairies do not exist ! LMAO
« Last Edit: August 29, 2012, 12:01:46 pm by gulyone »

taazz

  • Hero Member
  • *****
  • Posts: 5368
Re: Generic way to get back enumeration value as string i/o integer
« Reply #7 on: August 29, 2012, 12:08:02 pm »
Thanks you all!

One more stupid question:why I can not make use of unit sysutils,typinfo in a pure console app?
Say I would like to make use of function Format,GetEnumName etc,but the compiler can't recognize either of them even if I've already imported sysutils and Typinfo into the current project.


I have no problem using them in any type of application. here is a small test up for you.
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

ludob

  • Hero Member
  • *****
  • Posts: 1173
Re: Generic way to get back enumeration value as string i/o integer
« Reply #8 on: August 29, 2012, 12:27:15 pm »
Quote
Say I would like to make use of function Format,GetEnumName etc,but the compiler can't recognize either of them even if I've already imported sysutils and Typinfo into the current project.
You have to add the units to the uses clause in every unit where you use these functions.

KpjComp

  • Hero Member
  • *****
  • Posts: 680
Re: Generic way to get back enumeration value as string i/o integer
« Reply #9 on: August 29, 2012, 12:45:42 pm »
Quote
There is no magic or trick or whatever you want in all that ! fairies do not exist ! LMAO

Oh Dear!!.  If you don't understand the meaning of compiler magic I would suggest you don't reply.   Btw: You do have access to FPC source, for a bit of fun go and look for the WriteStr procedure.   A little tip, you might find it implemented in the compiler source.. :) 

Also there are plenty of teenager XBox/PS3 forums you would feel much more at home with.   Most users on Lazarus forum are adults so your LMAO's are wasted here.

gulyone

  • Guest
Re: Generic way to get back enumeration value as string i/o integer
« Reply #10 on: August 29, 2012, 01:06:26 pm »
Quote
There is no magic or trick or whatever you want in all that ! fairies do not exist ! LMAO

Oh Dear!!.  If you don't understand the meaning of compiler magic I would suggest you don't reply.   Btw: You do have access to FPC source, for a bit of fun go and look for the WriteStr procedure.   A little tip, you might find it implemented in the compiler source.. :) 

Also there are plenty of teenager XBox/PS3 forums you would feel much more at home with.   Most users on Lazarus forum are adults so your LMAO's are wasted here.

        MOV RAX,[KpjComp]
        PUSH RAX
        RET

wowwww trick ! rofl

i wana free trial from what ya smoke

KpjComp

  • Hero Member
  • *****
  • Posts: 680
Re: Generic way to get back enumeration value as string i/o integer
« Reply #11 on: August 29, 2012, 01:22:40 pm »
@gulyone,  I can now see I have no chance against somebody like yourself with such superior intellect.  You win!!.

gulyone

  • Guest
Re: Generic way to get back enumeration value as string i/o integer
« Reply #12 on: August 29, 2012, 01:29:36 pm »
ikr sir

u began
i finish

felipemdc

  • Administrator
  • Hero Member
  • *
  • Posts: 3538
Re: Generic way to get back enumeration value as string i/o integer
« Reply #13 on: August 29, 2012, 03:07:49 pm »
ikr sir

u began
i finish

This conflict better stop right here or else I will need to take more agressive moderation steps.

One way to solve this is simple: Just don't answer to each other posts, and it will all be fine.

Knipfty

  • Full Member
  • ***
  • Posts: 232
Re: Generic way to get back enumeration value as string i/o integer
« Reply #14 on: August 29, 2012, 03:26:10 pm »
Hi Goodman H__,

I too have a similar requirement but ultimately threw in the towel and wrote specific functions.  My issue with the generic was that I like to put a 2 character prefix in front of the string.

SO a Type definitition may look like this:
Code: [Select]
  TInvestment = (itDeposit, itReturn, itRealized, itDistribution, itAdjsutment);  // Investment table investment types

The string I want to return is less the "it"s.  SO I write 2 function for each enumeration type:
Code: [Select]
function TInvestmentDesciption(const IT: TInvestment): shortstring;
begin
  case IT of
    itDeposit:      Result := 'Deposit';
    itReturn:       Result := 'Return';
    itRealized:     Result := 'Realized';
    itDistribution: Result := 'Distribution';
    itAdjsutment:   Result := 'Adjsutment'
  end
end;

function TInvestmentEnum(const IT: shortstring): TInvestment;
begin
  case IT of
    'Deposit':      Result := itDeposit;
    'Return':       Result := itReturn;
    'Realized':     Result := itRealized;
    'Distribution': Result := itDistribution;
    'Adjsutment':   Result := itAdjsutment
  end
end;

Then anytime I need to convert the ordinal value to a string I call the description function.  Anytime I need to go the other way, I call the Enum function.  These come in real handy in OnGetText event handlers that I use on DBGrids.

I hope that helps,

Knipfty
64-bit Lazarus 2.2.0 FPC 3.2.2, 64-bit Win 11

 

TinyPortal © 2005-2018