DCVARGROUP

This forum is for ideas and or code to be contributed for general use.
Post Reply
Message
Author
c-tec
Posts: 379
Joined: Tue Apr 20, 2010 1:36 am
Location: SALZBURG/AUSTRIA
Contact:

DCVARGROUP

#1 Post by c-tec »

Hello,
DCVARGROUP is very helpful for me, but get a problem calling the function twice, the vars are protected. So I save the classes in a static array, and destroy them when the thread is closed.
Now you forget classdestroy() without problems and it is also threadsave. Maybe someone can use it.
regards
Rudolf

Code: Select all

#INCLUDE "class.CH"

FUNCTION DC_VarGroup( cName, aVars )
******************************************************************
static aClass
LOCAL oClass, nAttr, aIvar, xRet, oVars, oVarsInstance, i, ;
      aVars2 := {},cPName,aThreads := threadinfo(3),lFound := .f.,x,y,nDel := 0,nSysHndl := -1
default aClass to {}
FOR i := 1 TO Len(aVars)
  AAdd(aVars2,aVars[i,1])
NEXT
DEFAULT cName := '1'
cPName := cName
cName := 'VARGROUP_' + str(threadid(),0) + "_" + cName
for x := 1 to len(aThreads)
     if aThreads[x,1] = threadid()
          nSysHndl := aThreads[x,2]
          exit
     endif
next x
for x := 1 to len(aClass) // delete classes in closed threads
     lFound := .f.
     for y := 1 to len(aThreads)
          if  aClass[x,1] = aThreads[y,1] .and. aClass[x,2] = aThreads[y,2]
               lFound := .t. // thread still active
               exit
          endif
     next y
     if !lFound
          classdestroy(aClass[x,3])
          nDel++
     endif
next x
asize(aClass,len(aClass)-nDel)
oClass := ClassObject( cName )
IF oClass <> NIL
   ClassDestroy(oClass)
  //RETURN oClass                 // Class already exists
ENDIF
nAttr   := CLASS_EXPORTED + VAR_INSTANCE
aIVar   := AEval( aVars2, {|a| a := {a, nAttr} } ,,, .T.)
oVars := ClassCreate( cName,, aIVar )
oVarsInstance := oVars:new()
FOR i := 1 TO Len(aVars)
  oVarsInstance:&(aVars[i,1]) := aVars[i,2]
NEXT
aadd(aClass,{threadid(),nSysHndl,cPName,cName,.f.})
RETURN oVarsInstance
Rudolf Reinthaler
digital pen & paper systems
http://www.formcommander.net

User avatar
rdonnay
Site Admin
Posts: 4722
Joined: Wed Jan 27, 2010 6:58 pm
Location: Boise, Idaho USA
Contact:

Re: DCVARGROUP

#2 Post by rdonnay »

Rudolf -

You are correct. That is a much better implementation of DC_VarGroup().

Thank you.

I want to study this concept a bit more before adding it to the next release.

Are you using the DCVARGROUP command in your application, or the DC_VarGroup() function?

Roger
The eXpress train is coming - and it has more cars.

c-tec
Posts: 379
Joined: Tue Apr 20, 2010 1:36 am
Location: SALZBURG/AUSTRIA
Contact:

Re: DCVARGROUP

#3 Post by c-tec »

Hello Roger,
thank you, I user DCVARGROUP and have now added this function in my Lib so that I must not change your sourcecode.
regards
Rudolf
Rudolf Reinthaler
digital pen & paper systems
http://www.formcommander.net

Post Reply