问题
I have two types. One Type A and one Type B. The Problem Type A contains Type B and Type B contains Type A. Such a thing like this won't work:
type
typeA = record
test1 : typeB;
end;
type
typeB = record
test2 : typeA;
end;
Edit: Thats not my design. I converting C Header files (to access a DLL) that include such constructs to delphi.
Edit2: "C++ structs are another name for classes AFAIR. And there must have been pointers, not values themselves. – Arioch 'The 1 min ago" Yes you are right that was a Pointer to a Type:
There I definied:
test1 : ^typeB;
Will that work instead?
test1 : Pointer;
Edit3: The C Structs:
/* DLPDFPAGE */
typedef struct dlpdfpage
{
CosObj Page;
CosObj PrintSelect;
ASFixedRect PageBBox;
ASFixedRect ContentBBox;
struct dlpdfpage *Next;
PDRotate Angle;
struct dlpdfdoc *Doc;
DLPDFSTREAM *Content;
long PageNumber;
char Complete;
char FontSubstituted;
char FontMM;
char FontBad;
} DLPDFPAGE;
/* DLPDFDOC */
typedef struct dlpdfdoc
{
DLPDFINSTANCE *dliInstance;
PDDoc pdDoc;
CosDoc cosDoc;
DLPDFOUTLINE *Outlines;
char *PDFFileName;
char *PDFPostFileName;
DLPOS LastPageEnd;
DLPOS BeforeDef;
ASFixedRect DocBBox;
long PageCount;
long PageTreeWidth;
long PageTreeDepth;
long PageTreeDepthUsed;
DLPDFPAGETREEARRAY *AllPages;
DLPDFFONTLIST *AllFonts;
DLPDFFORMLIST *AllForms;
DLPDFFORMLIST *AllColors;
DLPDFIMAGELIST *AllImages;
DLPDFSPOTCOLORLIST *AllSpotColors;
DLPDFSPOTCOLORLIST *AllPatterns;
DLPDFEXTGSTATELIST *AllExtGStates;
DLPDFPAGE *PageList;
DLPDFPAGE *LastPage;
DLPDFDEST *DeferedDests;
DLPDFSIGNATURE *signatureHolder;
struct dlpdfacroform *AcroFormBase;
CosObj PatternColorObj,
PatternColorRGBObj,
PatternColorCMYKObj,
PatternColorGrayObj,
PrintSelect,
PrintSelectCriteria;
CosObj IdentH, IdentV;
ASAtom DocumentEncoding;
long FontCount;
long FormCount;
long PatCount;
long ImageCount;
char Compress;
char Linearize;
char PageTreeComplete;
char EmbedFonts;
char PatternColorsDefined;
char MakeThumbNails;
ASBool psSevenBitSafe;
ASInt32 EncryptKeyByteCount;
char condenseResDicts;
CosObj resourceDict;
ASInt16 pdfMajorVer;
ASInt16 pdfMinorVer;
DLPDFINCLUDEDRES *InclRes;
DLPDFSPOTCOLORLIST *AllShadings;
long ShadeCount;
} DLPDFDOC;
回答1:
You misunderstood what those C structs represent. That's because a record
is a value type: it's stored right there where you declare the variable. So let's do a few levels of recursive declarations, and you'll understand what I mean; Assuming the two structures aren't absolutely identical:
type
TA = record
test1 : TB;
SomethingElseFromA: Byte;
end;
TB = record
test2 : TA;
SomethingElseFromB: Byte;
end;
Structure TA
could be rewritten to mean this:
type
TA = record
// Replaced test1 : TB with the actual content of TB, because that's
// what a record means.
test1_test2: TA;
test1_SomethingElseFromB: Byte;
SomethingElseFromA: Byte;
end;
Of course, we've now got a nice recursive inclusion of self into the TA
record, something along the lines of:
TA = record
// Replaces test1_test: TA
test1_test2: TA; // Oops, still not fixed, need to do it again...
test1_SomethingElseFromB: Byte;
SomethingElseFromA: Byte;
test1_SomethingElseFromB: Byte;
SomethingElseFromA: Byte;
end;
You probably want to use reference types to get something that looks similar, but it's not similar. A reference type is always a pointer, so it's a fixed size; The compiler can allocate it without a problem. This would be valid, using pointers-to-records:
type
pTypeB = ^typeB;
pTypeA = ^typeA;
typeA = record
test1 : pTypeB;
end;
typeB = record
test2 : pTypeA;
end;
Alternatively you could use classes; That works for the same reason, classes are reference types; they work the same way as pointers. When you declare a variable of pointer-type, the compiler allocates SizeOf(Pointer)
bytes.
Since you've posted the C structs, I can tell they're too long for me to attempt a complete translation, but I can make a few suggestions: You should declare all your types in a single Type
block; don't write the Type
before each type declaration. This allows you to create the pointer type before the record type, like this:
Type
PMyRecord = ^TMyRecord;
// Somewhere in the same Type block
TMyRecord = record
end;
For each type that requires pointers-to-records, declare the pointers first thing after the Type
keyword, it's simpler that way. Next, you need to identify the C pointers. If there's a *
between the name of the data type and the name of the field, that's a pointer. This is usually written like this:
int *PointerToSomeInt;
But those would be just as valid:
int * PointerToSomeInt;
int* VarName1, * VarName1, * VarName3; // Three pointers to integer.
Finally, you'll need to deal with alignment issues. If you can, check the size of the structures on the C side, and then check the size on the Delphi side: you should get the same size. If you don't, you should try a couple of random {$ALIGN}
compiler directives before your structure declaration and repeat until you strike the correct alignment. If all else fails you'll need to find what's wrong (what fields are aligned differently on the Delphi side) and put in some alignment bytes to artificially fix it.
回答2:
Perhaps the best solution is to rethink the design. But you might also be interested in so-called forward declarations of classes:
type
TTypeB = class;
TTypeA = class
test: TTypeB;
end;
TTypeB = class
test: TTypeA;
end;
SIC! This only works for classes, not records.
回答3:
The Delphi translation of the C code you showed would look like this:
type
DLPDFDOC = record; // forward declaration
{ DLPDFPAGE }
DLPDFPAGE = record
Page: CosObj;
PrintSelect: CosObj;
PageBBox: ASFixedRect;
ContentBBox: ASFixedRect;
Next: ^DLPDFPAGE;
Angle: PDRotate;
Doc: ^DLPDFDOC;
Content: ^DLPDFSTREAM;
PageNumber: Longint;
Complete: AnsiChar;
FontSubstituted: AnsiChar;
FontMM: AnsiChar;
FontBad: AnsiChar;
end;
{ DLPDFDOC }
DLPDFDOC = record
dliInstance: ^DLPDFINSTANCE;
pdDoc: PDDoc;
cosDoc: CosDoc;
Outlines: ^DLPDFOUTLINE;
PDFFileName: PAnsiChar;
PDFPostFileName: PAnsiChar;
LastPageEnd: DLPOS;
BeforeDef: DLPOS;
DocBBox: ASFixedRect;
PageCount: Longint;
PageTreeWidth: Longint;
PageTreeDepth: Longint;
PageTreeDepthUsed: Longint;
AllPages: ^DLPDFPAGETREEARRAY;
AllFonts: ^DLPDFFONTLIST;
AllForms: ^DLPDFFORMLIST;
AllColors: ^DLPDFFORMLIST;
AllImages: ^DLPDFIMAGELIST;
AllSpotColors: ^DLPDFSPOTCOLORLIST;
AllPatterns: ^DLPDFSPOTCOLORLIST;
AllExtGStates: ^DLPDFEXTGSTATELIST;
PageList: ^DLPDFPAGE;
LastPage: ^DLPDFPAGE;
DeferedDests: ^DLPDFDEST;
signatureHolder: ^DLPDFSIGNATURE;
AcroFormBase: ^DLPDFACROFORM;
PatternColorObj: CosObj;
PatternColorRGBObj: CosObj;
PatternColorCMYKObj: CosObj;
PatternColorGrayObj: CosObj;
PrintSelect: CosObj;
PrintSelectCriteria: CosObj;
IdentH: CosObj;
IdentV: CosObj;
DocumentEncoding: ASAtom;
FontCount: Longint;
FormCount: Longint;
PatCount: Longint;
ImageCount: Longint;
Compress: AnsiChar;
Linearize: AnsiChar;
PageTreeComplete: AnsiChar;
EmbedFonts: AnsiChar;
PatternColorsDefined: AnsiChar;
MakeThumbNails: AnsiChar;
psSevenBitSafe: ASBool;
EncryptKeyByteCount: ASInt32;
condenseResDicts: AnsiChar;
resourceDict: CosObj;
pdfMajorVer: ASInt16;
pdfMinorVer: ASInt16;
InclRes: ^DLPDFINCLUDEDRES;
AllShadings: ^DLPDFSPOTCOLORLIST;
ShadeCount: Longint;
end;
来源:https://stackoverflow.com/questions/14437435/how-to-define-a-type-a-in-type-b-and-type-b-in-type-a