2
0
mirror of https://github.com/boostorg/python.git synced 2026-01-23 17:52:17 +00:00

initial development of NumericDispatcher

[SVN r8022]
This commit is contained in:
Ullrich Köthe
2000-10-20 08:48:01 +00:00
parent 6b0144ef31
commit 90fca10190
6 changed files with 407 additions and 1 deletions

67
numeric.h Normal file
View File

@@ -0,0 +1,67 @@
#ifndef PYCPP_NUMERIC_H
#define PYCPP_NUMERIC_H
#include <map>
#include <utility>
#include "py.h"
#include "extclass.h"
namespace py {
template <class T, class U>
struct NumericOperators
{
static PyObject * add(PyObject * l, PyObject * r)
{
return to_python(from_python(l, py::Type<T const &>()) +
from_python(r, py::Type<U const &>()));
}
static PyObject * subtract(PyObject * l, PyObject * r)
{
return to_python(from_python(l, py::Type<T const &>()) -
from_python(r, py::Type<U const &>()));
}
};
typedef std::pair<PyTypeObject *, PyTypeObject *> TypePair;
bool operator<(TypePair const & l, TypePair const & r);
struct NumericDispatcher
: public PyObject
{
typedef PyObject * (*NumericFunction)(PyObject *, PyObject *);
typedef std::map<TypePair, NumericFunction> FunctionRepository;
static FunctionRepository add_functions;
static FunctionRepository sub_functions;
static PyTypeObject type_object;
static PyNumberMethods number_methods;
friend class Module;
// store this function in NumericDispatcher's 'nb_add' slot
static PyObject * add(PyObject * l, PyObject * r);
static PyObject * sub(PyObject * l, PyObject * r);
static void coerce(PyObject * & l, PyObject * & r);
static void init_type_object();
static void free_type_object();
NumericDispatcher(PyObject * o);
static void dealloc(PyObject *self);
PyObject * object;
};
py::Tuple coerce_wrapped(PyObject * l, PyObject * r);
}
inline PyObject * to_python(py::NumericDispatcher * n) { return n; }
#endif /* PYCPP_NUMERIC_H */