00001 #ifndef _COMPLEX_H_
00002 #define _COMPLEX_H_
00003
00004
00005 #include "Object.h"
00006 #include <iostream>
00007 #include "ObjectPool.h"
00008 #include "ObjectParser.h"
00009 #include "typetraits.h"
00010 #include <complex>
00011 #include "binio.h"
00012 #include "net_types.h"
00013
00014 namespace FD {
00015
00021 template <class T>
00022 class Complex : public std::complex<T>, public Object {
00023
00024 public:
00025
00027 typedef std::complex<T> basicType;
00028
00030 Complex() : std::complex<T>() {}
00031
00033 Complex(const std::complex<T> &val) : std::complex<T>(val) {}
00034
00036 Complex(const Complex<T> &val) : std::complex<T>(val) {}
00037
00039 Complex(const NetCType<std::complex<T> > &obj) : std::complex<T>(obj.val()) {}
00040
00046 void printOn(std::ostream &out) const {
00047 out << "<"<<className()<<" ";
00048 out << *((std::complex<T>*) this);
00049 out << ">"<<std::endl;
00050 }
00051
00057 void readFrom(std::istream &in) {
00058 std::complex<T> value;
00059 in >> value;
00060 *this = value;
00061 char ch;
00062 in >> ch;
00063 if (ch != '>') {
00064 throw new GeneralException(std::string("Error reading ") + className() + std::string(" > expected"), __FILE__, __LINE__);
00065 }
00066 }
00067
00073 void serialize(std::ostream &out) const {
00074 out << "{" << className() << " |";
00075 BinIO::write(out, (std::complex<T>*) this, 1);
00076 out << " }";
00077 }
00078
00084 void unserialize(std::istream &in) {
00085 std::complex<T> value;
00086 BinIO::read(in, &value, 1);
00087 *this = value;
00088 char ch;
00089 in >> ch;
00090 if (ch != '}') {
00091 throw new GeneralException(std::string("Error reading ") + className() + std::string(" } expected"), __FILE__, __LINE__);
00092 }
00093 }
00094
00099 void prettyPrint(std::ostream &out) const {
00100 out << *((std::complex<T>*) this);
00101 }
00102
00107 std::complex<T>& val() const {return *((std::complex<T>*) this);}
00108
00109
00114 static Complex<T> *alloc() {return ObjectPool<Complex<T> >::alloc();}
00115
00121 static Complex<T> *alloc(const Complex<T> &obj)
00122 {
00123 Complex<T> *ret = ObjectPool<Complex<T> >::alloc();
00124 *ret = obj;
00125 return ret;
00126 }
00127
00131 void destroy() {ObjectPool<Complex<T> >::release(this);}
00132
00136 virtual ObjectRef clone() {
00137 return ObjectRef(Complex<T>::alloc(*(std::complex<T>*) this));
00138 }
00139
00140 };
00141
00148 template <class T>
00149 std::istream &operator >> (std::istream &in, Complex<T> &value) {
00150
00151 char ch;
00152 in >> ch;
00153
00154 std::string expected = ObjectGetClassName<Complex<T> >();
00155
00156 if (ch == '<') {
00157 std::string type;
00158 in >> type;
00159
00160 if (expected != type) {
00161 throw new ParsingException ("Parser expected type " + expected + " and got " + type);
00162 }
00163
00164
00165 value.readFrom(in);
00166 }
00167 else if (ch == '{') {
00168 std::string type;
00169 in >> type;
00170
00171 if (expected != type) {
00172 throw new ParsingException ("Parser expected type " + expected + " and got " + type);
00173 }
00174
00175
00176 char dummy;
00177 do {
00178 in >> dummy;
00179 } while(dummy != '|');
00180
00181 value.unserialize(in);
00182
00183 } else {
00184 throw new ParsingException ("Parser expected < or { while parsing type " + expected);
00185 }
00186
00187 return in;
00188 }
00189
00190 _DEF_OBJECT_TYPE(Complex<float>)
00191 _DEF_OBJECT_TYPE(Complex<double>)
00192
00193 }
00194 #endif