Can I determine the type of the type in the compiler directive
Is it possible to create a conditional definition like this:
{$if typeof(TNode) = record}
type PNode = ^TNode;
{$else}
type PNode = TNode;
{$end}
Why do I need it?
I alternate using class
and record
for a specific problem.
I want to use recording for speed reasons, but also want to use class
for convenience.
For this reason, I switch between them.
Obviously I can add an operator {$define}
, but it would be nice to automate this.
source to share
While I personally recommend the general DEFINE approach, you can be successful in cases where the record does not have a specific size:
{$if Sizeof(TNode) <> Sizeof(Pointer)}
type PNode = ^TNode;
{$else}
type PNode = TNode;
{$end}
Ok I know this is dirty programming, but you asked for it in the first place.
source to share
If you are in control of the TNode definition, you can do it like this (not necessarily in the same unit, but must refer to the same constant):
const
NODE_IS_RECORD = False;
type
{$if NODE_IS_RECORD}
TNode = record
end;
PNode = ^TNode;
{$ELSE}
TNode = class
end;
PNode = TNode;
{$IFEND}
If you only manage 1 TNode declaration, you can still do it like this:
{Unit1}
type
TNode = record
end;
PNode = ^TNode;
{Unit2}
{$IF not DECLARED(PNode)}
//if you don't use the unit where TNode is a record, then PNode shouldn't be declared.
PNode = TNode;
{$ENDIF}
If you control neither declaration, but declare in different units (in fact, I think this is necessary ...) and you never use both of them, or using both methods always means that you want to use a specific PNode declaration :
{$IF DECLARED(UnitContainingTNodeAsRecord)}
PNode = ^TNode;
{$ELSE}
PNode = TNode;
{$IFEND}
You might want to prefix TNode with the device name if you have both devices in use. "DECLARED" only ensures that it is declared and not "closest" in the scope.
I think this applies to most situations.
source to share