mirror of
https://github.com/boostorg/python.git
synced 2026-01-23 05:42:30 +00:00
initial development of NumericDispatcher
[SVN r8022]
This commit is contained in:
52
num_demo.cpp
Normal file
52
num_demo.cpp
Normal file
@@ -0,0 +1,52 @@
|
||||
#include "numeric.h"
|
||||
#include "class_wrapper.h"
|
||||
|
||||
struct Int
|
||||
{
|
||||
Int(int i) : i_(i) {}
|
||||
|
||||
Int operator+(int const & r) const { return Int(i_ + r); }
|
||||
Int operator-(int const & r) const { return Int(i_ - r); }
|
||||
Int operator+(Int const & r) const { return Int(i_ + r.i_); }
|
||||
Int operator-(Int const & r) const { return Int(i_ - r.i_); }
|
||||
|
||||
int i() const { return i_; }
|
||||
|
||||
int i_;
|
||||
};
|
||||
|
||||
Int operator+(int const & i, Int const & j) { return Int(i+j.i()); }
|
||||
Int operator-(int const & i, Int const & j) { return Int(i-j.i()); }
|
||||
|
||||
ostream & operator<<(ostream & o, Int const & i)
|
||||
{
|
||||
o << i.i();
|
||||
return o;
|
||||
}
|
||||
|
||||
extern "C"
|
||||
void initnum()
|
||||
{
|
||||
try
|
||||
{
|
||||
py::Module num("num");
|
||||
py::ClassWrapper<Int> int_class(num, "Int");
|
||||
int_class.def(py::Constructor<int>());
|
||||
int_class.def(py::Constructor<Int>());
|
||||
int_class.def(py::coerce_wrapped, "__coerce__");
|
||||
int_class.def(&Int::i, "i");
|
||||
|
||||
num.def_numeric(int_class.get_extension_class(),
|
||||
int_class.get_extension_class(), "__add__");
|
||||
num.def_numeric(int_class.get_extension_class(),
|
||||
int_class.get_extension_class(), "__sub__");
|
||||
num.def_numeric(int_class.get_extension_class(), &PyInt_Type, "__add__");
|
||||
num.def_numeric(&PyInt_Type, int_class.get_extension_class(), "__add__");
|
||||
|
||||
}
|
||||
catch(...)
|
||||
{
|
||||
py::handle_exception();
|
||||
} // Need a way to report other errors here
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user