Avro C++
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator
GenericDatum.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_GenericDatum_hh__
20 #define avro_GenericDatum_hh__
21 
22 #include <stdint.h>
23 #include <vector>
24 #include <map>
25 #include <string>
26 
27 #include <boost/any.hpp>
28 
29 #include "Node.hh"
30 #include "ValidSchema.hh"
31 
32 namespace avro {
55 class AVRO_DECL GenericDatum {
56  Type type_;
57  boost::any value_;
58 
59  GenericDatum(Type t) : type_(t) { }
60 
61  template <typename T>
62  GenericDatum(Type t, const T& v) : type_(t), value_(v) { }
63 
64  void init(const NodePtr& schema);
65 public:
69  Type type() const {
70  return type_;
71  }
72 
78  template<typename T> const T& value() const {
79  return *boost::any_cast<T>(&value_);
80  }
81 
91  template<typename T> T& value() {
92  return *boost::any_cast<T>(&value_);
93  }
94 
98  bool isUnion() const { return type_ == AVRO_UNION; }
99 
104  size_t unionBranch() const;
105 
110  void selectBranch(size_t branch);
111 
113  GenericDatum() : type_(AVRO_NULL) { }
114 
116  GenericDatum(bool v) : type_(AVRO_BOOL), value_(v) { }
117 
119  GenericDatum(int32_t v) : type_(AVRO_INT), value_(v) { }
120 
122  GenericDatum(int64_t v) : type_(AVRO_LONG), value_(v) { }
123 
125  GenericDatum(float v) : type_(AVRO_FLOAT), value_(v) { }
126 
128  GenericDatum(double v) : type_(AVRO_DOUBLE), value_(v) { }
129 
131  GenericDatum(const std::string& v) : type_(AVRO_STRING), value_(v) { }
132 
135  GenericDatum(const std::vector<uint8_t>& v) :
136  type_(AVRO_BYTES), value_(v) { }
137 
144  GenericDatum(const NodePtr& schema);
145 
152  template<typename T>
153  GenericDatum(const NodePtr& schema, const T& v) :
154  type_(schema->type()) {
155  init(schema);
156  value<T>() = v;
157  }
158 
165  GenericDatum(const ValidSchema& schema);
166 };
167 
171 class AVRO_DECL GenericContainer {
172  NodePtr schema_;
173  static void assertType(const NodePtr& schema, Type type);
174 protected:
178  GenericContainer(Type type, const NodePtr& s) : schema_(s) {
179  assertType(s, type);
180  }
181 
182 public:
184  const NodePtr& schema() const {
185  return schema_;
186  }
187 };
188 
192 class AVRO_DECL GenericUnion : public GenericContainer {
193  size_t curBranch_;
194  GenericDatum datum_;
195 
196 public:
202  GenericUnion(const NodePtr& schema) :
203  GenericContainer(AVRO_UNION, schema), curBranch_(schema->leaves()) {
204  }
205 
209  size_t currentBranch() const { return curBranch_; }
210 
215  void selectBranch(size_t branch) {
216  if (curBranch_ != branch) {
217  datum_ = GenericDatum(schema()->leafAt(branch));
218  curBranch_ = branch;
219  }
220  }
221 
227  return datum_;
228  }
229 
234  const GenericDatum& datum() const {
235  return datum_;
236  }
237 };
238 
242 class AVRO_DECL GenericRecord : public GenericContainer {
243  std::vector<GenericDatum> fields_;
244 public:
249  GenericRecord(const NodePtr& schema);
250 
254  size_t fieldCount() const {
255  return fields_.size();
256  }
257 
261  size_t fieldIndex(const std::string& name) const {
262  size_t index = 0;
263  if (!schema()->nameIndex(name, index)) {
264  throw Exception("Invalid field name: " + name);
265  }
266  return index;
267  }
268 
273  bool hasField(const std::string& name) const {
274  size_t index = 0;
275  return schema()->nameIndex(name, index);
276  }
277 
281  const GenericDatum& field(const std::string& name) const {
282  return fieldAt(fieldIndex(name));
283  }
284 
289  GenericDatum& field(const std::string& name) {
290  return fieldAt(fieldIndex(name));
291  }
292 
296  const GenericDatum& fieldAt(size_t pos) const {
297  return fields_[pos];
298  }
299 
304  GenericDatum& fieldAt(size_t pos) {
305  return fields_[pos];
306  }
307 
311  void setFieldAt(size_t pos, const GenericDatum& v) {
312  // assertSameType(v, schema()->leafAt(pos));
313  fields_[pos] = v;
314  }
315 };
316 
320 class AVRO_DECL GenericArray : public GenericContainer {
321 public:
325  typedef std::vector<GenericDatum> Value;
326 
331  GenericArray(const NodePtr& schema) : GenericContainer(AVRO_ARRAY, schema) {
332  }
333 
337  const Value& value() const {
338  return value_;
339  }
340 
344  Value& value() {
345  return value_;
346  }
347 private:
348  Value value_;
349 };
350 
354 class AVRO_DECL GenericMap : public GenericContainer {
355 public:
359  typedef std::vector<std::pair<std::string, GenericDatum> > Value;
360 
365  GenericMap(const NodePtr& schema) : GenericContainer(AVRO_MAP, schema) {
366  }
367 
371  const Value& value() const {
372  return value_;
373  }
374 
378  Value& value() {
379  return value_;
380  }
381 private:
382  Value value_;
383 };
384 
388 class AVRO_DECL GenericEnum : public GenericContainer {
389  size_t value_;
390 
391  static size_t index(const NodePtr& schema, const std::string& symbol) {
392  size_t result;
393  if (schema->nameIndex(symbol, result)) {
394  return result;
395  }
396  throw Exception("No such symbol");
397  }
398 
399 public:
404  GenericEnum(const NodePtr& schema) :
405  GenericContainer(AVRO_ENUM, schema), value_(0) {
406  }
407 
408  GenericEnum(const NodePtr& schema, const std::string& symbol) :
409  GenericContainer(AVRO_ENUM, schema), value_(index(schema, symbol)) {
410  }
411 
416  const std::string& symbol(size_t n) {
417  if (n < schema()->names()) {
418  return schema()->nameAt(n);
419  }
420  throw Exception("Not as many symbols");
421  }
422 
427  size_t index(const std::string& symbol) const {
428  return index(schema(), symbol);
429  }
430 
434  size_t set(const std::string& symbol) {
435  return value_ = index(symbol);
436  }
437 
441  void set(size_t n) {
442  if (n < schema()->names()) {
443  value_ = n;
444  return;
445  }
446  throw Exception("Not as many symbols");
447  }
448 
452  size_t value() const {
453  return value_;
454  }
455 
459  const std::string& symbol() const {
460  return schema()->nameAt(value_);
461  }
462 };
463 
467 class AVRO_DECL GenericFixed : public GenericContainer {
468  std::vector<uint8_t> value_;
469 public:
474  GenericFixed(const NodePtr& schema) : GenericContainer(AVRO_FIXED, schema) {
475  value_.resize(schema->fixedSize());
476  }
477 
478  GenericFixed(const NodePtr& schema, const std::vector<uint8_t>& v) :
479  GenericContainer(AVRO_FIXED, schema), value_(v) { }
480 
484  const std::vector<uint8_t>& value() const {
485  return value_;
486  }
487 
491  std::vector<uint8_t>& value() {
492  return value_;
493  }
494 };
495 
496 inline size_t GenericDatum::unionBranch() const {
497  return boost::any_cast<GenericUnion>(&value_)->currentBranch();
498 }
499 
500 inline void GenericDatum::selectBranch(size_t branch) {
501  boost::any_cast<GenericUnion>(&value_)->selectBranch(branch);
502 }
503 
504 } // namespace avro
505 #endif // avro_GenericDatum_hh__
std::vector< uint8_t > & value()
Returns the reference to the contents of this fixed.
Definition: GenericDatum.hh:491
Definition: Types.hh:37
GenericArray(const NodePtr &schema)
Constructs a generic array corresponding to the given schema schema, which should be of Avro type arr...
Definition: GenericDatum.hh:331
Definition: Types.hh:45
GenericDatum & fieldAt(size_t pos)
Returns the reference to the field at the given position pos, which can be used to change the content...
Definition: GenericDatum.hh:304
Definition: Types.hh:40
size_t index(const std::string &symbol) const
Returns the cardinal for the given symbol symbol.
Definition: GenericDatum.hh:427
The generic container for Avro maps.
Definition: GenericDatum.hh:354
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
void set(size_t n)
Set the value for this enum corresponding to the given cardinal n.
Definition: GenericDatum.hh:441
Generic container for Avro enum.
Definition: GenericDatum.hh:388
GenericDatum(double v)
Makes a new AVRO_DOUBLE datum whose value is of type double.
Definition: GenericDatum.hh:128
size_t fieldIndex(const std::string &name) const
Returns index of the field with the given name name.
Definition: GenericDatum.hh:261
GenericDatum(const std::string &v)
Makes a new AVRO_STRING datum whose value is of type std::string.
Definition: GenericDatum.hh:131
const Value & value() const
Returns the contents of this array.
Definition: GenericDatum.hh:337
Definition: Types.hh:43
const T & value() const
Returns the value held by this datum.
Definition: GenericDatum.hh:78
Definition: Types.hh:39
The generic container for Avro arrays.
Definition: GenericDatum.hh:320
const GenericDatum & field(const std::string &name) const
Returns the field with the given name name.
Definition: GenericDatum.hh:281
const std::string & symbol(size_t n)
Returns the symbol corresponding to the cardinal n.
Definition: GenericDatum.hh:416
Value & value()
Returns the reference to the contents of this map.
Definition: GenericDatum.hh:378
Definition: Types.hh:33
std::vector< std::pair< std::string, GenericDatum > > Value
The contents type for the map.
Definition: GenericDatum.hh:359
GenericContainer(Type type, const NodePtr &s)
Constructs a container corresponding to the given schema.
Definition: GenericDatum.hh:178
void setFieldAt(size_t pos, const GenericDatum &v)
Replaces the field at the given position pos with v.
Definition: GenericDatum.hh:311
T & value()
Returns the reference to the value held by this datum, which can be used to change the contents...
Definition: GenericDatum.hh:91
Value & value()
Returns the reference to the contents of this array.
Definition: GenericDatum.hh:344
const Value & value() const
Returns the contents of this map.
Definition: GenericDatum.hh:371
size_t unionBranch() const
Returns the index of the current branch, if this is a union.
Definition: GenericDatum.hh:496
Definition: Types.hh:44
GenericDatum(const std::vector< uint8_t > &v)
Makes a new AVRO_BYTES datum whose value is of type std::vector.
Definition: GenericDatum.hh:135
GenericMap(const NodePtr &schema)
Constructs a generic map corresponding to the given schema schema, which should be of Avro type map...
Definition: GenericDatum.hh:365
GenericDatum(const NodePtr &schema, const T &v)
Constructs a datum corresponding to the given avro type and set the value.
Definition: GenericDatum.hh:153
size_t set(const std::string &symbol)
Set the value for this enum corresponding to the given symbol symbol.
Definition: GenericDatum.hh:434
const std::vector< uint8_t > & value() const
Returns the contents of this fixed.
Definition: GenericDatum.hh:484
GenericEnum(const NodePtr &schema)
Constructs a generic enum corresponding to the given schema schema, which should be of Avro type enum...
Definition: GenericDatum.hh:404
GenericDatum()
Makes a new AVRO_NULL datum.
Definition: GenericDatum.hh:113
Definition: Types.hh:35
Definition: Types.hh:47
Definition: Types.hh:34
GenericDatum(float v)
Makes a new AVRO_FLOAT datum whose value is of type float.
Definition: GenericDatum.hh:125
GenericDatum(int32_t v)
Makes a new AVRO_INT datum whose value is of type int32_t.
Definition: GenericDatum.hh:119
GenericDatum & datum()
Returns the datum corresponding to the currently selected branch in this union.
Definition: GenericDatum.hh:226
The generic container for Avro records.
Definition: GenericDatum.hh:242
GenericDatum & field(const std::string &name)
Returns the reference to the field with the given name name, which can be used to change the contents...
Definition: GenericDatum.hh:289
void selectBranch(size_t branch)
Selects a new branch.
Definition: GenericDatum.hh:215
Definition: Types.hh:38
GenericDatum(bool v)
Makes a new AVRO_BOOL datum whose value is of type bool.
Definition: GenericDatum.hh:116
std::vector< GenericDatum > Value
The contents type for the array.
Definition: GenericDatum.hh:325
void selectBranch(size_t branch)
Selects a new branch in the union if this is a union.
Definition: GenericDatum.hh:500
size_t value() const
Returns the cardinal for the current value of this enum.
Definition: GenericDatum.hh:452
size_t currentBranch() const
Returns the index of the current branch.
Definition: GenericDatum.hh:209
Wrapper for std::runtime_error that provides convenience constructor for boost::format objects...
Definition: Exception.hh:31
const GenericDatum & fieldAt(size_t pos) const
Returns the field at the given position pos.
Definition: GenericDatum.hh:296
A ValidSchema is basically a non-mutable Schema that has passed some minumum of sanity checks...
Definition: ValidSchema.hh:40
size_t fieldCount() const
Returns the number of fields in the current record.
Definition: GenericDatum.hh:254
const GenericDatum & datum() const
Returns the datum corresponding to the currently selected branch in this union.
Definition: GenericDatum.hh:234
Type type() const
The avro data type this datum holds.
Definition: GenericDatum.hh:69
const NodePtr & schema() const
Returns the schema for this object.
Definition: GenericDatum.hh:184
The base class for all generic type for containers.
Definition: GenericDatum.hh:171
Generic container for Avro fixed.
Definition: GenericDatum.hh:467
Definition: Types.hh:46
Generic container for unions.
Definition: GenericDatum.hh:192
const std::string & symbol() const
Returns the symbol for the current value of this enum.
Definition: GenericDatum.hh:459
Definition: Types.hh:36
GenericDatum(int64_t v)
Makes a new AVRO_LONG datum whose value is of type int64_t.
Definition: GenericDatum.hh:122
GenericFixed(const NodePtr &schema)
Constructs a generic enum corresponding to the given schema schema, which should be of Avro type fixe...
Definition: GenericDatum.hh:474
bool hasField(const std::string &name) const
Returns true if a field with the given name name is located in this r false otherwise.
Definition: GenericDatum.hh:273
bool isUnion() const
Returns true if and only if this datum is a union.
Definition: GenericDatum.hh:98
Generic datum which can hold any Avro type.
Definition: GenericDatum.hh:55
GenericUnion(const NodePtr &schema)
Constructs a generic union corresponding to the given schema schema, and the given value...
Definition: GenericDatum.hh:202