ValidatingWriter.hh

00001 
00019 #ifndef avro_ValidatingWriter_hh__
00020 #define avro_ValidatingWriter_hh__
00021 
00022 #include <boost/noncopyable.hpp>
00023 
00024 #include "Writer.hh"
00025 #include "Validator.hh"
00026 #include "AvroTraits.hh"
00027 
00028 namespace avro {
00029 
00030 class ValidSchema;
00031 class OutputStreamer;
00032 
00036 
00037 class ValidatingWriter : private boost::noncopyable
00038 {
00039 
00040   public:
00041 
00042     ValidatingWriter(const ValidSchema &schema, OutputStreamer &out);
00043 
00044     template<typename T>
00045     void writeValue(T val) {
00046         checkSafeToPut(type_to_avro<T>::type);
00047         writer_.writeValue(val);
00048         validator_.advance();
00049     }
00050 
00051     void writeValue(const std::string &val) {
00052         checkSafeToPut(type_to_avro<std::string>::type);
00053         writer_.writeValue(val);
00054         validator_.advance();
00055     }
00056 
00057     void writeBytes(const void *val, size_t size);
00058 
00059     template <size_t N>
00060     void writeFixed(const uint8_t (&val)[N]) {
00061         checkSafeToPut(AVRO_FIXED);
00062         checkSizeExpected(N);
00063         writer_.writeFixed(val);
00064         validator_.advance();
00065     }
00066 
00067     template <size_t N>
00068     void writeFixed(const boost::array<uint8_t, N> &val) {
00069         checkSafeToPut(AVRO_FIXED);
00070         checkSizeExpected(val.size());
00071         writer_.writeFixed(val);
00072         validator_.advance();
00073     }
00074 
00075     void writeRecord();
00076 
00077     void writeArrayBlock(int64_t size);
00078     void writeArrayEnd();
00079 
00080     void writeMapBlock(int64_t size);
00081     void writeMapEnd();
00082 
00083     void writeUnion(int64_t choice);
00084 
00085     void writeEnum(int64_t choice);
00086 
00087   private:
00088 
00089     void writeCount(int64_t count);
00090 
00091     void checkSafeToPut(Type type) const {
00092         if(! validator_.typeIsExpected(type)) {
00093             throw Exception("Type does not match schema");
00094         }
00095     }
00096 
00097     void checkSizeExpected(int size) const {
00098         if(validator_.nextSizeExpected() != size) {
00099             throw Exception("Wrong size of for fixed");
00100         }
00101     }
00102 
00103     Validator validator_;
00104     Writer writer_;
00105 
00106 };
00107 
00108 } // namespace avro
00109 
00110 #endif

Generated on Tue Feb 23 15:31:29 2010 for Avro C++ by  doxygen 1.6.1