[fpc-pascal] FPC Advanced Records
Sven Barth
pascaldragon at googlemail.com
Sat Mar 10 09:31:34 CET 2012
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;
Regards,
Sven
More information about the fpc-pascal
mailing list