lua-users home
lua-l archive

tolua wrapper suggestion: luabind

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


Hi, 
I just would like to put this idea on the list. I have the code, too,
but to me, my code seems still to be too much custom, so I do not post
it yet. If there would be interest, I would review it and put it on
the wiki together with a small patch to tolua4.
Juergen Fuhrmann
So this is my current description of luabind.
--
-- Purpose: Bind C code via specially marked up header
-- files to Lua. It is intended to work for simple interfaces
-- passing basic data types and user defined datatypes to Lua.
-- It works by generating a pkg description and piping it
-- directly to tolua.
-- The reason for this implementation is obvious: we want to
-- generate binding code just from the C header, so we clean
-- it up using some markup, instead of doing this manually
-- and introducing inconsistencies.
-- Currently, the markup consists of:
-- LuaBind : Pass line to tolua (subject to subsequent substitutions)
-- LuaBindSub(x,y) :substitute y for x in all passed lines 
-- LuaBindLua(file): embed Lua code from file 
-- LuaBindGC(class, destructor) : declare class destructor for gc
And here is a vector class header with luabind tags. It is work in
progress, so don't get shocked... I put a Lua shadow class around it,
allowing for lua-typical constructors like
v=DBlockVector{nrow=10,ncol=20} and to write code like v[i] for
vectors and v[i][j] for block vectors.
#ifndef DVECTOR_H
#define DVECTOR_H
#include "stdsys/complex.h"
#include "stdsys/ref.h"
typedef struct struct_DBlockVector DBlockVectorData, *DBlockVector;
typedef struct struct_DVector DVectorData,*DVector;
#include "vector/partition.h"
#define LuaBind
/*
 LuaBind typedef struct {} DVectorData;
 LuaBind typedef struct {} DBlockVectorData;
 LuaBindSub(DVector, DVectorData*);
 LuaBindSub(DBlockVector, DBlockVectorData*);
 LuaBindGC(DVectorData,release);
 LuaBindGC(DBlockVectorData,release);
 LuaBindLua(dvector.lua)
 LuaBind void release(DVector v);
 LuaBind void release(DBlockVector a);
*/
#define dbv_UNKNOWN 0
/*---------------------------------------------------------------*/
LuaBind DBlockVector dbvCreate (int nrow, int ncol); 
/* 
 Create block vector with nrow rows and ncol columns. 
 The size of a block vector is adjusteable. 
 Notably, we can choose nrow=dbv_UNKNOWN in order
 to create an adjusteable array.
*/
 DBlockVector dbvClone (DBlockVector a);
/* 
 Clone array. Return an array with the same number of rows,
 columns, and the same partition as a.
*/
LuaBind int dbvNCol (DBlockVector a); /* number of columns n_c*/
LuaBind int dbvNRow (DBlockVector a); /* number of rows n_r*/
LuaBind int dbvNAll (DBlockVector a); /* n_r*n_c */
LuaBind void dbvCheck (DBlockVector a); /* check if bounds had been overwritten*/
LuaBind void dbvAdjust (DBlockVector a); /* Adjust allocated space to actual size.*/
double* dbvColVal (DBlockVector a, int irow); /* data of column */
double* dbvRowVal (DBlockVector a, int icol); /* data of row */
double* dbvVal (DBlockVector a); /* data */
/* All dbv*Val routines return data in such a way that the first
 valid data item is stored at offset 1 so that C code can
 uses loops of type for (i=1;i<=n;i++) on it.
*/
double* dbvColVal77 (DBlockVector a, int irow); /* data of column */
double* dbvRowVal77 (DBlockVector a, int icol); /* data of row */
double* dbvVal77 (DBlockVector a); /* data */
/* All dbv*Val77 routines return data in such a way that the first
 valid data item is stored at offset 0 so that fortran code can
 use loops of type do i=1,n on it.
*/
Partition dbvGetPartition(DBlockVector a);
void dbvSetPartition(DBlockVector a, Partition ap);
/* Get/Set partition of a */
int * dbvParts(DBlockVector a);
int dbvNParts(DBlockVector a);
#ifndef BV
#define BV(a,i2,i1) ((a)->val[(i2-1)*a->ncol+i1])
#endif
/*Access macro, can be used as lvalue */
int dbvLineHandler(DBlockVector a, int iline, int argc, char *argv[]);
/* Line handler for file parsing: collect a full row */
/*---------------------------------------------------------------*/
LuaBind DVector dvCreate(int n);
/* 
Create vector of length n with stride 1. 
If n=dbv_UNKNOWN then the vector becomes of variable length, 
i.e. any index set operation is accepted, and if necessary, 
the vector is prolonged.
*/
DVector dvClone(DVector v);
/* 
 Clone vector v. If v is a vector with stride >1,
 the result will be of stride 1.
 */ 
