SOLVED Help storeing filename lazarus / 21-General ---------------------------------------------------------------------- DreamVB · 2022-07-03 17:55 Hi, I am making a small tabbed notepad program and I not sure were to store the filename of an opened file. as the TTabsheet has a property for tag but can only store a integer. so I was wondering how is the best way of doing it. I thought of using a string list to keep each of the opened filenames in the list but I don't know if this is effective way of doing it. I did have the idea of maybe turning a string into a pointer I done this is C++ but not sure how to do it in lazarus then store that pointer in the tag and then to get the string I need a way to convert the pointer back to a string. anyway I hope you understand what I am trying to archive and any help will be great full Thank you, ---------------------------------------------------------------------- MarkMLl · 2022-07-03 20:28 Quote from: DreamVB on July 03, 2022, 07:55:01 pmHi, I am making a small tabbed notepad program and I not sure were to store the filename of an opened file. as the TTabsheet has a property for tag but can only store a integer. so I was wondering how is the best way of doing it. I thought of using a string list to keep each of the opened filenames in the list but I don't know if this is effective way of doing it. A stringlist with the same number of entries as the tabsheet would seem ideal. Otherwise a dynamic array of strings. QuoteI did have the idea of maybe turning a string into a pointer I done this is C++ but not sure how to do it in lazarus then store that pointer in the tag and then to get the string I need a way to convert the pointer back to a string. That sort of thing is a bad idea in C++ and a worse idea in a well- designed language :-) Seriously: neither necessary nor wise. At the application level I use an explicit pointer every five years or so. MarkMLl ---------------------------------------------------------------------- Bart · 2022-07-03 20:46 I suppose that on the TTabSheet you have a component fort the actual editing of that file. My notepad replacement has a TSynEdit derived component on each TTabSheet. In the derived class I added a field for the filename. e.g. Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i type  TEditor = class(TSynEdit)  private    FFileName: String;  public    property FileName: string read FFileName write FFileName;  end; I store the filename in the TEditor and display the filename (without path) in the Tab of the TTabSheet. Bart ---------------------------------------------------------------------- DreamVB · 2022-07-03 20:47 Hi thanks for the info MarkMLl I did sort of find a way around it, what I did was store the filename in the TabSheets hint property seems to work. thanks again ---------------------------------------------------------------------- PascalDragon · 2022-07-04 11:40 Quote from: MarkMLl on July 03, 2022, 10:28:12 pmQuoteI did have the idea of maybe turning a string into a pointer I done this is C++ but not sure how to do it in lazarus then store that pointer in the tag and then to get the string I need a way to convert the pointer back to a string. That sort of thing is a bad idea in C++ and a worse idea in a well- designed language :-) Seriously: neither necessary nor wise. At the application level I use an explicit pointer every five years or so. In fact that is the purpose of the Tag property, that's why it's a PtrInt after all. @DreamVB: the important point when you store something in the Tag property is that you need to clean up correctly. So if you'd store a string it would be like this: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i // storing:// tmp is of type PtrIntPAnsiString(tmp)^ := filename;tabsheet.Tag := tmp; // retrieving:filename := PAnsiString(tabsheet.Tag); // deleting:tmp := tabsheet.Tag;tabsheet.Tag := 0;PAnsiString(tmp)^ := ''; Similar if you use a class instance or a pointer to a record as value for the Tag (if you need to store more than one piece of information). Quote from: DreamVB on July 03, 2022, 10:47:21 pmI did sort of find a way around it, what I did was store the filename in the TabSheets hint property seems to work. thanks again Which will only work until you need to display a different hint for the tab sheet... ---------------------------------------------------------------------- MarkMLl · 2022-07-04 11:56 Quote from: PascalDragon on July 04, 2022, 01:40:41 pmIn fact that is the purpose of the Tag property, that's why it's a PtrInt after all. Didn't used to be: Delphi defined it as a straight integer of indeterminate size. And not even as an unsigned, which would have been reasonable if they anticipated people putting pointers in that field. In any event, it would be better to put a reference to an object into it, since at that point type can be checked at runtime. Which I suppose raises the interesting question of what the lowest- overhead object is which can store a single string. MarkMLl ---------------------------------------------------------------------- DreamVB · 2022-07-04 15:50 Thanks PascalDragon I try that ---------------------------------------------------------------------- PascalDragon · 2022-07-05 12:13 Quote from: MarkMLl on July 04, 2022, 01:56:50 pmQuote from: PascalDragon on July 04, 2022, 01:40:41 pmIn fact that is the purpose of the Tag property, that's why it's a PtrInt after all. Didn't used to be: Delphi defined it as a straight integer of indeterminate size. And not even as an unsigned, which would have been reasonable if they anticipated people putting pointers in that field. In FPC it originally wasn't either, but that was already changed around a decade ago to prepare for Delphi 64-bit compatibility which changed it from LongInt to NativeInt (also for a pointer it's irrelevant whether it's stored as a signed or unsigned ordinal as long as the bit width is correct, cause it needs to be cast anyway). Also please note that Delphi's documentation explicitly mentions that Tag is often used for pointer values (emphasis mine): QuoteTag has no predefined meaning. The Tag property can store any additional integer value for the convenience of developers. Often, Tag stores a pointer. A Tag value can be typecast to the appropriate pointer type. Notice that on 64-bit platforms, all pointer types are 8 bytes in size, while on 32-bit platforms, pointer types are 4 bytes. These pointer sizes correspond to sizes of NativeInt integral values on 64-bit and 32-bit platforms. Quote from: MarkMLl on July 04, 2022, 01:56:50 pmWhich I suppose raises the interesting question of what the lowest-overhead object is which can store a single string. Depends: if you know it's less than 4 characters it would be a UInt32 or Int32 (e.g. for ACPI device names) (in MacPas mode FPC supports assigning 4 character strings to 32-bit ordinals ), otherwise it would probably be PChar, followed by ShortString (with the obvious restriction in length), then a dynamic array of Char and then AnsiString. ---------------------------------------------------------------------- MarkMLl · 2022-07-05 13:05 Quote from: PascalDragon on July 05, 2022, 02:13:37 pmQuote from: MarkMLl on July 04, 2022, 01:56:50 pmWhich I suppose raises the interesting question of what the lowest-overhead object is which can store a single string. Depends: if you know it's less than 4 characters it would be a UInt32 or Int32 (e.g. for ACPI device names) (in MacPas mode FPC supports assigning 4 character strings to 32-bit ordinals ), otherwise it would probably be PChar, followed by ShortString (with the obvious restriction in length), then a dynamic array of Char and then AnsiString. Thanks for that but I did specifically say object, i.e. something whose type/class can be tested at runtime which I presume isn't possible for a dynamic array or an ansistring. Superficially one would assume that a StringList with a single entry is overkill, but that depends on how much per-instance space is allocated and on whether anything time-consuming is done during creation. MarkMLl ---------------------------------------------------------------------- PascalDragon · 2022-07-06 11:28 Quote from: MarkMLl on July 05, 2022, 03:05:09 pmQuote from: PascalDragon on July 05, 2022, 02:13:37 pmQuote from: MarkMLl on July 04, 2022, 01:56:50 pmWhich I suppose raises the interesting question of what the lowest-overhead object is which can store a single string. Depends: if you know it's less than 4 characters it would be a UInt32 or Int32 (e.g. for ACPI device names) (in MacPas mode FPC supports assigning 4 character strings to 32-bit ordinals ), otherwise it would probably be PChar, followed by ShortString (with the obvious restriction in length), then a dynamic array of Char and then AnsiString. Thanks for that but I did specifically say object, i.e. something whose type/class can be tested at runtime which I presume isn't possible for a dynamic array or an ansistring. “Object” is a rather overloaded world in the Pascal world. Aside from an object instance it might mean TP-style object or - as I had interpreted it - simply a collective name for something that represents something. Please note that you can't reliably detect however the case that the value is not 0, but also not a valid object (e.g. store 1 into the Tag and try to handle it as a TObject-descendant). Quote from: MarkMLl on July 05, 2022, 03:05:09 pmSuperficially one would assume that a StringList with a single entry is overkill, but that depends on how much per-instance space is allocated and on whether anything time-consuming is done during creation. If you want to go with a TObject-descendant then a direct descendant of TObject with a String field is the best bet (and some do in fact use the Tag property like this) ---------------------------------------------------------------------- MarkMLl · 2022-07-06 12:14 Quote from: PascalDragon on July 06, 2022, 01:28:48 pm“Object” is a rather overloaded world in the Pascal world. Aside from an object instance it might mean TP-style object or - as I had interpreted it - simply a collective name for something that represents something. My apologies, I generally try to avoid the word instead favouring something like item/entity vs instance. QuotePlease note that you can't reliably detect however the case that the value is not 0, but also not a valid object (e.g. store 1 into the Tag and try to handle it as a TObject-descendant). Although the misalignment should be a strong hint. That's actually how classic Smalltalk distinguished between objects (their term :-) and integers: whether the LSB was set; and I think I've noticed hardware support in recent x86 to specifically make that check. QuoteIf you want to go with a TObject-descendant then a direct descendant of TObject with a String field is the best bet (and some do in fact use the Tag property like this) Thanks, that's what I was thinking (but I was interested in case I was overlooking something). MarkMLl ---------------------------------------------------------------------- PascalDragon · 2022-07-07 11:54 Quote from: MarkMLl on July 06, 2022, 02:14:25 pmQuotePlease note that you can't reliably detect however the case that the value is not 0, but also not a valid object (e.g. store 1 into the Tag and try to handle it as a TObject-descendant). Although the misalignment should be a strong hint. That's actually how classic Smalltalk distinguished between objects (their term :-) and integers: whether the LSB was set; and I think I've noticed hardware support in recent x86 to specifically make that check. In principle nothing forbids the heap manager to allocate its memory 1-Byte aligned and thus you could have a class instance that starts at an odd address. And even then you'd only detect this every second value. ---------------------------------------------------------------------- MarkMLl · 2022-07-07 12:07 Quote from: PascalDragon on July 07, 2022, 01:54:02 pmIn principle nothing forbids the heap manager to allocate its memory 1-Byte aligned and thus you could have a class instance that starts at an odd address. And even then you'd only detect this every second value. Which is why I went no stronger than "hint", and why I remain a fan of tagged or descriptor-based architectures. It will be interesting to see whether CHERI takes off https://www.cl.cam.ac.uk/research/security/ctsrd/cheri/ MarkMLl