Variants vs. Variant Records

classic Classic list List threaded Threaded
8 messages Options
Reply | Threaded
Open this post in threaded view
|

Variants vs. Variant Records

Agustin Barto
I have to build some container classes (like java Vector) for a
project. I was thinking on how to store some primitive values (so far
I only need to store Integer, Real or Extended and ShortString) and I
can't decide between variants (variant arrays) and variant records
(dynamic arrays of variant records). Any hints?

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

re: Variants vs. Variant Records

David Emerson
keep in mind that when Pascal stores a variant record, each record is the same size -- the maximally sized variant.

So, for example, if you have

type my_variant_record = record
case rec_type : longint of
   1: ( my_bool : boolean );
   2: ( p_to_ss  : ^shortstring );
   3: ( x, y : longint );
   end;

Then All records of this type will be the maximum size, which would be three longints: rec_type, x, y (for the case 3). So even when you only have case 1, a boolean and the first longint, that record size would be the maximum size 3 * sizeof (longint) = 3 * (4 bytes) = 12 bytes.

Then, you could also do something like

type
   primitive_value_type = (prim_value_1_shortstring, prim_value_2_real, etc...);
   my_variant_array_element = record
      elem_type : primitive_value_type;
      p : pointer;   // points to a variable of type elem_type;

I don't know if the above will work. It may be that you would have to structure it as a variant record:

   my_variant_array_element = record
      case elem_type : primitive_value_type of
         prim_value_1_shortstring : ( shortstring_pointer : ^shortstring );
         prim_value_2_real : (real_pointer : ^real );
         etc...
      end;

(Note: my syntax for those pointers might not be correct; I don't use pointers much.)

Last I checked (fpc 1.9.4) it was not possible to give variant array elements of the same size and position duplicate names, so even though they're all pointers, you could not give the same name, e.g. "p" to "shortstring_pointer" and "real_pointer". This should not be too big a deal, because you really ought to know what you're working with all the time anyway. But there was one time when I had a record type that was somewhat similar to three variants like (x, a) (x, b) (q, r, s, t) and I just had to bring x out of the variant types even though it would have been sensible in this case to have x address the first longint in variants type 1 and 2. Oh well.

The nice thing about the latter two methods is that you get an array with relatively small elements, so if your arrays are sizeable, it should save memory. If the arrays are not too big, the first one might be the way to go, since it ought to be slightly faster (saving only dereferencing a pointer in some variant cases)

I guess I don't actually know what you mean by "variant arrays"!

~David.

 -------- Original Message --------

> From: Agustin Barto <[hidden email]>
> Sent: Saturday, July 23, 2005 5:57 PM
> To: FPC-Pascal users discussions <[hidden email]>
> Subject: [fpc-pascal] Variants vs. Variant Records
>
> I have to build some container classes (like java Vector) for a
> project. I was thinking on how to store some primitive values (so far
> I only need to store Integer, Real or Extended and ShortString) and I
> can't decide between variants (variant arrays) and variant records
> (dynamic arrays of variant records). Any hints?
>
> _______________________________________________
> fpc-pascal maillist  -  [hidden email]
> http://lists.freepascal.org/mailman/listinfo/fpc-pascal 





_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Variants vs. Variant Records

Florian Klaempfl-2
In reply to this post by Agustin Barto
Agustin Barto wrote:
> I have to build some container classes (like java Vector) for a
> project. I was thinking on how to store some primitive values (so far
> I only need to store Integer, Real or Extended and ShortString) and I
> can't decide between variants (variant arrays) and variant records
> (dynamic arrays of variant records). Any hints?

Variant arrays, i.e. arrays _stored_ in variants are dog slow. A good
compromise is a dyn. array of variant:

var
  a : array of variant;


If you want real speed, using dyn. arrays of variant records is the best
solution of course.

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Exporting pointers to classes or objects

L505
Questions on returning a pointer to a class with regards to DLL/SO's (exporting
pointers)

(I realize you can't export a class yet for FPC)

Would returning a pointer to a class (either through a function result, or a
function Var (by reference) parameter) allow you to use classes across module
boundaries (library/executable).

 I am going to experiment with this, but even if it works in some odd way, I of
course want to know if it would be reliable or just hit and miss.

Would a pointer be more efficient or roughly the same in comparison to exporting
class?

Also, are classes from FPC different binary constructs than the classes from
Delphi?.. so you wouldn't be able to use a exported pointer or exported class in
a Delphi program from an FPC library?

Regards,
Lars


_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Exporting pointers to classes or objects

Marc Santhoff
Am Sonntag, den 24.07.2005, 14:32 -0700 schrieb L505:
> Questions on returning a pointer to a class with regards to DLL/SO's (exporting
> pointers)
[...]

I've found an old local copy of the article I mentioned in the thread
about using dialogs from dlls. Since it's not online at Borlands website
any more (from somewhen in 1998), I would send you a copy if you like.

On the other hand I'd really appreciate a validation on the techniques
mentioned in that article with respect to fpc and lazarus from the pros,
so if anybody else wants to read it ...

Title and table of contents:

<snip>
Sharing Code and Objects Between Delphi and C++
Conrad Herrmann, Staff Engineer, Borland International

Sharing Functions
Packaging
Sharing Objects Between Borland C++ and Delphi
Using A Delphi Form As A Modeless Window In A C++ MFC Or Owl Program
Conclusion
</snip>

HTH,
Marc



_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Exporting pointers to classes or objects

L505

| I've found an old local copy of the article I mentioned in the thread
| about using dialogs from dlls. Since it's not online at Borlands website
| any more (from somewhen in 1998), I would send you a copy if you like.
|

Thanks will read that one if I can find it.  One other thing I should have
considered, is that the object/class pointers being exported, if even possible,
would of course have to use manual memory managed strings. So any object/class
that used an ansistring would have to be modified to use pchars, shortstrings,
or a combination pchar/pascal string. I guess the real problem is tedious memory
management of each string within the class (if the class uses strings), even if
you can export a pointer to an object.


_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

RE: Exporting pointers to classes or objects

Jeff Pohlmeyer
In reply to this post by L505
> Since it's not online at Borlands website any more (from somewhen in 1998)
Is this it ?
http://info.borland.com/borlandcpp/papers/bc360/

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: RE: Exporting pointers to classes or objects

Marc Santhoff
Am Montag, den 25.07.2005, 13:29 -0500 schrieb Jeff Pohlmeyer:
> > Since it's not online at Borlands website any more (from somewhen in 1998)
> Is this it ?
> http://info.borland.com/borlandcpp/papers/bc360/

Yes, that's the one I was talking about.

Marc



_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal