* -----------------------------------------------------------------------------
*
TPublic() * Clase para el reemplazo de Variables Publicas
* Version
2.2b -
01/
04/
2003 *
* Andrade A. Daniel
* Rosario - Santa Fe - Argentina
*
* andrade_2knews@hotmail.com
* http:
//www.dbwide.com.ar *
* Aportes:
[ER
] Eduardo Rizzolo
*
[WA
] Wilson Alves -
wolverine@sercomtel.com.br 18/
05/
2002 *
[JJMG
] Juan J. Mendez Gonzalez
*
* DATAS
* -----------------------------------------------------------------------------
* aVars - Arreglo de variables
* cName - Nombre ultima variable accedida
* nPos - Valor ultimo variable accedida
* lAutomatic - Asignación automatica, por defecto
TRUE [WA
] *
* METODOS
* -----------------------------------------------------------------------------
*
New() - Contructor
* Add
() - Agrega/
define nueva variable
* Del
() - Borra variable
*
Get() - Accede a una veriable directamente
* Set
() -
Define nuevo valor directamente
* GetPos
() - Obtener la posición en el array
*
Release() - Borra todas las variables
* IsDef
() - Chequea si una variable fue definida
* Clone
() - Clona la
DATA aVars
* nCount
() - Devuelve cantidad de variables definidas
* Save
() - Salva
DATA aVars
* Restore
() - Restaura
DATA aVars
*
* NOTA
* -----------------------------------------------------------------------------
* Para acceder al valor de una variable, se puede hacer de
2 formas,
* una directa usando oPub:
Get("Codigo") o por Prueba/Error oPub:
Codigo,
* este último es mas simple de usar pero más lento.
*
* Para definir un nuevo valor a una variable tambien puede ser por
2 formas,
* directamente por oPub:
Set("Codigo",
"ABC" ), o por Prueba/Error
* oPub:
Codigo :=
"ABC".
*
* Atencion:
Los metodos
Get() y Set
() no controlan si la variable existe,
* para ganar en velocidad.
*
* Las variables definidas NO son
case sensitive.
*
* MODIFICACIONES Y AGREGADOS
* -----------------------------------------------------------------------------
*
2.2b Correción Bug en metodo Add
() sobre FWH.
*
2.2a Modificado para não restringir o número de variáveis em
[x
]Harbour
[WA
] *
2.2 Modificada para funcionar en
[x
]Harbour
*
*
2.1 Se guarda el Nombre y Posición de la última variable accedida para incrementar
* la velocidad.
(Implementado por Eduardo Rizzolo
) *
* EJEMPLO
* -----------------------------------------------------------------------------
*
FUNCTION Test
() *
local oP :=
TPublic():
New(), aSave, nPos
*
* oP:
Add("Codigo") // Defino variable sin valor inicial * oP:
Add("Precio",
1.15) // Defino variable con valor inicial * oP:
Add("Cantidad",
10 ) * oP:
Add("TOTAL" ) *
*
// Acceso a variables por prueba/error * oP:
Total := oP:
Precio * oP:
Cantidad *
*
// Definicion y Acceso a variables directamente * oP:
Set("Total", oP:
Get("precio") * oP:
Get("CANTIDAD") ) *
* oP:
Del("Total") // Borro una variable * ? oP:
IsDef("TOTAL") // Varifico si existe una variable *
* nPos := oP:
GetPos("Total") // Obtengo la posición en el array * oP:
aVars[nPos,
2] :=
0 // Modifico el Valor en el array directo *
* aSave := oP:
Save() // Guardo las Variables * oP:
Release() // Borro TODAS las variables * oP:
Restore( aSave
) // Restauro las variables *
* oP:
End() // Termino *
*
RETURN NIL *
* EXEMPLO
(Asignación Automática
) *
*
FUNCTION MAIN
() *
LOCAL oP:=
TPublic():
New(.T.
) *
* op:
nome :=
"Wilson Alves" * op:
Endereco :=
"Rua dos Cravos,75" * op:
Cidade :=
"Londrina-PR" * op:
Celular :=
"9112-5495" * op:
Empresa :=
"WCW Software" *
* ? op:
Nome,op:
Endereco,op:
Cidade,op:
celular,op:
empresa *
* op:
End() *
RETURN NIL *
*
# include
"FiveWin.ch"#ifdef __HARBOUR__
# xtranslate BYNAME <V>
[, <VN>
] => ::<V> := <V>
[; ::<VN> := <VN>
] # xtranslate BYNAME <V>
DEFAULT <Val> => ::<V> := BYDEFAULT <V>, <Val>
# xtranslate BYNAME <V> IFNONIL =>
if <V> !=
NIL ; ::<V> := <V> ;
endif # xtranslate BYDEFAULT <V>, <Val> =>
if( <V> ==
NIL, <Val>, <V>
)#endif
*
*
TPublic()*
CLASS TPublic DATA lAutomatic AS LOGICAL
INIT .T.
// [WA] DATA aVars AS ARRAY
INIT NIL DATA nPos AS NUMERIC
INIT 0 READONLY
// [ER] DATA cName AS CHARACTER
INIT "" READONLY
// [ER] METHOD New( lAutomatic
) METHOD End
() INLINE ::
Release() METHOD Add
( cName, xValue
) METHOD Del
( cName
) METHOD Get( cName
) METHOD Set
( cName, xValue
) METHOD GetPos
( cName
) METHOD Release() METHOD IsDef
( cName
) METHOD Clone
() INLINE aClone
( ::
aClone ) METHOD nCount
() INLINE Len
( ::
aVars ) METHOD Save
() INLINE aClone
( ::
aVars ) METHOD Restore
( aVars
) INLINE ::
aVars := aClone
( aVars
)#ifdef __HARBOUR__
ERROR HANDLER OnError
( uParam1
)#else
ERROR HANDLER OnError
( cMsg, nError
)#endif
ENDCLASS*
*
TPublic:
New()*
METHOD New( lAutomatic
) CLASS TPublic // [WA] ::
aVars :=
{} BYNAME lAutomatic
DEFAULT .T.
// [WA]RETURN Self*
*
TPublic:
Add()*
METHOD Add
( cName, xValue
) CLASS TPublic // [ER] if cName !=
NIL if (::
nPos := aScan
( ::
aVars,
{ |e,n| e
[1] == AllTrim
(Upper
(cName
)) } )) !=
0 ::
aVars[::
nPos,
2] := xValue
#ifndef __HARBOUR__
// [WA] elseif Len
(::
aVars) <
4000 // [JJMG] aAdd
( ::
aVars,
{ AllTrim
(Upper
(cName
)), xValue
} ) ::
nPos := Len
(::
aVars) else MsgAlert
("Demasiadas variables definidas para la Clase TPublic()")#else
else aAdd
( ::
aVars,
{ AllTrim
(Upper
(cName
)), xValue
} ) ::
nPos := Len
(::
aVars)#endif
// [WA] endif ::
cName := cName
endifRETURN Self*
*
TPublic:
Del()*
METHOD Del
( cName
) CLASS TPublic local nPos
if cName !=
NIL if (nPos := aScan
( ::
aVars,
{ |e,n| e
[1] == AllTrim
(Upper
(cName
)) } )) !=
0 aDel
( ::
aVars, nPos
) ::
aVars := aSize
( ::
aVars, Len
(::
aVars) -
1 ) ::
nPos :=
0 ::
cName :=
"" endif endifRETURN Self*
*
TPublic:
Get()*
METHOD Get( cName
) CLASS TPublic // [by ER] if cName != ::
cName ::
nPos := aScan
( ::
aVars,
{ |e,n| e
[1] == AllTrim
(Upper
(cName
)) } ) ::
cName := cName
endifRETURN ::
aVars[::
nPos,
2]*
*
TPublic:
Set()*
METHOD Set
( cName, xValue
) CLASS TPublic // [by ER] if cName != ::
cName ::
nPos := aScan
( ::
aVars,
{ |e,n| e
[1] == AllTrim
(Upper
(cName
)) } ) ::
cName := cName
endif ::
aVars[::
nPos,
2] := xValue
RETURN Self*
*
TPublic:
GetPos() // [by ER]*
METHOD GetPos
( cName
) CLASS TPublic ::
cName := cName
RETURN ::
nPos := aScan
( ::
aVars,
{ |e,n| e
[1] == AllTrim
(Upper
(cName
)) } )*
*
TPublic:
Release()*
METHOD Release() CLASS TPublic ASIZE
(::
aVars,
0) ::
cName :=
"" ::
nPos :=
0RETURN Self*
*
TPublic:
IsDef()*
METHOD IsDef
( cName
) CLASS TPublic // [by ER] local lOk := .F.
if cName !=
NIL if (::
nPos := aScan
( ::
aVars,
{ |e,n| e
[1] == AllTrim
(Upper
(cName
)) } )) !=
0 ::
cName := cName
lOk := .T.
endif endifRETURN lOk
*
* OnError
()*
#ifdef __HARBOUR__
METHOD OnError
( uParam1
) CLASS TPublic local cMsg := __GetMessage
() local nError :=
If( SubStr( cMsg,
1,
1 ) ==
"_",
1005,
1004 )#else
METHOD OnError
( cMsg, nError
) CLASS TPublic local uParam1 := GetParam
( 1,
1 )#endif
cMsg := Upper
( AllTrim
( cMsg
)) if SubStr( cMsg,
1,
1 ) ==
"_" cMsg :=
SubStr( cMsg,
2 ) if cMsg == Upper
(::
cName) ::
aVars[::
nPos,
2] := uParam1
elseif
( ::
nPos := aScan
( ::
aVars,
{ |e,n| e
[1] == cMsg
} ) ) !=
0 ::
cName := cMsg
::
aVars[::
nPos,
2] := uParam1
else if !::
lAutomatic // [WA] _ClsSetError
( _GenError
( nError, ::
ClassName(), cMsg
) ) ::
cName :=
"" ::
nPos :=
0 else ::
add(cmsg
) ::
aVars[::
nPos,
2] := uParam1
endif endif else if cMsg == Upper
(::
cName) // [by ER] RETURN ::
aVars[::
nPos,
2] elseif
( ::
nPos := aScan
( ::
aVars,
{ |e,n| e
[1] == cMsg
} ) ) !=
0 ::
cName := cMsg
RETURN ::
aVars[::
nPos,
2] else _ClsSetError
( _GenError
( nError, ::
ClassName(), cMsg
) ) ::
cName :=
"" ::
nPos :=
0 endif endifRETURN NIL// Andrade Daniel (2001-2003)