Avro C++
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 <memory>
26 #include <boost/noncopyable.hpp>
27 
28 #include "Exception.hh"
29 #include "LogicalType.hh"
30 #include "Types.hh"
31 #include "SchemaResolution.hh"
32 
33 namespace avro {
34 
35 class Node;
36 class GenericDatum;
37 
38 typedef std::shared_ptr<Node> NodePtr;
39 
40 class AVRO_DECL Name {
41  std::string ns_;
42  std::string simpleName_;
43 public:
44  Name() { }
45  Name(const std::string& fullname);
46  Name(const std::string& simpleName, const std::string& ns) : ns_(ns), simpleName_(simpleName) { check(); }
47 
48  const std::string fullname() const;
49  const std::string& ns() const { return ns_; }
50  const std::string& simpleName() const { return simpleName_; }
51 
52  void ns(const std::string& n) { ns_ = n; }
53  void simpleName(const std::string& n) { simpleName_ = n; }
54  void fullname(const std::string& n);
55 
56  bool operator < (const Name& n) const;
57  void check() const;
58  bool operator == (const Name& n) const;
59  bool operator != (const Name& n) const { return !((*this) == n); }
60  void clear() {
61  ns_.clear();
62  simpleName_.clear();
63  }
64  operator std::string() const {
65  return fullname();
66  }
67 };
68 
69 inline
70 std::ostream& operator << (std::ostream& os, const Name& n) {
71  return os << n.fullname();
72 }
73 
88 
89 class AVRO_DECL Node : private boost::noncopyable
90 {
91  public:
92 
93  Node(Type type) :
94  type_(type),
95  logicalType_(LogicalType::NONE),
96  locked_(false)
97  {}
98 
99  virtual ~Node();
100 
101  Type type() const {
102  return type_;
103  }
104 
105  LogicalType logicalType() const {
106  return logicalType_;
107  }
108 
109  void setLogicalType(LogicalType logicalType);
110 
111  void lock() {
112  locked_ = true;
113  }
114 
115  bool locked() const {
116  return locked_;
117  }
118 
119  virtual bool hasName() const = 0;
120 
121  void setName(const Name &name) {
122  checkLock();
123  checkName(name);
124  doSetName(name);
125  }
126  virtual const Name &name() const = 0;
127 
128  virtual const std::string &getDoc() const = 0;
129  void setDoc(const std::string &doc) {
130  checkLock();
131  doSetDoc(doc);
132  }
133 
134  void addLeaf(const NodePtr &newLeaf) {
135  checkLock();
136  doAddLeaf(newLeaf);
137  }
138  virtual size_t leaves() const = 0;
139  virtual const NodePtr& leafAt(int index) const = 0;
140  virtual const GenericDatum& defaultValueAt(int index) {
141  throw Exception(boost::format("No default value at: %1%") % index);
142  }
143 
144  void addName(const std::string &name) {
145  checkLock();
146  checkName(name);
147  doAddName(name);
148  }
149  virtual size_t names() const = 0;
150  virtual const std::string &nameAt(int index) const = 0;
151  virtual bool nameIndex(const std::string &name, size_t &index) const = 0;
152 
153  void setFixedSize(int size) {
154  checkLock();
155  doSetFixedSize(size);
156  }
157  virtual int fixedSize() const = 0;
158 
159  virtual bool isValid() const = 0;
160 
161  virtual SchemaResolution resolve(const Node &reader) const = 0;
162 
163  virtual void printJson(std::ostream &os, int depth) const = 0;
164 
165  virtual void printBasicInfo(std::ostream &os) const = 0;
166 
167  virtual void setLeafToSymbolic(int index, const NodePtr &node) = 0;
168 
169  // Serialize the default value GenericDatum g for the node contained
170  // in a record node.
171  virtual void printDefaultToJson(const GenericDatum& g, std::ostream &os,
172  int depth) const = 0;
173 
174  protected:
175 
176  void checkLock() const {
177  if(locked()) {
178  throw Exception("Cannot modify locked schema");
179  }
180  }
181 
182  virtual void checkName(const Name &name) const {
183  name.check();
184  }
185 
186  virtual void doSetName(const Name &name) = 0;
187  virtual void doSetDoc(const std::string &name) = 0;
188 
189  virtual void doAddLeaf(const NodePtr &newLeaf) = 0;
190  virtual void doAddName(const std::string &name) = 0;
191  virtual void doSetFixedSize(int size) = 0;
192 
193  private:
194 
195  const Type type_;
196  LogicalType logicalType_;
197  bool locked_;
198 };
199 
200 } // namespace avro
201 
202 namespace std {
203 inline std::ostream& operator<<(std::ostream& os, const avro::Node& n)
204 {
205  n.printJson(os, 0);
206  return os;
207 }
208 }
209 
210 
211 #endif
Node is the building block for parse trees.
Definition: Node.hh:89
Definition: Node.hh:40
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:30
Definition: Node.hh:202
SchemaResolution
Definition: SchemaResolution.hh:27
Definition: LogicalType.hh:28
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:61