Avro C++
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator
Node.hh
1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements. See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership. The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the
7  * "License"); you may not use this file except in compliance
8  * with the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */
18 
19 #ifndef avro_Node_hh__
20 #define avro_Node_hh__
21 
22 #include "Config.hh"
23 
24 #include <cassert>
25 #include <boost/noncopyable.hpp>
26 #include <boost/shared_ptr.hpp>
27 
28 #include "Exception.hh"
29 #include "Types.hh"
30 #include "SchemaResolution.hh"
31 
32 namespace avro {
33 
34 class Node;
35 class GenericDatum;
36 
37 typedef boost::shared_ptr<Node> NodePtr;
38 
39 class AVRO_DECL Name {
40  std::string ns_;
41  std::string simpleName_;
42 public:
43  Name() { }
44  Name(const std::string& fullname);
45  Name(const std::string& simpleName, const std::string& ns) : ns_(ns), simpleName_(simpleName) { check(); }
46 
47  const std::string fullname() const;
48  const std::string& ns() const { return ns_; }
49  const std::string& simpleName() const { return simpleName_; }
50 
51  void ns(const std::string& n) { ns_ = n; }
52  void simpleName(const std::string& n) { simpleName_ = n; }
53  void fullname(const std::string& n);
54 
55  bool operator < (const Name& n) const;
56  void check() const;
57  bool operator == (const Name& n) const;
58  bool operator != (const Name& n) const { return !((*this) == n); }
59  void clear() {
60  ns_.clear();
61  simpleName_.clear();
62  }
63  operator std::string() const {
64  return fullname();
65  }
66 };
67 
68 inline
69 std::ostream& operator << (std::ostream& os, const Name& n) {
70  return os << n.fullname();
71 }
72 
87 
88 class AVRO_DECL Node : private boost::noncopyable
89 {
90  public:
91 
92  Node(Type type) :
93  type_(type),
94  locked_(false)
95  {}
96 
97  virtual ~Node();
98 
99  Type type() const {
100  return type_;
101  }
102 
103  void lock() {
104  locked_ = true;
105  }
106 
107  bool locked() const {
108  return locked_;
109  }
110 
111  virtual bool hasName() const = 0;
112 
113  void setName(const Name &name) {
114  checkLock();
115  checkName(name);
116  doSetName(name);
117  }
118  virtual const Name &name() const = 0;
119 
120  void addLeaf(const NodePtr &newLeaf) {
121  checkLock();
122  doAddLeaf(newLeaf);
123  }
124  virtual size_t leaves() const = 0;
125  virtual const NodePtr& leafAt(int index) const = 0;
126  virtual const GenericDatum& defaultValueAt(int index) {
127  throw Exception(boost::format("No default value at: %1%") % index);
128  }
129 
130  void addName(const std::string &name) {
131  checkLock();
132  checkName(name);
133  doAddName(name);
134  }
135  virtual size_t names() const = 0;
136  virtual const std::string &nameAt(int index) const = 0;
137  virtual bool nameIndex(const std::string &name, size_t &index) const = 0;
138 
139  void setFixedSize(int size) {
140  checkLock();
141  doSetFixedSize(size);
142  }
143  virtual int fixedSize() const = 0;
144 
145  virtual bool isValid() const = 0;
146 
147  virtual SchemaResolution resolve(const Node &reader) const = 0;
148 
149  virtual void printJson(std::ostream &os, int depth) const = 0;
150 
151  virtual void printBasicInfo(std::ostream &os) const = 0;
152 
153  virtual void setLeafToSymbolic(int index, const NodePtr &node) = 0;
154 
155  protected:
156 
157  void checkLock() const {
158  if(locked()) {
159  throw Exception("Cannot modify locked schema");
160  }
161  }
162 
163  virtual void checkName(const Name &name) const {
164  name.check();
165  }
166 
167  virtual void doSetName(const Name &name) = 0;
168  virtual void doAddLeaf(const NodePtr &newLeaf) = 0;
169  virtual void doAddName(const std::string &name) = 0;
170  virtual void doSetFixedSize(int size) = 0;
171 
172  private:
173 
174  const Type type_;
175  bool locked_;
176 };
177 
178 } // namespace avro
179 
180 namespace std {
181 inline std::ostream& operator<<(std::ostream& os, const avro::Node& n)
182 {
183  n.printJson(os, 0);
184  return os;
185 }
186 }
187 
188 
189 #endif
Node is the building block for parse trees.
Definition: Node.hh:88
Definition: Node.hh:39
Type
The "type" for the schema.
Definition: Types.hh:31
A bunch of templates and specializations for encoding and decoding specific types.
Definition: AvroParse.hh:31
Definition: Node.hh:180
SchemaResolution
Definition: SchemaResolution.hh:27
Wrapper for std::runtime_error that provides convenience constructor for boost::format objects...
Definition: Exception.hh:31
Generic datum which can hold any Avro type.
Definition: GenericDatum.hh:55