Commit 8ddf512a authored by Anton Gladky's avatar Anton Gladky
Browse files

Remove some 'using namespace boost'

All of them should bve removed from the source
parent b829e20c
......@@ -36,7 +36,7 @@ class EnergyTracker: public Serializable{
}
Real getItem_py(const std::string& name){
int id=-1; findId(name,id,false,false);
if (id<0) {PyErr_SetString(PyExc_KeyError,("Unknown energy name '"+name+"'.").c_str()); python::throw_error_already_set(); }
if (id<0) {PyErr_SetString(PyExc_KeyError,("Unknown energy name '"+name+"'.").c_str()); py::throw_error_already_set(); }
return energies.get(id);
}
void setItem_py(const std::string& name, Real val){
......
......@@ -14,7 +14,7 @@
void Serializable::pyRegisterClass(boost::python::object _scope) {
checkPyClassRegistersItself("Serializable");
boost::python::scope thisScope(_scope);
python::class_<Serializable, shared_ptr<Serializable>, noncopyable >("Serializable")
boost::python::class_<Serializable, shared_ptr<Serializable>, noncopyable >("Serializable")
.def("__str__",&Serializable::pyStr).def("__repr__",&Serializable::pyStr)
.def("dict",&Serializable::pyDict,"Return dictionary of attributes.")
.def("updateAttrs",&Serializable::pyUpdateAttrs,"Update object attributes from given dictionary")
......@@ -25,7 +25,7 @@ void Serializable::pyRegisterClass(boost::python::object _scope) {
.add_property("__getstate_manages_dict__",&Serializable::getClassName,"just define the attr, return some bogus data")
#endif
// constructor with dictionary of attributes
.def("__init__",python::raw_constructor(Serializable_ctor_kwAttrs<Serializable>))
.def("__init__",boost::python::raw_constructor(Serializable_ctor_kwAttrs<Serializable>))
// comparison operators
.def(boost::python::self == boost::python::self)
.def(boost::python::self != boost::python::self)
......@@ -36,26 +36,13 @@ void Serializable::checkPyClassRegistersItself(const std::string& thisClassName)
if(getClassName()!=thisClassName) throw std::logic_error(("Class "+getClassName()+" does not register with YADE_CLASS_BASE_DOC_ATTR*, would not be accessible from python.").c_str());
}
#if 1
void Serializable::pyUpdateAttrs(const python::dict& d){
python::list l=d.items(); size_t ll=python::len(l); if(ll==0) return;
for(size_t i=0; i<ll; i++){
python::tuple t=python::extract<python::tuple>(l[i]);
string key=python::extract<string>(t[0]);
pySetAttr(key,t[1]);
}
callPostLoad();
}
#else
void Serializable::pyUpdateAttrs(const shared_ptr<Serializable>& instance, const python::dict& d){
python::list l=d.items(); size_t ll=python::len(l); if(ll==0) return;
python::object self(instance);
for(size_t i=0; i<ll; i++){
python::tuple t=python::extract<python::tuple>(l[i]);
string key=python::extract<string>(t[0]);
self.attr(key.c_str())=t[1];
}
}
#endif
void Serializable::pyUpdateAttrs(const boost::python::dict& d){
boost::python::list l=d.items(); size_t ll=boost::python::len(l); if(ll==0) return;
for(size_t i=0; i<ll; i++){
boost::python::tuple t=boost::python::extract<python::tuple>(l[i]);
string key=python::extract<string>(t[0]);
pySetAttr(key,t[1]);
}
callPostLoad();
}
......@@ -36,10 +36,7 @@
#include<yade/lib/base/Math.hpp>
using namespace boost;
using namespace std;
// empty functions for ADL
//namespace{
template<typename T> void preLoad(T&){}; template<typename T> void postLoad(T& obj){ /* cerr<<"Generic no-op postLoad("<<typeid(T).name()<<"&) called for "<<obj.getClassName()<<std::endl; */ }
......@@ -185,7 +182,7 @@ void make_setter_postLoad(C& instance, const T& val){ instance.*A=val; /* cerr<<
_REGISTER_ATTRIBUTES_DEPREC(thisClass,baseClass,attrs,deprec) \
REGISTER_CLASS_AND_BASE(thisClass,baseClass) \
/* accessors for deprecated attributes, with warnings */ BOOST_PP_SEQ_FOR_EACH(_ACCESS_DEPREC,thisClass,deprec) \
/* python class registration */ virtual void pyRegisterClass(python::object _scope) { checkPyClassRegistersItself(#thisClass); boost::python::scope thisScope(_scope); YADE_SET_DOCSTRING_OPTS; boost::python::class_<thisClass,shared_ptr<thisClass>,boost::python::bases<baseClass>,boost::noncopyable> _classObj(#thisClass,docString); _classObj.def("__init__",python::raw_constructor(Serializable_ctor_kwAttrs<thisClass>)); BOOST_PP_SEQ_FOR_EACH(_PYATTR_DEF,thisClass,attrs); (void) _classObj BOOST_PP_SEQ_FOR_EACH(_PYATTR_DEPREC_DEF,thisClass,deprec); (void) _classObj extras ; }
/* python class registration */ virtual void pyRegisterClass(boost::python::object _scope) { checkPyClassRegistersItself(#thisClass); boost::python::scope thisScope(_scope); YADE_SET_DOCSTRING_OPTS; boost::python::class_<thisClass,shared_ptr<thisClass>,boost::python::bases<baseClass>,boost::noncopyable> _classObj(#thisClass,docString); _classObj.def("__init__",boost::python::raw_constructor(Serializable_ctor_kwAttrs<thisClass>)); BOOST_PP_SEQ_FOR_EACH(_PYATTR_DEF,thisClass,attrs); (void) _classObj BOOST_PP_SEQ_FOR_EACH(_PYATTR_DEPREC_DEF,thisClass,deprec); (void) _classObj extras ; }
// use later: void must_use_both_YADE_CLASS_BASE_DOC_ATTRS_and_YADE_PLUGIN();
// #define YADE_CLASS_BASE_DOC_ATTRS_PY(thisClass,baseClass,docString,attrs,extras) YADE_CLASS_BASE_DOC_ATTRS_DEPREC_PY(thisClass,baseClass,docString,attrs,,extras)
......@@ -208,8 +205,8 @@ void make_setter_postLoad(C& instance, const T& val){ instance.*A=val; /* cerr<<
#define _STATCLASS_PY_REGISTER_CLASS(thisClass,baseClass,docString,attrs)\
virtual void pyRegisterClass(python::object _scope) { checkPyClassRegistersItself(#thisClass); initSetStaticAttributesValue(); boost::python::scope thisScope(_scope); YADE_SET_DOCSTRING_OPTS; \
boost::python::class_<thisClass,shared_ptr<thisClass>,boost::python::bases<baseClass>,boost::noncopyable> _classObj(#thisClass,docString "\n\n" BOOST_PP_SEQ_FOR_EACH(_STATATTR_MAKE_DOC,thisClass,attrs) ); _classObj.def("__init__",python::raw_constructor(Serializable_ctor_kwAttrs<thisClass>)); \
virtual void pyRegisterClass(boost::python::object _scope) { checkPyClassRegistersItself(#thisClass); initSetStaticAttributesValue(); boost::python::scope thisScope(_scope); YADE_SET_DOCSTRING_OPTS; \
boost::python::class_<thisClass,shared_ptr<thisClass>,boost::python::bases<baseClass>,boost::noncopyable> _classObj(#thisClass,docString "\n\n" BOOST_PP_SEQ_FOR_EACH(_STATATTR_MAKE_DOC,thisClass,attrs) ); _classObj.def("__init__",boost::python::raw_constructor(Serializable_ctor_kwAttrs<thisClass>)); \
BOOST_PP_SEQ_FOR_EACH(_STATATTR_PY,thisClass,attrs); \
}
......@@ -217,7 +214,7 @@ void make_setter_postLoad(C& instance, const T& val){ instance.*A=val; /* cerr<<
_REGISTER_ATTRIBUTES_DEPREC(thisClass,baseClass,attrs,deprec) \
REGISTER_CLASS_AND_BASE(pyClassName,baseClass) \
/* accessors for deprecated attributes, with warnings */ BOOST_PP_SEQ_FOR_EACH(_ACCESS_DEPREC,thisClass,deprec) \
/* python class registration */ virtual void pyRegisterClass(python::object _scope) { checkPyClassRegistersItself(#pyClassName); boost::python::scope thisScope(_scope); YADE_SET_DOCSTRING_OPTS; boost::python::class_<thisClass,shared_ptr<thisClass>,boost::python::bases<baseClass>,boost::noncopyable> _classObj(#pyClassName,docString); _classObj.def("__init__",python::raw_constructor(Serializable_ctor_kwAttrs<thisClass>)); BOOST_PP_SEQ_FOR_EACH(_PYATTR_DEF,thisClass,attrs); (void) _classObj BOOST_PP_SEQ_FOR_EACH(_PYATTR_DEPREC_DEF,thisClass,deprec); (void) _classObj extras ; }
/* python class registration */ virtual void pyRegisterClass(boost::python::object _scope) { checkPyClassRegistersItself(#pyClassName); boost::python::scope thisScope(_scope); YADE_SET_DOCSTRING_OPTS; boost::python::class_<thisClass,shared_ptr<thisClass>,boost::python::bases<baseClass>,boost::noncopyable> _classObj(#pyClassName,docString); _classObj.def("__init__",boost::python::raw_constructor(Serializable_ctor_kwAttrs<thisClass>)); BOOST_PP_SEQ_FOR_EACH(_PYATTR_DEF,thisClass,attrs); (void) _classObj BOOST_PP_SEQ_FOR_EACH(_PYATTR_DEPREC_DEF,thisClass,deprec); (void) _classObj extras ; }
/********************** USER MACROS START HERE ********************/
......@@ -293,7 +290,7 @@ class Serializable: public Factorable {
virtual void pyHandleCustomCtorArgs(boost::python::tuple& args, boost::python::dict& kw){ return; }
//! string representation of this object
std::string pyStr() { return "<"+getClassName()+" instance at "+lexical_cast<string>(this)+">"; }
std::string pyStr() { return "<"+getClassName()+" instance at "+boost::lexical_cast<string>(this)+">"; }
REGISTER_CLASS_NAME(Serializable);
REGISTER_BASE_CLASS_NAME(Factorable);
......@@ -302,11 +299,11 @@ class Serializable: public Factorable {
// helper functions
template <typename T>
shared_ptr<T> Serializable_ctor_kwAttrs(python::tuple& t, python::dict& d){
shared_ptr<T> Serializable_ctor_kwAttrs(boost::python::tuple& t, boost::python::dict& d){
shared_ptr<T> instance;
instance=shared_ptr<T>(new T);
instance->pyHandleCustomCtorArgs(t,d); // can change t and d in-place
if(python::len(t)>0) throw runtime_error("Zero (not "+lexical_cast<string>(python::len(t))+") non-keyword constructor arguments required [in Serializable_ctor_kwAttrs; Serializable::pyHandleCustomCtorArgs might had changed it after your call].");
if(python::len(d)>0){ instance->pyUpdateAttrs(d); instance->callPostLoad(); }
if(boost::python::len(t)>0) throw runtime_error("Zero (not "+boost::lexical_cast<string>(boost::python::len(t))+") non-keyword constructor arguments required [in Serializable_ctor_kwAttrs; Serializable::pyHandleCustomCtorArgs might had changed it after your call].");
if(boost::python::len(d)>0){ instance->pyUpdateAttrs(d); instance->callPostLoad(); }
return instance;
}
......@@ -16,12 +16,18 @@
#include<boost/version.hpp>
#include<boost/math/distributions/normal.hpp>
#ifndef __GXX_EXPERIMENTAL_CXX0X__
# include<boost/shared_ptr.hpp>
using boost::shared_ptr;
#else
# include<memory>
using std::shared_ptr;
#endif
#ifndef FOREACH
#define FOREACH BOOST_FOREACH
#endif
using namespace std;
using namespace boost;
#include<yade/lib/base/Math.hpp>
......@@ -46,7 +52,7 @@ struct GridContainer{
Vector2i xy2cell(Vector2r xy, bool* inGrid=NULL) const {
Vector2i ret((int)(floor((xy[0]-lo[0])/cellSizes[0])),(int)(floor((xy[1]-lo[1])/cellSizes[1])));
if(ret[0]<0 || ret[0]>=nCells[0] || ret[1]<0 || ret[1]>=nCells[1]){
if(inGrid) *inGrid=false; else throw std::invalid_argument("Cell coordinates outside grid (xy="+lexical_cast<string>(xy[0])+","+lexical_cast<string>(xy[1])+", computed cell coordinates "+lexical_cast<string>(ret[0])+","+lexical_cast<string>(ret[1])+").");
if(inGrid) *inGrid=false; else throw std::invalid_argument("Cell coordinates outside grid (xy="+boost::lexical_cast<string>(xy[0])+","+boost::lexical_cast<string>(xy[1])+", computed cell coordinates "+boost::lexical_cast<string>(ret[0])+","+boost::lexical_cast<string>(ret[1])+").");
} else {if(inGrid) *inGrid=true;}
return ret;
}
......@@ -170,13 +176,13 @@ struct SGDA_Scalar2d: public WeightedAverage<Scalar2d,Real> {
* */
class pyGaussAverage{
//struct Scalar2d{Vector2r pos; Real val;};
Vector2r tuple2vec2r(const python::tuple& t){return Vector2r(python::extract<Real>(t[0])(),python::extract<Real>(t[1])());}
Vector2i tuple2vec2i(const python::tuple& t){return Vector2i(python::extract<int>(t[0])(),python::extract<int>(t[1])());}
Vector2r tuple2vec2r(const boost::python::tuple& t){return Vector2r(boost::python::extract<Real>(t[0])(),boost::python::extract<Real>(t[1])());}
Vector2i tuple2vec2i(const boost::python::tuple& t){return Vector2i(boost::python::extract<int>(t[0])(),boost::python::extract<int>(t[1])());}
shared_ptr<SGDA_Scalar2d> sgda;
struct Poly2d{vector<Vector2r> vertices; bool inclusive;};
vector<Poly2d> clips;
public:
pyGaussAverage(python::tuple lo, python::tuple hi, python::tuple nCells, Real stDev, Real relThreshold=3.){
pyGaussAverage(boost::python::tuple lo, boost::python::tuple hi, boost::python::tuple nCells, Real stDev, Real relThreshold=3.){
shared_ptr<GridContainer<Scalar2d> > g(new GridContainer<Scalar2d>(tuple2vec2r(lo),tuple2vec2r(hi),tuple2vec2i(nCells)));
sgda=shared_ptr<SGDA_Scalar2d>(new SGDA_Scalar2d(g,stDev));
sgda->relThreshold=relThreshold;
......@@ -189,36 +195,36 @@ class pyGaussAverage{
}
return false;
}
bool addPt(Real val, python::tuple pos){Scalar2d d; d.pos=tuple2vec2r(pos); if(ptIsClipped(d.pos)) return false; d.val=val; sgda->grid->add(d,d.pos); return true; }
bool addPt(Real val, boost::python::tuple pos){Scalar2d d; d.pos=tuple2vec2r(pos); if(ptIsClipped(d.pos)) return false; d.val=val; sgda->grid->add(d,d.pos); return true; }
Real avg(Vector2r pt){ if(ptIsClipped(pt)) return std::numeric_limits<Real>::quiet_NaN(); return sgda->computeAverage(pt);}
Real avgPerUnitArea(Vector2r pt){ if(ptIsClipped(pt)) return std::numeric_limits<Real>::quiet_NaN(); return sgda->computeAvgPerUnitArea(pt); }
Real stDev_get(){return sgda->stDev;} void stDev_set(Real s){sgda->stDev=s;}
Real relThreshold_get(){return sgda->relThreshold;} void relThreshold_set(Real rt){sgda->relThreshold=rt;}
python::tuple aabb_get(){return python::make_tuple(sgda->grid->getLo(),sgda->grid->getHi());}
python::list clips_get(){
python::list ret;
boost::python::tuple aabb_get(){return boost::python::make_tuple(sgda->grid->getLo(),sgda->grid->getHi());}
boost::python::list clips_get(){
boost::python::list ret;
FOREACH(const Poly2d& poly, clips){
python::list vertices;
FOREACH(const Vector2r& v, poly.vertices) vertices.append(python::make_tuple(v[0],v[1]));
ret.append(python::make_tuple(vertices,poly.inclusive));
boost::python::list vertices;
FOREACH(const Vector2r& v, poly.vertices) vertices.append(boost::python::make_tuple(v[0],v[1]));
ret.append(boost::python::make_tuple(vertices,poly.inclusive));
}
return ret;
}
void clips_set(python::list l){
void clips_set(boost::python::list l){
/* [ ( [(x1,y1),(x2,y2),…], true), … ] */
clips.clear();
for(int i=0; i<python::len(l); i++){
python::tuple polyDesc=python::extract<python::tuple>(l[i])();
python::list coords=python::extract<python::list>(polyDesc[0]);
Poly2d poly; poly.inclusive=python::extract<bool>(polyDesc[1]);
for(int j=0; j<python::len(coords); j++){
poly.vertices.push_back(tuple2vec2r(python::extract<python::tuple>(coords[j])));
for(int i=0; i<boost::python::len(l); i++){
boost::python::tuple polyDesc=boost::python::extract<boost::python::tuple>(l[i])();
boost::python::list coords=boost::python::extract<boost::python::list>(polyDesc[0]);
Poly2d poly; poly.inclusive=boost::python::extract<bool>(polyDesc[1]);
for(int j=0; j<boost::python::len(coords); j++){
poly.vertices.push_back(tuple2vec2r(boost::python::extract<boost::python::tuple>(coords[j])));
}
clips.push_back(poly);
}
}
python::tuple data_get(){
python::list x,y,val;
boost::python::tuple data_get(){
boost::python::list x,y,val;
const Vector2i& dim=sgda->grid->getSize();
for(int i=0; i<dim[0]; i++){
for(int j=0; j<dim[1]; j++){
......@@ -227,7 +233,7 @@ class pyGaussAverage{
}
}
}
return python::make_tuple(x,y,val);
return boost::python::make_tuple(x,y,val);
}
Vector2i nCells_get(){ return sgda->grid->getSize(); }
int cellNum(const Vector2i& cell){ return sgda->grid->grid[cell[0]][cell[1]].size(); }
......
......@@ -17,7 +17,7 @@ bool pyGaussAverage::pointInsidePolygon(const Vector2r& pt, const vector<Vector2
BOOST_PYTHON_MODULE(WeightedAverage2d)
{
boost::python::scope().attr("__doc__")="Smoothing (2d gauss-weighted average) for postprocessing scalars in 2d.";
boost::python::class_<pyGaussAverage>("GaussAverage",python::init<python::tuple,python::tuple,python::tuple,Real,python::optional<Real> >(python::args("min","max","nCells","stDev","relThreshold"),"Create empty container for data, which can be added using add and later retrieved using avg."))
boost::python::class_<pyGaussAverage>("GaussAverage",boost::python::init<boost::python::tuple,boost::python::tuple,boost::python::tuple,Real,boost::python::optional<Real> >(boost::python::args("min","max","nCells","stDev","relThreshold"),"Create empty container for data, which can be added using add and later retrieved using avg."))
.def("add",&pyGaussAverage::addPt)
.def("avg",&pyGaussAverage::avg)
.def("avgPerUnitArea",&pyGaussAverage::avgPerUnitArea)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment