Structures, memory and interfaces

Just starting out? Need help? Post your questions and find answers here.
jimcorr
New User
New User
Posts: 6
Joined: Sun Dec 14, 2014 11:38 pm

Structures, memory and interfaces

Post by jimcorr »

Hi Guys,

I'm trying to achieve some kind of structure where i can access variables and methods at the same time. I got something similar using structure and prototype but i have to pass the pointer as a parameter on every method.

I read some messages explaining how to obtain the frame pointer thru asm, but you need to call the macro on every method, and i didn't like it too.

I came up with this mix, is it safe? I'm pretty new on working with purebasic and pointers, so i could be making some huge mistakes. The code works but i'm not sure about its safety.

Code: Select all

DeclareModule mcurrency
  
  Structure st_base
    *ptr_methods
    text.s
    value.i
  EndStructure   
  
  Interface in_methods
    mshow_text()
    madd(vi.i)
  EndInterface
  
  Structure st_currency Extends mcurrency::st_base
    methods.mcurrency::in_methods
  EndStructure
  
  Declare.i new(*ptr.st_currency)
  Declare destroy(*ptr.st_currency)
EndDeclareModule
Module mcurrency
  
  Procedure madd(*pst_currency.st_currency, vi.i)
    
    *pst_currency\value = *pst_currency\value + vi
    
  EndProcedure
  Procedure mshow_text(*pst_currency.st_currency)
    
    Debug "text: " + *pst_currency\text
    
  EndProcedure
  
  Procedure add_method(*pst_currency.st_currency, *ptr_adicionar)
    
    Define vi_size.i
    Define vi_size_add.i = SizeOf(*ptr_adicionar)
    If *pst_currency\ptr_methods > 0
        vi_size = MemorySize(*pst_currency\ptr_methods)
      Else
      vi_size = 0
    EndIf
    
    *pst_currency\ptr_methods = ReAllocateMemory(*pst_currency\ptr_methods, vi_size + vi_size_add, #PB_Memory_NoClear)
    PokeI(*pst_currency\ptr_methods + vi_size, *ptr_adicionar)
    
  EndProcedure
  
  
  Procedure.i new(*pst_currency.st_currency)
    Define *vst_currency.st_currency = AllocateStructure(st_currency)
    *pst_currency = *vst_currency
    *pst_currency\methods = *vst_currency
    
    add_method(*vst_currency, @mshow_text())
    add_method(*vst_currency, @madd())
    
    ProcedureReturn *vst_currency
  EndProcedure
  
  Procedure destroy(*pst_currency.st_currency)
    ;Debug "destroying"
    FreeMemory(*pst_currency\ptr_methods)
    FreeStructure(*pst_currency)
    ProcedureReturn 0
  EndProcedure
  
EndModule


Define *vcurrency.mcurrency::st_currency
*vcurrency = mcurrency::new(*vcurrency)


*vcurrency\text = "this text will be displayed"
*vcurrency\methods\mshow_text()

*vcurrency\value = 1
*vcurrency\methods\madd(2)

Debug "it should be 3: " + *vcurrency\value

*vcurrency = mcurrency::destroy(*vcurrency)
User avatar
mk-soft
Always Here
Always Here
Posts: 6313
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: Scructures, memory and interfaces

Post by mk-soft »

The transfer of the pointer at new makes none are with you, because this is zero (ByVal) and is not used internally, is overwritten.

Little simpler ...

Code: Select all

DeclareModule mcurrency
  
  Interface iObject
    destroy()
    show_text()
    add(vi.i)
  EndInterface
  
  Structure sObject
    *vTable
    text.s
    value.i
    methods.iObject
  EndStructure   
  
  Declare.i new()
  
EndDeclareModule

Module mcurrency
  
  Procedure add(*this.sObject, vi.i)
    
    *this\value = *this\value + vi
    
  EndProcedure
  
  Procedure show_text(*this.sObject)
    
    Debug "text: " + *this\text
    
  EndProcedure
  
  Procedure destroy(*this.sObject)
    Debug "destroy"
    FreeStructure(*this)
    ProcedureReturn 0
  EndProcedure
  
  Procedure.i new()
    Protected *object.sObject = AllocateStructure(sObject)
    If *object
      *object\vTable = ?vTable
      *object\methods = *object
    EndIf
    ProcedureReturn *object
  EndProcedure
  
  DataSection
    vTable:
    Data.i @destroy()
    Data.i @show_text()
    Data.i @add()
  EndDataSection
    
EndModule


Define *vcurrency.mcurrency::sObject
*vcurrency = mcurrency::new()


*vcurrency\text = "this text will be displayed"
*vcurrency\methods\show_text()

*vcurrency\value = 1
*vcurrency\methods\add(2)

Debug "it should be 3: " + *vcurrency\value

*vcurrency\methods\destroy()
*vcurrency = 0
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
Post Reply