OutputStreamer.hh

00001 
00019 #ifndef avro_OutputStreamer_hh__
00020 #define avro_OutputStreamer_hh__
00021 
00022 #include <iostream>
00023 #include <stdint.h>
00024 
00025 namespace avro {
00026 
00036     
00037 class OutputStreamer {
00038 
00039   public:
00040 
00041     virtual ~OutputStreamer()
00042     { }
00043 
00044     virtual size_t writeByte(uint8_t byte) = 0;
00045     virtual size_t writeWord(uint32_t word) = 0;
00046     virtual size_t writeLongWord(uint64_t word) = 0;
00047     virtual size_t writeBytes(const void *bytes, size_t size) = 0;
00048 };
00049 
00050 
00055     
00056 class ScreenStreamer : public OutputStreamer {
00057 
00058     size_t writeByte(uint8_t byte) {
00059         std::cout << "0x" << std::hex << static_cast<int32_t>(byte) << std::dec << " ";
00060         return 1;
00061     }
00062 
00063     size_t writeWord(uint32_t word) {
00064         ScreenStreamer::writeBytes(&word, sizeof(word));
00065         return sizeof(uint32_t);
00066     }
00067 
00068     size_t writeLongWord(uint64_t word) {
00069         ScreenStreamer::writeBytes(&word, sizeof(word));
00070         return sizeof(uint64_t);
00071     }
00072 
00073     size_t writeBytes(const void *bytes, size_t size) {
00074         const uint8_t *ptr = reinterpret_cast<const uint8_t *>(bytes);
00075         for (size_t i= 0; i < size; ++i) {
00076             ScreenStreamer::writeByte(*ptr++);
00077         }
00078         std::cout << std::endl;
00079         return size;
00080     }
00081 
00082 };
00083 
00091     
00092 class OStreamer : public OutputStreamer {
00093 
00094   public:
00095 
00096     OStreamer(std::ostream &os) :
00097         os_(os)
00098     {}
00099 
00100     size_t writeByte(uint8_t byte) {
00101         os_.put(byte);
00102         return 1;
00103     }
00104 
00105     size_t writeWord(uint32_t word) {
00106         os_.write(reinterpret_cast<char *>(&word), sizeof(word));
00107         return sizeof(uint32_t);
00108     }
00109 
00110     size_t writeLongWord(uint64_t word) {
00111         os_.write(reinterpret_cast<char *>(&word), sizeof(word));
00112         return sizeof(uint64_t);
00113     }
00114 
00115     size_t writeBytes(const void *bytes, size_t size) {
00116         os_.write(reinterpret_cast<const char *>(bytes), size);
00117         return size;
00118     }
00119 
00120   private:
00121 
00122     std::ostream &os_;
00123 };
00124 
00125 } // namespace avro
00126 
00127 #endif

Generated on Thu Mar 25 14:58:22 2010 for Avro C++ by  doxygen 1.6.1