Recent

Author Topic: need a dummies guide to constructors/destructors  (Read 1150 times)

srvaldez

  • Full Member
  • ***
  • Posts: 117
need a dummies guide to constructors/destructors
« on: October 10, 2024, 03:21:43 pm »
I looked at the gmp.pas that's found in the units directory but it's rather confusing to me
for example
Code: [Select]
  IMPBase = interface
    ['{390336B5-6B78-47E0-BB0B-48F3AF9D5CCC}']
    function refs: longint;
  end;

  MPInteger = interface(IMPBase)
    ['{F6A977E7-B5E6-42BB-981F-E1A7C7EE0D30}']
    function ptr: mpz_ptr;
  end;
...
and
Code: [Select]

  { TMPBase }

  TMPBase = class(TInterfacedObject, IMPBase)
  private
    function refs: longint;  inline;
  end;

  { TMPInteger }

  TMPInteger = class(TMPBase, MPInteger)
  private
    fmpz: mpz_t;
    function ptr: mpz_ptr;  inline;
  public
    destructor destroy; override;
  end;
is the interface above mandatory?
what is it's purpose ?
and why the GUID ?

VisualLab

  • Hero Member
  • *****
  • Posts: 575
Re: need a dummies guide to constructors/destructors
« Reply #1 on: October 10, 2024, 06:10:46 pm »
As for the IMPBase interface, whether it is mandatory and what specific role it plays in the given library file, you would have to ask its author. On the other hand, interfaces can be of the COM and CORBA type. COM interfaces use GUIDs. In contrast, CORBA interfaces do not use GUIDs. This page sheds some light on interfaces:

https://wiki.freepascal.org/Understanding_Interfaces

Thaddy

  • Hero Member
  • *****
  • Posts: 16193
  • Censorship about opinions does not belong here.
Re: need a dummies guide to constructors/destructors
« Reply #2 on: October 10, 2024, 08:30:34 pm »
That link describes it somewhat nice, but is not complete and the section interfaces to what that link refers to is completely wrong.
A small example:
COM interfaces allow for automatic memory management (corba not), which means you can create an object to its interface instead of a class and you do not need to free it.
Probably, therefor the interfaces are needed. small example:
Code: Pascal  [Select][+][-]
  1. program testcom;
  2. {$mode objfpc}{$apptype console}
  3. type
  4.   ITest = interface
  5.   ['{976DA5DF-01CD-44B0-910A-B02CE2F6F29A}']  
  6.     procedure Show;
  7.   end;
  8.    
  9.   TTest = class(Tinterfacedobject, ITest)
  10.   strict private
  11.     procedure Show;
  12.   end;
  13.  
  14.   procedure TTest.Show;
  15.   begin
  16.     writeln('test');
  17.   end;
  18.  
  19. var
  20.   I:ITest;
  21. begin
  22.   I:=TTest.create;
  23.   I.Show;
  24.   // no memleaks
  25.   readln;
  26. end.
Programming in that style - be consequent - makes it easy to control memory, even from external code.
gmp.pas contains com interfaces and I think precisely for that reason.
« Last Edit: October 11, 2024, 09:58:20 am by Thaddy »
If I smell bad code it usually is bad code and that includes my own code.

srvaldez

  • Full Member
  • ***
  • Posts: 117
Re: need a dummies guide to constructors/destructors
« Reply #3 on: October 10, 2024, 11:08:44 pm »
thanks  :)

silvercoder70

  • Jr. Member
  • **
  • Posts: 96
    • Tim Coates
Re: need a dummies guide to constructors/destructors
« Reply #4 on: November 07, 2024, 11:24:39 pm »
Just an small side note...

as Thaddy said what you are looking at or referred to are Interfaces,

A class needs (?) to have a constructor and destructor - well, it should if you are allocating and freeing memory. Have a look at the code here

https://wiki.freepascal.org/Destructor

