Hook - OOP - Modular

Share your advanced PureBasic knowledge/code with the community.
User avatar
StarBootics
Addict
Addict
Posts: 984
Joined: Sun Jul 07, 2013 11:35 am
Location: Canada

Hook - OOP - Modular

Post by StarBootics »

Hello everyone,

This is something I need for a project, an image worth 1000 words :

Image

A linked list of hooks to hang different elements so this is the hook code to do that :

Code: Select all

; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
; Project name : Hook
; File Name : Hook - OOP.pb
; File version: 1.0.1
; Programming : OK
; Programmed by : StarBootics
; Date : 10-04-2019
; Last Update : 29-10-2020
; PureBasic code : V5.70 LTS
; Platform : Windows, Linux, MacOS X
; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

DeclareModule Hook

  Interface Hook

    GetTag.l()
    GetPointer.i()
    SetTag(P_Tag.l)
    SetPointer(P_Pointer.i)
    Free()

  EndInterface

  Declare.i New(P_Tag.l, P_Pointer.i)

EndDeclareModule

Module Hook

  Structure Private_Members

    VirtualTable.i
    Tag.l
    Pointer.i
  
  EndStructure

  Procedure.l GetTag(*This.Private_Members)

    ProcedureReturn *This\Tag
  EndProcedure

  Procedure.i GetPointer(*This.Private_Members)

    ProcedureReturn *This\Pointer
  EndProcedure

  Procedure SetTag(*This.Private_Members, P_Tag.l)

    *This\Tag = P_Tag

  EndProcedure

  Procedure SetPointer(*This.Private_Members, P_Pointer.i)

    *This\Pointer = P_Pointer

  EndProcedure

  Procedure Free(*This.Private_Members)

    FreeStructure(*This)

  EndProcedure

  Procedure.i New(P_Tag.l, P_Pointer.i)

    *This.Private_Members = AllocateStructure(Private_Members)
    *This\VirtualTable = ?START_METHODS

    SetTag(*This, P_Tag)
    SetPointer(*This, P_Pointer)
    
    ProcedureReturn *This
  EndProcedure

  DataSection
    START_METHODS:
    Data.i @GetTag()
    Data.i @GetPointer()
    Data.i @SetTag()
    Data.i @SetPointer()
    Data.i @Free()
    END_METHODS:
  EndDataSection

EndModule

