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