summaryrefslogtreecommitdiffstats
path: root/src/variant.h
blob: 7c20e0481b905b9b3d958e6fadb0b712e77f3def (plain)
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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
/*
 * Copyright (c) 2017-2024 OARC, Inc.
 * Copyright (c) 2011-2017, IIS - The Internet Foundation in Sweden
 * All rights reserved.
 *
 * This file is part of PacketQ.
 *
 * PacketQ is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * PacketQ is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with PacketQ.  If not, see <http://www.gnu.org/licenses/>.
 */

#ifndef __packetq_variant_h
#define __packetq_variant_h

#include <cstdlib>

#include "MurmurHash3.h"
#include "refcountstring.h"

namespace packetq {

inline std::size_t hash_bytes(const char* bytes, int len)
{
    uint32_t result = 0;
    MurmurHash3_x86_32(bytes, len, 0, &result);
    return result;
}

// must be defined in this order - see the "if" statement
#define COLTYPE_MAX 4
namespace Coltype {
    enum Type {
        _bool  = 0,
        _int   = 1,
        _float = 2,
        _text  = 3,
    };
};

typedef bool     bool_column;
static const int bool_size  = sizeof(bool_column);
static const int bool_align = ((sizeof(bool_column) / sizeof(void*)) + 1) * sizeof(void*);

typedef int      int_column;
static const int int_size  = sizeof(int_column);
static const int int_align = ((sizeof(int_column) / sizeof(void*)) + 1) * sizeof(void*);

typedef double   float_column;
static const int float_size  = sizeof(float_column);
static const int float_align = ((sizeof(float_column) / sizeof(void*)) + 1) * sizeof(void*);

typedef RefCountString* text_column;
static const int        text_size  = sizeof(text_column);
static const int        text_align = ((sizeof(text_column) / sizeof(void*)) + 1) * sizeof(void*);

inline bool_column convert_column_to_bool(float_column v) { return v; }
inline bool_column convert_column_to_bool(int_column v) { return v; }
inline bool_column convert_column_to_bool(bool_column v) { return v; }
inline bool_column convert_column_to_bool(text_column v)
{
    return std::atoi(v->data);
}

inline int_column convert_column_to_int(float_column v) { return int(v); }
inline int_column convert_column_to_int(int_column v) { return v; }
inline int_column convert_column_to_int(bool_column v) { return v; }
inline int_column convert_column_to_int(text_column v)
{
    return v->data[0] != '\0';
}

inline float_column convert_column_to_float(float_column v) { return v; }
inline float_column convert_column_to_float(int_column v) { return v; }
inline float_column convert_column_to_float(bool_column v) { return v; }
inline float_column convert_column_to_float(text_column v)
{
    return std::atof(v->data);
}

inline text_column convert_column_to_text(float_column v)
{
    const int       bufsize = 50;
    RefCountString* str     = RefCountString::allocate(bufsize);
    snprintf(str->data, bufsize, "%g", v); // lgtm[cpp/badly-bounded-write]
    return str;
}
inline text_column convert_column_to_text(int_column v)
{
    const int       bufsize = (sizeof(int_column) * 8 + 1) / 3 + 1;
    RefCountString* str     = RefCountString::allocate(bufsize);
    snprintf(str->data, bufsize, "%d", v); // lgtm[cpp/badly-bounded-write]
    return str;
}
inline text_column convert_column_to_text(bool_column v)
{
    const int       bufsize = 1 + 1;
    RefCountString* str     = RefCountString::allocate(bufsize);
    if (v)
        str->data[0] = '1';
    else
        str->data[1] = '0';
    str->data[1] = '\0';
    return str;
}
inline text_column convert_column_to_text(text_column v)
{
    // to stay symmetric with above functions that allocate a new string,
    // increment reference count
    v->inc_refcount();
    return v;
}

// Variant represents a value that can be either one of the column types,
// plus a type field to figure out which kind it represents
class Variant {
public:
    Variant()
    {
        m_type      = Coltype::_int;
        m_val.m_int = 0;
    }

    Variant(bool_column val)
    {
        m_type       = Coltype::_bool;
        m_val.m_bool = val;
    }

    Variant(int_column val)
    {
        m_type      = Coltype::_int;
        m_val.m_int = val;
    }

    Variant(float_column val)
    {
        m_type        = Coltype::_float;
        m_val.m_float = val;
    }

