Re: [fpc-pascal] Unrelated type helpers with the same members? Implement an interface with a type helper?

2022-12-22 Thread Michael Van Canneyt via fpc-pascal




On Thu, 22 Dec 2022, Andrew Haines via fpc-pascal wrote:

Hi what I want to do is similar to this question here: 
https://en.delphipraxis.net/topic/423-rtti-determine-record-helper-type-for-a-base-type/


I am going to create multiple type helpers for enums and I would like to look 
up the helper and use common methods AsString AsOrdinal to get/set the 
values. I am recieving them as json that I dont have control over. I am using 
typeinfo to fill in all the properties but I would like to convert the 
strings I am receiving to the enum values semi-automatically.


LString := 'STATUS';

LHelper := GetHelper(LPropInfo^.PropType); // enum proptype


Since there can be multiple type helpers for a type, this will never be
possible. At best you would get a list.

But I don't think there is even a list of type helpers per type.
Sven will need to confirm.

In your case, I would simply register a function that does this for each
known enum type.

Michael.

___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal


Re: [fpc-pascal] Unrelated type helpers with the same members? Implement an interface with a type helper?

2022-12-22 Thread Andrew Haines via fpc-pascal

So, I came up with a solution using Custom Attributes.

I can declare this:

[TEnumAttr('YES', 'NO', 'COULD_BE')]
  TMyEnum = (meYes, meNo, meCouldBe);

and at runtime look for the TEnumAttr attribute and use that.


TEnumAttr = class(TCustomAttribute)
  protected
    FValues: TStringArray;
    FTypeName: String;
  public
    class function FromType(ATypeInfo: PTypeInfo): TEnumAttr;
    constructor Create(Val1: String);
    constructor Create(Val1, Val2: String);

etc

Maybe this can help someone else with a similar use case.


Regards,

Andrew Haines

On 12/22/22 8:40 PM, Andrew Haines via fpc-pascal wrote:
Hi what I want to do is similar to this question here: 
https://en.delphipraxis.net/topic/423-rtti-determine-record-helper-type-for-a-base-type/


I am going to create multiple type helpers for enums and I would like 
to look up the helper and use common methods AsString AsOrdinal to 
get/set the values. I am recieving them as json that I dont have 
control over. I am using typeinfo to fill in all the properties but I 
would like to convert the strings I am receiving to the enum values 
semi-automatically.


LString := 'STATUS';

LHelper := GetHelper(LPropInfo^.PropType); // enum proptype

LToStringInterface := LHelper as IToStringInterface;

SetOrdProp(LObject, 'propname', LToStringInterface.OrdValue(LString);


Something like that. I'm not sure if it's even possible to find the 
helper type with typeinfo.


Thanks for your suggestions :)

Regards,

Andrew

___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal

___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal


[fpc-pascal] Unrelated type helpers with the same members? Implement an interface with a type helper?

2022-12-22 Thread Andrew Haines via fpc-pascal
Hi what I want to do is similar to this question here: 
https://en.delphipraxis.net/topic/423-rtti-determine-record-helper-type-for-a-base-type/


I am going to create multiple type helpers for enums and I would like to 
look up the helper and use common methods AsString AsOrdinal to get/set 
the values. I am recieving them as json that I dont have control over. I 
am using typeinfo to fill in all the properties but I would like to 
convert the strings I am receiving to the enum values semi-automatically.


LString := 'STATUS';

LHelper := GetHelper(LPropInfo^.PropType); // enum proptype

LToStringInterface := LHelper as IToStringInterface;

SetOrdProp(LObject, 'propname', LToStringInterface.OrdValue(LString);


Something like that. I'm not sure if it's even possible to find the 
helper type with typeinfo.


Thanks for your suggestions :)

Regards,

Andrew

___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal