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