aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/libs/apache/avro/api/Generic.hh
blob: e1b3a8290c8c6e221f9089c5db5f0f4cc7cdbb32 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *     https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef avro_Generic_hh__
#define avro_Generic_hh__

#include <boost/utility.hpp>

#include "Config.hh"
#include "Types.hh"
#include "Encoder.hh"
#include "Decoder.hh"
#include "GenericDatum.hh"

namespace avro {
/**
 * A utility class to read generic datum from decoders.
 */
class AVRO_DECL GenericReader : boost::noncopyable {
    const ValidSchema schema_;
    const bool isResolving_;
    const DecoderPtr decoder_;

    static void read(GenericDatum& datum, Decoder& d, bool isResolving);
public:
    /**
     * Constructs a reader for the given schema using the given decoder.
     */
    GenericReader(const ValidSchema& s, const DecoderPtr& decoder);

    /**
     * Constructs a reader for the given reader's schema \c readerSchema
     * using the given
     * decoder which holds data matching writer's schema \c writerSchema.
     */
    GenericReader(const ValidSchema& writerSchema,
        const ValidSchema& readerSchema, const DecoderPtr& decoder);

    /**
     * Reads a value off the decoder.
     */
    void read(GenericDatum& datum) const;

    /**
     * Drains any residual bytes in the input stream (e.g. because
     * reader's schema has no use of them) and return unused bytes
     * back to the underlying input stream.
     */
    void drain() {
        decoder_->drain();
    }
    /**
     * Reads a generic datum from the stream, using the given schema.
     */
    static void read(Decoder& d, GenericDatum& g);

    /**
     * Reads a generic datum from the stream, using the given schema.
     */
    static void read(Decoder& d, GenericDatum& g, const ValidSchema& s);
};


/**
 * A utility class to write generic datum to encoders.
 */
class AVRO_DECL GenericWriter : boost::noncopyable {
    const ValidSchema schema_;
    const EncoderPtr encoder_;

    static void write(const GenericDatum& datum, Encoder& e);
public:
    /**
     * Constructs a writer for the given schema using the given encoder.
     */
    GenericWriter(const ValidSchema& s, const EncoderPtr& encoder);

    /**
     * Writes a value onto the encoder.
     */
    void write(const GenericDatum& datum) const;

    /**
     * Writes a generic datum on to the stream.
     */
    static void write(Encoder& e, const GenericDatum& g);

    /**
     * Writes a generic datum on to the stream, using the given schema.
     * Retained for backward compatibility.
     */
    static void write(Encoder& e, const GenericDatum& g, const ValidSchema&) {
        write(e, g);
    }
};

template <typename T> struct codec_traits;

/**
 * Specialization of codec_traits for Generic datum along with its schema.
 * This is maintained for compatibility with old code. Please use the
 * cleaner codec_traits<GenericDatum> instead.
 */
template <> struct codec_traits<std::pair<ValidSchema, GenericDatum> > {
    /** Encodes */
    static void encode(Encoder& e,
        const std::pair<ValidSchema, GenericDatum>& p) {
        GenericWriter::write(e, p.second, p.first);
    }

    /** Decodes */
    static void decode(Decoder& d, std::pair<ValidSchema, GenericDatum>& p) {
        GenericReader::read(d, p.second, p.first);
    }
};

/**
 * Specialization of codec_traits for GenericDatum.
 */
template <> struct codec_traits<GenericDatum> {
    /** Encodes */
    static void encode(Encoder& e, const GenericDatum& g) {
        GenericWriter::write(e, g);
    }

    /** Decodes */
    static void decode(Decoder& d, GenericDatum& g) {
        GenericReader::read(d, g);
    }
};

}   // namespace avro
#endif