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