Skip to content
Snippets Groups Projects
Commit a9a15a9e authored by Jonathan Lambrechts's avatar Jonathan Lambrechts
Browse files

bindings documentation code (content still missing)

parent c97064fc
No related branches found
No related tags found
No related merge requests found
#include <stdarg.h>
#include "Bindings.h"
#include <stdio.h>
void methodBinding::setArgNames(const char *arg1, ...) {
va_list ap;
va_start(ap,arg1);
const char * name=arg1;
while(name!=NULL){
_argNames.push_back(name);
name = va_arg(ap,const char *);
}
va_end(ap);
}
#ifndef _BINDINGS_H_ #ifndef _BINDINGS_H_
#define _BINDINGS_H_ #define _BINDINGS_H_
#include <string> #include <string>
#include <list> #include <vector>
#include <typeinfo> #include <typeinfo>
#include "GmshConfig.h"
class methodBinding{ class methodBinding{
std::string _description; std::string _description;
std::vector<std::string> _argNames;
public: public:
void setArgNames(std::string arg1, ...){} inline const std::vector<std::string> &getArgNames()const{
return _argNames;
}
void setArgNames(const char * arg1, ...);
void setDescription(std::string description){_description=description;} void setDescription(std::string description){_description=description;}
inline const std::string getDescription() const {return _description;} inline const std::string getDescription() const {return _description;}
}; };
#if defined(HAVE_LUA) #if defined(HAVE_LUA)
#include "LuaBindings.h" #include "LuaBindings.h"
#else //no bindings #else //no bindings
......
#ifndef BINDINGS_TYPE_NAME_H
#define BINDINGS_TYPE_NAME_H
#include <vector>
#include <string>
/*** store a unique static class name for each binded class ***/
template <typename type>
class className{
static std::string _name;
public:
static void set(std::string name){
if(_name!=""){
throw;
}
_name=name;
}
static const std::string &get(){
if(_name==""){
throw;
}
return _name;
}
};
template<typename type>
std::string className<type>::_name;
template <>
class className<std::string>{
public:
static const std::string get(){
return "string";
}
};
template <>
class className<int>{
public:
static const std::string get(){
return "int";
}
};
template <>
class className<double>{
public:
static const std::string get(){
return "double";
}
};
template <>
class className<void>{
public:
static const std::string get(){
return "void";
}
};
template <typename t>
class className<t*>{
public:
static const std::string get(){
return className<t>::get();
}
};
template <typename t>
class className<const t &>{
public:
static const std::string get(){
return className<t>::get();
}
};
template <typename cb>
class argTypeNames;
template <typename tr, typename tObj, typename t0, typename t1, typename t2, typename t3>
class argTypeNames<tr (tObj::*)(t0,t1,t2,t3)>{
public:
static void get(std::vector<std::string> &names){
names.clear();
names.push_back(className<tr>::get());
names.push_back(className<t0>::get());
names.push_back(className<t1>::get());
names.push_back(className<t2>::get());
names.push_back(className<t3>::get());
}
};
template <typename tr, typename tObj, typename t0, typename t1, typename t2>
class argTypeNames<tr (tObj::*)(t0,t1,t2)>{
public:
static void get(std::vector<std::string> &names){
names.clear();
names.push_back(className<tr>::get());
names.push_back(className<t0>::get());
names.push_back(className<t1>::get());
names.push_back(className<t2>::get());
}
};
template <typename tr, typename tObj, typename t0, typename t1>
class argTypeNames<tr (tObj::*)(t0,t1)>{
public:
static void get(std::vector<std::string> &names){
names.clear();
names.push_back(className<tr>::get());
names.push_back(className<t0>::get());
names.push_back(className<t1>::get());
}
};
template <typename tr, typename tObj, typename t0>
class argTypeNames<tr (tObj::*)(t0)>{
public:
static void get(std::vector<std::string> &names){
names.clear();
names.push_back(className<tr>::get());
names.push_back(className<t0>::get());
}
};
template <typename tr, typename tObj>
class argTypeNames<tr (tObj::*)()>{
public:
static void get(std::vector<std::string> &names){
names.clear();
names.push_back(className<tr>::get());
}
};
template <typename cb>
class argTypeNames;
template <typename tr, typename tObj, typename t0, typename t1, typename t2, typename t3>
class argTypeNames<tr (tObj::*)(t0,t1,t2,t3)const>{
public:
static void get(std::vector<std::string> &names){
argTypeNames<tr (tObj::*)(t0,t1,t2,t3)>::get(names);
}
};
template <typename tr, typename tObj, typename t0, typename t1, typename t2>
class argTypeNames<tr (tObj::*)(t0,t1,t2)const>{
public:
static void get(std::vector<std::string> &names){
argTypeNames<tr (tObj::*)(t0,t1,t2)>::get(names);
}
};
template <typename tr, typename tObj, typename t0, typename t1>
class argTypeNames<tr (tObj::*)(t0,t1)const>{
public:
static void get(std::vector<std::string> &names){
argTypeNames<tr (tObj::*)(t0,t1)>::get(names);
}
};
template <typename tr, typename tObj, typename t0>
class argTypeNames<tr (tObj::*)(t0)const>{
public:
static void get(std::vector<std::string> &names){
argTypeNames<tr (tObj::*)(t0)>::get(names);
}
};
template <typename tr, typename tObj>
class argTypeNames<tr (tObj::*)()const>{
public:
static void get(std::vector<std::string> &names){
argTypeNames<tr (tObj::*)()>::get(names);
}
};
#endif
...@@ -23,6 +23,7 @@ set(SRC ...@@ -23,6 +23,7 @@ set(SRC
TreeUtils.cpp avl.cpp TreeUtils.cpp avl.cpp
MallocUtils.cpp MallocUtils.cpp
ConnectionManager.cpp ConnectionManager.cpp
Bindings.cpp
) )
file(GLOB HDR RELATIVE ${CMAKE_SOURCE_DIR}/Common *.h) file(GLOB HDR RELATIVE ${CMAKE_SOURCE_DIR}/Common *.h)
......
...@@ -41,13 +41,30 @@ const char *colorBlue = "\033[1;34m"; ...@@ -41,13 +41,30 @@ const char *colorBlue = "\033[1;34m";
const char *colorDefault = "\033[0m"; const char *colorDefault = "\033[0m";
const char *colorBold = "\033[1m"; const char *colorBold = "\033[1m";
static void print_method(std::string name, luaMethodBinding *mb, bool isConstructor=false) {
std::vector<std::string> argTypeNames;
mb->getArgTypeNames(argTypeNames);
std::cout<<" ";
if(!isConstructor)
std::cout<<colorBold<<argTypeNames[0];
std::cout<<colorBlue<<" "<<name<<colorDefault<<colorBold<<" (";
for(int i=1;i< argTypeNames.size(); i++){
if(i!=1)
std::cout<<", ";
std::cout<<colorBold<<argTypeNames[i]<<colorDefault;
if(mb->getArgNames().size()>i-1)
std::cout<<" "<<mb->getArgNames()[i-1];
}
std::cout<<colorBold<<")\n"<<colorDefault;
const std::string description=mb->getDescription();
std::cout<<(description.empty()?"no help available":description) <<"\n";
}
static void list_methods(classBinding *cb){ static void list_methods(classBinding *cb){
if(cb->methods.size()) if(cb->methods.size())
std::cout<<colorBold<<"Methods from "<<cb->getClassName()<<colorDefault<<"\n"; std::cout<<colorGreen<<"Methods from "<<cb->getClassName()<<colorDefault<<"\n";
for(std::map<std::string,luaMethodBinding *>::iterator it = cb->methods.begin(); it!=cb->methods.end(); it++){ for(std::map<std::string,luaMethodBinding *>::iterator it = cb->methods.begin(); it!=cb->methods.end(); it++){
std::cout<<colorBlue<<" "<<it->first<<colorDefault<<" : "; print_method(it->first,it->second);
const std::string description=it->second->getDescription();
std::cout<<(description.empty()?"no help available":description) <<"\n";
} }
if(cb->getParent()) if(cb->getParent())
list_methods(cb->getParent()); list_methods(cb->getParent());
...@@ -76,12 +93,17 @@ static int lua_help (lua_State *L){ ...@@ -76,12 +93,17 @@ static int lua_help (lua_State *L){
const std::string description=cb->getDescription(); const std::string description=cb->getDescription();
std::cout<<(description.empty()?"no help available":description) <<"\n"; std::cout<<(description.empty()?"no help available":description) <<"\n";
std::cout<<"\n"; std::cout<<"\n";
if(cb->getConstructor()){
std::cout<<colorGreen<<"Constructor"<<colorDefault<<"\n";
print_method(className,cb->getConstructor(),true);
std::cout<<"\n";
}
list_methods(cb); list_methods(cb);
std::cout<<"\n"; std::cout<<"\n";
if(cb->children.size()){ if(cb->children.size()){
std::cout<<colorBold<<"Children of "<<cb->getClassName()<<colorDefault<<"\n"; std::cout<<colorGreen<<"Children of "<<cb->getClassName()<<colorDefault<<"\n";
for(std::set<classBinding *>::iterator it = cb->children.begin(); it!=cb->children.end(); it++){ for(std::set<classBinding *>::iterator it = cb->children.begin(); it!=cb->children.end(); it++){
std::cout<<" "<<colorGreen<<(*it)->getClassName()<<colorDefault<<" : "; std::cout<<" "<<colorBlue<<(*it)->getClassName()<<colorDefault<<" : ";
const std::string description=(*it)->getDescription(); const std::string description=(*it)->getDescription();
std::cout<<(description.empty()?"no help available":description) <<"\n"; std::cout<<(description.empty()?"no help available":description) <<"\n";
} }
...@@ -167,15 +189,15 @@ binding::binding(){ ...@@ -167,15 +189,15 @@ binding::binding(){
// Register Lua bindings // Register Lua bindings
GModel::registerBindings(this); GModel::registerBindings(this);
fullMatrix<double>::registerBindings(this);
function::registerBindings(this);
dgConservationLaw::registerBindings(this);
dgSystemOfEquations::registerBindings(this); dgSystemOfEquations::registerBindings(this);
dgBoundaryCondition::registerBindings(this); dgBoundaryCondition::registerBindings(this);
dgConservationLaw::registerBindings(this);
dgConservationLawShallowWater2dRegisterBindings(this); dgConservationLawShallowWater2dRegisterBindings(this);
dgConservationLawWaveEquationRegisterBindings(this); dgConservationLawWaveEquationRegisterBindings(this);
dgConservationLawAdvectionRegisterBindings(this); dgConservationLawAdvectionRegisterBindings(this);
dgPerfectGasLaw2dRegisterBindings(this); dgPerfectGasLaw2dRegisterBindings(this);
fullMatrix<double>::registerBindings(this);
function::registerBindings(this);
functionLua::registerBindings(this); functionLua::registerBindings(this);
function::registerDefaultFunctions(); function::registerDefaultFunctions();
MVertex::registerBindings(this); MVertex::registerBindings(this);
......
#ifndef _LUA_BINDINGS_H_ #ifndef _LUA_BINDINGS_H_
#define _LUA_BINDINGS_H_ #define _LUA_BINDINGS_H_
#include <map>
#include <vector>
#include <set>
#include "GmshConfig.h"
#include "GmshMessage.h"
#include "Bindings.h" #include "Bindings.h"
#ifdef HAVE_LUA #ifdef HAVE_LUA
//#include "BindingsDocTemplates.h" #include "BindingsTypeName.h"
extern "C" { extern "C" {
#include "lua.h" #include "lua.h"
#include "lauxlib.h" #include "lauxlib.h"
} }
#include <vector>
#include <set>
class classBinding; class classBinding;
class binding { class binding {
...@@ -27,26 +30,6 @@ class binding { ...@@ -27,26 +30,6 @@ class binding {
classBinding *addClass(std::string className); classBinding *addClass(std::string className);
}; };
/*** store a unique static class name for each binded class ***/
template <typename type>
class className{
static std::string _name;
public:
static void set(std::string name){
if(_name!="")
throw;
_name=name;
}
static const std::string &get(){
if(_name=="")
throw;
return _name;
}
};
template<typename type>
std::string className<type>::_name;
/*** lua Stack : templates to get/push value from/on the lua stack ***/ /*** lua Stack : templates to get/push value from/on the lua stack ***/
...@@ -55,7 +38,7 @@ template<class type> ...@@ -55,7 +38,7 @@ template<class type>
class luaStack { class luaStack {
public: public:
static type get(lua_State *L, int ia){ static type get(lua_State *L, int ia){
printf("error cannot get generic class in lua, only pointers are implemented\n"); Msg::Error("error cannot get generic class in lua, only pointers are implemented\n");
} }
static void push(lua_State *L, type obj){ static void push(lua_State *L, type obj){
Msg::Error("cannot push generic class in lua, only pointers are implemented\n"); Msg::Error("cannot push generic class in lua, only pointers are implemented\n");
...@@ -309,6 +292,8 @@ static int luaCall(lua_State *L,void (tObj::*_f)() const) { ...@@ -309,6 +292,8 @@ static int luaCall(lua_State *L,void (tObj::*_f)() const) {
return 0; return 0;
}; };
//non const, no return //non const, no return
template <typename tObj, typename t0, typename t1, typename t2, typename t3> template <typename tObj, typename t0, typename t1, typename t2, typename t3>
static int luaCall(lua_State *L,void (tObj::*_f)(t0,t1,t2,t3)) { static int luaCall(lua_State *L,void (tObj::*_f)(t0,t1,t2,t3)) {
...@@ -337,6 +322,9 @@ static int luaCall(lua_State *L,void (tObj::*_f)()) { ...@@ -337,6 +322,9 @@ static int luaCall(lua_State *L,void (tObj::*_f)()) {
}; };
/*** arg names ***/
/*** actual bindings classes ***/ /*** actual bindings classes ***/
class luaMethodBinding :public methodBinding{ class luaMethodBinding :public methodBinding{
public: public:
...@@ -348,6 +336,7 @@ class luaMethodBinding :public methodBinding{ ...@@ -348,6 +336,7 @@ class luaMethodBinding :public methodBinding{
luaMethodBinding(){ luaMethodBinding(){
_luaname=""; _luaname="";
} }
virtual void getArgTypeNames(std::vector<std::string> &names){};
}; };
template <typename cb> template <typename cb>
...@@ -360,8 +349,8 @@ class methodBindingT:public luaMethodBinding { ...@@ -360,8 +349,8 @@ class methodBindingT:public luaMethodBinding {
int call (lua_State *L) { int call (lua_State *L) {
return luaCall(L,_f); return luaCall(L,_f);
} }
void getArgNames(std::string &returnTypeName, std::vector<std::string> &argTypeName) { void getArgTypeNames(std::vector<std::string> &names) {
getArgNames(_f,returnTypeName,argTypeName); argTypeNames<cb>::get(names);
} }
}; };
...@@ -373,6 +362,9 @@ class constructorBindingT:public luaMethodBinding { ...@@ -373,6 +362,9 @@ class constructorBindingT:public luaMethodBinding {
(luaStack<tObj*>::push(L,new tObj(luaStack<t0>::get(L,1),luaStack<t1>::get(L,2),luaStack<t2>::get(L,3), luaStack<t3>::get(L,4)))); (luaStack<tObj*>::push(L,new tObj(luaStack<t0>::get(L,1),luaStack<t1>::get(L,2),luaStack<t2>::get(L,3), luaStack<t3>::get(L,4))));
return 1; return 1;
} }
void getArgTypeNames(std::vector<std::string> &names) {
argTypeNames<void (tObj::*)(t0,t1,t2,t3)>::get(names);
}
}; };
template <typename tObj> template <typename tObj>
...@@ -383,6 +375,9 @@ class constructorBindingT<tObj,void,void,void,void>:public luaMethodBinding { ...@@ -383,6 +375,9 @@ class constructorBindingT<tObj,void,void,void,void>:public luaMethodBinding {
(luaStack<tObj*>::push(L,new tObj())); (luaStack<tObj*>::push(L,new tObj()));
return 1; return 1;
} }
void getArgTypeNames(std::vector<std::string> &names) {
argTypeNames<void (tObj::*)()>::get(names);
}
}; };
template <typename tObj, typename t0> template <typename tObj, typename t0>
class constructorBindingT<tObj,t0,void,void,void>:public luaMethodBinding { class constructorBindingT<tObj,t0,void,void,void>:public luaMethodBinding {
...@@ -392,6 +387,9 @@ class constructorBindingT<tObj,t0,void,void,void>:public luaMethodBinding { ...@@ -392,6 +387,9 @@ class constructorBindingT<tObj,t0,void,void,void>:public luaMethodBinding {
(luaStack<tObj*>::push(L,new tObj(luaStack<t0>::get(L,1)))); (luaStack<tObj*>::push(L,new tObj(luaStack<t0>::get(L,1))));
return 1; return 1;
} }
void getArgTypeNames(std::vector<std::string> &names) {
argTypeNames<void (tObj::*)(t0)>::get(names);
}
}; };
template <typename tObj, typename t0, typename t1> template <typename tObj, typename t0, typename t1>
class constructorBindingT<tObj,t0,t1,void,void>:public luaMethodBinding { class constructorBindingT<tObj,t0,t1,void,void>:public luaMethodBinding {
...@@ -401,6 +399,9 @@ class constructorBindingT<tObj,t0,t1,void,void>:public luaMethodBinding { ...@@ -401,6 +399,9 @@ class constructorBindingT<tObj,t0,t1,void,void>:public luaMethodBinding {
(luaStack<tObj*>::push(L,new tObj(luaStack<t0>::get(L,1),luaStack<t1>::get(L,2)))); (luaStack<tObj*>::push(L,new tObj(luaStack<t0>::get(L,1),luaStack<t1>::get(L,2))));
return 1; return 1;
} }
void getArgTypeNames(std::vector<std::string> &names) {
argTypeNames<void (tObj::*)(t0,t1)>::get(names);
}
}; };
template <typename tObj, typename t0, typename t1, typename t2> template <typename tObj, typename t0, typename t1, typename t2>
class constructorBindingT<tObj,t0,t1,t2,void>:public luaMethodBinding { class constructorBindingT<tObj,t0,t1,t2,void>:public luaMethodBinding {
...@@ -410,11 +411,15 @@ class constructorBindingT<tObj,t0,t1,t2,void>:public luaMethodBinding { ...@@ -410,11 +411,15 @@ class constructorBindingT<tObj,t0,t1,t2,void>:public luaMethodBinding {
(luaStack<tObj*>::push(L,new tObj(luaStack<t0>::get(L,1),luaStack<t1>::get(L,2),luaStack<t2>::get(L,3)))); (luaStack<tObj*>::push(L,new tObj(luaStack<t0>::get(L,1),luaStack<t1>::get(L,2),luaStack<t2>::get(L,3))));
return 1; return 1;
} }
void getArgTypeNames(std::vector<std::string> &names) {
argTypeNames<void (tObj::*)(t0,t1,t2)>::get(names);
}
}; };
class classBinding { class classBinding {
std::string _className; std::string _className;
binding *_b; binding *_b;
luaMethodBinding *_constructor;
static int callMethod(lua_State *L) { static int callMethod(lua_State *L) {
return static_cast<luaMethodBinding*>(lua_touserdata(L, lua_upvalueindex(1)))->call(L); return static_cast<luaMethodBinding*>(lua_touserdata(L, lua_upvalueindex(1)))->call(L);
} }
...@@ -444,17 +449,22 @@ class classBinding { ...@@ -444,17 +449,22 @@ class classBinding {
lua_pushcclosure(L, callMethod, 1); lua_pushcclosure(L, callMethod, 1);
lua_setfield(L, mt,"__call"); lua_setfield(L, mt,"__call");
lua_pop(L,2); lua_pop(L,2);
_constructor=constructor;
} }
//for the doc //for the doc
std::string _description; std::string _description;
classBinding *_parent; classBinding *_parent;
public: public:
std::set<classBinding *> children; std::set<classBinding *> children;
inline luaMethodBinding *getConstructor(){
return _constructor;
}
// get userdata from Lua stack and return pointer to T object // get userdata from Lua stack and return pointer to T object
classBinding(binding *b, std::string name){ classBinding(binding *b, std::string name){
_b=b; _b=b;
lua_State *L = _b->L; lua_State *L = _b->L;
_className=name; _className=name;
_constructor=NULL;
// there are 3 tables involved : // there are 3 tables involved :
// methods : the table of the C++ functions we bind (exept constructor) // methods : the table of the C++ functions we bind (exept constructor)
......
...@@ -293,10 +293,17 @@ template<> ...@@ -293,10 +293,17 @@ template<>
void fullMatrix<double>::registerBindings(binding *b) void fullMatrix<double>::registerBindings(binding *b)
{ {
classBinding *cb = b->addClass<fullMatrix<double> >("fullMatrix"); classBinding *cb = b->addClass<fullMatrix<double> >("fullMatrix");
cb->addMethod("size1", &fullMatrix<double>::size1); methodBinding *cm;
cb->addMethod("size2", &fullMatrix<double>::size2); cm = cb->addMethod("size1", &fullMatrix<double>::size1);
cb->addMethod("get", &fullMatrix<double>::get); cm->setDescription("return the number of rows in the matrix");
cb->addMethod("set", &fullMatrix<double>::set); cm = cb->addMethod("size2", &fullMatrix<double>::size2);
cb->addMethod("gemm", &fullMatrix<double>::gemm); cm->setDescription("return the number of columns in the matrix");
cb->setConstructor<fullMatrix<double>,int,int>(); cm = cb->addMethod("get", &fullMatrix<double>::get);
cm->setArgNames("i","j",NULL);
cm->setDescription("return the (i,j) entry of the matrix");
cm = cb->addMethod("set", &fullMatrix<double>::set);
cm->setArgNames("i","j","v",NULL);
cm->setDescription("set the (i,j) entry of the matrix to v");
cm = cb->addMethod("gemm", &fullMatrix<double>::gemm);
cm = cb->setConstructor<fullMatrix<double>,int,int>();
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment