From 7836556296f95b58583de032f8bb6cc9e7bb3252 Mon Sep 17 00:00:00 2001 From: Dave Abrahams Date: Wed, 18 Oct 2000 04:23:53 +0000 Subject: [PATCH] Numerics support [SVN r7988] --- subclass.h | 190 ++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 189 insertions(+), 1 deletion(-) diff --git a/subclass.h b/subclass.h index 4d745bc0..a442ca30 100644 --- a/subclass.h +++ b/subclass.h @@ -44,7 +44,32 @@ class Instance : public PythonObject // Sequence methods PyObject* get_slice(int start, int finish); void set_slice(int start, int finish, PyObject* value); - + + // Number methods + PyObject* add(PyObject* other); + PyObject* subtract(PyObject* other); + PyObject* multiply(PyObject* other); + PyObject* divide(PyObject* other); + PyObject* remainder(PyObject* other); + PyObject* divmod(PyObject* other); + PyObject* power(PyObject*, PyObject*); + PyObject* negative(); + PyObject* positive(); + PyObject* absolute(); + int nonzero(); + PyObject* invert(); + PyObject* lshift(PyObject* other); + PyObject* rshift(PyObject* other); + PyObject* do_and(PyObject* other); + PyObject* do_xor(PyObject* other); + PyObject* do_or(PyObject* other); + int coerce(PyObject**, PyObject**); + PyObject* as_int(); + PyObject* as_long(); + PyObject* as_float(); + PyObject* oct(); + PyObject* hex(); + private: // noncopyable, without the size bloat Instance(const Instance&); void operator=(const Instance&); @@ -92,6 +117,31 @@ class Class PyObject* instance_sequence_slice(PyObject*, int start, int finish) const; int instance_sequence_ass_slice(PyObject*, int start, int finish, PyObject* value) const; + private: // Implement number methods on instances + PyObject* instance_number_add(PyObject*, PyObject*) const; + PyObject* instance_number_subtract(PyObject*, PyObject*) const; + PyObject* instance_number_multiply(PyObject*, PyObject*) const; + PyObject* instance_number_divide(PyObject*, PyObject*) const; + PyObject* instance_number_remainder(PyObject*, PyObject*) const; + PyObject* instance_number_divmod(PyObject*, PyObject*) const; + PyObject* instance_number_power(PyObject*, PyObject*, PyObject*) const; + PyObject* instance_number_negative(PyObject*) const; + PyObject* instance_number_positive(PyObject*) const; + PyObject* instance_number_absolute(PyObject*) const; + int instance_number_nonzero(PyObject*) const; + PyObject* instance_number_invert(PyObject*) const; + PyObject* instance_number_lshift(PyObject*, PyObject*) const; + PyObject* instance_number_rshift(PyObject*, PyObject*) const; + PyObject* instance_number_and(PyObject*, PyObject*) const; + PyObject* instance_number_xor(PyObject*, PyObject*) const; + PyObject* instance_number_or(PyObject*, PyObject*) const; + int instance_number_coerce(PyObject*, PyObject**, PyObject**) const; + PyObject* instance_number_int(PyObject*) const; + PyObject* instance_number_long(PyObject*) const; + PyObject* instance_number_float(PyObject*) const; + PyObject* instance_number_oct(PyObject*) const; + PyObject* instance_number_hex(PyObject*) const; + private: // Miscellaneous "special" methods PyObject* instance_call(PyObject* instance, PyObject* args, PyObject* keywords) const; @@ -316,6 +366,144 @@ PyObject* Class::instance_call(PyObject* instance, PyObject* args, PyObject* return Downcast(instance)->call(args, keywords); } +template +PyObject* Class::instance_number_add(PyObject* instance, PyObject* other) const +{ + return Downcast(instance)->add(other); +} + +template +PyObject* Class::instance_number_subtract(PyObject* instance, PyObject* other) const +{ + return Downcast(instance)->subtract(other); +} + +template +PyObject* Class::instance_number_multiply(PyObject* instance, PyObject* other) const +{ + return Downcast(instance)->multiply(other); +} + +template +PyObject* Class::instance_number_divide(PyObject* instance, PyObject* other) const +{ + return Downcast(instance)->divide(other); +} + +template +PyObject* Class::instance_number_remainder(PyObject* instance, PyObject* other) const +{ + return Downcast(instance)->remainder(other); +} + +template +PyObject* Class::instance_number_divmod(PyObject* instance, PyObject* other) const +{ + return Downcast(instance)->divmod(other); +} + +template +PyObject* Class::instance_number_power(PyObject* instance, PyObject* exponent, PyObject* modulus) const +{ + return Downcast(instance)->power(exponent, modulus); +} + +template +PyObject* Class::instance_number_negative(PyObject* instance) const +{ + return Downcast(instance)->negative(); +} + +template +PyObject* Class::instance_number_positive(PyObject* instance) const +{ + return Downcast(instance)->positive(); +} + +template +PyObject* Class::instance_number_absolute(PyObject* instance) const +{ + return Downcast(instance)->absolute(); +} + +template +int Class::instance_number_nonzero(PyObject* instance) const +{ + return Downcast(instance)->nonzero(); +} + +template +PyObject* Class::instance_number_invert(PyObject* instance) const +{ + return Downcast(instance)->invert(); +} + +template +PyObject* Class::instance_number_lshift(PyObject* instance, PyObject* other) const +{ + return Downcast(instance)->lshift(other); +} + +template +PyObject* Class::instance_number_rshift(PyObject* instance, PyObject* other) const +{ + return Downcast(instance)->rshift(other); +} + +template +PyObject* Class::instance_number_and(PyObject* instance, PyObject* other) const +{ + return Downcast(instance)->do_and(other); +} + +template +PyObject* Class::instance_number_xor(PyObject* instance, PyObject* other) const +{ + return Downcast(instance)->do_xor(other); +} + +template +PyObject* Class::instance_number_or(PyObject* instance, PyObject* other) const +{ + return Downcast(instance)->do_or(other); +} + +template +int Class::instance_number_coerce(PyObject* instance, PyObject** x, PyObject** y) const +{ + return Downcast(instance)->coerce(x, y); +} + +template +PyObject* Class::instance_number_int(PyObject* instance) const +{ + return Downcast(instance)->as_int(); +} + +template +PyObject* Class::instance_number_long(PyObject* instance) const +{ + return Downcast(instance)->as_long(); +} + +template +PyObject* Class::instance_number_float(PyObject* instance) const +{ + return Downcast(instance)->as_float(); +} + +template +PyObject* Class::instance_number_oct(PyObject* instance) const +{ + return Downcast(instance)->oct(); +} + +template +PyObject* Class::instance_number_hex(PyObject* instance) const +{ + return Downcast(instance)->hex(); +} + template Dict& Class::dict() {