Avro C++
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 "Config.hh"
00023 
00024 #include <cassert>
00025 #include <boost/noncopyable.hpp>
00026 #include <boost/shared_ptr.hpp>
00027 
00028 #include "Exception.hh"
00029 #include "Types.hh"
00030 #include "SchemaResolution.hh"
00031 
00032 namespace avro {
00033 
00034 class Node;
00035 
00036 typedef boost::shared_ptr<Node> NodePtr;
00037 
00038 class AVRO_DECL Name {
00039     std::string ns_;
00040     std::string simpleName_;
00041 public:
00042     Name() { }
00043     Name(const std::string& fullname);
00044     Name(const std::string& simpleName, const std::string& ns) : ns_(ns), simpleName_(simpleName) { check(); }
00045 
00046     const std::string fullname() const;
00047     const std::string& ns() const { return ns_; }
00048     const std::string& simpleName() const { return simpleName_; }
00049 
00050     void ns(const std::string& n) { ns_ = n; }
00051     void simpleName(const std::string& n) { simpleName_ = n; }
00052     void fullname(const std::string& n);
00053 
00054     bool operator < (const Name& n) const;
00055     void check() const;
00056     bool operator == (const Name& n) const;
00057     bool operator != (const Name& n) const { return !((*this) == n); }
00058     void clear() {
00059         ns_.clear();
00060         simpleName_.clear();
00061     }
00062     operator std::string() const {
00063         return fullname();
00064     }
00065 };
00066 
00067 inline
00068 std::ostream& operator << (std::ostream& os, const Name& n) {
00069     return os << n.fullname();
00070 }
00071 
00086 
00087 class AVRO_DECL Node : private boost::noncopyable
00088 {
00089   public:
00090 
00091     Node(Type type) :
00092         type_(type),
00093         locked_(false)
00094     {}
00095 
00096     virtual ~Node();
00097 
00098     Type type() const {
00099         return type_;
00100     }
00101 
00102     void lock() {
00103         locked_ = true;
00104     }
00105 
00106     bool locked() const {
00107         return locked_;
00108     }
00109 
00110     virtual bool hasName() const = 0;
00111 
00112     void setName(const Name &name) {
00113         checkLock();
00114         checkName(name);
00115         doSetName(name);
00116     }
00117     virtual const Name &name() const = 0;
00118 
00119     void addLeaf(const NodePtr &newLeaf) {
00120         checkLock();
00121         doAddLeaf(newLeaf);
00122     }
00123     virtual size_t leaves() const = 0;
00124     virtual const NodePtr& leafAt(int index) const = 0;
00125 
00126     void addName(const std::string &name) {
00127         checkLock();
00128         checkName(name);
00129         doAddName(name);
00130     }
00131     virtual size_t names() const = 0;
00132     virtual const std::string &nameAt(int index) const = 0;
00133     virtual bool nameIndex(const std::string &name, size_t &index) const = 0;
00134 
00135     void setFixedSize(int size) {
00136         checkLock();
00137         doSetFixedSize(size);
00138     }
00139     virtual int fixedSize() const = 0;
00140 
00141     virtual bool isValid() const = 0;
00142 
00143     virtual SchemaResolution resolve(const Node &reader) const = 0;
00144 
00145     virtual void printJson(std::ostream &os, int depth) const = 0;
00146 
00147     virtual void printBasicInfo(std::ostream &os) const = 0;
00148 
00149     virtual void setLeafToSymbolic(int index, const NodePtr &node) = 0;
00150 
00151   protected:
00152 
00153     void checkLock() const {
00154         if(locked()) {
00155             throw Exception("Cannot modify locked schema");
00156         }
00157     }
00158 
00159     virtual void checkName(const Name &name) const {
00160         name.check();
00161     }
00162 
00163     virtual void doSetName(const Name &name) = 0;
00164     virtual void doAddLeaf(const NodePtr &newLeaf) = 0;
00165     virtual void doAddName(const std::string &name) = 0;
00166     virtual void doSetFixedSize(int size) = 0;
00167 
00168   private:
00169 
00170     const Type type_;
00171     bool locked_;
00172 };
00173 
00174 } // namespace avro
00175 
00176 namespace std {
00177 inline std::ostream& operator<<(std::ostream& os, const avro::Node& n)
00178 {
00179     n.printJson(os, 0);
00180     return os;
00181 }
00182 }
00183 
00184 
00185 #endif