diff --git a/Common/Bindings.h b/Common/Bindings.h index 14a51e2a641923b1d08cbf048b5868c499342a08..c0d2f0368ce4d2a8c4934ed958c6e1756d029bce 100644 --- a/Common/Bindings.h +++ b/Common/Bindings.h @@ -11,28 +11,6 @@ class methodBinding{ void setDescription(std::string description){} }; -// c++ does not allow to take pointer to constructor. We wrap the constructor with those function so that pointer to constructor-function are available. -template <typename tObj, typename t0, typename t1, typename t2, typename t3> -tObj *constructorPtr(t0 a0, t1 a1, t2 a2, t3 a3){ - return new tObj(a0,a1,a2,a3); -} -template <typename tObj, typename t0, typename t1, typename t2> -tObj *constructorPtr(t0 a0, t1 a1, t2 a2){ - return new tObj(a0,a1,a2); -} -template <typename tObj, typename t0, typename t1> -tObj *constructorPtr(t0 a0, t1 a1){ - return new tObj(a0,a1); -} -template <typename tObj, typename t0> -tObj *constructorPtr(t0 a0){ - return new tObj(a0); -} -template <typename tObj> -tObj *constructorPtr(){ - return new tObj(); -} - #if defined(HAVE_LUA) #include "LuaBindings.h" #else //no bindings diff --git a/Common/DummyBindings.h b/Common/DummyBindings.h index 1d5eceb5ec811e518353212b2cd86ae5d5335d8b..c30feffcf25f46d79aceab5d5a93a863696401e2 100644 --- a/Common/DummyBindings.h +++ b/Common/DummyBindings.h @@ -9,10 +9,16 @@ public: methodBinding *addMethod(std::string n, cb f){ return new methodBinding(); } - template <typename cb> - methodBinding *setConstructor(cb f){ - return new methodBinding(); - } + template <typename tObj, typename t0, typename t1, typename t2, typename t3 > + methodBinding *setConstructor(){} + template <typename tObj, typename t0, typename t1, typename t2 > + methodBinding *setConstructor(){} + template <typename tObj, typename t0, typename t1> + methodBinding *setConstructor(){} + template <typename tObj, typename t0> + methodBinding *setConstructor(){} + template<typename tObj> + methodBinding *setConstructor(){} }; class binding { diff --git a/Common/LuaBindings.h b/Common/LuaBindings.h index 6ef329eceb49687248c8f2d3ea78dafcf70daae3..96756a0b5445c3bc3372f442359158bc7ebd74ad 100644 --- a/Common/LuaBindings.h +++ b/Common/LuaBindings.h @@ -144,7 +144,7 @@ class luaStack<const type *>{ }; /*** template to call c function from the lua stack ***/ -//static, return (only used for contructor now) +//static, return template < typename tRet, typename t0, typename t1, typename t2, typename t3> static int luaCall(lua_State *L,tRet (*_f)(t0,t1,t2,t3)) { lua_remove(L,1); @@ -284,6 +284,7 @@ static int luaCall(lua_State *L,void (tObj::*_f)()) { return 0; }; + /*** actual bindings classes ***/ class luaMethodBinding :public methodBinding{ public: @@ -292,6 +293,9 @@ class luaMethodBinding :public methodBinding{ luaMethodBinding(const std::string luaname){ _luaname=luaname; } + luaMethodBinding(){ + _luaname=""; + } }; template <typename cb> @@ -306,6 +310,53 @@ class methodBindingT:public luaMethodBinding { } }; +template <typename tObj, typename t0=void, typename t1=void , typename t2=void, typename t3=void> +class constructorBindingT:public luaMethodBinding { + public: + int call (lua_State *L) { + lua_remove(L,1); + (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; + } +}; + +template <typename tObj> +class constructorBindingT<tObj,void,void,void,void>:public luaMethodBinding { + public: + int call (lua_State *L) { + lua_remove(L,1); + (luaStack<tObj*>::push(L,new tObj())); + return 1; + } +}; +template <typename tObj, typename t0> +class constructorBindingT<tObj,t0,void,void,void>:public luaMethodBinding { + public: + int call (lua_State *L) { + lua_remove(L,1); + (luaStack<tObj*>::push(L,new tObj(luaStack<t0>::get(L,1)))); + return 1; + } +}; +template <typename tObj, typename t0, typename t1> +class constructorBindingT<tObj,t0,t1,void,void>:public luaMethodBinding { + public: + int call (lua_State *L) { + lua_remove(L,1); + (luaStack<tObj*>::push(L,new tObj(luaStack<t0>::get(L,1),luaStack<t1>::get(L,2)))); + return 1; + } +}; +template <typename tObj, typename t0, typename t1, typename t2> +class constructorBindingT<tObj,t0,t1,t2,void>:public luaMethodBinding { + public: + int call (lua_State *L) { + lua_remove(L,1); + (luaStack<tObj*>::push(L,new tObj(luaStack<t0>::get(L,1),luaStack<t1>::get(L,2),luaStack<t2>::get(L,3)))); + return 1; + } +}; + class classBinding { std::string _className; @@ -329,6 +380,16 @@ class classBinding { lua_pushfstring(L, "%s (%s)", name, buff); return 1; } + void setConstructorLuaMethod(luaMethodBinding *constructor){ + lua_getglobal(_L,_className.c_str()); + int methods = lua_gettop(_L); + lua_getmetatable(_L,methods); + int mt = lua_gettop(_L); + lua_pushlightuserdata(_L,(void*)constructor); + lua_pushcclosure(_L, callMethod, 1); + lua_setfield(_L, mt,"__call"); + lua_pop(_L,2); + } public: // get userdata from Lua stack and return pointer to T object classBinding(lua_State *L, std::string name){ @@ -388,19 +449,35 @@ public: lua_pop(_L,1); return mb; } - template <typename cb> - methodBinding *setConstructor(cb f){ - luaMethodBinding *_constructorMethod = new methodBindingT<cb>("constructor",f); - - lua_getglobal(_L,_className.c_str()); - int methods = lua_gettop(_L); - lua_getmetatable(_L,methods); - int mt = lua_gettop(_L); - lua_pushlightuserdata(_L,(void*)_constructorMethod); - lua_pushcclosure(_L, callMethod, 1); - lua_setfield(_L, mt,"__call"); - lua_pop(_L,2); - return _constructorMethod; + template <typename tObj, typename t0, typename t1, typename t2, typename t3 > + methodBinding *setConstructor(){ + luaMethodBinding *constructorLua = new constructorBindingT<tObj,t0,t1,t2,t3>; + setConstructorLuaMethod(constructorLua); + return constructorLua; + } + template <typename tObj, typename t0, typename t1, typename t2 > + methodBinding *setConstructor(){ + luaMethodBinding *constructorLua = new constructorBindingT<tObj,t0,t1,t2>; + setConstructorLuaMethod(constructorLua); + return constructorLua; + } + template <typename tObj, typename t0, typename t1> + methodBinding *setConstructor(){ + luaMethodBinding *constructorLua = new constructorBindingT<tObj,t0,t1>; + setConstructorLuaMethod(constructorLua); + return constructorLua; + } + template <typename tObj, typename t0> + methodBinding *setConstructor(){ + luaMethodBinding *constructorLua = new constructorBindingT<tObj,t0>; + setConstructorLuaMethod(constructorLua); + return constructorLua; + } + template<typename tObj> + methodBinding *setConstructor(){ + luaMethodBinding *constructorLua = new constructorBindingT<tObj>; + setConstructorLuaMethod(constructorLua); + return constructorLua; } }; diff --git a/Geo/GModel.cpp b/Geo/GModel.cpp index 1f16e39ba2f4983f8a10c5d7f72f7b41ee578c2d..db55455256cac03723feac617750f0a8bd8873bf 100644 --- a/Geo/GModel.cpp +++ b/Geo/GModel.cpp @@ -1380,17 +1380,12 @@ void GModel::save(std::string fileName){ #ifdef HAVE_LUA #include "Bindings.h" -static GModel *myConstructorPtr(){ - return new GModel; -} void GModel::registerBindings(binding *b){ classBinding *cb = b->addClass<GModel>("GModel"); methodBinding *cm; cm = cb->addMethod("mesh",&GModel::mesh); cm = cb->addMethod("load",&GModel::load); cm = cb->addMethod("save",&GModel::save); - // cb->setConstructor(constructorPtr<GModel>); - // FIXME AGAIN !!!!!!!! JF - cb->setConstructor(myConstructorPtr); + cm = cb->setConstructor<GModel>(); } #endif diff --git a/Numeric/fullMatrix.cpp b/Numeric/fullMatrix.cpp index 25c4bc3af1ddad40f67f8fedcb0364ded84c7f63..dc103d8ab27d1ea0a188ec57b6d5dc0beda6cea1 100644 --- a/Numeric/fullMatrix.cpp +++ b/Numeric/fullMatrix.cpp @@ -293,11 +293,6 @@ bool fullMatrix<double>::svd(fullMatrix<double> &V, fullVector<double> &S) #include "Bindings.h" -// I DID FIX IT FOR NOW ... SHOULD BE BETTER -- JF -static fullMatrix<double> *myConstructorPtr(int a0, int a1){ - return new fullMatrix<double>(a0,a1); -} - template<> void fullMatrix<double>::registerBindings(binding *b){ classBinding *cb = b->addClass<fullMatrix<double> >("fullMatrix"); @@ -307,7 +302,5 @@ void fullMatrix<double>::registerBindings(binding *b){ cb->addMethod("get",&fullMatrix<double>::get); cb->addMethod("set",&fullMatrix<double>::set); cb->addMethod("gemm",&fullMatrix<double>::gemm); - // FIXME DOES NOT COMPILE ON MAC - cb->setConstructor(myConstructorPtr); - // cb->setConstructor(constructorPtr<fullMatrix<double>,int,int>); + cb->setConstructor<fullMatrix<double>,int,int>(); } diff --git a/Solver/dgConservationLawAdvection.cpp b/Solver/dgConservationLawAdvection.cpp index adedf3a207233249ab3272e0fba10dc83b365d9c..76b61eb5df8a539aa6d13b221a0e3edb38a82b52 100644 --- a/Solver/dgConservationLawAdvection.cpp +++ b/Solver/dgConservationLawAdvection.cpp @@ -94,14 +94,8 @@ dgConservationLawAdvection::dgConservationLawAdvection(std::string vFunctionName } #include "Bindings.h" - -static dgConservationLawAdvection *myConstructorPtr(std::string a,std::string b){ - return new dgConservationLawAdvection(a,b); -} - void dgConservationLawAdvectionRegisterBindings (binding *b){ classBinding *cb = b->addClass<dgConservationLawAdvection>("dgConservationLawAdvection"); - // cb->setConstructor(constructorPtr<dgConservationLawAdvection,std::string,std::string>); - cb->setConstructor(myConstructorPtr); + cb->setConstructor<dgConservationLawAdvection,std::string,std::string>(); cb->setParentClass<dgConservationLaw>(); } diff --git a/Solver/dgConservationLawPerfectGas.cpp b/Solver/dgConservationLawPerfectGas.cpp index 756a397c54986c9c38359dca76e0d91718774f06..8e221b7e4ecaf24be1064d0f79ed88e937b8858f 100644 --- a/Solver/dgConservationLawPerfectGas.cpp +++ b/Solver/dgConservationLawPerfectGas.cpp @@ -311,16 +311,10 @@ class dgBoundaryConditionPerfectGasLaw2dFreeStream : public dgBoundaryCondition #endif #include "Bindings.h" - -static dgPerfectGasLaw2d *myConstructorPtr(){ - return new dgPerfectGasLaw2d ; -} - void dgPerfectGasLaw2dRegisterBindings (binding *b){ classBinding *cb = b->addClass<dgPerfectGasLaw2d>("dgPerfectGasLaw2d"); methodBinding *cm; cb->addMethod("newWallBoundary",&dgPerfectGasLaw2d::newWallBoundary); - // cb->setConstructor(constructorPtr<dgPerfectGasLaw2d>); - cb->setConstructor(myConstructorPtr); + cb->setConstructor<dgPerfectGasLaw2d>(); cb->setParentClass<dgConservationLaw>(); } diff --git a/Solver/dgConservationLawShallowWater2d.cpp b/Solver/dgConservationLawShallowWater2d.cpp index aefa83bed37821a86c87c238ef24aa7a18014f0c..5a1e4b68d132571db7aec41c6a268eacaf3ed126 100644 --- a/Solver/dgConservationLawShallowWater2d.cpp +++ b/Solver/dgConservationLawShallowWater2d.cpp @@ -141,14 +141,10 @@ dgBoundaryCondition *dgConservationLawShallowWater2d::newBoundaryWall(){ } #include "Bindings.h" -static dgConservationLawShallowWater2d *myConstructorPtr(){ - return new dgConservationLawShallowWater2d; -} void dgConservationLawShallowWater2dRegisterBindings (binding *b){ classBinding *cb = b->addClass<dgConservationLawShallowWater2d>("dgConservationLawShallowWater2d"); methodBinding *cm; cb->addMethod("newBoundaryWall",&dgConservationLawShallowWater2d::newBoundaryWall); - // cb->setConstructor(constructorPtr<dgConservationLawShallowWater2d>); - cb->setConstructor(myConstructorPtr); + cb->setConstructor<dgConservationLawShallowWater2d>(); cb->setParentClass<dgConservationLaw>(); } diff --git a/Solver/dgConservationLawWaveEquation.cpp b/Solver/dgConservationLawWaveEquation.cpp index cf9e894a32af21a514095669c25a0db3877f800d..893da29c76f7ebd86480fc9d12508d5b19c48183 100644 --- a/Solver/dgConservationLawWaveEquation.cpp +++ b/Solver/dgConservationLawWaveEquation.cpp @@ -136,14 +136,10 @@ dgBoundaryCondition *dgConservationLawWaveEquation::newBoundaryWall()const{ } #include "Bindings.h" -static dgConservationLawWaveEquation *myConstructorPtr(int d){ - return new dgConservationLawWaveEquation (d); -} void dgConservationLawWaveEquationRegisterBindings(binding *b){ classBinding *cb = b->addClass<dgConservationLawWaveEquation> ("dgConservationLawWaveEquation"); methodBinding *cm; cb->addMethod("newBoundaryWall",&dgConservationLawWaveEquation::newBoundaryWall); - // cb->setConstructor(constructorPtr<dgConservationLawWaveEquation,int>); - cb->setConstructor(myConstructorPtr); + cb->setConstructor<dgConservationLawWaveEquation,int>(); cb->setParentClass<dgConservationLaw>(); } diff --git a/Solver/dgSystemOfEquations.cpp b/Solver/dgSystemOfEquations.cpp index 60983b9eace4c3467baa332ce9e7e710e93e2687..211c9c684428eba7285a85032f97e1d3ed199a56 100644 --- a/Solver/dgSystemOfEquations.cpp +++ b/Solver/dgSystemOfEquations.cpp @@ -46,7 +46,7 @@ static dgSystemOfEquations *myConstructorPtr(GModel* gm){ void dgSystemOfEquations::registerBindings(binding *b){ classBinding *cb = b->addClass<dgSystemOfEquations>("dgSystemOfEquations"); cb->setDescription("a class to rule them all :-) -- bad description, this class will be removed anyway"); - cb->setConstructor(myConstructorPtr); + cb->setConstructor<dgSystemOfEquations,GModel*>(); methodBinding *cm; cm = cb->addMethod("setConservationLaw",&dgSystemOfEquations::setConservationLaw); cm->setArgNames("law",NULL); diff --git a/Solver/function.cpp b/Solver/function.cpp index 3a1189d9ab761419e23010ee671be0e19bc34569..1193a69606ab017aeb41174af402eb3563463575 100644 --- a/Solver/function.cpp +++ b/Solver/function.cpp @@ -146,16 +146,11 @@ void function::registerDefaultFunctions() { function::add("XYZ", new functionXYZ); } - -static functionConstant *myConstructorPtr(fullMatrix<double>*m){ - return new functionConstant(m); -} void function::registerBindings(binding *b){ classBinding *cb = b->addClass<function>("function"); cb->addMethod("getName",&function::getName); cb = b->addClass<functionConstant>("functionConstant"); - cb->setConstructor(myConstructorPtr); - // cb->setConstructor(constructorPtr<functionConstant,fullMatrix<double>*>); + cb->setConstructor<functionConstant,fullMatrix<double>*>(); cb->setParentClass<function>(); } diff --git a/Solver/luaFunction.cpp b/Solver/luaFunction.cpp index 832ddded3f3ef713c83b3dd4de480b224ee3b7c3..2cf123532e98f3c3717e372f4a52e6cb1470b1fc 100644 --- a/Solver/luaFunction.cpp +++ b/Solver/luaFunction.cpp @@ -32,7 +32,7 @@ class functionLua::data : public dataCacheDouble{ lua_call(_function->_L,_dependencies.size()+1,0); /* call Lua function */ } }; -functionLua::functionLua (int nbCol, std::string &luaFunctionName, std::vector<std::string> &dependencies, lua_State *L) +functionLua::functionLua (int nbCol, std::string luaFunctionName, std::vector<std::string> dependencies, lua_State *L) : _luaFunctionName(luaFunctionName), _dependenciesName(dependencies),_L(L),_nbCol(nbCol) { static int c=0; @@ -48,7 +48,7 @@ dataCacheDouble *functionLua::newDataCache(dataCacheMap *m) void functionLua::registerBindings(binding *b){ classBinding *cb= b->addClass<functionLua>("functionLua"); - cb->setConstructor(constructorPtr<functionLua,int,std::string,std::vector<std::string>,lua_State*>); + cb->setConstructor<functionLua,int,std::string,std::vector<std::string>,lua_State*>(); cb->setParentClass<function>(); } diff --git a/Solver/luaFunction.h b/Solver/luaFunction.h index 2ded4ce76d655b820ba84692eeb3861f2e43329a..0ffb9456dddf08b75755990e3e56150881624019 100644 --- a/Solver/luaFunction.h +++ b/Solver/luaFunction.h @@ -14,7 +14,7 @@ class functionLua : public function { int _nbCol; class data; public: - functionLua (int nbCol, std::string &luaFunctionName, std::vector<std::string> &dependenciesName, lua_State *L); + functionLua (int nbCol, std::string luaFunctionName, std::vector<std::string> dependenciesName, lua_State *L); dataCacheDouble *newDataCache(dataCacheMap *m); static void registerBindings(binding *b);