Node.hh

00001 
00019 #ifndef avro_Node_hh__
00020 #define avro_Node_hh__
00021 
00022 #include <cassert>
00023 #include <boost/noncopyable.hpp>
00024 #include <boost/shared_ptr.hpp>
00025 
00026 #include "Exception.hh"
00027 #include "Types.hh"
00028 #include "SchemaResolution.hh"
00029 
00030 namespace avro {
00031 
00032 class Node;
00033 
00034 typedef boost::shared_ptr<Node> NodePtr;
00035 
00036 
00051 
00052 class Node : private boost::noncopyable
00053 {
00054   public:
00055 
00056     Node(Type type) :
00057         type_(type),
00058         locked_(false)
00059     {}
00060 
00061     virtual ~Node();
00062 
00063     Type type() const {
00064         return type_;
00065     }
00066 
00067     void lock() {
00068         locked_ = true;
00069     }
00070 
00071     bool locked() const {
00072         return locked_;
00073     }
00074 
00075     virtual bool hasName() const = 0;
00076 
00077     void setName(const std::string &name) {
00078         checkLock();
00079         checkName(name);
00080         doSetName(name);
00081     }
00082     virtual const std::string &name() const = 0;
00083 
00084     void addLeaf(const NodePtr &newLeaf) {
00085         checkLock();
00086         doAddLeaf(newLeaf);
00087     }
00088     virtual size_t leaves() const = 0;
00089     virtual const NodePtr& leafAt(int index) const = 0;
00090 
00091     void addName(const std::string &name) {
00092         checkLock();
00093         checkName(name);
00094         doAddName(name);
00095     }
00096     virtual size_t names() const = 0;
00097     virtual const std::string &nameAt(int index) const = 0;
00098     virtual bool nameIndex(const std::string &name, size_t &index) const = 0;
00099 
00100     void setFixedSize(int size) {
00101         checkLock();
00102         doSetFixedSize(size);
00103     }
00104     virtual int fixedSize() const = 0;
00105 
00106     virtual bool isValid() const = 0;
00107 
00108     virtual SchemaResolution resolve(const Node &reader) const = 0;
00109 
00110     virtual void printJson(std::ostream &os, int depth) const = 0;
00111 
00112     virtual void printBasicInfo(std::ostream &os) const = 0;
00113 
00114   protected:
00115 
00116     friend class ValidSchema;
00117 
00118     virtual void setLeafToSymbolic(int index, const NodePtr &node) = 0;
00119 
00120     void checkLock() const {
00121         if(locked()) {
00122             throw Exception("Cannot modify locked schema");
00123         }
00124     }
00125 
00126     void checkName(const std::string &name) const;
00127 
00128     virtual void doSetName(const std::string &name) = 0;
00129     virtual void doAddLeaf(const NodePtr &newLeaf) = 0;
00130     virtual void doAddName(const std::string &name) = 0;
00131     virtual void doSetFixedSize(int size) = 0;
00132 
00133   private:
00134 
00135     const Type type_;
00136     bool locked_;
00137 };
00138 
00139 } // namespace avro
00140 
00141 #endif