Page 1 of 4

A OOP class for implementing stacks -massively upgraded

Posted: Tue Oct 30, 2007 10:27 pm
by srod
Update : 22nd November 2007.
Have added a new method, namely; Peek().

This allows you to examine an element from a stack without affecting the stack at all; that is no kind of 'pop' action is performed.

Note that the index supplied to this method is zero-based and thus must be a value within the range 0 to NumberOfElementsPushed()-1 etc.

The user manual has been updated.

Download


Bug fix : 8th November 2007.

A bug with popping structures containing string fields has been fixed. :)

Download


Upgraded : 1st November 2007.

Hi,

I have restructured the class so that we now have two stack classes inheriting ( :wink: ) from a single base class. The first class gives a basic stack (as previous) in which stack objects can be created dynamically and 32-bit values pushed and popped at will.

A second class (sharing the same method names to give some kind of polymorphism!) allows you to push and pop any kind of structure, even those containing string fields. No need to worry about memory leaks as all strings are carefully freed etc.

It is important to understand exactly how these more complex stack objects function :
When the developer pushes a structured variable, the entire structure is copied (including string fields) and placed on the underlying stack. This means that, having pushed such a variable onto a structured stack, the developer is then free to modify the original variable, safe in the knowledge that the original fields are preserved on the stack.

The only proviso is that all string fields must be placed before all other fields.

The download includes a couple of examples + a very short user guide. Don't worry if you're not up to scratch with OOP terminology as this little library is very simple to use. :)

Download



Hi,

I'm rewriting some old utility routines of mine, but this time in the form of simple OOP classes as this is the way I seem to be heading lately. And why not indeed? Basic OOP is a breeze in Purebasic! :wink:

This first utility allows the developer to easily create stack objects with all the usual functionality etc.

Create a stack object with something like :

Code: Select all

MyStackObject.StackObject
MyStackObject = NewStack(100)  ;For a stack of size 100 etc.
Push a value with :

Code: Select all

MyStackObject\Push(25)  ;Pushes the value 25 for example.
and so on.

REMAINDER OF POST REMOVED.

Posted: Tue Oct 30, 2007 10:27 pm
by srod
REMOVED.

Posted: Tue Oct 30, 2007 11:43 pm
by #NULL
i just have a question: where are the limits for linked lists to be used as
stack? is it just a speed thing or is there something else?

Posted: Tue Oct 30, 2007 11:45 pm
by srod
There are no linked lists in this code!

You are mistaking method calls for linked list functions.

Posted: Wed Oct 31, 2007 12:38 am
by citystate
I think #NULL is asking how a stack using a linked list compares to your code re speed - I mean, apart from the obvious coolness of using OOP in PB, is there a reason to reinvent the wheel?

incidently, nice code :)

Posted: Wed Oct 31, 2007 12:52 am
by srod
citystate wrote:I think #NULL is asking how a stack using a linked list compares to your code re speed - I mean, apart from the obvious coolness of using OOP in PB, is there a reason to reinvent the wheel?

incidently, nice code :)
A linked list would be over-kill for implementing a stack in my opinion, not really a natural way of doing it. The method given here, aside from the usual overheads when using OOP (indirect function calling etc.) is about as direct as it gets.

As for reinventing the wheel... if this code helps anyone or simply fits in with their way of doing things then all is well and good. Otherwise, just ignore it.

As I said, I am moving more and more towards OOP now for my own reasons and am more than happy to do so using PB.

Posted: Wed Oct 31, 2007 1:37 am
by Dare
Thanks for the code.

Maybe when you have a spare moment ( :lol: ) you can write an "'OOP in PureBasic' for the brain dead" tutorial.

As the #1 qualifier among your potential reader base I look forward to reading it!
:D

Posted: Wed Oct 31, 2007 10:35 am
by milan1612
Nice code, could be useful in one of my projects :P

Posted: Wed Oct 31, 2007 11:29 am
by #NULL
i find the code interesting too, nice work i can learn from, thanks.
and about my own question (citystate understood right): one can use your code to get stacks dynamically and you can use pointers in structures to it. as far as i know you couldn't do that with PB-LLs, so that would be an advantage.

Posted: Wed Oct 31, 2007 12:29 pm
by srod
Thanks all.

@#NULL : still not sure I understand, but sure you could use a stack to hold pointers into a linked list etc. However, keeping such code threadsafe would involve a little work. I prefer to instead directly allocate memory for each structure and hold a pointer to it in the stack etc. Have to take care with freeing strings this way, but as I say it is my preferred option.

@Dare : you must be kidding lad; 'spare moment'? Who do you think I am, the Deli Lama? :wink: Anyhow I think there are already a couple of tutorials floating around. Are you asking about using objects / classes in your own code or creating them with interfaces etc?

**EDIT : @#NULL : ah now I understand what you are saying! Doh, we Brits are a little slow in the head sometimes. The only time I work up a head of steam is when the pubs open! :wink: You are correct in that you can embed a stack object within any kind of structure; dynamic or otherwise. As long as you remember to use the Destroy() method immediately prior to the structure losing focus etc. as there are no destructors being called automatically.

Posted: Wed Oct 31, 2007 1:14 pm
by IceSoft
srod wrote:As I said, I am moving more and more towards OOP now for my own reasons and am more than happy to do so using PB.
Yes. You are on the right way.
OOP is more and more important for reusable sources.

Posted: Wed Oct 31, 2007 1:33 pm
by srod
IceSoft wrote:
srod wrote:As I said, I am moving more and more towards OOP now for my own reasons and am more than happy to do so using PB.
Yes. You are on the right way.
OOP is more and more important for reusable sources.
sshhhhh, (srod whispers) we don't want to start a debate about that old war-horse again! :wink:

Actually, it's a debate which has always puzzled me a little; if people want to use OOP then PB has all the necessary ingredients required to do so. Okay, it is lacking some of the 'heavier' elements required for what some would describe as 'true oop', but to my mind that is a good thing! As it stands PB allows for clean and very efficient use of basic oop techniques without all of the clutter and bloat. I would say that the only thing missing of any real consequence are constructors and destructors, but even this isn't really a big deal. No, PB has a lightweight and very easy to use means of employing simple oop and I think offers a great balance between functionality, ease of use and without the outright absurdties that comes with many other implementations.

Posted: Wed Oct 31, 2007 7:24 pm
by Hroudtwolf
Nice code.
Thank you very much for sharing.

Best regards

Wolf

Posted: Wed Oct 31, 2007 8:09 pm
by srod
You're welcome wolfie. :)

Posted: Fri Nov 02, 2007 1:02 am
by srod
Upgraded : 1st November 2007.

Hi,

I have restructured the class so that we now have two stack classes inheriting ( :wink: ) from a single base class. The first class gives a basic stack (as previous) in which stack objects can be created dynamically and 32-bit values pushed and popped at will.

A second class (sharing the same method names to give some kind of polymorphism!) allows you to push and pop any kind of structure, even those containing string fields. No need to worry about memory leaks as all strings are carefully freed etc.

It is important to understand exactly how these more complex stack objects function :
When the developer pushes a structured variable, the entire structure is copied (including string fields) and placed on the underlying stack. This means that, having pushed such a variable onto a structured stack, the developer is then free to modify the original variable, safe in the knowledge that the original fields are preserved on the stack.

The only proviso is that all string fields must be placed before all other fields.
See the first post for the download link.