PureBasic Forum
http://forums.purebasic.com/english/

Hook - OOP - Modular
http://forums.purebasic.com/english/viewtopic.php?f=12&t=72623
Page 1 of 1

Author:  StarBootics [ Thu Apr 11, 2019 3:57 am ]
Post subject:  Hook - OOP - Modular

Hello everyone,

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

Image
https://www.dropbox.com/s/twybxo8435wn2a6/Polymoph%20List.png?dl=0

A linked list of hooks to hang different elements so this is the hook code to do that :
Code:
; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
; Project name : Hook
; File Name : Hook - OOP.pb
; File version: 1.0.0
; Programming : OK
; Programmed by : StarBootics
; Date : 10-04-2019
; Last Update : 10-04-2019
; 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)

    ClearStructure(*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

Author:  StarBootics [ Mon Apr 22, 2019 2:41 pm ]
Post subject:  Re: Hook - OOP

Hello everyone,

The exact same thing but using a Modular approach :
Code:
; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
; 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

Page 1 of 1 All times are UTC + 1 hour
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
http://www.phpbb.com/