[fpc-devel] GetPropValue and enumerated types (Bug #4738 follow-up)

Michael Van Canneyt michael at freepascal.org
Wed May 17 11:36:35 CEST 2006



On Wed, 17 May 2006, Graeme Geldenhuys wrote:

>> Even so:
>> > This Assign() method does not need to use a variant, but can still offer
>> > the same functionality as it does now. It just means that the method
>> > needs to determine the actual type itself...
>> 
>> tiOPF has done a lot to get rid of using variants and it isn't used
>> that much anymore.  If there is anything else we can do to get rid of
>> the last few cases, we would.
>> Below is one of the methods used in the Assign method.  GetPropValue
>> and SetPropValue returns/uses Variant, so how would you do it
>> otherwise using RTTI?
>> eg: Query PropInfo and call the individual methods (GetOrdProp,
>> GetStrProp, etc.) based on PropInfo.Kind in a case statement?
>> 
>> const
>>   // All string type properties
>>   ctkString = [ tkChar, tkString, tkWChar, tkLString, tkWString
>>                         {$IFDEF FPC},tkAString{$ENDIF} ] ;
>>   // Integer type properties
>>   ctkInt    = [ tkInteger, tkInt64 ] ;
>>   // Float type properties
>>   ctkFloat  = [ tkFloat ] ;
>>   // Numeric type properties
>>   ctkNumeric = [tkInteger, tkInt64, tkFloat];
>>   // All simple types (string, int, float)
>>   ctkSimple = ctkString + ctkInt + ctkFloat ;
>> 
>> procedure TtiObject.AssignPublishedProp( pSource : TtiObject ;
>> psPropName : string) ;
>> var
>>   lPropType: TTypeKind;
>>   lPropValue: Variant;
>> begin
>>   lPropType := TypInfo.PropType( pSource, psPropName ) ;
>>   if lPropType in ctkSimple + [tkVariant, tkEnumeration] then
>>   begin
>>     { Graeme 2006-05-16: Passing in False, as a workaround for a FPC bug. }

Change your code to something like

        Case lproptype of
          tkInteger : SetIntProp(Self,psPropName,GetIntProp(psource,pspropname));
          tkString  : SetStrProp(Self,psPropName,GetStrProp(psource,pspropname));
          // And so on. See e.g. RTTIUtils for a more complete list.
       else
         Raise Exception.Create(UnSupportedRTTIType)
       end;
                      ;
>> > Variants are for (excuse me the term) lazy coders...
>> 
>> I've heard that before.. :-)

It must be true then :-)

Michael.



More information about the fpc-devel mailing list