CompilerIf #PB_Compiler_IsMainFile
  
  Enumeration 
    #HOOK_TAG_DODECAHEDRON
    #HOOK_TAG_SPHERE
    #HOOK_TAG_CUBE
    #HOOK_TAG_PYRAMID
  EndEnumeration
  
  Structure Dodecahedron
    
    Volume.d
    Size.d
    Whatever.s
    
  EndStructure
  
  Structure Sphere
    
    Volume.d
    Radius.d
    Whatever.s
    
  EndStructure
  
  Structure Cube
    
    Volume.d
    Size.d
    Whatever.s
    
  EndStructure
  
  Structure Pyramid
    
    Volume.d
    Size.d
    Whatever.s
    
  EndStructure
  
  NewList MyList.Hook::Hook()
  
  AddElement(MyList())
  MyList() = Hook::New(#HOOK_TAG_DODECAHEDRON, AllocateStructure(Dodecahedron))
  
  AddElement(MyList())
  MyList() = Hook::New(#HOOK_TAG_SPHERE, AllocateStructure(Sphere))
  
  AddElement(MyList())
  MyList() = Hook::New(#HOOK_TAG_CUBE, AllocateStructure(Cube))
  
  AddElement(MyList())
  MyList() = Hook::New(#HOOK_TAG_PYRAMID, AllocateStructure(Pyramid))
  
  ForEach MyList()
    
    Select MyList()\GetTag()
        
      Case #HOOK_TAG_DODECAHEDRON
        If MyList()\GetPointer() <> #Null
          Debug "Dodecahedron"
        EndIf

      Case #HOOK_TAG_SPHERE
        If MyList()\GetPointer() <> #Null
          Debug "Sphere"
        EndIf

      Case #HOOK_TAG_CUBE
        If MyList()\GetPointer() <> #Null
          Debug "Cube"
        EndIf
  
      Case #HOOK_TAG_PYRAMID
        If MyList()\GetPointer() <> #Null
          Debug "Pyramid"
        EndIf

    EndSelect
  
  Next
  
  ForEach Mylist()
    
    If MyList()\GetPointer() <> #Null
      FreeStructure(MyList()\GetPointer())
    EndIf
    
    Mylist()\Free()
    
  Next
  
  FreeList(MyList())
  
CompilerEndIf

; <<<<<<<<<<<<<<<<<<<<<<<
; <<<<< END OF FILE <<<<<
; <<<<<<<<<<<<<<<<<<<<<<<
Best regards
StarBootics
Last edited by StarBootics on Fri Oct 30, 2020 12:42 am, edited 2 times in total.
The Stone Age did not end due to a shortage of stones !
User avatar
StarBootics
Addict
Addict
Posts: 984
Joined: Sun Jul 07, 2013 11:35 am
Location: Canada

Re: Hook - OOP

Post by StarBootics »

Hello everyone,

The exact same thing but using a Modular approach :

Code: Select all

; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
; AUTOMATICALLY GENERATED CODE, DO NOT MODIFY
; UNLESS YOU REALLY, REALLY, REALLY MEAN IT !!
; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
; Code generated by : Dev-Module - V1.0.0 Beta
; Project name : Hook - Module
; File name : Hook - Module.pb
; File Version : 1.0.0
; Programmation : OK
; Programmed by : StarBootics
; Creation Date : 22-04-2019
; Last update : 22-04-2019
; Coded for PureBasic : V5.71 beta 1 LTS
; Platform : Windows, Linux, MacOS X
; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

DeclareModule Hook

  ; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
  ; <<<<< Structure declaration <<<<<

  Structure Hook

    Tag.l
    DataPtr.i

  EndStructure

  Declare.l GetTag(*HookA.Hook)
  Declare.i GetDataPtr(*HookA.Hook)
  Declare SetTag(*HookA.Hook, P_Tag.l)
  Declare SetDataPtr(*HookA.Hook, P_DataPtr.i)
  Declare Update(*HookA.Hook, P_Tag.l, P_DataPtr.i)
  Declare Reset(*HookA.Hook)

EndDeclareModule

Module Hook

  ; <<<<<<<<<<<<<<<<<<<<<<<<<<<
  ; <<<<< The observators <<<<<

  Procedure.l GetTag(*HookA.Hook)

    ProcedureReturn *HookA\Tag
  EndProcedure

  Procedure.i GetDataPtr(*HookA.Hook)

    ProcedureReturn *HookA\DataPtr
  EndProcedure

  ; <<<<<<<<<<<<<<<<<<<<<<<<
  ; <<<<< The mutators <<<<<

  Procedure SetTag(*HookA.Hook, P_Tag.l)

    *HookA\Tag = P_Tag

  EndProcedure

  Procedure SetDataPtr(*HookA.Hook, P_DataPtr.i)

    *HookA\DataPtr = P_DataPtr

  EndProcedure

  ; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
  ; <<<<< The Update operator <<<<<

  Procedure Update(*HookA.Hook, P_Tag.l, P_DataPtr.i)

    *HookA\Tag = P_Tag
    *HookA\DataPtr = P_DataPtr

  EndProcedure

  ; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
  ; <<<<< The Reset operator <<<<<

  Procedure Reset(*HookA.Hook)
    
    *HookA\Tag = 0
    *HookA\DataPtr = 0
    
  EndProcedure

EndModule

; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
; <<<<< Code generated in : 00.001 seconds (93000.00 lines/second) <<<<<
; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

CompilerIf #PB_Compiler_IsMainFile
 
  Enumeration 1
    #HOOK_TAG_DODECAHEDRON
    #HOOK_TAG_SPHERE
    #HOOK_TAG_CUBE
    #HOOK_TAG_PYRAMID
  EndEnumeration
 
  Structure Dodecahedron
   
    Volume.d
    Size.d
    Whatever.s
   
  EndStructure
 
  Structure Sphere
   
    Volume.d
    Radius.d
    Whatever.s
   
  EndStructure
 
  Structure Cube
   
    Volume.d
    Size.d
    Whatever.s
   
  EndStructure
 
  Structure Pyramid
   
    Volume.d
    Size.d
    Whatever.s
   
  EndStructure
 
  NewList MyList.Hook::Hook()
 
  AddElement(MyList())
  Hook::SetTag(MyList(), #HOOK_TAG_DODECAHEDRON)
  Hook::SetDataPtr(MyList(), AllocateStructure(Dodecahedron))
  
  AddElement(MyList())
  Hook::Update(MyList(), #HOOK_TAG_SPHERE, AllocateStructure(Sphere))
 
  AddElement(MyList())
  Hook::Update(MyList(), #HOOK_TAG_CUBE, AllocateStructure(Cube))

  AddElement(MyList())
  Hook::Update(MyList(), #HOOK_TAG_PYRAMID, AllocateStructure(Pyramid))

  ForEach MyList()
   
    Select Hook::GetTag(MyList())
       
      Case #HOOK_TAG_DODECAHEDRON
        If Hook::GetDataPtr(MyList()) <> #Null
          Debug "Dodecahedron"
        EndIf

      Case #HOOK_TAG_SPHERE
        If Hook::GetDataPtr(MyList()) <> #Null
          Debug "Sphere"
        EndIf

      Case #HOOK_TAG_CUBE
        If Hook::GetDataPtr(MyList()) <> #Null
          Debug "Cube"
        EndIf
 
      Case #HOOK_TAG_PYRAMID
        If Hook::GetDataPtr(MyList()) <> #Null
          Debug "Pyramid"
        EndIf

    EndSelect
 
  Next
 
  ForEach Mylist()
   
    If Hook::GetDataPtr(MyList()) <> #Null
      FreeStructure(Hook::GetDataPtr(MyList()))
    EndIf
   
  Next
 
  FreeList(MyList())
 
CompilerEndIf

; <<<<<<<<<<<<<<<<<<<<<<<
; <<<<< END OF FILE <<<<<
; <<<<<<<<<<<<<<<<<<<<<<<
Best regards
StarBootics
The Stone Age did not end due to a shortage of stones !
Post Reply