[fpc-pascal] operator overloading and counting references / creating / destoying anonymous instances

Mark Morgan Lloyd markMLl.fpc-pascal at telemetry.co.uk
Thu Jul 28 23:04:37 CEST 2011


Bernd wrote:
> Hi,
> 
> I am just trying to wrap (parts of) the OpenSSLĀ¹ bignum library (large
> integer arithmetic) into something that makes using it look at least a
> little bit more elegant than using the naked procedural C API  while
> at the same time trying to not overdo it and not create a heavy code
> monster instead of a lightweight wrapper.
> 
> The problem is: Each bignum needs to be allocated by OpenSSL and freed
> when no longer needed. If I make a Pascal Object or Class representing
> a bignum and overload the arithmetic operators and then have
> expressions like (a+b)*(c+d) I would need to find a way to create and
> destroy anonymous instances of bignums on the fly. I have tried making
> use of Interface and TInterfacedObject and this seems to do what I
> want: for example when witing A := A + B the + operator would return a
> new instance and the reference counting would then automatically call
> the destructor of A when it assigns the newly created number.
> 
> The question is: Isn't using interfaces only to get reference counting
> complete overkill in this situation, isn't there a more lightweight
> way of doing this (or something similar)? And also maybe does some
> clever and elegant pattern exist that would enable me to simply reuse
> the instance of A (if an initialized object exists already on the left
> side of the assignment) instead of destroying it and creating a new
> one? (OpenSSL would theoretically allow me to reuse the same allocated
> number handle for the result of an operation, I could do BN_add(a,a,a)
> without allocating more than one number handle)?

I wonder if I could ask a silly question here, without displaying too 
much ignorance.

I generally understand the significance of an interface in the Windows 
context, where COM (or whatever today's name for it) is integrated 
fairly deeply into the OS.

But what of other OSes like Linux? Does the use of interfaces as a way 
of getting refcounted storage assume the presence of CORBA etc., and how 
much overhead is there at program startup- does it demand that an ORB be 
loaded into memory?

-- 
Mark Morgan Lloyd
markMLl .AT. telemetry.co .DOT. uk

[Opinions above are the author's, not those of his employers or colleagues]



More information about the fpc-pascal mailing list