and then... https://wiki.freepascal.org/Class
Explore the beauty of modern Pascal programming with Delphi & Free Pascal - https://www.youtube.com/@silvercoder70

srvaldez

  • Full Member
  • ***
  • Posts: 117
Re: need a dummies guide to constructors/destructors
« Reply #5 on: November 08, 2024, 12:55:23 am »
hello silvercoder70  :)
you can also achieve the same functionality using advanced records, see https://forum.lazarus.freepascal.org/index.php/topic,68899.0.html on page 2 I posted a couple of examples

TRon

  • Hero Member
  • *****
  • Posts: 3643
Re: need a dummies guide to constructors/destructors
« Reply #6 on: November 08, 2024, 12:58:49 am »
you can also achieve the same functionality using advanced records ...
And at the same time you would be giving up other features that objects do have and advanced records not.

It depends on the use case which one would be more adequate.
This tagline is powered by AI (AI advertisement: Free Pascal the only programming language that matters)

cdbc

  • Hero Member
  • *****
  • Posts: 1670
    • http://www.cdbc.dk
Re: need a dummies guide to constructors/destructors
« Reply #7 on: November 08, 2024, 07:04:46 am »
Hi
What all the others said +
About GUID's - *Do Not Forget Them* They are essential to COM-interfaces as they're unique identifiers that amo. enable the use of as / is operators & enable them to work with windows com-system etc.
To CORBA, the guid's are just *unique* strings, nothing else.
...Oh, when working with COM-interfaces(default), be careful with inter-mixing classes and interfaces  ...and there's a little *gotcha* if you use interfaces as CONST parameters, the ref-count of said interface *doesn't* get increased!
Just my 2 cents worth
Regards Benny
If it ain't broke, don't fix it ;)
PCLinuxOS(rolling release) 64bit -> KDE5 -> FPC 3.2.2 -> Lazarus 2.2.6 up until Jan 2024 from then on it's: KDE5/QT5 -> FPC 3.3.1 -> Lazarus 3.0

jamie

  • Hero Member
  • *****
  • Posts: 6735
Re: need a dummies guide to constructors/destructors
« Reply #8 on: November 09, 2024, 04:20:00 pm »
What I find is more educational is the background on how the ObjectS/Classes actaully work.

This means using examples of non-object code to show how a Function/procedure works using records and the SELF parameter being assigned each time a function is called that acts like an object/class.

 From this perspective gives rise to show how things work in the background to better understand what is happening in the foreground.
 The Constructor in this case would initialize the SELF parameter and the rest that gets set using this now new chuck of memory would be the record being past to the non-object type of code.

 Classes of course and Objects with Virtual items in it requires more processing at the point of creating this new SELF parameter.

 It's just a thought but I find many people coming here are quite capable of understanding this at the lower level and more than likely prefer it this way, I Know I do.

The only true wisdom is knowing you know nothing

cdbc

  • Hero Member
  • *****
  • Posts: 1670
    • http://www.cdbc.dk
