[omniORB] Complex numbers

Marc Eberhard m.a.eberhard at aston.ac.uk
Thu Oct 9 09:29:31 BST 2003


Dear all,

as far as I know, IDL doesn't support complex numbers natively. I have thus
defined a simple struct

        struct Complex
        {
                double re;
                double im;
        };

and a sequence of it

        typedef sequence <Complex> FieldValues;

and finally a field

        struct Field
        {
                double start;
                double end;
                FieldValues u;
        };

in my IDL file to hold the real and imaginary part. However, this obviously
means, that none of the C++ builtin operations on complex numbers will work
with this type as it is not recognised as a C++ complex number.

I've tried to overcome this by casting the C++ mapped IDL type to the native
C++ type

	typedef double flt;
	typedef std::complex<flt> cmplx;

        #define CAST_TO_CMPLX(c) (cmplx&) (c)
        #define CAST_TO_CMPLX_PTR(c) (cmplx*) ((void*) c)
        #define CAST_TO_COMPLEX(c) (Complex&) (c)
        #define CAST_TO_COMPLEX_PTR(c) (Complex*) ((void*) c)

and asserted, that the structures are actually binary compatible

        cmplx cs = cmplx(1.23432528324e+23, -5.72343241324e-17);
        Complex& cm = CAST_TO_COMPLEX(cs);
        assert((cs.real() == cm.re) && (cs.imag() == cm.im));

so that I can now access the data in my application in the following way:

        Field_var f = ... // get from somewhere 
        cmplx* u_ptr = CAST_TO_CMPLX_PTR(&f->u[0]);

        for (unsigned int i = 0; i < f->u.length(); i++, u_ptr++)
        {
             *u_ptr = ... // some function using the native C++ complex type
        }

This works, but I'm far from convinced, that it is a really good idea... 

And now my question: Are there other or better ways to link IDL defined
types and native C++ types? Any comments or suggestions are greatly
appreciated!

I could obviously implement the operations on native C++ complex numbers for
the IDL type one by overloading operators, but I don't really want to do
this as my own implementations of these operators would most likely be less
optimised than the standard ones.

Thanks,
Marc
_______________________________________________________________________________

email: marc at greenie.net, marc.eberhard at alumni.tum.de, marc at affs.org.uk
email: m.a.eberhard at aston.ac.uk, web: http://www.aston.ac.uk/~eberhama/



More information about the omniORB-list mailing list