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

Bernd prof7bit at googlemail.com
Thu Jul 28 12:44:48 CEST 2011


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)?

Bernd
_____
¹ I depend on OpenSSL already anyways, so I thought why not make use
of its bignum too when I need it.



More information about the fpc-pascal mailing list