Simple and Understanable OOP Structure

Share your advanced PureBasic knowledge/code with the community.
<David.C#S4701L>
New User
New User
Posts: 4
Joined: Thu Sep 20, 2007 1:37 pm
Location: On your computer screen!

Simple and Understanable OOP Structure

Post by <David.C#S4701L> »

Hi everyone, I'm somewhat new to the forums (not PB though! :D ) and thought i would come, say hi and post a little. Well, anyway - i wrote a simple and easy to understand OOP structure, more for beginners but still, have a look over it and tell me what you think! Use it, don't use it, change it, i don't mind.

Structure

Code: Select all

  ;Created by <David.C#S4701L> 2007
  ;If you wish to redistribute please ask first! :)
  ;Official Topic: http://www.purebasic.fr/english/viewtopic.php?t=28818
  
  Global oMax = 5 ;Your maximum amount of objects
  Global Dim Object(oMax)
  
  Enumeration
    #Create
    #Step
    #Destroy
  EndEnumeration

  Procedure EventInstance()
    Repeat
      For i=0 To oMax
        If Object(i) <> 0
          CallFunctionFast(Object(i),i,#Step)
        EndIf
      Next
      Delay(16)
    ForEver
  EndProcedure
  
  Procedure CreateInstance(CodeAddress.l)
    For i=0 To oMax
      If Object(i)=0
        Object(i)=CodeAddress.l
        CallFunctionFast(Object(i),i,#Create)
        ProcedureReturn i
      EndIf
    Next
    ProcedureReturn -1
  EndProcedure
  
  Procedure DestroyInstance(InstanceId)
    For i=0 To oMax
      If Object(i) = InstanceId
        CallFunctionFast(Object(i),#Destroy)
        Object(i) = 0
        ProcedureReturn 1
      EndIf
    Next
    ProcedureReturn
  EndProcedure
Structure with example

Code: Select all

  ;Created by <David.C#S4701L> 2007
  ;If you wish to redistribute please ask first! :)
  ;Official Topic: http://www.purebasic.fr/english/viewtopic.php?t=28818
  
  Global oMax = 5 ;Your maximum amount of objects
  Global Dim Object(oMax)
  
  Enumeration
    #Create
    #Step
    #Destroy
  EndEnumeration
  
  Procedure Test(InstanceId.l,Type.l=0)
    Select Type
      Case #Create       ;Run straight after the object is made
        MessageRequester("Hi","Hello there.")
      Case #Step          ;Run every "Step"
      	Debug "Object: Test - made event Step."
      Case #Destroy      ;Run when we want to remove the object
      	MessageRequester("MAYDAY!","I'm going down! I'm going down!!")
    EndSelect
  EndProcedure
  
  Procedure EventInstance()
    Repeat
      For i=0 To oMax
        If Object(i) <> 0
          CallFunctionFast(Object(i),i,#Step)
        EndIf
      Next
      Delay(16)
    ForEver
  EndProcedure
  
  Procedure CreateInstance(CodeAddress.l)
    For i=0 To oMax
      If Object(i)=0
        Object(i)=CodeAddress.l
        CallFunctionFast(Object(i),i,#Create)
        ProcedureReturn i
      EndIf
    Next
    ProcedureReturn -1
  EndProcedure
  
  Procedure DestroyInstance(InstanceId)
    For i=0 To oMax
      If Object(i) = InstanceId
        CallFunctionFast(Object(i),#Destroy)
        Object(i) = 0
        ProcedureReturn 1
      EndIf
    Next
    ProcedureReturn
  EndProcedure
  
  Object1 = CreateInstance(@Test())
  ObjectThreadID = CreateThread(@EventInstance(),"")

  Repeat
  	Delay(16)
  ForEver
Hope i could help with what little skill i have, Enjoy! :)
David C
Ollivier
Enthusiast
Enthusiast
Posts: 281
Joined: Mon Jul 23, 2007 8:30 pm
Location: FR

Post by Ollivier »

Thanks! I copy it into the french forum. (Name of this old topic : «OOC : advantages or disadvantages»)
Last edited by Ollivier on Tue Sep 25, 2007 6:40 pm, edited 1 time in total.
Ollivier
Enthusiast
Enthusiast
Posts: 281
Joined: Mon Jul 23, 2007 8:30 pm
Location: FR

Post by Ollivier »

@C#

Is the order of the execution of each proc («object») important?
<David.C#S4701L>
New User
New User
Posts: 4
Joined: Thu Sep 20, 2007 1:37 pm
Location: On your computer screen!

Post by <David.C#S4701L> »

Ollivier wrote:Thanks! I copy it into the french forum. (Name of this old topic : «OOC : advantages or disadvantages»)
Thankyou.
@C#

Is the order of the execution of each proc («object») important?
No, it is not. By default the objects are executed from 0 to X. (X being the total of obects)
Ollivier
Enthusiast
Enthusiast
Posts: 281
Joined: Mon Jul 23, 2007 8:30 pm
Location: FR

Post by Ollivier »

I ask this question to change the management of the task in order to reduce EventInstance duration, but increase destroying duration. I'll do and post such a code.
Ollivier
Enthusiast
Enthusiast
Posts: 281
Joined: Mon Jul 23, 2007 8:30 pm
Location: FR

Post by Ollivier »

No, it is not. By default the objects are executed from 0 to X. (X being the total of obects)
>> Yes, it's this idea I thank

Ignoring OO, I'll try to build a code (displaying app) with this skeleton in order to discover this concept.

If you have other details, there isn't problems! As I like structured buffers to the inputs\outputs procedure communication, I'll change it again.

Thank for this beautiful gate to the OOC!

Code: Select all

;Created by <David.C#S4701L> 2007 
  ;If you wish to redistribute please ask first! :) 
  ;Official Topic: http://www.purebasic.fr/english/viewtopic.php?t=28818 
  
  Global oMax = 5 ;Your maximum amount of objects 
  Global Dim Object(oMax) 
  Global NewInstancePointor.L
  
  Enumeration 
    #Create 
    #Step 
    #Destroy 
  EndEnumeration 


 
  Procedure Test(InstanceId.l,Type.l=0) 
    Select Type 
      Case #Create       ;Run straight after the object is made 
        MessageRequester("Hi","Hello there.") 
      Case #Step          ;Run every "Step" 
         Debug "Object: Test - made event Step." 
      Case #Destroy      ;Run when we want to remove the object 
         MessageRequester("MAYDAY!","I'm going down! I'm going down!!") 
    EndSelect 
  EndProcedure 

;____________________________________________________________________________ 
;¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯ 
;  CREATION
;
;  Output : -1 : Caution ! It's the ultimate instance you can create !
;  ¯¯¯¯¯¯   Other : InstanceId
;____________________________________________________________________________ 
;¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯   
Procedure CreateInstance(CodeAddress.l) 

      i = NewInstancePointor
      NewInstancePointor + 1
      If i = oMax: i = -1: EndIf
      Object(i)=CodeAddress.l 
      CallFunctionFast(Object(i),i,#Create) 
      ProcedureReturn i 
      
EndProcedure 
  
;____________________________________________________________________________ 
;¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯ 
;  EVENT
;____________________________________________________________________________ 
;¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯   
  Procedure EventInstance() 
  
    Repeat 
      For i=0 To NewInstancePointor - 1
          CallFunctionFast(Object(i),i,#Step) 
      Next 
      Delay(5) 
    ForEver 
    
  EndProcedure 
  
;____________________________________________________________________________ 
;¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯ 
;  DESTRUCTION
;____________________________________________________________________________ 
;¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯   
  Procedure DestroyInstance(InstanceId) 
  
    For i=0 To oMax 
      If Object(i) = InstanceId 
        CallFunctionFast(Object(i),#Destroy) 
        NewInstancePointor - 1
        Object(i) = Object(NewInstancePointor)
        ProcedureReturn 1 
      EndIf 
    Next 
    ProcedureReturn 
    
  EndProcedure 
  
  Object1 = CreateInstance(@Test()) 
  ObjectThreadID = CreateThread(@EventInstance(),"") 

  Repeat 
     Delay(16) 
  ForEver 
byo
Enthusiast
Enthusiast
Posts: 635
Joined: Mon Apr 02, 2007 1:43 am
Location: Brazil

Post by byo »

Ollivier wrote:

Code: Select all

Repeat 
     Delay(16)
ForEver 
:?:
Ollivier
Enthusiast
Enthusiast
Posts: 281
Joined: Mon Jul 23, 2007 8:30 pm
Location: FR

Post by Ollivier »

@byo

You forget the line above the loop

Code: Select all

  ObjectThreadID = CreateThread(@EventInstance(),"") 

  Repeat 
     Delay(16) 
  ForEver 
 
It's near equal

Code: Select all

  Repeat 
     Delay(16) 
     CallFunctionFast(@EventInstance() )
  ForEver 
 
I wrote near equal :wink:
byo
Enthusiast
Enthusiast
Posts: 635
Joined: Mon Apr 02, 2007 1:43 am
Location: Brazil

Post by byo »

I see. :D

It's just strange to see that. :lol:
Ollivier
Enthusiast
Enthusiast
Posts: 281
Joined: Mon Jul 23, 2007 8:30 pm
Location: FR

Post by Ollivier »

Yes, I think David.C# had a good idea posting this thread.
Ollivier
Enthusiast
Enthusiast
Posts: 281
Joined: Mon Jul 23, 2007 8:30 pm
Location: FR

Post by Ollivier »

If it's too strange for you, you can first replace this loop

Code: Select all

Repeat
    Delay(16)
ForEver
by this line

Code: Select all

WaitThread(ObjectThreadID)
byo
Enthusiast
Enthusiast
Posts: 635
Joined: Mon Apr 02, 2007 1:43 am
Location: Brazil

Post by byo »

Cool.

With the first method, how do I end that loop after the thread is finished?

I know that WaitThread pauses while the thread is executing and then returns after the thread is finished. How do the thread end that Repeat...Forever loop?
Ollivier
Enthusiast
Enthusiast
Posts: 281
Joined: Mon Jul 23, 2007 8:30 pm
Location: FR

Post by Ollivier »

Yes, I correct the 2, 3 errors in order to start, step and stop the thread.

Code: Select all

  ;Created by <David.C#S4701L> 2007 
  ;If you wish to redistribute please ask first! :) 
  ;Official Topic: http://www.purebasic.fr/english/viewtopic.php?t=28818 
  
      Global oMax = 5 ;Your maximum amount of objects 
      Global Dim Object(oMax) 
  
      Declare DestroyInstance(InstanceId) 
  
      Enumeration 
            #Create 
            #Step 
            #Destroy 
      EndEnumeration 



Procedure Test(InstanceId.L, Type.L) 

      Select Type 

            Case #Create       ;Run straight after the object is made 
                  MessageRequester("Hi","Hello there.") 

            Case #Step          ;Run every "Step" 
                  If MessageRequester("Message", "Looping..." + Chr(10) + "Do you want to stay?", 4) = 7 
                        DestroyInstance(InstanceId) 
                  EndIf 

            Case #Destroy      ;Run when we want to remove the object 
                  MessageRequester("MAYDAY!","I'm going down! I'm going down!!") 

      EndSelect 

EndProcedure 


  
Procedure CreateInstance(CodeAddress.L) 

      For i = 0 To oMax 
      
            If Object(i) = 0 
            
                  Object(i) = CodeAddress
                  CallFunctionFast(CodeAddress, CodeAddress, #Create) 
                  ProcedureReturn i 
                  
            EndIf
             
      Next 
      
      ProcedureReturn -1 

EndProcedure 
  


Procedure EventInstance() 

      Protected Nothing.L 
      Protected CodeAddress.L

      Repeat 

            Nothing.L = 1 
            
            For i = 0 To oMax
            
                  CodeAddress = Object(i)
                  
                  If CodeAddress <> 0 
                  
                        CallFunctionFast(CodeAddress, CodeAddress, #Step) 
                        Nothing = 0 
                        
                  EndIf
                   
            Next 
            
            Delay(16) 

      Until Nothing 

EndProcedure 
  


Procedure DestroyInstance(CodeAddress.L) 
  
      For i = 0 To oMax
            If Object(i) = CodeAddress
                  CallFunctionFast(CodeAddress, CodeAddress, #Destroy)
                  Object(i) = 0
            EndIf
      Next            
  
EndProcedure 
  
      Object1 = CreateInstance(@Test()) 
      ObjectThreadID = CreateThread(@EventInstance(),"") 

      WaitThread(ObjectThreadID) 
byo
Enthusiast
Enthusiast
Posts: 635
Joined: Mon Apr 02, 2007 1:43 am
Location: Brazil

Post by byo »

Very nice. I'll try it later when I'm home.
Thanks, Ollivier and David.C#S4701L.
<David.C#S4701L>
New User
New User
Posts: 4
Joined: Thu Sep 20, 2007 1:37 pm
Location: On your computer screen!

Post by <David.C#S4701L> »

byo wrote:Very nice. I'll try it later when I'm home.
Thanks, Ollivier and David.C#S4701L.
No problem, just call me David. :)
Post Reply