ioconnections.h

Go to the documentation of this file.
00001 /*******************************************************************\
00002 
00003                    SESAME project software license
00004 
00005               Copyright (C) 2002 University of Amsterdam
00006 
00007     This program is free software; you can redistribute it and/or
00008      modify it under the terms of the GNU General Public License
00009     as published by the Free Software Foundation; either version 2
00010         of the License, or (at your option) any later version.
00011 
00012    This program is distributed in the hope that it will be useful,
00013     but WITHOUT ANY WARRANTY; without even the implied warranty of
00014     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015              GNU General Public License for more details.
00016 
00017   You should have received a copy of the GNU General Public License
00018      along with this program; if not, write to the Free Software
00019       Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
00020                            02111-1307, USA.
00021 
00022       For information regarding the SESAME software project see
00023               http://sesamesim.sourceforge.net or email
00024                     jcofflan@users.sourceforge.net
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   //virtual FifoBase* selector() const;
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   //cout << "Reading " << name << " " << portId << endl;
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()); // Data
00076   delete data->detatch(); // DataBlock
00077   delete data;  // DataBlockClass
00078 }
00079 
00080 template<class T> inline
00081 void Reader<T>::read(T *p, unsigned int n) {
00082 //  THROW("Not implemented!");
00083 
00084         for (unsigned int i=0; i<n; i++)
00085                 read(p[i]);
00086 }
00087 
00088 // *** Writer ***
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   //virtual FifoBase* selector() const;
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   //cout << "Writing to " << loader->getName() << '.' << name << endl;
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 //  THROW("Not implemented!");
00127 
00128         for (unsigned int i=0; i<n; i++)
00129                 write(p[i]);
00130 }
00131 #endif // IOCONNECTIONS_INCLUDE_ONCE

Generated on Wed Apr 5 20:57:45 2006 for Sesame by  doxygen 1.4.6