[fpc-pascal] FPC Advanced Records

Mark Morgan Lloyd markMLl.fpc-pascal at telemetry.co.uk
Sat Mar 10 10:23:32 CET 2012


Sven Barth wrote:
> Am 09.03.2012 23:26, schrieb Mark Morgan Lloyd:
>> Martin wrote:
>>> On 09/03/2012 21:26, Mark Morgan Lloyd wrote:
>>>>
>>>> but is there any way to define something like an
>>>> endianness-correcting type, i.e.:
>>>>
>>>> Type TAWSHeader=Record
>>>> ThisSize: WordLE;
>>>> ..
>>>>
>>>> where by the time ThisSize is accessed any disparity has been 
>>>> corrected?
>>>>
>>>
>>> Not sure if this will be of any use.
>>>
>>> but you can always define an assignment incompatible type
>>> WordLE = record data: Word; end;
>>>
>>> and define all required overloaded operators
>>
>> I think that could be of a lot of use, but I'll need to tinker :-)
>>
>> Noted your example, but apart from that is there any concise way to
>> define a type such as WordLE with the explicit restriction that it
>> requires an explicit definition of assignment operators (i.e. is never
>> subject to implicit casts or type conversions)? Can this be done such
>> that WordLE has a predefined size even if the assignment has to go via
>> code?
>>
> 
> You could play around with a construct like the following:
> 
> type
>   generic TMyLEType<T> = record
>     Data: T;
>     class operator := (aRight: TMyLEType): TMyLEType;
>     class operator := (aRight: TMyLEType): T;
>   end;
> 
>   TWordLE = specialize TMyLEType<Word>;
>   TLongWordLE = specialize TMyLEType<LongWord>;
>   // ...
> 
> class operator TMyLEType.:= (aRight: TMyLEType): TMyLEType;
> begin
>   Result.Data := aRight.Data
> end;
> 
> class operator TMyLEType.:= (aRight: TMyLEType): T;
> begin
>   // convert the value to the correct endianess here
>   // you might want to use SizeOf(T) for a determination of the
>   // correct size of T, so you can swap correctly
> end;

Thanks. Thinking about it, Martin's example probably fits my criteria of 
conciseness etc.

-- 
Mark Morgan Lloyd
markMLl .AT. telemetry.co .DOT. uk

[Opinions above are the author's, not those of his employers or colleagues]



More information about the fpc-pascal mailing list