File:  [CENS] / python / pyGiNaC / wrappers / exlist.py
Revision 1.1: download - view: text, annotated - select for diffs - revision graph
Sun Apr 8 20:52:50 2001 UTC (16 years, 7 months ago) by pearu
Branches: MAIN
CVS tags: HEAD
Now str/repr functions are independent of GiNaC.print functions. Reorganized wrappers. Minor version 1.

# This file is part of the PyGiNaC package.
# http://cens.ioc.ee/projects/pyginac/
#
# $Revision: 1.1 $
# $Id: exlist.py,v 1.1 2001-04-08 20:52:50 pearu Exp $
# Pearu Peterson
#
# 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.
#

depends = []
uses = ['ex']

wrapperclass = '''
class exlist_w : public GiNaC::exlist {
  PyObject * self;
public:
  exlist_w(python::ref);

  exlist_w(PyObject * self_): GiNaC::exlist(), self(self_) {
    DEBUG_C("exlist_w::exlist_w()");
  }
  exlist_w(PyObject* self_, const std::size_t n)
  : GiNaC::exlist(n), self(self_) {
    DEBUG_C("exlist_w::exlist_w(size_t)");
  }
  exlist_w(PyObject* self_, const GiNaC::exlist & obj)
  : GiNaC::exlist(obj), self(self_) {
    DEBUG_C("exlist_w::exlist_w(GiNaC::exlist)");
  }
  exlist_w(PyObject * self_, python::ref obj)
  : GiNaC::exlist(exlist_w(obj)), self(self_) {
    DEBUG_C("exlist_w::exlist_w(ref)");
  }
  ~exlist_w() {
    DEBUG_C("exlist_w::~exlist_w()");
  }

  static std::string torepr(exlist_w &);
  GiNaC::ex 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::exlist& 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<exlist_w> exlist_w_class(this_module, "_exlist_w");
python::class_builder<GiNaC::exlist, exlist_w> exlist_class(this_module, "exlist");
exlist_class.declare_base(exlist_w_class);
exlist_py_class = python::as_object(exlist_class.get_extension_class());
'''

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

defs = '''
exlist_class.def(&exlist_w::torepr, "__repr__");
exlist_class.def(&exlist_w::size, "__len__");
exlist_class.def(&exlist_w::getitem, "__getitem__");
exlist_class.def(&exlist_w::setitem, "__setitem__");
exlist_class.def(&exlist_w::delitem, "__delitem__");
'''

implementation = '''
exlist_w::exlist_w(python::ref obj)
: GiNaC::exlist(), self() {
  PyObject * o = obj.get();
  DEBUG_C("exlist_w::exlist_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(ex_w(python::ref(PySequence_GetItem(o,i))));
  } else {
    PyErr_SetString(PyExc_TypeError, "exlist() argument must be a sequence");
    throw python::error_already_set();
  }
}

std::string exlist_w::torepr(exlist_w & e) {
  std::ostrstream s;
  GiNaC::exlist::iterator viter = e.begin();
  std::size_t l = e.size();
  DEBUG_S("torepr(exlist)");
  s << "exlist([";
  if (l>0) {
    GiNaC::python_repr(s, *viter);
    ++viter;
  }
  for(unsigned int i = 1; i < l; ++i, ++viter) {
    s << ", ";
    GiNaC::python_repr(s, *viter);
  }
  s << "])" << std::ends;
  return s.str();
}


GiNaC::ex exlist_w::getitem (const std::size_t key) {
    GiNaC::exlist::const_iterator viter = (*this).begin();
    exlist_w::throw_index_error_if(*this, key);
    for(unsigned int i = 0; i < key; ++i,++viter);
    return *viter;
}

void exlist_w::setitem(const std::size_t key, python::ref obj) {
    GiNaC::exlist::iterator viter = (*this).begin();
    exlist_w::throw_index_error_if(*this, key);
    for(unsigned int i = 0; i < key; ++i,++viter);
    (*this).insert(viter,ex_w(obj));
    viter = (*this).begin();
    for(unsigned int i = 0; i < key; ++i,++viter);
    viter++;
    (*this).erase(viter);
}

void exlist_w::delitem(const std::size_t key) {
    GiNaC::exlist::iterator viter = (*this).begin();
    exlist_w::throw_index_error_if(*this, key);
    for(unsigned int i = 0; i < key; ++i,++viter);
    (*this).erase(viter);
}
'''

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