00001 #ifndef CONNECTIONWRAP_H
00002 #define CONNECTIONWRAP_H
00003 #include <string>
00004 #include "Connection.h"
00005 #include "FilterConnection.h"
00006 #include "ConfigConnection.h"
00007 union ConnectionU {
00008 Connection<char> * charConn;
00009 Connection<double> * doubleConn;
00010 Connection<float> * floatConn;
00011 Connection<short> * shortConn;
00012 FilterConnection<char> * charFConn;
00013 FilterConnection<double>* doubleFConn;
00014 FilterConnection<float> * floatFConn;
00015 FilterConnection<short> * shortFConn;
00016 ConfigConnection * configConn;
00017 void * voidStar;
00018 } typedef ConnectionU;
00019 #define SHORTCONN 0
00020 #define CHARCONN 1
00021 #define BYTECONN 1
00022 #define FLOATCONN 2
00023 #define DOUBLECONN 3
00024 #define CONFIGCONN 4
00025 class ConnectionWrap {
00026 public:
00027 ConnectionU conn;
00028 char type;
00029 ConnectionWrap(): conn() { type = (char)0; conn.voidStar = NULL; }
00030 ConnectionWrap(void * connection,int type): conn() {
00031 this->conn.voidStar = connection;
00032 this->type = (char)type;
00033 }
00034 int getFileHandle() {
00035 if (type==SHORTCONN) {
00036 return conn.shortConn->getFileHandle();
00037 } else if (type==CHARCONN) {
00038 return conn.charConn->getFileHandle();
00039 } else if (type==FLOATCONN) {
00040 return conn.floatConn->getFileHandle();
00041 } else if (type==DOUBLECONN) {
00042 return conn.doubleConn->getFileHandle();
00043 }
00044 return 0;
00045 }
00046 bool readable() {
00047 if (type==SHORTCONN) {
00048 return conn.shortConn->readable();
00049 } else if (type==CHARCONN) {
00050 return conn.charConn->readable();
00051 } else if (type==FLOATCONN) {
00052 return conn.floatConn->readable();
00053 } else if (type==DOUBLECONN) {
00054 return conn.doubleConn->readable();
00055 }
00056 return 0;
00057 }
00058 string * getXML() {
00059 if (type==SHORTCONN) {
00060 return conn.shortFConn->getXML();
00061 } else if (type==CHARCONN) {
00062 return conn.charFConn->getXML();
00063 } else if (type==FLOATCONN) {
00064 return conn.floatFConn->getXML();
00065 } else if (type==DOUBLECONN) {
00066 return conn.doubleFConn->getXML();
00067 }
00068 return 0;
00069 }
00070 void disconnect() {
00071 if (type==SHORTCONN) {
00072 return conn.shortConn->disconnect();
00073 } else if (type==CHARCONN) {
00074 return conn.charConn->disconnect();
00075 } else if (type==FLOATCONN) {
00076 return conn.floatConn->disconnect();
00077 } else if (type==DOUBLECONN) {
00078 return conn.doubleConn->disconnect();
00079 } else if (type==CONFIGCONN) {
00080 return conn.configConn->disconnect();
00081 }
00082 }
00083 void process() {
00084 if (type==SHORTCONN) {
00085 conn.shortFConn->process();
00086 } else if (type==CHARCONN) {
00087 conn.charFConn->process();
00088 } else if (type==FLOATCONN) {
00089 conn.floatFConn->process();
00090 } else if (type==DOUBLECONN) {
00091 conn.doubleFConn->process();
00092 }
00093 }
00094 ~ConnectionWrap() {
00095
00096 if (conn.voidStar!=NULL) {
00097 if (type==SHORTCONN) {
00098 delete conn.shortFConn;
00099 } else if (type==CHARCONN) {
00100 delete conn.charFConn;
00101 } else if (type==FLOATCONN) {
00102 delete conn.floatFConn;
00103 } else if (type==DOUBLECONN) {
00104 delete conn.doubleFConn;
00105 } else if (type==CONFIGCONN) {
00106 delete conn.configConn;
00107 }
00108 }
00109 conn.voidStar = NULL;
00110 }
00111 void write(char * buffer,int size) {
00112 if (type==SHORTCONN ) {
00113 conn.shortConn->write((short*)buffer,size);
00114 } else if (type==CHARCONN || type==CONFIGCONN) {
00115 conn.charConn->write((char*)buffer,size);
00116 } else if (type==FLOATCONN ) {
00117 conn.floatConn->write((float*)buffer,size);
00118 } else if (type==DOUBLECONN ) {
00119 conn.doubleConn->write((double*)buffer,size);
00120 } else if (type==CONFIGCONN ) {
00121 conn.configConn->write((char*)buffer,size);
00122 }
00123 }
00124 int read(char * buffer,int size) {
00125 if (type==SHORTCONN ) {
00126 return conn.shortConn->write((short*)buffer,size);
00127 } else if (type==CHARCONN || type==CONFIGCONN) {
00128 return conn.charConn->write((char*)buffer,size);
00129 } else if (type==FLOATCONN ) {
00130 return conn.floatConn->write((float*)buffer,size);
00131 } else if (type==DOUBLECONN ) {
00132 return conn.doubleConn->write((double*)buffer,size);
00133 } else if (type==CONFIGCONN ) {
00134 return conn.configConn->write((char*)buffer,size);
00135 }
00136 return 0;
00137 }
00138 bool readReady() {
00139 if (type==SHORTCONN ) {
00140 return conn.shortConn->readReady();
00141 } else if (type==CHARCONN || type==CONFIGCONN) {
00142 return conn.charConn->readReady();
00143 } else if (type==FLOATCONN ) {
00144 return conn.floatConn->readReady();
00145 } else if (type==DOUBLECONN ) {
00146 return conn.doubleConn->readReady();
00147 }
00148 return false;
00149 }
00150 bool writeReady() {
00151 if (type==SHORTCONN ) {
00152 return conn.shortConn->writeReady();
00153 } else if (type==CHARCONN || type==CONFIGCONN) {
00154 return conn.charConn->writeReady();
00155 } else if (type==FLOATCONN ) {
00156 return conn.floatConn->writeReady();
00157 } else if (type==DOUBLECONN ) {
00158 return conn.doubleConn->writeReady();
00159 }
00160 return false;
00161 }
00162 int getDataSize() {
00163 if (type==SHORTCONN ) {
00164 return conn.shortConn->getDataSize();
00165 } else if (type==CHARCONN || type==CONFIGCONN) {
00166 return conn.charConn->getDataSize();
00167 } else if (type==FLOATCONN ) {
00168 return conn.floatConn->getDataSize();
00169 } else if (type==DOUBLECONN ) {
00170 return conn.doubleConn->getDataSize();
00171 }
00172 return 0;
00173 }
00174 void disconnectFrom(ConnectionWrap * w) {
00175 if (type==SHORTCONN && w->type ==SHORTCONN) {
00176 ((FilterConnection<short>*)(conn.shortConn))->disconnectFrom((Connection<short>*)w->conn.shortConn);
00177 } else if (type==CHARCONN && w->type==CHARCONN) {
00178 ((FilterConnection<char>*)(conn.charConn))->disconnectFrom((Connection<char>*)w->conn.charConn);
00179 } else if (type==FLOATCONN && w->type==FLOATCONN) {
00180 ((FilterConnection<float>*)(conn.floatConn))->disconnectFrom((Connection<float>*)w->conn.floatConn);
00181 } else if (type==DOUBLECONN && w->type==DOUBLECONN) {
00182 ((FilterConnection<double>*)(conn.doubleConn))->disconnectFrom((Connection<double>*)w->conn.doubleConn);
00183 } else {
00184 throw new string("Connections are of different data types!");
00185 }
00186 }
00187 void disconnectTo(ConnectionWrap * w) {
00188 if (type==SHORTCONN && w->type ==SHORTCONN) {
00189 ((FilterConnection<short>*)(conn.shortConn))->disconnectTo((FilterConnection<short>*)w->conn.shortFConn);
00190 } else if (type==CHARCONN && w->type==CHARCONN) {
00191 ((FilterConnection<char>*)(conn.charConn))->disconnectTo((FilterConnection<char>*)w->conn.charFConn);
00192 } else if (type==FLOATCONN && w->type==FLOATCONN) {
00193 ((FilterConnection<float>*)(conn.floatConn))->disconnectTo((FilterConnection<float>*)w->conn.floatFConn);
00194 } else if (type==DOUBLECONN && w->type==DOUBLECONN) {
00195 ((FilterConnection<double>*)(conn.doubleConn))->disconnectTo((FilterConnection<double>*)w->conn.doubleFConn);
00196 } else {
00197 throw new string("Connections are of different data types!");
00198 }
00199 }
00200 void connectFrom(ConnectionWrap * w) {
00201 if (type==SHORTCONN && w->type ==SHORTCONN) {
00202 ((FilterConnection<short>*)(conn.shortConn))->connectFrom(*(Connection<short>*)w->conn.shortConn);
00203 } else if (type==CHARCONN && w->type==CHARCONN) {
00204 return ((FilterConnection<char>*)(conn.charConn))->connectFrom(*(Connection<char>*)w->conn.charConn);
00205 } else if (type==FLOATCONN && w->type==FLOATCONN) {
00206 return ((FilterConnection<float>*)(conn.floatConn))->connectFrom(*(Connection<float>*)w->conn.floatConn);
00207 } else if (type==DOUBLECONN && w->type==DOUBLECONN) {
00208 return ((FilterConnection<double>*)(conn.doubleConn))->connectFrom(*(Connection<double>*)w->conn.doubleConn);
00209 } else {
00210 throw new string("Connections are of different data types!");
00211 }
00212 }
00213 };
00214 #endif