lmsg/TransOutput.hh

00001 /* -*- mode: c++; c-basic-offset: 4; -*- */
00002 #ifndef LMSG_TRANSOUTPUT_HH
00003 #define LMSG_TRANSOUTPUT_HH
00004 
00005 #include "lmsg/MsgTypes.hh"
00006 #include <time.h>
00007 #include <string>
00008 #include <stdexcept>
00009 #include "Grinder.hh"
00010 
00011 class Time;
00012 
00013 namespace lmsg {
00014 
00015     class Buffer;
00016     class MsgAddr;
00017 
00025     class TransOutput {
00026     public:
00031         TransOutput(Buffer& b);
00032 
00038         TransOutput(void);
00039 
00045         void align(size_type N);
00046 
00053         bool available(size_type N) const;
00054 
00059         bool full(void) const;
00060 
00065         void setBuffer(Buffer& b);
00066 
00078         template<class T> 
00079         size_type write(const T x[], size_type N=1);
00080 
00089         template<class T> 
00090         TransOutput& operator<<(const T& x) throw(std::runtime_error);
00091 
00100         TransOutput& operator<<(const char* x) throw(std::runtime_error);
00101 
00106         size_type getNBytes(void) const throw();
00107 
00108     private:
00109         char*  mData;
00110         size_type mIndex;
00111         size_type mLength;
00112     };  //  class lmsg::TransOutput
00113 
00114 
00115     inline bool
00116     TransOutput::available(size_type N) const {
00117         return mIndex + N <= mLength;
00118     }
00119 
00120     inline bool
00121     TransOutput::full(void) const {
00122         return mIndex >= mLength;
00123     }
00124 
00125     inline size_type
00126     TransOutput::getNBytes(void) const throw() {
00127         return mIndex;
00128     }
00129 
00130     //--------------------------------------  Implement write template
00133     template<> size_type TransOutput::write(const char x[], size_type N);
00134 
00137     template<> size_type TransOutput::write(const MsgAddr x[], size_type N);
00138 
00141     template<> size_type TransOutput::write(const std::string x[],size_type N);
00142 
00145     template<> size_type TransOutput::write(const Time x[], size_type N);
00146 
00147     template<class T> 
00148     inline size_type 
00149     TransOutput::write(const T x[], size_type N) {
00150         align(sizeof(T));
00151         if (full()) return 0;
00152         if (!available(N*sizeof(T))) N = (mLength - mIndex)/sizeof(T);
00153         export_format_grinder.SwapOut(x, mData+mIndex, N);
00154         mIndex += N*sizeof(T);
00155         return N;
00156     }
00157 
00158     //--------------------------------------  Implement << by template;
00159     template<class T>
00160     inline TransOutput&
00161     TransOutput::operator<<(const T& x) throw(std::runtime_error) {
00162         if (!write(&x)) throw std::runtime_error("Buffer overflow");
00163         return *this;
00164     }
00165 
00166     inline TransOutput& 
00167     TransOutput::operator<<(const char* x) throw(std::runtime_error) {
00168         return operator<<(std::string(x));
00169     }
00170 
00171 }    // namespace lmsg
00172 
00173 #endif  //  LMSG_TRANSOUTPUT_HH

Generated on Sun Mar 8 19:20:50 2009 for dmt by  doxygen 1.5.4