Page 1 of 1
Creating Objects
Posted: Wed Feb 16, 2005 6:43 pm
by Andras
Hi,
my (german) Help says at the end of the "Interfaces"-Description, that an example for creating own objects is located in the advanced examples in 'PureBasic/Sources'... But I cannot find these examples...
I have seen, that it is possible to create your own objects in PB using Interfaces (
viewtopic.php?t=9553&highlight=components ), but an "official" example would be nice.
Posted: Wed Feb 16, 2005 7:14 pm
by Psychophanta
Just jesterday i was looking for samething.
I second your request for an PB "official" example for OOP using Interfaces.
For your help, you can take a look at
www.purearea.net in the code-archive section.
Re: Creating Objects
Posted: Wed Feb 16, 2005 7:16 pm
by Psychophanta
BTW
Andras wrote:optical mouse, 17" monitor, keyboard

Posted: Thu Feb 17, 2005 12:48 pm
by Andras
Hmmm, few people seem to be interested in creating objects/components in PB... But I'm going to write a tool where you will be able to define a 'class' and that will write the resulting PB-Sourcefile...
Posted: Thu Feb 17, 2005 1:39 pm
by Psychophanta
Andras wrote:Hmmm, few people seem to be interested in creating objects/components in PB... But I'm going to write a tool where you will be able to define a 'class' and that will write the resulting PB-Sourcefile...
That's a nice idea.
At
viewtopic.php?t=11339 i just tried to understand, but i think it is too much ugly just only to have code in a specific
way of sort, the
OOP way of sort is not the best way for the apps i make.
However your idea is good.

Posted: Fri Feb 18, 2005 1:24 pm
by GedB
Andras,
I've been playing with this myself for a while.
The big problem is memory management. As soon as you start creating objects you start to allocate lots of memory and pass it around.
This is why Java, Eiffel and Smalltalk all have garbage collection.
C++ does not have garbage collection, but it does allow the allocation of objects on the stack that are guaranteed to be reclaimed.
Once you have an object has to live beyond the scope of its calling frame you either have to handle all memory management yourself or take complex approaches that depend on strict discipline. For example:
http://www.relisoft.com/resource/resmain.html
None of this really lives in the spirit of purebasic. Garbage collection adds runtime overhead and manaul memory management coding complexity.
I've been playing with different ideas, but I'm not going to burden the general population with anything until I have something worthwhile.
At the moment I'm looking at IOC (inversion of control).
With this approach using objects would be rather like using Windows. ie, you request an object from an object engine and provide it with callback functions for your implementation. The allocation and deallocation of memory would all be taken care of by the engine.
Anybody else who is interested in working on this problem with me, I'd appreciate having somebody to bounce ideas off.
Posted: Mon Feb 21, 2005 6:03 pm
by Andras
Hi GedB,
how should the object-engine be able to automatically deallocate the memory, when purebasic does not tell anybody, if a "variable" (memory address) is still in use?
Posted: Mon Feb 21, 2005 6:24 pm
by GedB
Andras,
That is the problem in a nutshell.
Using the object engine would work more like using a database than objects in C++ or Java. Sessions would be opened, and the objects accessed within that session. Once the session is completed all objects referenced are considered finished with.
Posted: Mon Feb 21, 2005 6:27 pm
by Andras
An extra thread, that deletes the 'object-session' after a 'time-out'?
Posted: Mon Feb 21, 2005 6:55 pm
by GedB
That would be an option.
It could be optional. During the development the thread could be used to clean up any sessions left open, logging a report every time it does so.
The programmer can use this to make sure that they don't have any memory leaks before going live.
Once they are confident the code is memory tight, they can turn the thread off.
Posted: Mon Feb 21, 2005 9:12 pm
by Andras
I would prefer something, that automatically destroys objects... For example the object-engine could own a 'destruction-method' that destroys all objects, that aren't selected to 'survive'. I think it's easier to tell, what should survive, than what should be destroyed.
Code: Select all
procedure GetASheep.l()
ObjEngine\ProcStart() ; Marking Start- and EndPoints enables us to know, inside of which procedure-depths the objects are created...
Milky.anObj=ObjEngine\GetCow(@Milky)
Dolly.anObj=ObjEngine\GetSheep(@Dolly)
Dolly\Survive() ;Dolly will survive the procedure, Milky won't...
ObjEngine\ProcEnd()
ProcedureReturn(@Dolly)
endprocedure
If we would make the 'mistake' to create an object twice, the object-engine will notice this, because it knows the PointerAddress.
Sorry, if my english is not so good..
Posted: Tue Feb 22, 2005 10:08 am
by GedB
Andras,
This is very different to the approach that I want to take.
The first thing I want to get rid of is the reliance on memory pointers are object identifiers. I would want pointers to be used only locally within a procedure and never passed around as references.
I also want to avoid having to add runtime overhead, such as a separate thread.
Instead of adding complexity at coding time, such as having to deal with strong and weak references, or at runtime, with separate threads having to run, I would like to make the engine simple and transparant.
The objects would be created in a separate space, with the option of sharing between programs in a dll.
The objects would have full reflection. For example, you would be able to query the object engine to list all objects referencing another object. Programmers could use this facility to add automatic resource management if the felt the need.
The objects would have there own lifespan independant of the code using them, with the option for automatic persistance.
The whole thing would have to be small, so that he option of having it statically bound to an executable would be acceptable.
I'd also want to have to methods of using it. Firstly, through interfaces. Secondly, using the Purebasic style of Object usage, such as that used by the Windows library.
I don't want to implement Objects just at they are in every other language. That has alreaedy been done. There is no way I could do something better that SmartEiffel or Ocaml.
I want something that, like Purebasic, does things differently.