Writer.hh
00001
00019 #ifndef avro_Writer_hh__
00020 #define avro_Writer_hh__
00021
00022 #include <boost/noncopyable.hpp>
00023
00024 #include "OutputStreamer.hh"
00025 #include "Zigzag.hh"
00026 #include "Types.hh"
00027
00028 namespace avro {
00029
00031
00032 class Writer : private boost::noncopyable
00033 {
00034
00035 public:
00036
00037 explicit Writer(OutputStreamer &out) :
00038 out_(out)
00039 {}
00040
00041 void writeValue(const Null &) {}
00042
00043 void writeValue(bool val) {
00044 int8_t byte = (val != 0);
00045 out_.writeByte(byte);
00046 }
00047
00048 void writeValue(int32_t val) {
00049 boost::array<uint8_t, 5> bytes;
00050 size_t size = encodeInt32(val, bytes);
00051 out_.writeBytes(bytes.data(), size);
00052 }
00053
00054 void writeValue(int64_t val) {
00055 boost::array<uint8_t, 10> bytes;
00056 size_t size = encodeInt64(val, bytes);
00057 out_.writeBytes(bytes.data(), size);
00058 }
00059
00060 void writeValue(float val) {
00061 union {
00062 float f;
00063 int32_t i;
00064 } v;
00065
00066 v.f = val;
00067 out_.writeWord(v.i);
00068 }
00069
00070 void writeValue(double val) {
00071 union {
00072 double d;
00073 int64_t i;
00074 } v;
00075
00076 v.d = val;
00077 out_.writeLongWord(v.i);
00078 }
00079
00080 void writeValue(const std::string &val) {
00081 writeBytes(val.c_str(), val.size());
00082 }
00083
00084 void writeBytes(const void *val, size_t size) {
00085 this->writeValue(static_cast<int64_t>(size));
00086 out_.writeBytes(val, size);
00087 }
00088
00089 template <size_t N>
00090 void writeFixed(const uint8_t (&val)[N]) {
00091 out_.writeBytes(val, N);
00092 }
00093
00094 template <size_t N>
00095 void writeFixed(const boost::array<uint8_t, N> &val) {
00096 out_.writeBytes(val.data(), val.size());
00097 }
00098
00099 void writeRecord() {}
00100
00101 void writeArrayBlock(int64_t size) {
00102 this->writeValue(static_cast<int64_t>(size));
00103 }
00104
00105 void writeArrayEnd() {
00106 out_.writeByte(0);
00107 }
00108
00109 void writeMapBlock(int64_t size) {
00110 this->writeValue(static_cast<int64_t>(size));
00111 }
00112
00113 void writeMapEnd() {
00114 out_.writeByte(0);
00115 }
00116
00117 void writeUnion(int64_t choice) {
00118 this->writeValue(static_cast<int64_t>(choice));
00119 }
00120
00121 void writeEnum(int64_t choice) {
00122 this->writeValue(static_cast<int64_t>(choice));
00123 }
00124
00125 private:
00126
00127 OutputStreamer &out_;
00128
00129 };
00130
00131 }
00132
00133 #endif