Re: need a dummies guide to constructors/destructors
« Reply #9 on: November 09, 2024, 04:26:00 pm »
Hi
If you really want your head to spin, then have a look at this:
Code: Pascal  [Select][+][-]
  1. program intf_no_class; ///// was tmanintf;
  2.  
  3. {$mode objfpc}
  4. {$WARN 5028 off : Local $1 "$2" is not used}
  5. type
  6.   TInterface = class
  7.   public
  8.     function QueryInterface(constref {%H-}IID: TGUID;{%H-} out {%H-}Obj): HResult; {$IFNDEF WINDOWS}cdecl{$ELSE}stdcall{$ENDIF}; virtual;
  9.     function _AddRef: LongInt; {$IFNDEF WINDOWS}cdecl{$ELSE}stdcall{$ENDIF}; virtual; abstract;
  10.     function _Release: LongInt; {$IFNDEF WINDOWS}cdecl{$ELSE}stdcall{$ENDIF};  virtual; abstract;
  11.   end;
  12.  
  13.   TRawInterface = class(TInterface)
  14.   public
  15.     function _AddRef: LongInt; override;
  16.     function _Release: LongInt; override;
  17.   end;
  18.  
  19.   ICompare = interface
  20.     function Compare(const ALeft, ARight: Int8): Integer;
  21.   end;
  22.  
  23.   PComparerVMT = ^TComparerVMT;
  24.   TComparerVMT = packed record
  25.     QueryInterface: CodePointer;
  26.     _AddRef: CodePointer;
  27.     _Release: CodePointer;
  28.     Compare: CodePointer;
  29.   end;
  30.  
  31.   TCompare = class
  32.     class function Int8(const ALeft, ARight: Int8): Integer;
  33.   end;
  34.  
  35. const
  36.   { please note that while this uses instance methods of TRawInterface this does not necessarily
  37.     mean that a TRawInterface instance is passed in; e.g. the code in Generics.Defaults uses
  38.     a different type as Self that contains the reference count for those types where the
  39.     reference count is necessary }
  40.   Comparer_Int8_VMT  : TComparerVMT = (QueryInterface: @TRawInterface.QueryInterface; _AddRef: @TRawInterface._AddRef; _Release: @TRawInterface._Release; Compare: @TCompare.Int8);
  41.  
  42.   Comparer_Int8_Instance  : Pointer = @Comparer_Int8_VMT;
  43.  
  44. function TInterface.QueryInterface(constref IID: TGUID; out Obj): HResult; {$IFNDEF WINDOWS}cdecl{$ELSE}stdcall{$ENDIF};
  45. begin
  46.   Result := E_NOINTERFACE;
  47. end;
  48.  
  49. function TRawInterface._AddRef: LongInt; {$IFNDEF WINDOWS}cdecl{$ELSE}stdcall{$ENDIF};
  50. begin
  51.   Result := -1;
  52. end;
  53.  
  54. function TRawInterface._Release: LongInt; {$IFNDEF WINDOWS}cdecl{$ELSE}stdcall{$ENDIF};
  55. begin
  56.   Result := -1;
  57. end;
  58.  
  59. class function TCompare.Int8(const ALeft, ARight: Int8): Integer;
  60. begin
  61.   Result := ALeft - ARight;
  62. end;
  63.  
  64. var
  65.   intf: ICompare;
  66. begin
  67.   intf := ICompare(@Comparer_Int8_Instance);
  68.   Writeln(intf.Compare(2, 4));
  69.   Writeln(intf.Compare(4, 2));
  70.   Writeln(intf.Compare(4, 4));
  71. end.
  72. (*
  73. <postscript>
  74. Look at the Generics.Collections unit he mentioned, and find stuff like "    // IComparer VMT"
  75. => constructing the interface without the compiler (if you know the memory layout that the compiler expects....)
  76.  
  77. An interface is (simplified) a jump table (a VMT), and maybe some data. So putting together your own VMT, then it can have any code pointer, as long as the code they point to, matches the method signatures in the interface.  But this "any code" then does not need to go to an object/instance...
  78.  
  79. I have not checked, but there may also be code to inject those "interfaces" into classes, so that "SomeClass as IThisInterface" will return such an interface. And then such an interface, despite being returned by that class, has no link to that class.
  80. </postscript>
  81. *)
  82.  

edit: the code stems from @PascalDragon, in this thread.

Regards Benny
« Last Edit: November 09, 2024, 04:30:02 pm by cdbc »
If it ain't broke, don't fix it ;)
PCLinuxOS(rolling release) 64bit -> KDE5 -> FPC 3.2.2 -> Lazarus 2.2.6 up until Jan 2024 from then on it's: KDE5/QT5 -> FPC 3.3.1 -> Lazarus 3.0

 

TinyPortal © 2005-2018