A OOP class for dynamically implementing arrays! (upgraded!)

Share your advanced PureBasic knowledge/code with the community.
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

A OOP class for dynamically implementing arrays! (upgraded!)

Post by srod »

Update : 9/12/2007.
This is proving to be more useful than I imagined it would be when I first wrote this utility! Consequently, I have seen fit to add two additional methods and to think very carefully about a potential dll problem (described below and in detail within the user guide).

The new methods are :
  • SwapElements() - no explanation required here I think! :) This is very fast even with structured arrays because all it does in these cases is swap pointers etc.
  • GetPointerToValue().
    Now this is for structured array classes only and is much faster than the GetValue() method in these cases because it does not populate a given structure from an array, but simply returns a pointer. (DO NOT use this pointer to directly alter string fields within the array itself though!)
Now this second method has been added because (speed issues aside), for structured arrays containing string fields, it is not 'safe' to use the GetValue() method across a 'dll boundary'. This is because PB dll's use a separate heap upon which to store their strings.
Please see the user guide for a detailed explanation of this.

Of course, this is really only an issue for those who decide to stuff the code for the array classes within a dll and attempt to call the resulting functions from the host application etc. Not a problem in general, just for this rather special case.

Download

============================================


Hi,

this is the second of my long standing utilities which I've now converted to OOP and this one I use more than any other utility I possess. Indeed, as I now spend the majority of my coding time creating custom Window's controls, this utility deals very nicely with just about all of the dynamic memory requirements which creating such controls demand.

In converting to OOP I have also taken the opportunity to upgrade the library somewhat.

Basically, this library (provided in source code form) allows you to dynamically create and nest dynamic arrays of any structured type - including those containing string fields. Such arrays can be nested and embedded in structures or linked lists or even other arrays etc.

The library basically wraps PB's memory functions, but takes care to ensure, in particular, that string fields are handled correctly so that there are no memory leaks etc. This is always tricky when dealing with dynamic memory allocations and strings. I tend to avoid linked lists for such things in order to ensure threadsafety without having to resort to synchronisation techniques etc.

The library is fully cross-platform and the download includes all the source code, two examples (one each for the different types of array class) and a short user guide.

Methods exposed by each of the two array classes :
  • Destroy()
  • GetUpperBound()
  • GetValue()
  • ReDim()
  • SetValue()
  • ShiftElementsLeft()
  • ShiftElementsRight()
Download

I will ask that if you are no fan of OOP or indeed cannot see any use (or have no use) for this utility then please refrain from commenting here. Start another thread, send a pm, write a letter, send a carrier pigeon, contact your local MP... etc. :)
Last edited by srod on Sun Dec 09, 2007 4:02 pm, edited 3 times in total.
I may look like a mule, but I'm not a complete ass.
milan1612
Addict
Addict
Posts: 894
Joined: Thu Apr 05, 2007 12:15 am
Location: Nuremberg, Germany
Contact:

Post by milan1612 »

YAISP ( = YetAnotherIngeniousSrodProduction)
Quite useful, keep your oop stuff coming :)
Windows 7 & PureBasic 4.4
User avatar
pdwyer
Addict
Addict
Posts: 2813
Joined: Tue May 08, 2007 1:27 pm
Location: Chiba, Japan

Post by pdwyer »

:shock:

Even comes with a help Doc!

You're raising the bar around here! very 8) !
Paul Dwyer

“In nature, it’s not the strongest nor the most intelligent who survives. It’s the most adaptable to change” - Charles Darwin
“If you can't explain it to a six-year old you really don't understand it yourself.” - Albert Einstein
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

Post by srod »

Thanks. I hope it's useful.
pdwyer wrote::shock:

Even comes with a help Doc!

You're raising the bar around here! very 8) !
Nice of you to say, but such tools have to be classed as being relatively simple. :)
I may look like a mule, but I'm not a complete ass.
Heathen
Enthusiast
Enthusiast
Posts: 498
Joined: Tue Sep 27, 2005 6:54 pm
Location: At my pc coding..

Post by Heathen »

I look forward to checking this out :)
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

Post by srod »

Update : 9/12/2007.
This is proving to be more useful than I imagined it would be when I first wrote this utility! Consequently, I have seen fit to add two additional methods and to think very carefully about a potential dll problem (described below and in detail within the user guide).

The new methods are :
  • SwapElements() - no explanation required here I think! :) This is very fast even with structured arrays because all it does in these cases is swap pointers etc.
  • GetPointerToValue().
    Now this is for structured array classes only and is much faster than the GetValue() method in these cases because it does not populate a given structure from an array, but simply returns a pointer. (DO NOT use this pointer to directly alter string fields within the array itself though!)
Now this second method has been added because (speed issues aside), for structured arrays containing string fields, it is not 'safe' to use the GetValue() method across a 'dll boundary'. This is because PB dll's use a separate heap upon which to store their strings.
Please see the user guide for a detailed explanation of this.

Of course, this is really only an issue for those who decide to stuff the code for the array classes within a dll and attempt to call the resulting functions from the host application etc. Not a problem in general, just for this rather special case.


Please see the first post for the download.
I may look like a mule, but I'm not a complete ass.
SFSxOI
Addict
Addict
Posts: 2970
Joined: Sat Dec 31, 2005 5:24 pm
Location: Where ya would never look.....

Post by SFSxOI »

srod,

I've never used anything you provided that did not perform better then imagined.

Thank you very much :)
dagcrack
Addict
Addict
Posts: 1868
Joined: Sun Mar 07, 2004 8:47 am
Location: Argentina
Contact:

Post by dagcrack »

Very nice srod!
! Black holes are where God divided by zero !
My little blog!
(Not for the faint hearted!)
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

Post by srod »

You're welcome SFSxOI, dagcrack.

:)
I may look like a mule, but I'm not a complete ass.
Post Reply