Skip to content

Instantly share code, notes, and snippets.

@fushnisoft
Last active December 25, 2015 21:18
Show Gist options
  • Select an option

  • Save fushnisoft/7041137 to your computer and use it in GitHub Desktop.

Select an option

Save fushnisoft/7041137 to your computer and use it in GitHub Desktop.
My fugly ref handler. I wanted to be able to have dynamic length strings stored in a clarion table (IMDD in particular). This is intended for a very specific situation and does not attempt to deal with threading, multiple tables, or any other fancy things but it appears to work well enough for my needs in this case! my_CStringClass could be anyt…

Examples of usage

Inserting a new record

  Clear(PRE:Record)
  PRE:ID = 1
  RefHandler.Init(PRE:LongRef)
  RefHandler.CS.Str('Put some text into it')
  Access:MyTable.Insert()

Update a record

  PRE:ID = 1
  Access:MyTable.Fetch(PRE:PK_ID)
  RefHandler.Init(PRE:LongRef)
  RefHandler.CS.Cat(' - New Text') ! -- Concatination method in string class
  Access:MyTable.Update()

Using the value somewhere

  PRE:ID = 1
  Access:MyTable.Fetch(PRE:PK_ID)
  RefHandler.Init(PRE:LongRef)
  Message('PRE:LongRef CString class contains: ' & RefHandler.CS.Str())

Don't forget to cleanup the references before closing the table!

  Set(PRE:PK_ID)
  LOOP UNTIL Access:MyTable.Next()
    RefHandler.DisposeRef(PRE:LongRef)
    Access:MyTable.DeleteRecord(FALSE)
  END
MEMBER()
INCLUDE('RefHandler.inc'),ONCE
MAP
END
RefHandler.Init PROCEDURE(*LONG pTheField)
CODE
IF pTheField = 0
! Create a new instance and assign the address to the storage field
SELF.CS &= New(my_CStringClass)
pTheField = Address(SELF.CS)
ELSE
! Reload the existing instance
SELF.CS &= pTheField + 0
END
RefHandler.DisposeRef PROCEDURE(*LONG pTheField)
TempCS &my_CStringClass
CODE
! NOTE: we are not using SELF.CS here.
! I guess it is possible that you could be still using
! that but disposing a different ref at the same time so we use a temp variable
IF pTheField <> 0
TempCS &= pTheField + 0
IF NOT TempCS &= NULL
Dispose(TempCS)
END
END
RefHandler CLASS(),Type,Module('RefHandler.Clw'),LINK('RefHandler.Clw',1)
CS &my_CStringClass
Init PROCEDURE(*LONG pTheField)
DisposeRef PROCEDURE(*LONG pTheField)
END
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment