// This example shows how to create a Harbour Class entirely from language C (low level)
#include "FiveWin.ch"
function Main()
local o := Another():New()
MsgInfo( o:ClassName() )
MsgInfo( o:Data1 )
o:Data1 = 345
MsgInfo( o:Data1 )
MsgInfo( o:Data2 )
return nil
init procedure First
Test()
return
CLASS Another FROM Test
ENDCLASS
#pragma BEGINDUMP
#include <hbapi.h>
#include <hbapiitm.h>
#include <hbapicls.h>
#include <hbstack.h>
HB_FUNC_STATIC( TESTGETDATA1 )
{
PHB_ITEM pSelf = hb_param( 0, HB_IT_OBJECT );
hb_arrayGet( pSelf, 1, hb_stackReturnItem() );
}
HB_FUNC_STATIC( TESTSETDATA1 )
{
PHB_ITEM pSelf = hb_param( 0, HB_IT_OBJECT );
hb_arraySet( pSelf, 1, hb_param( 1, HB_IT_ANY ) );
}
HB_FUNC_STATIC( TESTGETDATA2 )
{
PHB_ITEM pSelf = hb_param( 0, HB_IT_OBJECT );
hb_arrayGet( pSelf, 2, hb_stackReturnItem() );
}
HB_FUNC_STATIC( TESTSETDATA2 )
{
PHB_ITEM pSelf = hb_param( 0, HB_IT_OBJECT );
hb_arraySet( pSelf, 2, hb_param( 1, HB_IT_ANY ) );
}
HB_FUNC_STATIC( TESTNEW )
{
PHB_ITEM pSelf = hb_param( 0, HB_IT_OBJECT );
hb_arraySetNL( pSelf, 1, 123 ); // pSelf[ 1 ] = 123
hb_arraySetC( pSelf, 2, "Hello world!" ); // pSelf[ 2 ] = "Hello world!"
hb_itemReturn( pSelf ); // return Self
}
HB_FUNC( TEST )
{
static HB_USHORT usClassH = 0;
if( usClassH == 0 )
{
HB_SYMB symTest = { "TEST", { HB_FS_PUBLIC | HB_FS_LOCAL }, { HB_FUNCNAME( TEST ) }, NULL };
hb_dynsymNew( &symTest );
usClassH = hb_clsCreate( 2, "TEST" ); // 2 DATAs
hb_clsAdd( usClassH, "DATA1", HB_FUNCNAME( TESTGETDATA1 ) );
hb_clsAdd( usClassH, "_DATA1", HB_FUNCNAME( TESTSETDATA1 ) );
hb_clsAdd( usClassH, "DATA2", HB_FUNCNAME( TESTGETDATA2 ) );
hb_clsAdd( usClassH, "_DATA2", HB_FUNCNAME( TESTSETDATA2 ) );
hb_clsAdd( usClassH, "NEW", HB_FUNCNAME( TESTNEW ) );
}
hb_clsAssociate( usClassH ); // Creates an object of Class usClassH
}
#pragma ENDDUMP
init procedure First
Test()
return
HB_SYMB symTest = { "TEST", { HB_FS_PUBLIC | HB_FS_LOCAL }, { HB_FUNCNAME( TEST ) }, NULL };
hb_dynsymNew( &symTest );
// This example shows how to create a Harbour Class entirely from language C (low level)
#include "FiveWin.ch"
function Main()
local o := Another( Test() ):New()
MsgInfo( o:ClassName() )
MsgInfo( o:Data1 )
o:Data1 = 345
MsgInfo( o:Data1 )
MsgInfo( o:Data2 )
return nil
CLASS Another FROM Test
ENDCLASS
#pragma BEGINDUMP
#include <hbapi.h>
#include <hbapiitm.h>
#include <hbapicls.h>
#include <hbstack.h>
HB_FUNC_STATIC( TESTGETDATA1 )
{
PHB_ITEM pSelf = hb_param( 0, HB_IT_OBJECT );
hb_arrayGet( pSelf, 1, hb_stackReturnItem() );
}
HB_FUNC_STATIC( TESTSETDATA1 )
{
PHB_ITEM pSelf = hb_param( 0, HB_IT_OBJECT );
hb_arraySet( pSelf, 1, hb_param( 1, HB_IT_ANY ) );
}
HB_FUNC_STATIC( TESTGETDATA2 )
{
PHB_ITEM pSelf = hb_param( 0, HB_IT_OBJECT );
hb_arrayGet( pSelf, 2, hb_stackReturnItem() );
}
HB_FUNC_STATIC( TESTSETDATA2 )
{
PHB_ITEM pSelf = hb_param( 0, HB_IT_OBJECT );
hb_arraySet( pSelf, 2, hb_param( 1, HB_IT_ANY ) );
}
HB_FUNC_STATIC( TESTNEW )
{
PHB_ITEM pSelf = hb_param( 0, HB_IT_OBJECT );
hb_arraySetNL( pSelf, 1, 123 ); // pSelf[ 1 ] = 123
hb_arraySetC( pSelf, 2, "Hello world!" ); // pSelf[ 2 ] = "Hello world!"
hb_itemReturn( pSelf ); // return Self
}
HB_FUNC( TEST )
{
static HB_USHORT usClassH = 0;
if( usClassH == 0 )
{
hb_dynsymSymbol( hb_dynsymFindName( "TEST" ) )->scope.value |= HB_FS_LOCAL;
usClassH = hb_clsCreate( 2, "TEST" ); // 2 DATAs
hb_clsAdd( usClassH, "DATA1", HB_FUNCNAME( TESTGETDATA1 ) );
hb_clsAdd( usClassH, "_DATA1", HB_FUNCNAME( TESTSETDATA1 ) );
hb_clsAdd( usClassH, "DATA2", HB_FUNCNAME( TESTGETDATA2 ) );
hb_clsAdd( usClassH, "_DATA2", HB_FUNCNAME( TESTSETDATA2 ) );
hb_clsAdd( usClassH, "NEW", HB_FUNCNAME( TESTNEW ) );
}
hb_clsAssociate( usClassH ); // Creates an object of Class usClassH
}
#pragma ENDDUMP
HB_FUNC_STATIC( TESTEJMETODO )
{
PHB_ITEM pSelf = hb_param( 0, HB_IT_OBJECT );
PHB_ITEM pObj = hb_itemNew( NULL );
hb_arrayGet( pSelf, 3, pObj ); // 3 is the DATA position for DATA hObj
hb_objSendMsg( pObj, "EJEELMETODO", 0 ); // 0 parameters sent
}
...
hb_clsAdd( usClassH, "EJMETODO", HB_FUNCNAME( TESTEJMETODO ) );
...
//-----------------------------------------------------------------------------
// De (x)Harbour
#include <hbapi.h>
#include <hbapiitm.h>
#include <hbstack.h>
// Compatibiliza
#if !defined(__XHARBOUR__)
#include <hbapicls.h>
#else
#include <classes.h>
#define HB_USHORT USHORT
#define HB_ULONG ULONG
#endif
//-----------------------------------------------------------------------------
// Manipulacion de Objetos
// Esta es para DATAs que van a tener una funcion get y otra set
#define IVAR( szName, pGetFuncName, pSetFuncName ) \
hb_clsAdd( usClassH, szName, pGetFuncName ); \
hb_clsAdd( usClassH, "_"szName, pSetFuncName )
// Esta es para DATAs que van a tener una funcion unica para get y set
#define SETGETVAR( szName, pFuncName ) \
hb_clsAdd( usClassH, szName, pFuncName ); \
hb_clsAdd( usClassH, "_"szName, pFuncName )
// Metodos
#define METHOD( szName, pFuncName ) hb_clsAdd( usClassH, szName, pFuncName )
//-----------------------------------------------------------------------------
// Funcion de clase
HB_USHORT CreateClass( const char * szClsName, HB_USHORT uiDatas );
void SetGet( HB_ULONG ulIndex );
//-----------------------------------------------------------------------------
//---------------------------------------------------------------------------//
#include "generic.h"
//-----------------------------------------------------------------------------
// Crea la clase. Esta puede sergenerica
HB_USHORT CreateClass( const char * szClsName, HB_USHORT uiDatas )
{
hb_dynsymSymbol( hb_dynsymFindName( szClsName ) )->scope.value |= HB_FS_LOCAL;
return hb_clsCreate( uiDatas, szClsName );
}
//-----------------------------------------------------------------------------
// Funcion generica SET GET
void SetGet( HB_ULONG ulIndex )
{
if( hb_pcount() )
{
hb_arraySet( hb_stackSelfItem(), ulIndex, hb_stackItemFromBase( 1 ) );
}
else
{
hb_arrayGet( hb_stackSelfItem(), ulIndex, hb_stackReturnItem() );
}
}
//-----------------------------------------------------------------------------
//---------------------------------------------------------------------------//
#include "generic.h"
#include "mijerarquia.h"
//-----------------------------------------------------------------------------
// Funcion de clase
HB_FUNC( TEST )
{
static HB_USHORT usClassH = 0;
if( usClassH == 0 )
{
usClassH = CreateClass( "TEST", 3 );
InheritTest( usClassH );
}
hb_clsAssociate( usClassH );
}
//-----------------------------------------------------------------------------
// DATAS
HB_FUNC_STATIC( TESTDATA1 ) { SetGet( 1 ); }
HB_FUNC_STATIC( TESTDATA2 ) { SetGet( 2 ); }
HB_FUNC_STATIC( TESTDATA3 ) { SetGet( 3 ); }
//-----------------------------------------------------------------------------
// Metodos
HB_FUNC_STATIC( TESTNEW ) // Constructor
{
PHB_ITEM pSelf = hb_stackSelfItem();
hb_arraySetNL( pSelf, 1, 123 );
hb_arraySetC( pSelf, 2, "Hello world!" );
hb_arraySet( pSelf, 3, hb_itemArrayNew( 0 ) );
hb_itemReturn( pSelf );
}
//-----------------------------------------------------------------------------
// Copia de metodos y datas para el objeto
void InheritTest( HB_USHORT usClassH )
{
SETGETVAR( "DATA1", HB_FUNCNAME( TESTDATA1 ) );
SETGETVAR( "DATA2", HB_FUNCNAME( TESTDATA2 ) );
SETGETVAR( "DATA3", HB_FUNCNAME( TESTDATA3 ) );
METHOD( "NEW", HB_FUNCNAME( TESTNEW ) );
}
//-----------------------------------------------------------------------------
//---------------------------------------------------------------------------//
#include "generic.h"
#include "mijerarquia.h"
#include "hbapigt.h"
//-----------------------------------------------------------------------------
HB_FUNC( ANOTHER )
{
static HB_USHORT usClassH = 0;
if( usClassH == 0 )
{
usClassH = CreateClass( "ANOTHER", 5 );
InheritAnother( usClassH );
}
hb_clsAssociate( usClassH );
}
//-----------------------------------------------------------------------------
// DATAS
//-------------------------------------------------------
// Data4 tipo setget
HB_FUNC_STATIC( ANOTHERDATA4 ) { SetGet( 4 ); }
//-------------------------------------------------------
// Data5 con una funcion para get y otra para el set
HB_FUNC_STATIC( ANOTHERGETDATA5 )
{
PHB_ITEM pSelf = hb_stackSelfItem();
hb_arrayGet( pSelf, 5, hb_stackReturnItem() );
}
//-------------------------------------------------------
HB_FUNC_STATIC( ANOTHERSETDATA5 )
{
PHB_ITEM pSelf = hb_stackSelfItem();
hb_arraySet( pSelf, 5, hb_stackItemFromBase( 1 ) );
}
//-----------------------------------------------------------------------------
// Metodos
HB_FUNC_STATIC( ANOTHERHOLA )
{
PHB_ITEM pSelf = hb_stackSelfItem();
hb_arraySetC( pSelf, 4, "Hola desde la data4" );
hb_retc( "Este es el metodo Hola de Another" );
}
//-----------------------------------------------------------------------------
HB_FUNC_STATIC( ANOTHERNUMDATAS )
{
PHB_ITEM pSelf = hb_stackSelfItem();
hb_retni( hb_arrayLen( pSelf ) );
}
//-----------------------------------------------------------------------------
// Copia de metodos y datas para el objeto
void InheritAnother( HB_USHORT usClassH )
{
InheritTest( usClassH );
SETGETVAR( "DATA4", HB_FUNCNAME( ANOTHERDATA4 ) );
IVAR( "DATA5", HB_FUNCNAME( ANOTHERGETDATA5 ), HB_FUNCNAME( ANOTHERSETDATA5 ) );
METHOD( "HOLA", HB_FUNCNAME( ANOTHERHOLA ) );
METHOD( "NUMDATAS", HB_FUNCNAME( ANOTHERNUMDATAS ) );
}
//-----------------------------------------------------------------------------
//---------------------------------------------------------------------------//
//-----------------------------------------------------------------------------
// Prototipos de funcciones
void InheritTest( HB_USHORT usClassH );
void InheritAnother( HB_USHORT usClassH );
//-----------------------------------------------------------------------------
REQUEST HB_GT_WIN
function Main()
local kk := Test():new()
local o := Another():New()
Alert( "Tipo de la o:data3: " + ValType( o:Data3 ) )
Alert( "Tipo de la kk:data3: " + ValType( kk:Data3 ) )
Alert( "o:ClassName() -> " + o:ClassName() )
Alert( "o:Data1 -> " + Transform( o:Data1, "@" ) )
o:Data1 = 345
Alert( "Despues de 345 o:Data1 -> " + Transform( o:Data1, "@!" ) )
Alert( "o:Data2 -> " + Transform( o:Data2, "@" ) )
Alert( "o:Hola() -> " + Transform( o:Hola(), "@" ) )
Alert( "data4: " + o:Data4 + " Tipo: " + ValType( o:Data4 ) )
o:Data4 := 4
Alert( "data4 modificado: " + Transform( o:Data4, "@" ) + " Tipo: " + ValType( o:Data4 ) )
Alert( "NumDatas: " + StrZero( o:NumDatas(), 2 ) )
o:Data5 := "Prueba de data 5 ..."
Alert( o:Data5 )
Alert( "Este es hb_objGetClass: " + /*Str( hb_objGetClass( o ) ) +*/ Str( o:ClassH ) )
inKey( 5 )
return nil
Return to FiveWin para Harbour/xHarbour
Users browsing this forum: No registered users and 65 guests