Partition dvGetPartition(DVector a);
void dvSetPartition(DVector v, Partition ap);
/* Partition vector v */
int * dvParts(DVector a);
int dvNParts(DVector a);
LuaBind void dvAdjust(DVector v);
/* Adjust physical length (allocated memory) 
 to actual length, after the call, no change
 of length is possible anymore.
*/
LuaBind int dvN(DVector v); /* Get Vector Length */
 int dvStride(DVector v); /* Get storage stride */
LuaBind double dvGeti(DVector v,int i); 
LuaBind void dvSeti(DVector v, int i, double val); 
/* Get/Set value at index i, adjust length if necessary. */
#ifndef V
#define V(v,i) ((v)->val[((i)-1)*v->stride+1])
#endif 
/*Access macro, can be used as lvalue */
double *dvVal(DVector v);
/*
 Return pointer to data array:
 
 +---+---+---+---+---+---+---+---+---+---
 | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | ... 
 +---+---+---+---+---+---+---+---+---+---
 ^
 
 such that if v=dVal(vec), in the case of stride 1
 dvGeti(vec,i) and v[i] are equivalent (counting from one).
*/ 
double *dvVal77(DVector v);
LuaBind void dvCheck(DVector v); 
/* Check memory bounds */
LuaBind void dvPrint(DVector v);
/* Print contents */
LuaBind DVector dbvRow(DBlockVector a, int irow);
/*
 Create a stride 1 vector of lentgth ncol(a) containing row irow of a 
*/
LuaBind DVector dbvCol(DBlockVector a, int icol);
/*
 Create a stride ncol(a) vector of length nrow(a) contaning column icol
*/
LuaBind DVector dbvAll(DBlockVector a);
/*
 Create a stride 1 vector of length ncol(a)*nrow(a) containing
 all the data of a.
*/
int dvLineHandler(DVector v , int iline, int argc, char *argv[]);
/*
 * PRIVATE PART
 */
typedef struct struct_DMassif DMassifData,*DMassif;
/* A massif is the actual vecto data container. Russian programmers
 often say "massif" when they mean an array. Why shouldn't
 we say it too -- at least this name gives the feeling of
 something huge. This is our intention.
*/
#ifdef PDELIB
#include "grid/grid.h"
#endif
struct struct_DMassif
{
 double *val;
 int nrow;
 int ncol;
 int nalloc;
 Partition partition;
#ifdef PDELIB
 Grid grid; /* Pointer to grid */ 
 gAtomType type; /* TODO to be replaced by stencil */
#endif
 int refcount;
 void (*destroy)(DMassif m);
};
struct struct_DBlockVector
{
 double *val;
 DMassif data;
 int nrow;
 int ncol;
 int refcount;
 void (*destroy)(DBlockVector a);
};
struct struct_DVector
{
 double *val;
 DMassif data;
 int n;
 int stride;
 int refcount;
 void (*destroy)(DVector v);
};
#undef LuaBind
#endif

AltStyle によって変換されたページ (->オリジナル) /