TypeInfo question

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

TypeInfo question

Ryan Joseph
I have some generics which operate on multiple types and instead of making subclasses for certain types and overriding methods I wonder if using the RTTI like:

PTypeInfo(TypeInfo(T))^.kind = tkClass

would be a good idea. How is TypeInfo() implemented exactly? I’m curious if there are any string comparing, allocating memory etc... that would adversely affect performance since these classes need to perform fast data lookups/insertions.

Thanks.

Regards,
        Ryan Joseph

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: TypeInfo question

Free Pascal - General mailing list

Am 23.03.2017 16:29 schrieb "Ryan Joseph" <[hidden email]>:
>
> I have some generics which operate on multiple types and instead of making subclasses for certain types and overriding methods I wonder if using the RTTI like:
>
> PTypeInfo(TypeInfo(T))^.kind = tkClass
>
> would be a good idea. How is TypeInfo() implemented exactly? I’m curious if there are any string comparing, allocating memory etc... that would adversely affect performance since these classes need to perform fast data lookups/insertions.

TypeInfo() simply inserts the load of a pointer value, so there's not even a call.

Regards,
Sven


_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: TypeInfo question

Ryan Joseph

> On Mar 23, 2017, at 11:16 PM, Sven Barth via fpc-pascal <[hidden email]> wrote:
>
> TypeInfo() simply inserts the load of a pointer value, so there's not even a call.
>
>

Great, that’s what I was hoping. Thanks.

Regards,
        Ryan Joseph

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: TypeInfo question

Ryan Joseph
I think I already learned this and I forget when I had the idea about TypeInfo. :)

This is basically the pattern I’m attempting. I have a generic class and I thought I could avoid specializing and making a subclass so that I could call a method if the type of the generic was a class. So what happens as you now is that I can’t cast “value” to TObject because if the generic was specialized as an integer for example, the type cast would be illegal and I get stuck at compile time (Illegal type conversion: "LongInt" to "TObject”).

Is there anyway I could push the type checking to runtime? I wanted to using writeln also to perform some printing for debugging but I get stuck at compile time again trying to mix, integers, strings, records, classes etc… in the generic. I would use an interface but there are compiler types and records involved so that’s not an option.

type
        TLongIntMatrix = specialize TMatrix<LongInt>;
        TObjectMatrix = specialize TMatrix<TObject>;


function TMatrix.RetainValue (value: T): T;
begin
        if typeKind = tkClass then
                TObject(value).Retain;
               
        result := value;
end;



Regards,
        Ryan Joseph

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: TypeInfo question

Free Pascal - General mailing list

Am 24.03.2017 03:51 schrieb "Ryan Joseph" <[hidden email]>:
> Is there anyway I could push the type checking to runtime? I wanted to using writeln also to perform some printing for debugging but I get stuck at compile time again trying to mix, integers, strings, records, classes etc… in the generic. I would use an interface but there are compiler types and records involved so that’s not an option.
>
> type
>         TLongIntMatrix = specialize TMatrix<LongInt>;
>         TObjectMatrix = specialize TMatrix<TObject>;
>
>
> function TMatrix.RetainValue (value: T): T;
> begin
>         if typeKind = tkClass then
>                 TObject(value).Retain;
>
>         result := value;
> end;
>

You could try to do it with a pointer:

PObject(@value)^.Retain;

Regards,
Sven


_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal