Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <string>
- #include "cppbind.h"
- class Account
- {
- public:
- int coins;
- int numtransactions;
- std::string name;
- Account() : coins( 0 ), numtransactions( 0 ){}
- int Add( SGS_CTX )
- {
- int orig = coins;
- sgs_Integer I;
- if( sgs_StackSize( C ) != 2 ||
- !sgs_ParseInt( C, 1, &I ) )
- return sgs_Printf( C, SGS_WARNING, "unexpected arguments" );
- coins += (int) I;
- numtransactions++;
- printf( "Transaction #%04d | before=%d after=%d\n",
- numtransactions, orig, coins );
- return 0;
- }
- };
- SGS_DECLARE_IFACE( Account );
- SGS_METHOD_WRAPPER( Account, Add );
- SGS_BEGIN_GENERIC_GETINDEXFUNC( Account )
- SGS_GGIF_METHOD( Account, Add )
- SGS_GGIF_CUSTOM( Account, coins, sgs_PushInt(C,item->coins) )
- SGS_GGIF_CUSTOM( Account, name, sgs_PushString(C,item->name.c_str()) )
- SGS_GGIF_CUSTOM( Account, transaction_count, sgs_PushInt(C,item->numtransactions) )
- SGS_END_GENERIC_GETINDEXFUNC;
- SGS_BEGIN_GENERIC_SETINDEXFUNC( Account )
- SGS_GSIF_CUSTOM( Account, name,
- char* str; sgs_SizeVal strlen;
- if( !sgs_ParseString( C, 1, &str, &strlen ) )
- return SGS_EINVAL;
- item->name.assign( str, strlen );
- )
- SGS_END_GENERIC_SETINDEXFUNC;
- SGS_GENERIC_DESTRUCTOR( Account );
- SGS_DEFINE_IFACE( Account )
- SGS_IFACE_ENTRY( GETINDEX, SGS_GETINDEXNAME( Account ) ),
- SGS_IFACE_ENTRY( SETINDEX, SGS_SETINDEXNAME( Account ) ),
- SGS_IFACE_ENTRY( DESTRUCT, SGS_DTORNAME( Account ) ),
- SGS_DEFINE_IFACE_END;
- SGS_DEFINE_EMPTY_CTORFUNC( Account );
- const char* code =
- "\n\
- a = Account();\n\
- a.Add( 1337 );\n\
- print 'reading from property: ' $ a.coins $ '\\n';\n\
- a.name = 'special';\n\
- printvars( a.name, a.transaction_count, a.coins );\n\
- ";
- int main( int argc, char* argv[] )
- {
- printf( "CPP binding test...\n" );
- SGS_CTX = sgs_CreateEngine();
- SGS_REGISTER( Account );
- sgs_ExecString( C, code );
- sgs_DestroyEngine( C );
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement