summaryrefslogtreecommitdiff
path: root/native/utils/variant.h
blob: 551a8228b790caf06c4b7bc125b8367d2baefd77 (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
/*
 * Copyright (C) 2018 The Android Open Source Project
 *
 * Licensed 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
 *
 *      http://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 LIBTEXTCLASSIFIER_UTILS_VARIANT_H_
#define LIBTEXTCLASSIFIER_UTILS_VARIANT_H_

#include <map>
#include <string>
#include <vector>

#include "utils/base/integral_types.h"
#include "utils/base/logging.h"
#include "utils/strings/stringpiece.h"

namespace libtextclassifier3 {

// Represents a type-tagged union of different basic types.
class Variant {
 public:
  enum Type {
    TYPE_EMPTY = 0,
    TYPE_INT8_VALUE = 1,
    TYPE_UINT8_VALUE = 2,
    TYPE_INT_VALUE = 3,
    TYPE_UINT_VALUE = 4,
    TYPE_INT64_VALUE = 5,
    TYPE_UINT64_VALUE = 6,
    TYPE_FLOAT_VALUE = 7,
    TYPE_DOUBLE_VALUE = 8,
    TYPE_BOOL_VALUE = 9,
    TYPE_STRING_VALUE = 10,
    TYPE_STRING_VECTOR_VALUE = 11,
    TYPE_FLOAT_VECTOR_VALUE = 12,
    TYPE_INT_VECTOR_VALUE = 13,
    TYPE_STRING_VARIANT_MAP_VALUE = 14,
  };

  Variant() : type_(TYPE_EMPTY) {}
  explicit Variant(const int8_t value)
      : type_(TYPE_INT8_VALUE), int8_value_(value) {}
  explicit Variant(const uint8_t value)
      : type_(TYPE_UINT8_VALUE), uint8_value_(value) {}
  explicit Variant(const int value)
      : type_(TYPE_INT_VALUE), int_value_(value) {}
  explicit Variant(const uint value)
      : type_(TYPE_UINT_VALUE), uint_value_(value) {}
  explicit Variant(const int64 value)
      : type_(TYPE_INT64_VALUE), long_value_(value) {}
  explicit Variant(const uint64 value)
      : type_(TYPE_UINT64_VALUE), ulong_value_(value) {}
  explicit Variant(const float value)
      : type_(TYPE_FLOAT_VALUE), float_value_(value) {}
  explicit Variant(const double value)
      : type_(TYPE_DOUBLE_VALUE), double_value_(value) {}
  explicit Variant(const StringPiece value)
      : type_(TYPE_STRING_VALUE), string_value_(value.ToString()) {}
  explicit Variant(const std::string value)
      : type_(TYPE_STRING_VALUE), string_value_(value) {}
  explicit Variant(const char* value)
      : type_(TYPE_STRING_VALUE), string_value_(value) {}
  explicit Variant(const bool value)
      : type_(TYPE_BOOL_VALUE), bool_value_(value) {}
  explicit Variant(const std::vector<std::string>& value)
      : type_(TYPE_STRING_VECTOR_VALUE), string_vector_value_(value) {}
  explicit Variant(const std::vector<float>& value)
      : type_(TYPE_FLOAT_VECTOR_VALUE), float_vector_value_(value) {}
  explicit Variant(const std::vector<int>& value)
      : type_(TYPE_INT_VECTOR_VALUE), int_vector_value_(value) {}
  explicit Variant(const std::map<std::string, Variant>& value)
      : type_(TYPE_STRING_VARIANT_MAP_VALUE),
        string_variant_map_value_(value) {}

  Variant& operator=(const Variant&) = default;

  template <class T>
  struct dependent_false : std::false_type {};

  template <typename T>
  T Value() const {
    static_assert(dependent_false<T>::value, "Not supported.");
  }

  template <>
  int8 Value() const {
    TC3_CHECK(Has<int8>());
    return int8_value_;
  }

  template <>
  uint8 Value() const {
    TC3_CHECK(Has<uint8>());
    return uint8_value_;
  }

  template <>
  int Value() const {
    TC3_CHECK(Has<int>());
    return int_value_;
  }

  template <>
  uint Value() const {
    TC3_CHECK(Has<uint>());
    return uint_value_;
  }

  template <>
  int64 Value() const {
    TC3_CHECK(Has<int64>());
    return long_value_;
  }

  template <>
  uint64 Value() const {
    TC3_CHECK(Has<uint64>());
    return ulong_value_;
  }

  template <>
  float Value() const {
    TC3_CHECK(Has<float>());
    return float_value_;
  }

  template <>
  double Value() const {
    TC3_CHECK(Has<double>());
    return double_value_;
  }

  template <>
  bool Value() const {
    TC3_CHECK(Has<bool>());
    return bool_value_;
  }

  template <typename T>
  const T& ConstRefValue() const;

  template <>
  const std::string& ConstRefValue() const {
    TC3_CHECK(Has<std::string>());
    return string_value_;
  }

  template <>
  const std::vector<std::string>& ConstRefValue() const {
    TC3_CHECK(Has<std::vector<std::string>>());
    return string_vector_value_;
  }

  template <>
  const std::vector<float>& ConstRefValue() const {
    TC3_CHECK(Has<std::vector<float>>());
    return float_vector_value_;
  }

  template <>
  const std::vector<int>& ConstRefValue() const {
    TC3_CHECK(Has<std::vector<int>>());
    return int_vector_value_;
  }

  template <>
  const std::map<std::string, Variant>& ConstRefValue() const {
    TC3_CHECK((Has<std::map<std::string, Variant>>()));
    return string_variant_map_value_;
  }

  template <typename T>
  bool Has() const;

  template <>
  bool Has<int8>() const {
    return type_ == TYPE_INT8_VALUE;
  }

  template <>
  bool Has<uint8>() const {
    return type_ == TYPE_UINT8_VALUE;
  }

  template <>
  bool Has<int>() const {
    return type_ == TYPE_INT_VALUE;
  }

  template <>
  bool Has<uint>() const {
    return type_ == TYPE_UINT_VALUE;
  }

  template <>
  bool Has<int64>() const {
    return type_ == TYPE_INT64_VALUE;
  }

  template <>
  bool Has<uint64>() const {
    return type_ == TYPE_UINT64_VALUE;
  }

  template <>
  bool Has<float>() const {
    return type_ == TYPE_FLOAT_VALUE;
  }

  template <>
  bool Has<double>() const {
    return type_ == TYPE_DOUBLE_VALUE;
  }

  template <>
  bool Has<bool>() const {
    return type_ == TYPE_BOOL_VALUE;
  }

  template <>
  bool Has<std::string>() const {
    return type_ == TYPE_STRING_VALUE;
  }

  template <>
  bool Has<std::vector<std::string>>() const {
    return type_ == TYPE_STRING_VECTOR_VALUE;
  }

  template <>
  bool Has<std::vector<float>>() const {
    return type_ == TYPE_FLOAT_VECTOR_VALUE;
  }

  template <>
  bool Has<std::vector<int>>() const {
    return type_ == TYPE_INT_VECTOR_VALUE;
  }

  template <>
  bool Has<std::map<std::string, Variant>>() const {
    return type_ == TYPE_STRING_VARIANT_MAP_VALUE;
  }

  // Converts the value of this variant to its string representation, regardless
  // of the type of the actual value.
  std::string ToString() const;

  Type GetType() const { return type_; }

  bool HasValue() const { return type_ != TYPE_EMPTY; }

 private:
  Type type_;
  union {
    int8_t int8_value_;
    uint8_t uint8_value_;
    int int_value_;
    uint uint_value_;
    int64 long_value_;
    uint64 ulong_value_;
    float float_value_;
    double double_value_;
    bool bool_value_;
  };
  std::string string_value_;
  std::vector<std::string> string_vector_value_;
  std::vector<float> float_vector_value_;
  std::vector<int> int_vector_value_;
  std::map<std::string, Variant> string_variant_map_value_;
};

// Pretty-printing function for Variant.
logging::LoggingStringStream& operator<<(logging::LoggingStringStream& stream,
                                         const Variant& value);

}  // namespace libtextclassifier3

#endif  // LIBTEXTCLASSIFIER_UTILS_VARIANT_H_