Jump to content

A.M. Hoornweg

Members
  • Content Count

    432
  • Joined

  • Last visited

  • Days Won

    8

Everything posted by A.M. Hoornweg

  1. Can't you make a read-only property out of it instead of a plain method? That way you could make it show up in the watch list of the debugger.
  2. A.M. Hoornweg

    A gem from the past (Goto)

    No need for this, the debugger has that functionality built-in: -Set a breakpoint on your corner case condition and wait until it fires -Right-click the line where you want to "go to" -Enter submenu "Debug" -Select menu item "Set next statement". The debugger will jump here when you single-step through your code.
  3. A thread using While not terminated do begin Dosomething; Sleep(xxx); end; is perfectly OK if the thread has to do something useful periodically and if xxx isn't a terribly small number. But if "Dosomething()" is merely polling for some state change to respond to, then it's rather inefficient to have this thread constantly wakeup, consume CPU time and sleep again. The "Sleep(xxx)" also limits the response time and the number of state changes that can be handled per second. For such a scenario it's much more efficient if you can wake up the thread "from the outside" ASAP when it has to react. This is where events really shine.
  4. I would personally write such a program as a service application and let it monitor messages like WM_POWERBROADCAST to log the events. This way you can be sure that the application is always running and will capture the events when they happen.
  5. Hello all, I see that the Delphi 12 source code formatter (Ctrl-D) still mutilates generic class declarations, it inserts inappropriate line feeds between keywords and ruins indentation. Is there a way to set markers in the source code that tell the formatter to skip those areas from formatting? Or can you recommend a better code formatter that handles generics properly? Have a nice day! Arthur
  6. Hello all, suppose I have a generic class that's going to be inherited. I want to give the base class a method that creates a new instance of the same object type (a class factory so to speak). How can I do that? I need to somehow tell the compiler which constructor to call. Type tSomeclass<T:iInterface>=CLass (tInterfacedObject, iSomeInterface<T>) //many methods here Procedure SomeMethod(intf: T); Function SomeFunction:T; Constructor Create(someparameters); Function CreateNewInstance:iSomeInterface<T>; end; ... Function tSomeclass<T>.CreateNewInstance:iSomeInterface<T> begin result:=Self.Classtype.Create(fSomeParameters); //Does not compile end;
  7. A.M. Hoornweg

    Create a new instance of a generic class

    Thank you! That seems to work indeed !
  8. A.M. Hoornweg

    Create a new instance of a generic class

    The reason I'm trying to achieve this is the following. I have made a generic tStreamableInterfaceList<T:iInterface> which is basically a managed Tlist<T> that implements iInterfacelist<T>. The elements are interfaces. This list object knows several special tricks such as saving its contents to XML and BSON. It can also load those elements back from such data files, which necessitates a virtual ClassFactory() method to create the correct objects "on the fly" based on the class names found in the data. So far, this all works very nicely. I wanted to enhance this list with a few LinQ-like methods such as: Function .Where(aCondition:tPredicate<T>):iInterfacelist<T>; Function .OrderBy(aCompare:tComparison<T>):iInterfacelist<T>; ... but in order to achieve that, the base class must be able to dynamically instantiate derived classes or else the resulting objects would have the base nonfunctional Classfactory() method. The "ugly" solution would be to put an abstract virtual Clone() method in the base class, but I'd very much like to avoid that.
  9. A.M. Hoornweg

    Create a new instance of a generic class

    That doesn't do the trick, because function TMyClass<T>.CloneObject: TMyClass<T>; ... will just construct an instance of the base class, I would have to re-implement that method in each derived class. With non-generic classes it is possible to achieve what I want (see below). With generics, the "class of..." syntax does not work. type SomeClassType = class of tSomeClass; tSomeClass = class protected SomeNumber: Integer; public constructor Create(aSomeNumber: Integer); virtual; function Copy: tSomeClass; procedure Whoami; end; tSomeClass2=Class(tSomeClass) End; tSomeClass3=Class(tSomeClass2) End; function tSomeClass.Copy: tSomeClass; var ctype: SomeClassType; begin ctype := SomeClassType(self.ClassType); result := ctype.Create(SomeNumber+1); end; constructor tSomeClass.Create(aSomeNumber: Integer); begin inherited Create; SomeNumber := aSomeNumber; end; procedure tSomeClass.Whoami; begin OutputDebugString(pchar( format('Class name = %s, number is %d',[self.classname,somenumber] ))); end; procedure TForm1.Button2Click(Sender: TObject); begin tSomeclass3.Create(1).Copy.Copy.Copy.Whoami; //yes, this test leaks some memory. end;
  10. Hello all, I'm using Delphi 12 enterprise and in this version I'm unable to create regions using the IDE context menu because the menu item surround is grayed out. What could have caused it and how can I re-enable it?
  11. A.M. Hoornweg

    Delphi 12 IDE, auto-formatter mutilates generics

    Eh what, the internal IDE source code formatter is deprecated? Are they just abandoning it without any replacement? I find it rather essential.
  12. tSearchrec is a record that contains a managed type (the file name is a string). When managed types go out of scope, the code generated by the compiler checks if they are no longer in use and that any heap memory they occupy is freed properly. That costs time.
  13. A.M. Hoornweg

    Rounding issue

    You misinterpret my idea. OP merely used power() and rounding to demonstrate that 32- and 64 bit code do not have identical FP accuracy. Unfortunately people think in decimal digits and want those represented and rounded accurately. I simply recommend to use a fixed point numeric variable type for storage if this is the purpose and if the number of decimals is <=4. A soon as there is a FP calculation involved there is the risk that the result (a double, single or extended) isn't an exact representation anymore. But still, replacing "double" by "currency" in OP's example produces the expected result in both 32 and 64 bit mode.
  14. A.M. Hoornweg

    Rounding issue

    I wouldn't. But I certainly do have a use for fixed comma numbers that must be compared for exactness. As in being part of the composite primary key of a database table. So I cherish the fact that this numeric type can be tested with exactness.
  15. A.M. Hoornweg

    Rounding issue

    One would only do this if one wants to convert floating point numbers to fixed point numbers. Fixed point numbers can be compared with exactness and there are use cases for that.
  16. A.M. Hoornweg

    Rounding issue

    If the number of decimal places is not greater than 4, OP might consider using the currency data type. Currencies are exact fixed-point numbers having four decimals after the dot so a value like 1.015 can be stored with perfect precision. Adding, subtracting and comparing currencies always gives an exact result. Internally, they are 64-bit signed integers with an implicit divisor of 10,000. [edit] functions like "power" return a double or extended. Prior to comparing the results of such functions, store them in currencies.
  17. A.M. Hoornweg

    TToolButton.ImageName Delphi 11 vs Delphi 10

    Or write a tiny command line program that scans a dfm file and removes that property. Run that program prior to committing code to your source code repository. TortoiseSVN allows you to fully automate this by setting "hooks", I don't know about Git.
  18. A.M. Hoornweg

    Replacement for TBits?

    OP needs 300 billion bits or so he said. I assume that collecting that amount of data takes a long time and that the data will be evaluated to obtain some sort of useful result afterwards. So the program must run for a long time and may not be terminated prematurely or else the contents of the tBits are lost and everything has to start over again. That sounds like a very time consuming development/debugging cycle that can be avoided by splitting up the program into an acquisition process and an evaluation process that share a common block of virtual memory. The acquisition part can run "forever" in the background, it can be straightforward because it needs not evaluate the results. It just supplies live data (bits) in memory that become more complete over time. The evaluation part can be a work in progress that can be comfortably debugged, refined and re-compiled. It can observe "live" data that become more complete over time.
  19. A.M. Hoornweg

    Replacement for TBits?

    Sure. But OP has to re-write tBits anyway because of its size limitations. I'd advise him to make the allocation/deallocation methods virtual to keep all options open.
  20. A.M. Hoornweg

    Replacement for TBits?

    David, OP can just use whatever allocation method pleases him. The end result is a pointer to a memory block whatever method he uses. It doesn't make one version worse than the other.
  21. A.M. Hoornweg

    Replacement for TBits?

    In the simplest case a MMF is just a block of bytes, why would that be inappropriate? Just because it's allocated by a different API? We happen to use them extensively for data acquisition, they have that nice little feature that allows us to share the same buffer between processes. One process collecting data, another evaluating it. There are tons of use cases for that.
  22. A.M. Hoornweg

    Replacement for TBits?

    Good to know!
  23. A.M. Hoornweg

    Replacement for TBits?

    I have never tried to allocate such enormous amounts of memory using Delphi's heap manager (Fastmm4), I really don't know how it behaves if you try to allocate one huge chunk that is bigger than what the machine has physically. The documentation says "For win32 and Win64, the default FastMM Memory Manager is optimized for applications that allocate large numbers of small- to medium-sized blocks, as is typical for object-oriented applications and applications that process string data. " MapViewOfFile() bypasses the Delphi heap completely and leaves it up to Windows to map a contiguous block of virtual memory.
  24. A.M. Hoornweg

    Hosting a console in a Delphi Application

    Isn't this what modern internet browser do, having separate child processes for tabs contained in a common host process ?
  25. A.M. Hoornweg

    Replacement for TBits?

    For size reasons. Memory mapped files let you - use contiguous arrays bigger than available RAM, mapping file-backed data into virtual 64-bit address space - use simple pointer arithmetics to access individual bytes, it is dead easy to implement SetBit() GetBit() etc - let the operating system's cache algorithm handle the intricacies of swapping pages in and out (LRU/MRU etc) - benefit from the speed and low latency of modern SSD's - have the data on disk, ready to be re-used Speed-wise this is only an option if the operating system can minimize swapping so accessing the elements shouldn't be totally random. If the probability of accessing an element is some kind of bell curve then it might just work. [edit] typo.
×