diff --git a/build/Makefile.gcc b/build/Makefile.gcc index 36412e3e..1a79c126 100644 --- a/build/Makefile.gcc +++ b/build/Makefile.gcc @@ -63,6 +63,7 @@ $(BPL_EXA)/getting_started3.cpp \ $(BPL_EXA)/getting_started4.cpp \ $(BPL_EXA)/getting_started5.cpp \ $(BPL_EXA)/passing_char.cpp \ +$(BPL_EXA)/cmplx.cpp \ $(BPL_EXA)/ivect.cpp \ $(BPL_EXA)/ivect.h \ $(BPL_EXA)/noncopyable_export.cpp \ @@ -75,6 +76,7 @@ $(BPL_EXA)/test_getting_started3.py \ $(BPL_EXA)/test_getting_started4.py \ $(BPL_EXA)/test_getting_started5.py \ $(BPL_EXA)/tst_passing_char.py \ +$(BPL_EXA)/tst_cmplx.py \ $(BPL_EXA)/tst_dvect.py \ $(BPL_EXA)/tst_ivect.py \ $(BPL_EXA)/tst_noncopyable.py @@ -88,7 +90,7 @@ OBJ = classes.o conversions.o extension_class.o functions.o \ all: libbpl.a test.so abstract.so \ getting_started1.so getting_started2.so getting_started3.so \ getting_started4.so getting_started5.so \ - passing_char.so \ + passing_char.so cmplx.so \ noncopyable_export.so noncopyable_import.so \ ivect.so dvect.so @@ -121,7 +123,7 @@ libbpl.a: $(OBJ) ar r libbpl.a $(OBJ) test.so: $(OBJ) comprehensive.o - $(LD) $(LDOPTS) $(OBJ) comprehensive.o -o test.so + $(LD) $(LDOPTS) $(OBJ) comprehensive.o -o test.so -lm abstract.so: $(OBJ) abstract.o $(LD) $(LDOPTS) $(OBJ) abstract.o -o abstract.so @@ -144,6 +146,9 @@ getting_started5.so: $(OBJ) getting_started5.o passing_char.so: $(OBJ) passing_char.o $(LD) $(LDOPTS) $(OBJ) passing_char.o -o passing_char.so +cmplx.so: $(OBJ) cmplx.o + $(LD) $(LDOPTS) $(OBJ) cmplx.o -o cmplx.so -lm + noncopyable_export.so: $(OBJ) noncopyable_export.o $(LD) $(LDOPTS) $(OBJ) $(HIDDEN) noncopyable_export.o -o noncopyable_export.so @@ -180,6 +185,7 @@ clean: rm -f getting_started4.o getting_started4.so rm -f getting_started5.o getting_started5.so rm -f passing_char.o passing_char.so + rm -f cmplx.o cmplx.so rm -f noncopyable_export.o noncopyable_export.so rm -f noncopyable_import.o noncopyable_import.so rm -f ivect.o ivect.so diff --git a/build/Makefile.tru64 b/build/Makefile.tru64 index 1791c528..87b45bae 100644 --- a/build/Makefile.tru64 +++ b/build/Makefile.tru64 @@ -66,6 +66,7 @@ $(BPL_EXA)/getting_started3.cpp \ $(BPL_EXA)/getting_started4.cpp \ $(BPL_EXA)/getting_started5.cpp \ $(BPL_EXA)/passing_char.cpp \ +$(BPL_EXA)/cmplx.cpp \ $(BPL_EXA)/ivect.cpp \ $(BPL_EXA)/ivect.h \ $(BPL_EXA)/noncopyable_export.cpp \ @@ -78,6 +79,7 @@ $(BPL_EXA)/test_getting_started3.py \ $(BPL_EXA)/test_getting_started4.py \ $(BPL_EXA)/test_getting_started5.py \ $(BPL_EXA)/tst_passing_char.py \ +$(BPL_EXA)/tst_cmplx.py \ $(BPL_EXA)/tst_dvect.py \ $(BPL_EXA)/tst_ivect.py \ $(BPL_EXA)/tst_noncopyable.py @@ -91,7 +93,7 @@ OBJ = classes.o conversions.o extension_class.o functions.o \ all: libbpl.a test.so abstract.so \ getting_started1.so getting_started2.so getting_started3.so \ getting_started4.so getting_started5.so \ - passing_char.so \ + passing_char.so cmplx.so \ noncopyable_export.so noncopyable_import.so \ ivect.so dvect.so @@ -149,6 +151,9 @@ getting_started5.so: $(OBJ) getting_started5.o passing_char.so: $(OBJ) passing_char.o $(LD) $(LDOPTS) $(OBJ) passing_char.o -o passing_char.so +cmplx.so: $(OBJ) cmplx.o + $(LD) $(LDOPTS) $(OBJ) cmplx.o -o cmplx.so -lm + noncopyable_export.so: $(OBJ) noncopyable_export.o $(LD) $(LDOPTS) $(OBJ) $(HIDDEN) noncopyable_export.o -o noncopyable_export.so @@ -185,6 +190,7 @@ clean: rm -f getting_started4.o getting_started4.so rm -f getting_started5.o getting_started5.so rm -f passing_char.o passing_char.so + rm -f cmplx.o cmplx.so rm -f noncopyable_export.o noncopyable_export.so rm -f noncopyable_import.o noncopyable_import.so rm -f ivect.o ivect.so diff --git a/example/cmplx.cpp b/example/cmplx.cpp new file mode 100644 index 00000000..b3f912c5 --- /dev/null +++ b/example/cmplx.cpp @@ -0,0 +1,42 @@ +#include +#include + +namespace { // Avoid cluttering the global namespace. + + std::complex dpolar(double rho, double theta) { + return std::polar(rho, theta); + } + double dreal(const std::complex& c) { return c.real(); } + double dimag(std::complex c) { return c.imag(); } + + std::complex fpolar(float rho, float theta) { + return std::polar(rho, theta); + } + double freal(const std::complex& c) { return c.real(); } + double fimag(std::complex c) { return c.imag(); } +} + +#include +namespace python = boost::python; + +extern "C" +DL_EXPORT(void) +initcmplx() +{ + try + { + // Create an object representing this extension module. + python::module_builder this_module("cmplx"); + + this_module.def(dpolar, "dpolar"); + this_module.def(dreal, "dreal"); + this_module.def(dimag, "dimag"); + this_module.def(fpolar, "fpolar"); + this_module.def(freal, "freal"); + this_module.def(fimag, "fimag"); + } + catch(...) + { + python::handle_exception(); // Deal with the exception for Python + } +} diff --git a/example/tst_cmplx.py b/example/tst_cmplx.py new file mode 100644 index 00000000..2448a1e1 --- /dev/null +++ b/example/tst_cmplx.py @@ -0,0 +1,20 @@ +import cmplx +c = cmplx.dpolar(1, 1) +print c +print cmplx.dreal(c) +print cmplx.dimag(c) + +c = cmplx.fpolar(1, 1) +print c +print cmplx.freal(c) +print cmplx.fimag(c) + +print cmplx.dreal(c) +print cmplx.dimag(c) + +try: + cmplx.freal("") +except TypeError: + pass +else: + raise SystemError