Os he dejado en la pagina del Curso de C para programadores de Harbour una clase hecha al 100x100 en C++ para ser usada desde Harbour.
Espero que la disfrutéis
//-----------------------------------------------------------------------------
// Clase THbArray
// include
//-----------------------------------------------------------------------------
#pragma once
#ifndef _THBARRAY_H
#define _THBARRAY_H
//-----------------------------------------------------------------------------
#include <hbapi.h>
#include <hbapiitm.h>
#include <hbapicls.h>
#include <hbstack.h>
//-----------------------------------------------------------------------------
const HB_SIZE _OBJC_POS = 1;
const HB_UINT _NUN_VARS = 1;
//-----------------------------------------------------------------------------
class THbArray
{
public:
//---------------------------------------------------------------------
// Propiedades
PHB_ITEM pSelf; // Objeto xBase: QUITAR?
PHB_ITEM FArray;
HB_SIZE FPos;
HB_BOOL bFirst;
HB_BOOL bLast;
//---------------------------------------------------------------------
// Getion interna (iran en una clase madre)
void *operator new( size_t );
void operator delete( void * );
static THbArray *getObjC( void );
static THbArray *getObjC( PHB_ITEM pSelf );
void create( PHB_ITEM );
void destroy( void );
HB_USHORT createClass( HB_USHORT, const char * ) const;
//---------------------------------------------------------------------
void addMethods( HB_USHORT ) const;
//---------------------------------------------------------------------
// Metodos de mantenimiento
HB_BOOL append( PHB_ITEM );
HB_BOOL insert( PHB_ITEM );
HB_BOOL update( PHB_ITEM );
HB_BOOL erase( void ); // Delete
//---------------------------------------------------------------------
// Metodos de movimiento
void goTo( HB_SIZE );
void moveBy( HB_ISIZ );
HB_SIZE getPos( void ) const;
HB_SIZE getSize( void ) const;
HB_ISIZ skipped( HB_ISIZ lSkip );
//---------------------------------------------------------------------
PHB_ITEM getValue( HB_SIZE nPos ) const;
PHB_ITEM current( void ) const;
};
//-----------------------------------------------------------------------------
// Declaración de funciones públicas.
HB_FUNC( THBARRAY ); // Funcion de clase xBase
//-----------------------------------------------------------------------------
#endif // Fin de _THBARRAY_H
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
// Clase THbArray
//-----------------------------------------------------------------------------
#include "thbarray.h"
#include "hbinit.h"
//-----------------------------------------------------------------------------
static void THBARRAY_NEW( void );
static void THBARRAY_APPEND( void );
static void THBARRAY_INSERT( void );
static void THBARRAY_UPDATE( void );
static void THBARRAY_DELETE( void );
static void THBARRAY_GOTO( void );
static void THBARRAY_GOFIRST( void );
static void THBARRAY_GOLAST( void );
static void THBARRAY_NEXT( void );
static void THBARRAY_PRIOR( void );
static void THBARRAY_MOVEBY( void );
static void THBARRAY_GETPOS( void );
static void THBARRAY_ISFIRST( void );
static void THBARRAY_ISLAST( void );
static void THBARRAY_GETSIZE( void );
static void THBARRAY_SKIPPER( void );
static void THBARRAY_GETVALUE( void );
static void THBARRAY_CURRENT( void );
static void THBARRAY_END( void );
//-----------------------------------------------------------------------------
// Metodos de la clase THbArray (cpp)
//-----------------------------------------------------------------------------
// Operador new
void *THbArray::operator new( size_t uiSize )
{
return hb_xgrab( uiSize );
}
// Operador delete
void THbArray::operator delete( void *pVoid )
{
hb_xfree( pVoid );
}
// Obtiene el objeto C++ tirando de la pila
THbArray *THbArray::getObjC( void )
{
return static_cast<THbArray *>( hb_arrayGetPtr( hb_stackSelfItem(), 1 ) );
}
// Pasando el objeto xBase
THbArray *THbArray::getObjC( PHB_ITEM pSelf )
{
return static_cast<THbArray *>( hb_arrayGetPtr( pSelf, 1 ) );
}
// Crea la clase Hb
HB_USHORT THbArray::createClass( HB_USHORT usSize, const char *szClassName ) const
{
HB_USHORT usClassH;
usClassH = hb_clsCreate( usSize, szClassName );
addMethods( usClassH );
return usClassH;
}
// Lista de Metodos xBase
void THbArray::addMethods( HB_USHORT usClassH ) const
{
hb_clsAdd( usClassH, "NEW", THBARRAY_NEW );
hb_clsAdd( usClassH, "APPEND", THBARRAY_APPEND );
hb_clsAdd( usClassH, "INSERT", THBARRAY_INSERT );
hb_clsAdd( usClassH, "UPDATE", THBARRAY_UPDATE );
hb_clsAdd( usClassH, "DELETE", THBARRAY_DELETE );
hb_clsAdd( usClassH, "GOTO", THBARRAY_GOTO );
hb_clsAdd( usClassH, "GOFIRST", THBARRAY_GOFIRST );
hb_clsAdd( usClassH, "GOLAST", THBARRAY_GOLAST );
hb_clsAdd( usClassH, "NEXT", THBARRAY_NEXT );
hb_clsAdd( usClassH, "PRIOR", THBARRAY_PRIOR );
hb_clsAdd( usClassH, "MOVEBY", THBARRAY_MOVEBY );
hb_clsAdd( usClassH, "GETPOS", THBARRAY_GETPOS );
hb_clsAdd( usClassH, "ISFIRST", THBARRAY_ISFIRST );
hb_clsAdd( usClassH, "ISLAST", THBARRAY_ISLAST );
hb_clsAdd( usClassH, "GETSIZE", THBARRAY_GETSIZE );
hb_clsAdd( usClassH, "SKIPPER", THBARRAY_SKIPPER );
hb_clsAdd( usClassH, "GETVALUE", THBARRAY_GETVALUE );
hb_clsAdd( usClassH, "CURRENT", THBARRAY_CURRENT );
hb_clsAdd( usClassH, "END", THBARRAY_END );
}
// Constructor. Inicializa las variables de instancia
void THbArray::create( PHB_ITEM pArray )
{
FArray = hb_itemNew( NULL );
if( pArray )
{
hb_arrayCloneTo( FArray, pArray );
}
else
{
hb_arrayNew( FArray, 0 );
}
FPos = getSize() > 0 ? 1 : 0;
bFirst = bLast = HB_FALSE;
}
// Destructor
void THbArray::destroy( void )
{
if( FArray != NULL )
{
hb_gcGripDrop( FArray );
}
if( pSelf != NULL )
{
hb_gcGripDrop( pSelf );
}
}
HB_BOOL THbArray::append( PHB_ITEM pItem )
{
HB_BOOL bRet = HB_FALSE;
if( pItem )
{
bRet = hb_arrayAdd( FArray, pItem );
}
return bRet;
}
HB_BOOL THbArray::insert( PHB_ITEM pItem )
{
HB_BOOL bRet = ( hb_arrayIns( FArray, FPos ) && pItem );
if( bRet )
{
bRet = hb_arraySet( FArray, FPos, pItem );
}
return bRet;
}
HB_BOOL THbArray::update( PHB_ITEM pItem )
{
return hb_arraySet( FArray, FPos, pItem );
}
HB_BOOL THbArray::erase( void )
{
HB_BOOL bRet = hb_arrayDel( FArray, FPos );
if( bRet )
{
bRet = hb_arraySize( FArray, getSize() - 1 );
}
return bRet;
}
void THbArray::goTo( HB_SIZE nPos )
{
HB_SIZE nSize;
nSize = getSize();
bFirst = bLast = HB_FALSE;
if( nPos > nSize )
{
FPos = nSize;
bLast = HB_TRUE;
}
else if( nPos < 1 )
{
FPos = 1;
bFirst = HB_TRUE;
}
else
{
FPos = nPos;
}
}
void THbArray::moveBy( HB_ISIZ lPos )
{
goTo( FPos + lPos );
}
HB_SIZE THbArray::getPos( void ) const
{
return FPos;
}
PHB_ITEM THbArray::getValue( HB_SIZE nPos ) const
{
return hb_arrayGetItemPtr( FArray, nPos );
}
PHB_ITEM THbArray::current( void ) const
{
return hb_arrayGetItemPtr( FArray, FPos );
}
HB_SIZE THbArray::getSize( void ) const
{
return hb_arrayLen( FArray );
}
HB_ISIZ THbArray::skipped( HB_ISIZ lSkip )
{
HB_ISIZ nPosOld = FPos;
moveBy( lSkip );
return FPos - nPosOld;
}
//-----------------------------------------------------------------------------
// Funciones de soporte a los metodos THbArray (hb)
//-----------------------------------------------------------------------------
// Declaracion de funciones privadas. Metodos de THbArray (hb)
static void THBARRAY_NEW( void )
{
PHB_ITEM pSelf = hb_stackSelfItem();
THbArray *pObjC = THbArray::getObjC();
pObjC->create( hb_param( 1, HB_IT_ARRAY ) );
hb_itemReturn( pSelf );
}
static void THBARRAY_APPEND( void )
{
THbArray *pObjC = THbArray::getObjC();
hb_retl( pObjC->append( hb_param( 1, HB_IT_ANY ) ) );
}
static void THBARRAY_INSERT( void )
{
THbArray *pObjC = THbArray::getObjC();
hb_retl( pObjC->insert( hb_param( 1, HB_IT_ANY ) ) );
}
static void THBARRAY_UPDATE( void )
{
THbArray *pObjC = THbArray::getObjC();
hb_retl( pObjC->update( hb_param( 1, HB_IT_ANY ) ) );
}
static void THBARRAY_DELETE( void )
{
THbArray *pObjC = THbArray::getObjC();
hb_retl( pObjC->erase() );
}
static void THBARRAY_GOTO( void )
{
PHB_ITEM pSelf = hb_stackSelfItem();
THbArray *pObjC = THbArray::getObjC( pSelf );
pObjC->goTo( hb_parnl( 1 ) );
hb_itemReturn( pSelf );
}
static void THBARRAY_GOFIRST( void )
{
PHB_ITEM pSelf = hb_stackSelfItem();
THbArray *pObjC = THbArray::getObjC( pSelf );
pObjC->goTo( 1 );
hb_itemReturn( pSelf );
}
static void THBARRAY_GOLAST( void )
{
PHB_ITEM pSelf = hb_stackSelfItem();
THbArray *pObjC = THbArray::getObjC( pSelf );
pObjC->goTo( pObjC->getSize() );
hb_itemReturn( pSelf );
}
static void THBARRAY_NEXT( void )
{
PHB_ITEM pSelf = hb_stackSelfItem();
THbArray *pObjC = THbArray::getObjC( pSelf );
pObjC->moveBy( 1 );
hb_itemReturn( pSelf );
}
static void THBARRAY_PRIOR( void )
{
PHB_ITEM pSelf = hb_stackSelfItem();
THbArray *pObjC = THbArray::getObjC( pSelf );
pObjC->moveBy( -1 );
hb_itemReturn( pSelf );
}
static void THBARRAY_MOVEBY( void )
{
PHB_ITEM pSelf = hb_stackSelfItem();
THbArray *pObjC = THbArray::getObjC( pSelf );
pObjC->moveBy( hb_parnl( 1 ) );
hb_itemReturn( pSelf );
}
static void THBARRAY_GETPOS( void )
{
THbArray *pObjC = THbArray::getObjC();
hb_retnint( pObjC->getPos() );
}
static void THBARRAY_ISFIRST( void )
{
THbArray *pObjC = THbArray::getObjC();
hb_retl( pObjC->bFirst );
}
static void THBARRAY_ISLAST( void )
{
THbArray *pObjC = THbArray::getObjC();
hb_retl( pObjC->bLast );
}
static void THBARRAY_GETSIZE( void )
{
THbArray *pObjC = THbArray::getObjC();
hb_retnint( pObjC->getSize() );
}
static void THBARRAY_GETVALUE( void )
{
THbArray *pObjC = THbArray::getObjC();
hb_itemReturn( pObjC->getValue( static_cast<HB_SIZE>( hb_parnintdef( 1, pObjC->FPos ) ) ) );
}
static void THBARRAY_CURRENT( void )
{
THbArray *pObjC = THbArray::getObjC();
hb_itemReturn( pObjC->current() );
}
static void THBARRAY_SKIPPER( void )
{
THbArray *pObjC = THbArray::getObjC();
hb_retnint( pObjC->skipped( hb_parnint( 1 ) ) );
}
static void THBARRAY_END( void )
{
PHB_ITEM pSelf = hb_stackSelfItem();
THbArray *pObjC = THbArray::getObjC();
//hb_itemRelease( pObjC->FArray );
pObjC->destroy();
delete pObjC;
hb_arraySetPtr( pSelf, _OBJC_POS, NULL );
}
//-----------------------------------------------------------------------------
// Funcion de clase para usar desde Harbour
//-----------------------------------------------------------------------------
HB_FUNC( THBARRAY )
{
static HB_USHORT usClassH = 0;
THbArray *pObjC;
pObjC = new THbArray; // Crea el objeto de C++
if( usClassH == 0 )
{
usClassH = pObjC->createClass( _NUN_VARS, "THBARRAY" );
}
hb_clsAssociate( usClassH ); // Pone el objeto en return de la pila
// Asigna el objeto xBase al objeto C++
pObjC->pSelf = hb_stackReturnItem();
// Asigna el objeto C++ a la data del objeto de xBase
hb_arraySetPtr( hb_stackReturnItem(), _OBJC_POS, pObjC );
}
//-----------------------------------------------------------------------------
// Inicializa los simbolos de las clase en la tabla de simbolos de harbour.
//-----------------------------------------------------------------------------
HB_INIT_SYMBOLS_BEGIN( THBARRAY__InitSymbols )
{ "THBARRAY", { HB_FS_PUBLIC | HB_FS_LOCAL }, { HB_FUNCNAME( THBARRAY ) }, NULL
}
HB_INIT_SYMBOLS_END( THBARRAY__InitSymbols )
#if defined( HB_PRAGMA_STARTUP )
#pragma startup THBARRAY__InitSymbols
#elif defined( HB_DATASEG_STARTUP )
#define HB_DATASEG_BODY HB_DATASEG_FUNC( THBARRAY__InitSymbols )
#include "hbiniseg.h"
#endif
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
// Ejemplo 0
//-----------------------------------------------------------------------------
procedure main
local ain := { "prueba", 2, .f., date(), time() }
local a := THbArray():new( ain )
local i, nLen
cls
? "Empezamos!!!!, presiona <INTRO> para continuar..."
Inkey( 100 )
? a:className(), len( a ), a:getSize()
? "-----------------------------------------------"
? "Seguimos?"
Inkey( 100 )
a:append( "Elemento 1" )
a:append( "Elemento 2" )
a:append( "Elemento 3" )
a:append( "Elemento 4" )
a:append( "Elemento 5" )
? nLen := a:getSize()
? "==============================================="
for i := 1 to nLen
? i, a:getValue( i )
next
? "==============================================="
Inkey( 100 )
a:goFirst()
i := 0
while !a:isLast()
? ++i, a:current()
a:next()
end
? "==============================================="
Inkey( 100 )
a:goLast()
i := 0
while !a:isFirst()
? ++i, a:current()
a:prior()
end
? "==============================================="
? "Se termino"
inkey( 100 )
a:end()
return
//-----------------------------------------------------------------------------
// Ejemplo 1
//-----------------------------------------------------------------------------
procedure main
local ain := { "prueba", 2, .f., date(), time() }
local a := THbArray():new( ain )
local i, nLen
cls
? "Empezamos!!!!, presiona <INTRO> para continuar..."
Inkey( 100 )
? a:className(), len( a ), a:getSize()
? "-----------------------------------------------"
? "Seguimos?"
Inkey( 100 )
? nLen := a:getSize()
? "==============================================="
for i := 1 to nLen
? i, a:getValue( i )
next
? "==============================================="
a:goTo( 3 ):update( "Elemento 3 modificado")
? "==============================================="
for i := 1 to nLen
? i, a:getValue( i )
next
? "==============================================="
Inkey( 100 )
a:end()
return
//-----------------------------------------------------------------------------
// Ejemplo 2
//-----------------------------------------------------------------------------
procedure main
local ain := { "prueba", 2, .f., date(), time() }
local a := THbArray():new( ain )
local i, nLen
cls
? "Empezamos!!!!, presiona <INTRO> para continuar..."
Inkey( 100 )
? a:className(), len( a ), a:getSize()
? "-----------------------------------------------"
? "Seguimos?"
Inkey( 100 )
? "Total elementos:", nLen := a:getSize()
? "==============================================="
? "Visualizacion de elemtos:"
for i := 1 to nLen
? i, a:getValue( i )
next
? "==============================================="
if a:goTo( 3 ):delete()
? "Se ha borrado el elemento 3"
else
? "No se ha podido borrar"
end if
? "==============================================="
? "Numero de lementos ahora:", nLen := a:getSize()
? "==============================================="
? "Visualizacion de elemtos:"
for i := 1 to nLen
? i, a:getValue( i )
next
? "==============================================="
Inkey( 100 )
a:end()
return
//-----------------------------------------------------------------------------
// Ejemplo 3
//-----------------------------------------------------------------------------
procedure main()
local oBrowse := TBrowseNew( 5, 5, 16, 30 )
local aTest0 := { "Fila 01", "Fila 02", "Fila 03", "Fila 04", "Fila 05", ;
"Fila 06", "Fila 07", "Fila 08", "Fila 09", "Fila 10", ;
"Fila 11", "Fila 12", "Fila 13", "Fila 14", "Fila 15", ;
"Fila 16", "Fila 17", "Fila 18", "Fila 19", "Fila 20" }
local a := THbArray():new( aTest0 )
local nCursor
local cColor
local nRow, nCol
oBrowse:colorSpec := "W+/B, N/BG"
oBrowse:ColSep := hb_UTF8ToStrBox( "│" )
oBrowse:HeadSep := hb_UTF8ToStrBox( "╤═" )
oBrowse:FootSep := hb_UTF8ToStrBox( "╧═" )
oBrowse:GoTopBlock := { || a:goFirst() }
oBrowse:GoBottomBlock := { || a:goLast() }
oBrowse:SkipBlock := { | nSkip | a:skipper( nSkip ) }
oBrowse:AddColumn( TBColumnNew( "Cabecera Columna 1", { || a:getValue() } ) )
oBrowse:GetColumn( 1 ):Footing := "Pie Columna 1"
oBrowse:Configure()
oBrowse:Freeze := 1
nCursor := SetCursor( 0 )
cColor := SetColor( "W+/B" )
nRow := Row()
nCol := Col()
hb_DispBox( 4, 4, 17, 31, hb_UTF8ToStrBox( "┌─┐│┘─└│ " ) )
while .t.
oBrowse:ForceStable()
if oBrowse:applykey( Inkey( 0 ) ) == -1
exit
endif
enddo
SetPos( nRow, nCol )
SetColor( cColor )
SetCursor( nCursor )
a:end()
return
Return to FiveWin para Harbour/xHarbour
Users browsing this forum: Google [Bot] and 43 guests