lmsg/TransInput.hh

00001 /* -*- mode: c++; c-basic-offset: 4; -*- */
00002 #ifndef LMSG_TRANSINPUT_HH
00003 #define LMSG_TRANSINPUT_HH
00004 
00005 #include <stdexcept>
00006 #include <string>
00007 #include "lmsg/MsgTypes.hh"
00008 #include "lmsg/Grinder.hh"
00009 
00010 class Time;
00011 
00012 namespace lmsg {
00013 
00014     class Buffer;
00015 
00025     class TransInput {
00026     public:
00031         TransInput(const Buffer& b);
00032 
00035         TransInput(void);
00036 
00039         void align(size_type bdry);
00040 
00044         bool available(size_type N) const;
00045 
00050         bool empty(void) const;
00051 
00058         template<class T> 
00059         size_type read(T x[], size_type N=1);
00060  
00063         template <class T> 
00064         TransInput& operator>>(T& x) throw(std::runtime_error);
00065 
00066     private:
00067         const char* mData;
00068         size_type      mIndex;
00069         size_type      mLength;
00070     };  //  class lmsg::TransInput
00071 
00072     //======================================  Specific templates
00073 
00080     template<> 
00081     size_type TransInput::read(char x[], size_type N);
00082 
00089     template<> 
00090     size_type TransInput::read(MsgAddr x[], size_type N);
00091 
00098     template<> 
00099     size_type TransInput::read(Time x[], size_type N);
00100 
00107     template<> 
00108     size_type TransInput::read(std::string x[], size_type N);
00109 
00110     //======================================  Inline methods
00111     template<class T>
00112     inline TransInput&
00113     TransInput::operator>> (T& x) throw(std::runtime_error) {
00114         if (!read(&x, 1)) throw std::runtime_error("End-Of-Data");
00115         return *this;
00116     }
00117 
00118     inline bool 
00119     TransInput::available(size_type N) const {
00120         return mIndex + N <= mLength;
00121     }
00122   
00123     inline bool
00124     TransInput::empty(void) const {
00125         return mIndex >= mLength;
00126     }
00127 
00128     template<class T> 
00129     inline size_type 
00130     TransInput::read(T x[], size_type N) {
00131         align(sizeof(T));
00132         if (empty()) return 0;
00133         if (!available(N*sizeof(T))) N = (mLength - mIndex)/sizeof(T);
00134         export_format_grinder.SwapIn(mData+mIndex, x, N);
00135         mIndex += N*sizeof(T);
00136         return N;
00137     }
00138 
00139 }    // namespace lmsg
00140 
00141 #endif  //  LMSG_TRANSINPUT_HH

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