0

I'm trying to make a generic class that can work with Delphi's "file of" feature. Being generic, it should be able to process any kind of Record structure. I can only think of passing the structure as a Pointer. Since the compiler doesn't allow a property to be of type Pointer, I must declare a public procedure like:

procedure SetStructure (aPointToStruct: Pointer);

This works fine for the sake of passing the structure as a Pointer, but unfortunately, all the structure information is missed since the procedure gets just a Pointer. I wonder if there is any way to retrieve the structure information, or to pass records (of any kind) as parameters.

This is a conceptual question, thus, I think there's no need for a code example. Nevertheless I can figure out one if you find that's really essential. In time: I apologize for my ignorance in case this a stupid or conceptually wrong question.

Jerry Dodge
  • 26,858
  • 31
  • 155
  • 327
user2383818
  • 709
  • 1
  • 8
  • 19
  • 2
    Could you [edit] your question to break that big wall of rambling text down into readable paragraphs? While it can be (sort of) read now, it would be much easier if it was more organized. The enter key works fine to insert paragraph breaks when you're entering questions or answers here; you don't even have to do any special formatting. You'll probably also want to add a general `delphi` tag while you're at it, along with the version specific `delphi-xe` you have now. Thanks. – Ken White Oct 30 '14 at 21:39
  • @Ken, what does the OP do if the specific XE version in use is, um, XE ? (I know it probably isn't but...) :) – Deltics Oct 30 '14 at 22:45
  • 1
    @Deltics: I said **in addition to**, not **instead of**. The general Delphi tag is seen by many more people than just those seeing version specific questions, and it makes the question available more readily to others searching for Delphi information in the future. Even if the user is specifically using XE (and not XE2 or Delphi 2009), the question is most likely relevant to other versions as well (as would be the answer). – Ken White Oct 30 '14 at 22:48
  • Tongue was somewhat in cheek Ken. :) With Delphi XE being a specific version a **delphi-xe** tag could be too vague or 100% accurate. There's just no way to know, unless we mandate "delphi-xe1" for the specific and "delphi-xe" for the general. For the general case anyone coming to Delphi post-XE(1) could be forgiven for thinking that XE identifies FMX/x-platform versions as distinct from any VCL only versions (which XE1 was). EMBT perhaps should have waited to apply the XE brand to avoid this. Too late now of course. :) #fridaydiscussions :) – Deltics Oct 30 '14 at 23:15
  • @KenWhite Not to mention the FAQ even tells you that you need to add both the core language tag, and if applicable, the specific version - specifically because of people who monitor certain tags regularly. I always monitor the `Delphi` tag. – Jerry Dodge Oct 30 '14 at 23:30
  • @KenWhite I took care of that editing for you :-) – Jerry Dodge Oct 30 '14 at 23:39
  • 1
    FWIW, the use of `file of` is somewhat archaic in Delphi. I can't remember the last time I saw that construct used - not to mention using it myself. – 500 - Internal Server Error Oct 31 '14 at 00:13
  • 1
    A few years ago, someone asked [a question about recovering the type of an untyped var parameter](http://stackoverflow.com/q/554100/33732). You're asking a similar question about an untyped pointer. Once you lose type information, there's no getting it back. – Rob Kennedy Oct 31 '14 at 01:57
  • 2
    Use generics and RTTI. – David Heffernan Oct 31 '14 at 02:53
  • @500. Do you know any alternative way for storing Records on a file? If so, would you be so kind to place a working sample? Thanks. – user2383818 Oct 31 '14 at 11:44
  • @David. Same request as with 500. Please place a working sample in a comment. Thanks. – user2383818 Oct 31 '14 at 11:45
  • I don't happen to have a sample around for that, but it is of course possible. I personally use XML for storing local and/or non-shared data and a proper database for storing the rest. JSON is also very popular now. I suggest that you google for serialization libraries for Delphi. – 500 - Internal Server Error Oct 31 '14 at 11:57
  • @500. I rather prefer not to use third party libraries, but thanks anyway. I'll google for my specific issue, that's storing and retrieving record structures on a file. – user2383818 Oct 31 '14 at 12:27
  • You should use refular `file` type and pass one additional parameter: `structSize : integer`. Later on, you can use `BlockRead` and `BlockWrite` for IO. – adlabac Nov 02 '14 at 23:45
  • @David. I'm working on your suggestion, and it seems that I'm geting somewhere. At the momment, I'm experiencing some difficulty in using Rtti & Records: I'm coding a "Rtti" class to simplify and generalize the working with Rtti, but I can't find the way to pass a Record as a property to that class. Should "generics" do the trick? How? – user2383818 Nov 06 '14 at 13:23
  • Generics won't help. RTTI will. I'd use JSON for this though. And not write new code but rather use a library like superobject. – David Heffernan Nov 06 '14 at 13:43
  • @David. As I said to 500, I rather prefer not to use third party libraries, even if this means an extra work of coding to achieve my goal. Can you post an example on how I pass a Record as aproperty? Thanks. – user2383818 Nov 06 '14 at 16:25
  • I prefer not to waste my time writing poor imitations of excellent libraries. I guess it's time for you to do the "extra work" that you described. Good luck! – David Heffernan Nov 06 '14 at 16:31
  • OK! As you said, is a question of preference. I have my restrictions towards third party libraries. Please be kind and answer if possible: Can you post an example on how I pass a Record as a property? Thanks. – user2383818 Nov 06 '14 at 16:35

0 Answers0