00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028 #ifndef IOCONNECTIONS_INCLUDE_ONCE
00029 #define IOCONNECTIONS_INCLUDE_ONCE
00030
00031 #include "io.h"
00032 #include "string.h"
00033 #include "cppprocessloader.h"
00034 #include "datablockclass.h"
00035
00036 #include <BasicUtils/BasicException.h>
00037 #include <BasicUtils/BasicString.h>
00038
00039 #include <unistd.h>
00040
00041 template<class T>
00042 class Reader : public In<T> {
00043 CPPProcessLoader *loader;
00044 int portId;
00045 const char *name;
00046
00047 public:
00048 Reader(CPPProcessLoader *loader, const char *name);
00049 virtual ~Reader() {}
00050
00051 virtual void read(T& value);
00052 virtual void read(T* p, unsigned int n);
00053
00054 };
00055
00056 template<class T> inline
00057 Reader<T>::Reader(CPPProcessLoader *loader, const char *name) :
00058 loader(loader), name(name) {
00059
00060 portId = loader->findPort(name)->getID();
00061 }
00062
00063 template<class T> inline
00064 void Reader<T>::read(T &value) {
00065 ASSERT_OR_THROW("NULL this pointer!", this);
00066
00067 DataBlockClass *data = loader->read(portId);
00068 ASSERT_OR_THROW("NULL data block", data);
00069
00070 if (data->getSize() != sizeof(T))
00071 THROW(string("Read datablock size ") + BasicString(data->getSize()) +
00072 " does not match expected size " + BasicString(sizeof(T)) + ".");
00073
00074 value = *((T *)data->getData());
00075 delete ((T *)data->getData());
00076 delete data->detatch();
00077 delete data;
00078 }
00079
00080 template<class T> inline
00081 void Reader<T>::read(T *p, unsigned int n) {
00082
00083
00084 for (unsigned int i=0; i<n; i++)
00085 read(p[i]);
00086 }
00087
00088
00089 template<class T>
00090 class Writer : public Out<T> {
00091 CPPProcessLoader *loader;
00092 int portId;
00093 const char *name;
00094
00095 public:
00096 Writer(CPPProcessLoader *loader, const char *name);
00097 virtual ~Writer() {}
00098
00099 virtual void write(const T& value);
00100 virtual void write(const T* p, unsigned int n);
00101
00102 };
00103
00104 template<class T> inline
00105 Writer<T>::Writer(CPPProcessLoader *loader, const char *name) :
00106 loader(loader), name(name) {
00107
00108 portId = loader->findPort(name)->getID();
00109 }
00110
00111 template<class T> inline
00112 void Writer<T>::write(const T &value) {
00113
00114
00115 DataBlock *dataBlock = new DataBlock;
00116
00117 T *copy = new T(value);
00118 dataBlock->size = sizeof(T);
00119 dataBlock->data = (char *)copy;
00120 DataBlockClass *data = new DataBlockClass(dataBlock);
00121 loader->write(portId, data);
00122 }
00123
00124 template<class T> inline
00125 void Writer<T>::write(const T *p, unsigned int n) {
00126
00127
00128 for (unsigned int i=0; i<n; i++)
00129 write(p[i]);
00130 }
00131 #endif // IOCONNECTIONS_INCLUDE_ONCE