aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/libs/apache/avro/api/NodeConcepts.hh
blob: e914d925b60e79e2ddee7727305eed882c88b23d (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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
/*
 * 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_NodeConcepts_hh__
#define avro_NodeConcepts_hh__

#include "Config.hh"

#include <vector>
#include <map>
#include "Exception.hh"

namespace avro {


///
/// The concept classes are used to simplify NodeImpl.  Since different types
/// of avro types carry different attributes, such as names, or field names for
/// record members.  Using the concept class of NoAttribute vs Attribute, the
/// NodeImpl object can enable/disable the attribute, but the code is the same
/// in either case.
///
/// Furthermore, attributes may have different types, for example, most
/// attributes are strings, but fixed types have a size attribute, which is
/// integer.
///
/// Since compound types are composed of other types, the leaf attribute
/// concepts extend a NodeImpl to include leaf nodes, and attributes for leaf
/// nodes, which are used to build parse trees.
///
///

namespace concepts {

template <typename Attribute>
struct NoAttribute
{
    static const bool hasAttribute = false;

    size_t size() const {
        return 0;
    }

    void add( const Attribute &) {
        // There must be an add function for the generic NodeImpl, but the
        // Node APIs ensure that it is never called, the throw here is
        // just in case
        throw Exception("This type does not have attribute");
    }

    const Attribute &get(size_t = 0) const {
        // There must be an get function for the generic NodeImpl, but the
        // Node APIs ensure that it is never called, the throw here is
        // just in case
        throw Exception("This type does not have attribute");
        // even though this code is unreachable the compiler requires it
        static const Attribute empty = Attribute();
        return empty;
    }

    Attribute &get(size_t = 0) {
        // There must be an get function for the generic NodeImpl, but the
        // Node APIs ensure that it is never called, the throw here is
        // just in case
        throw Exception("This type does not have attribute");
    }

};

template<typename Attribute>
struct SingleAttribute
{
    static const bool hasAttribute = true;

    SingleAttribute() : attr_()
    { }

    SingleAttribute(const Attribute& a) : attr_(a) { }
    // copy constructing from another single attribute is allowed
    SingleAttribute(const SingleAttribute<Attribute> &rhs) :
        attr_(rhs.attr_)
    { }

    // copy constructing from a no attribute is allowed
    SingleAttribute(const NoAttribute<Attribute> &) :
        attr_()
    { }

    size_t size() const {
        return 1;
    }

    void add(const Attribute &attr) {
        attr_ = attr;
    }

    const Attribute &get(size_t index = 0) const {
        if (index != 0) {
            throw Exception("SingleAttribute has only 1 value");
        }
        return attr_;
    }

    Attribute &get(size_t index = 0) {
        if (index != 0) {
            throw Exception("SingleAttribute has only 1 value");
        }
        return attr_;
    }

private:
    template<typename T> friend struct MultiAttribute;
    Attribute attr_;
};

template<typename Attribute>
struct MultiAttribute
{
    static const bool hasAttribute = true;

    MultiAttribute()
    { }

    // copy constructing from another single attribute is allowed, it
    // pushes the attribute
    MultiAttribute(const SingleAttribute<Attribute> &rhs)
    {
        // since map is the only type that does this we know it's
        // final size will be two, so reserve
        attrs_.reserve(2);
        attrs_.push_back(rhs.attr_);
    }

    MultiAttribute(const MultiAttribute<Attribute> &rhs)  :
        attrs_(rhs.attrs_)
    { }

    MultiAttribute(const NoAttribute<Attribute> &)
    {}

    size_t size() const {
        return attrs_.size();
    }

    void add(const Attribute &attr) {
        attrs_.push_back(attr);
    }

    const Attribute &get(size_t index = 0) const {
        return attrs_.at(index);
    }

    Attribute &get(size_t index) {
        return attrs_.at(index);
    }

  private:

    std::vector<Attribute> attrs_;
};


template<typename T>
struct NameIndexConcept {

    bool lookup(const std::string &, size_t &) const {
        throw Exception("Name index does not exist");
        return 0;
    }

    bool add(const::std::string &, size_t) {
        throw Exception("Name index does not exist");
        return false;
    }
};

template<>
struct NameIndexConcept < MultiAttribute<std::string> >
{
    typedef std::map<std::string, size_t> IndexMap;

    bool lookup(const std::string &name, size_t &index) const {
        IndexMap::const_iterator iter = map_.find(name);
        if(iter == map_.end()) {
            return false;
        }
        index = iter->second;
        return true;
    }

    bool add(const::std::string &name, size_t index) {
        bool added = false;
        IndexMap::iterator lb = map_.lower_bound(name);
        if(lb == map_.end() || map_.key_comp()(name, lb->first)) {
            map_.insert(lb, IndexMap::value_type(name, index));
            added = true;
        }
        return added;
    }

  private:

    IndexMap map_;
};

} // namespace concepts
} // namespace avro

#endif