Node.hh

00001 /*
00002  * Licensed to the Apache Software Foundation (ASF) under one
00003  * or more contributor license agreements.  See the NOTICE file
00004  * distributed with this work for additional information
00005  * regarding copyright ownership.  The ASF licenses this file
00006  * to you under the Apache License, Version 2.0 (the
00007  * "License"); you may not use this file except in compliance
00008  * with the License.  You may obtain a copy of the License at
00009  *
00010  *     http://www.apache.org/licenses/LICENSE-2.0
00011  *
00012  * Unless required by applicable law or agreed to in writing, software
00013  * distributed under the License is distributed on an "AS IS" BASIS,
00014  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00015  * See the License for the specific language governing permissions and
00016  * limitations under the License.
00017  */
00018 
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 "Config.hh"
00027 #include "Exception.hh"
00028 #include "Types.hh"
00029 #include "SchemaResolution.hh"
00030 
00031 namespace avro {
00032 
00033 class Node;
00034 
00035 typedef boost::shared_ptr<Node> NodePtr;
00036 
00037 
00052 
00053 class AVRO_DECL Node : private boost::noncopyable
00054 {
00055   public:
00056 
00057     Node(Type type) :
00058         type_(type),
00059         locked_(false)
00060     {}
00061 
00062     virtual ~Node();
00063 
00064     Type type() const {
00065         return type_;
00066     }
00067 
00068     void lock() {
00069         locked_ = true;
00070     }
00071 
00072     bool locked() const {
00073         return locked_;
00074     }
00075 
00076     virtual bool hasName() const = 0;
00077 
00078     void setName(const std::string &name) {
00079         checkLock();
00080         checkName(name);
00081         doSetName(name);
00082     }
00083     virtual const std::string &name() const = 0;
00084 
00085     void addLeaf(const NodePtr &newLeaf) {
00086         checkLock();
00087         doAddLeaf(newLeaf);
00088     }
00089     virtual size_t leaves() const = 0;
00090     virtual const NodePtr& leafAt(int index) const = 0;
00091 
00092     void addName(const std::string &name) {
00093         checkLock();
00094         checkName(name);
00095         doAddName(name);
00096     }
00097     virtual size_t names() const = 0;
00098     virtual const std::string &nameAt(int index) const = 0;
00099     virtual bool nameIndex(const std::string &name, size_t &index) const = 0;
00100 
00101     void setFixedSize(int size) {
00102         checkLock();
00103         doSetFixedSize(size);
00104     }
00105     virtual int fixedSize() const = 0;
00106 
00107     virtual bool isValid() const = 0;
00108 
00109     virtual SchemaResolution resolve(const Node &reader) const = 0;
00110 
00111     virtual void printJson(std::ostream &os, int depth) const = 0;
00112 
00113     virtual void printBasicInfo(std::ostream &os) const = 0;
00114 
00115     virtual void setLeafToSymbolic(int index, const NodePtr &node) = 0;
00116 
00117   protected:
00118 
00119     void checkLock() const {
00120         if(locked()) {
00121             throw Exception("Cannot modify locked schema");
00122         }
00123     }
00124 
00125     void checkName(const std::string &name) const;
00126 
00127     virtual void doSetName(const std::string &name) = 0;
00128     virtual void doAddLeaf(const NodePtr &newLeaf) = 0;
00129     virtual void doAddName(const std::string &name) = 0;
00130     virtual void doSetFixedSize(int size) = 0;
00131 
00132   private:
00133 
00134     const Type type_;
00135     bool locked_;
00136 };
00137 
00138 } // namespace avro
00139 
00140 namespace std {
00141 inline std::ostream& operator<<(std::ostream& os, const avro::Node& n)
00142 {
00143     n.printJson(os, 0);
00144     return os;
00145 }
00146 }
00147 
00148 
00149 #endif