File:  [CENS] / python / pyGiNaC / wrappers / epvector.py
Revision 1.3: download - view: text, annotated - select for diffs - revision graph
Wed Apr 11 20:31:30 2001 UTC (16 years, 7 months ago) by pearu
Branches: MAIN
CVS tags: HEAD
Exposed lots of functions. Documented most important classes/functions. minor_version = 2

# This file is part of the PyGiNaC package.
# http://cens.ioc.ee/projects/pyginac/
#
# $Revision: 1.3 $
# $Id: epvector.py,v 1.3 2001-04-11 20:31:30 pearu Exp $
#
# Copyright 2001 Pearu Peterson all rights reserved,
# Pearu Peterson <pearu@cens.ioc.ee>
# Permission to use, modify, and distribute this software is given under the
# terms of the LGPL.  See http://www.fsf.org
#
# NO WARRANTY IS EXPRESSED OR IMPLIED.  USE AT YOUR OWN RISK.
#
# TODO:
# slice

depends = ['expair']
uses = []

#STARTPROTO
class epvector:
    pass
#ENDPROTO

wrapperclass = '''

class epvector_w : public GiNaC::epvector {
  PyObject * self;
public:
  epvector_w(python::ref);

  epvector_w(PyObject * self_): GiNaC::epvector(), self(self_) {
    DEBUG_C("epvector_w::epvector_w()");
  }
  epvector_w(PyObject* self_, const std::size_t n)
  : GiNaC::epvector(n), self(self_) {
    DEBUG_C("epvector_w::epvector_w(size_t)");
  }
  epvector_w(PyObject* self_, const GiNaC::epvector & obj)
  : GiNaC::epvector(obj), self(self_) {
    DEBUG_C("epvector_w::epvector_w(GiNaC::epvector)");
  }
  epvector_w(PyObject * self_, python::ref obj)
  : GiNaC::epvector(epvector_w(obj)), self(self_) {
    DEBUG_C("epvector_w::epvector_w(ref)");
  }
  ~epvector_w() {
    DEBUG_C("epvector_w::~epvector_w()");
  }
  std::string python_str(void) const {
    std::ostrstream os;
    GiNaC::python_str(os, *this);
    os << std::ends;
    return os.str();
  }
  std::string python_repr(void) const {
    std::ostrstream os;
    os << "epvector(";
    GiNaC::python_repr(os, *this);
    os << ")" << std::ends;
    return os.str();
  }

  GiNaC::expair getitem(const std::size_t);
  void setitem(const std::size_t, python::ref);
  void delitem(const std::size_t);

  static void throw_index_error_if(const GiNaC::epvector & v, const std::size_t key) {
    if ((key >= v.size()) || (key < 0)) {
        PyErr_SetObject(PyExc_IndexError, BOOST_PYTHON_CONVERSION::to_python(key));
        throw python::error_already_set();
    }
  }
  
};

'''
protos = '''
'''

builder = '''
python::class_builder<epvector_w> epvector_w_class(this_module, "_epvector_w");
python::class_builder<GiNaC::epvector, epvector_w> epvector_class(this_module, "epvector");
epvector_class.declare_base(epvector_w_class);
epvector_py_class = python::as_object(epvector_class.get_extension_class());
'''

constructors = '''
epvector_class.def(python::constructor<>());
epvector_class.def(python::constructor<std::size_t>());
epvector_class.def(python::constructor<const GiNaC::epvector &>());
epvector_class.def(python::constructor<python::ref>());
'''

defs = '''
epvector_class.def(&epvector_w::python_str, "__str__");
epvector_class.def(&epvector_w::python_repr, "__repr__");

epvector_class.def(&epvector_w::size, "__len__");
epvector_class.def(&epvector_w::getitem, "__getitem__");
epvector_class.def(&epvector_w::setitem, "__setitem__");
epvector_class.def(&epvector_w::delitem, "__delitem__");
'''

implementation = '''
GiNaC::epvector as_epvector_w(python::ref obj) {
  return epvector_w(obj);
}

epvector_w::epvector_w(python::ref obj)
: GiNaC::epvector() {
  PyObject * o = obj.get();
  DEBUG_C("epvector_w::epvector_w(raw:ref)");
  if (PySequence_Check(o)) {
    std::size_t l = PySequence_Length(o);
    for(std::size_t i = 0; i < l; ++i)
      this->push_back(expair_w(python::ref(PySequence_GetItem(o,i))));
  } else
    this->push_back(expair_w(python::ref(o)));
}

GiNaC::expair epvector_w::getitem (const std::size_t key) {
  DEBUG_M("epvector::getitem");
  epvector_w::throw_index_error_if(*this, key);
  return (*this)[key];
}

void epvector_w::setitem(const std::size_t key, python::ref obj) {
  GiNaC::epvector::iterator viter = (*this).begin();
  DEBUG_M("epvector::setitem");
  epvector_w::throw_index_error_if(*this, key);
  viter[key] = expair_w(obj);
}

void epvector_w::delitem(const std::size_t key) {
  GiNaC::epvector::iterator viter = (*this).begin();
  DEBUG_M("epvector::delitem");
  epvector_w::throw_index_error_if(*this, key);
  (*this).erase(&viter[key]);
}
'''

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>