    Variant(text_column val)
    {
        m_type       = Coltype::_text;
        m_val.m_text = val;
        m_val.m_text->inc_refcount();
    }

    Variant(const Variant& other)
    {
        m_type = other.m_type;
        m_val  = other.m_val;
        if (m_type == Coltype::_text)
            m_val.m_text->inc_refcount();
    }

    // move constructor
    Variant(Variant&& other)
    {
        // would be cleaner to use default constructor, but alas
        // constructor delegation requires GCC >= 4.7
        m_type      = Coltype::_int;
        m_val.m_int = 0;

        swap(*this, other);
    }

    ~Variant()
    {
        if (m_type == Coltype::_text)
            m_val.m_text->dec_refcount();
    }

    Variant& operator=(Variant other)
    {
        // copy and swap idiom
        swap(*this, other);
        return *this;
    }

    inline friend void swap(Variant& first, Variant& second)
    {
        using std::swap;
        swap(first.m_type, second.m_type);
        swap(first.m_val, second.m_val);
    }

    bool_column get_bool() const
    {
        switch (m_type) {
        case Coltype::_float:
            return convert_column_to_bool(m_val.m_float);
        case Coltype::_int:
            return convert_column_to_bool(m_val.m_int);
        case Coltype::_bool:
            return convert_column_to_bool(m_val.m_bool);
        case Coltype::_text:
            return convert_column_to_bool(m_val.m_text);
        }
        return false;
    }

    int_column get_int() const
    {
        switch (m_type) {
        case Coltype::_float:
            return convert_column_to_int(m_val.m_float);
        case Coltype::_int:
            return convert_column_to_int(m_val.m_int);
        case Coltype::_bool:
            return convert_column_to_int(m_val.m_bool);
        case Coltype::_text:
            return convert_column_to_int(m_val.m_text);
        }
        return 0;
    }

    float_column get_float() const
    {
        switch (m_type) {
        case Coltype::_float:
            return convert_column_to_float(m_val.m_float);
        case Coltype::_int:
            return convert_column_to_float(m_val.m_int);
        case Coltype::_bool:
            return convert_column_to_float(m_val.m_bool);
        case Coltype::_text:
            return convert_column_to_float(m_val.m_text);
        }
        return 0.0;
    }

    // this returns a RefCountString with the ref-count incremented so
    // caller is responsible for decrementing after use
    text_column get_text() const
    {
        switch (m_type) {
        case Coltype::_float:
            return convert_column_to_text(m_val.m_float);
        case Coltype::_int:
            return convert_column_to_text(m_val.m_int);
        case Coltype::_bool:
            return convert_column_to_text(m_val.m_bool);
        case Coltype::_text:
            return convert_column_to_text(m_val.m_text);
        }
        return RefCountString::construct("");
    }

    int cmp(const Variant& rhs) const
    {
        switch (m_type) {
        case (Coltype::_bool):
            return m_val.m_bool - rhs.get_bool();
        case (Coltype::_int):
            return m_val.m_int - rhs.get_int();
        case (Coltype::_float): {
            float_column r = rhs.get_float();
            if (m_val.m_float < r)
                return -1;
            if (m_val.m_float > r)
                return 1;
            return 0;
        }
        case (Coltype::_text): {
            RefCountString* s   = rhs.get_text();
            auto            res = strcmp(m_val.m_text->data, s->data);
            s->dec_refcount();
            return res;
        }
        }
        return 0;
    }

    bool operator<(const Variant& rhs) const
    {
        return cmp(rhs) < 0;
    }
    bool operator==(const Variant& rhs) const
    {
        return cmp(rhs) == 0;
    }

    std::size_t hash() const
    {
        switch (m_type) {
        case (Coltype::_bool):
            return std::hash<bool>()(m_val.m_bool);
        case (Coltype::_int):
            return std::hash<int>()(m_val.m_int);
        case (Coltype::_float):
            return std::hash<float>()(m_val.m_float);
        case (Coltype::_text):
            return hash_bytes(m_val.m_text->data, strlen(m_val.m_text->data));
        }
        return 0;
    }

    Coltype::Type m_type;

private:
    union VariantUnion {
        bool_column  m_bool;
        int_column   m_int;
        float_column m_float;
        text_column  m_text;
    };

    VariantUnion m_val;
};

} // namespace packetq

#endif // __packetq_variant_h