SerializeStream.h

Go to the documentation of this file.
00001 /*
00002  *    Copyright 2005-2006 Intel Corporation
00003  * 
00004  *    Licensed under the Apache License, Version 2.0 (the "License");
00005  *    you may not use this file except in compliance with the License.
00006  *    You may obtain a copy of the License at
00007  * 
00008  *        http://www.apache.org/licenses/LICENSE-2.0
00009  * 
00010  *    Unless required by applicable law or agreed to in writing, software
00011  *    distributed under the License is distributed on an "AS IS" BASIS,
00012  *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  *    See the License for the specific language governing permissions and
00014  *    limitations under the License.
00015  */
00016 
00017 #ifndef __SERIALIZESTREAM_H__
00018 #define __SERIALIZESTREAM_H__
00019 
00020 #include "../io/IOClient.h"
00021 
00022 namespace oasys {
00023 
00024 //----------------------------------------------------------------------------
00029 class SerializeStream {
00030 public:
00031     virtual ~SerializeStream() {}
00032     
00034     int process_bits(const char* buf, size_t size) {
00035         return process_bits(const_cast<char*>(buf), size);
00036     }
00037 
00043     virtual int process_bits(char* buf, size_t size) = 0;
00044 };
00045 
00046 
00047 //----------------------------------------------------------------------------
00051 template<typename _Copy>
00052 class MemoryStream : public SerializeStream {
00053 public:
00054     MemoryStream(const char* buf, size_t size)
00055         : buf_(const_cast<char*>(buf)), 
00056           size_(size), pos_(0) {}
00057 
00058     virtual ~MemoryStream() {}
00059 
00061     int process_bits(char* buf, size_t size) {
00062         if (pos_ + size > size_) {
00063             return -1;
00064         }
00065         _Copy::op(buf, buf_ + pos_, size);            
00066         pos_ += size;
00067 
00068         return size;
00069     }
00070     
00071 private:
00072     char*  buf_;
00073     size_t size_;
00074     size_t pos_;                
00075 };
00076 
00077 
00078 //----------------------------------------------------------------------------
00079 template<typename _IO_Op>
00080 class IOStream : public SerializeStream {
00081 public:
00082     IOStream(IOClient* io) : io_(io) {}
00083     
00084     virtual ~IOStream() {}
00085     
00087     int process_bits(char* buf, size_t size) {
00088         return _IO_Op::op(io_, buf, size);
00089     }
00090 
00091 private:
00092     IOClient* io_;
00093 };
00094 
00095 
00096 //----------------------------------------------------------------------------
00097 namespace StreamOps {
00098 
00099 struct CopyTo {
00100     static void* op(void* dest, void* src, size_t n) {
00101         return memcpy(dest, src, n);
00102     }
00103 };
00104 struct CopyFrom {
00105     static void* op(void* src, void* dest, size_t n) {
00106         return memcpy(dest, src, n);
00107     }
00108 };
00109 
00110 struct Read {
00111     static int op(IOClient* io, char* buf, size_t size) {
00112         return io->readall(buf, size);
00113     }
00114 };
00115 
00116 struct Write {
00117     static int op(IOClient* io, char* buf, size_t size) {
00118         return io->readall(buf, size);
00119     }
00120 };
00121 
00122 } // namespace StreamOps
00123 
00124 typedef MemoryStream<StreamOps::CopyTo>   ReadMemoryStream;
00125 typedef MemoryStream<StreamOps::CopyFrom> WriteMemoryStream;
00126 typedef IOStream<StreamOps::Read>         ReadIOStream;
00127 typedef IOStream<StreamOps::Write>        WriteIOStream;
00128 
00129 
00130 //----------------------------------------------------------------------------
00131 class WriteBase64Stream : public SerializeStream {
00132 public:
00133     WriteBase64Stream(SerializeStream* stream);
00134 
00135     int process_bits(char* buf, size_t size);
00136 
00137 private:
00138     SerializeStream* stream_;
00139 };
00140 
00141 } // namespace oasys
00142 
00143 #endif /* __SERIALIZESTREAM_H__ */

Generated on Sat Sep 8 08:43:33 2007 for DTN Reference Implementation by  doxygen 1.5.3