File:  [CENS] / python / pyGiNaC / wrappers4 / constant.cpp
Revision 1.4: download - view: text, annotated - select for diffs - revision graph
Thu Nov 22 14:50:04 2001 UTC (16 years ago) by pearu
Branches: MAIN
CVS tags: HEAD
See NEWS.txt for changes. There too many changes to list them here

/*
  This file is part of the PyGiNaC package.
  http://cens.ioc.ee/projects/pyginac/

  $Revision: 1.4 $
  $Id: constant.cpp,v 1.4 2001-11-22 14:50:04 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.
*/

#ifndef PYGINAC_CONSTANT_CPP
#define PYGINAC_CONSTANT_CPP
/* prototypes */

GiNaC::ex constant(const std::string &, PyObject *, const std::string &);
PyObject * constant_get_init(const GiNaC::constant & c);

BOOST_PYTHON_BEGIN_CONVERSION_NAMESPACE

const GiNaC::constant & from_python(PyObject* o, py::type<const GiNaC::constant &>);

BOOST_PYTHON_END_CONVERSION_NAMESPACE
#else
#ifndef PYGINAC_CONSTANT_CPP_1
#define PYGINAC_CONSTANT_CPP_1
/* definitions */

this_module.def(constant,"constant");
ex_class.def(constant_get_init,"constant_get_init");
this_module.def(pyginac::constant_cache_erase,"constant_cache_erase");
this_module.add(BOOST_PYTHON_CONVERSION::to_python(GiNaC::ex(GiNaC::Pi)), "Pi");
this_module.add(BOOST_PYTHON_CONVERSION::to_python(GiNaC::ex(GiNaC::Catalan)), "Catalan");
this_module.add(BOOST_PYTHON_CONVERSION::to_python(GiNaC::ex(GiNaC::Euler)), "Euler");

#else
/* implementation */

GiNaC::ex constant(const std::string & name,
		   PyObject * init,
		   const std::string & texname) {
  GiNaC::constant c(name, pyginac::constant_dummy, texname);
  pyginac::constant_cache_set(c.gethash(), init);
  return c;
}

PyObject * constant_get_init(const GiNaC::constant & c) {
  PyObject *ret = pyginac::constant_cache_get(c.gethash());
  if (ret==NULL)
    return BOOST_PYTHON_CONVERSION::to_python(GiNaC::ex(c));
  Py_INCREF(ret);
  return ret;
}

BOOST_PYTHON_BEGIN_CONVERSION_NAMESPACE

const GiNaC::constant & from_python(PyObject* o, py::type<const GiNaC::constant &>) {
  if (PYOBJ_IS_EX(o)) {
    GiNaC::ex & e = pyobj_get_ex_nonconst(o);
    if (GiNaC::is_a<GiNaC::constant>(e))
      return GiNaC::ex_to<GiNaC::constant>(e);
  }
  PyErr_SetString(PyExc_TypeError,(std::string("from_python(constant) expected constant but got ")+PyString_AsString(PyObject_Repr(PyObject_Type(o)))).c_str());
  throw py::error_already_set();
}

BOOST_PYTHON_END_CONVERSION_NAMESPACE

#endif
#endif

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