variant.cpp 9.35 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
/*
 * variant.cpp
 * Copyright (C) 2017  Belledonne Communications SARL
 *
 * This program 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.
 *
 * This program 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 this program.  If not, see <http://www.gnu.org/licenses/>.
 */

Ronan's avatar
Ronan committed
19
20
#include "linphone/utils/utils.h"

21
22
23
24
#include "variant.h"

// =============================================================================

25
26
using namespace std;

27
28
29
30
31
32
33
LINPHONE_BEGIN_NAMESPACE

class VariantPrivate {
public:
	union Value {
		int i;
		unsigned int ui;
34
35
		short s;
		unsigned short us;
36
37
38
39
		long l;
		unsigned long ul;
		long long ll;
		unsigned long long ull;
40
		char c;
41
42
43
		bool b;
		double d;
		float f;
Ronan's avatar
Ronan committed
44
45
		string *str;
		void *g;
46
47
	};

Ronan's avatar
Ronan committed
48
49
	// Integer, because type can be a custom type.
	int type = Variant::Invalid;
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
	Value value = {};
};

Variant::Variant () {
	// Nothing. Construct an invalid invariant.
}

Variant::Variant (Type type) {
	// TODO.
}

Variant::Variant (const Variant &src) {
	// TODO.
}

Variant::Variant (Variant &&src) {
	// TODO.
}

Variant::Variant (int value) : Variant(Int) {
	L_D(Variant);
	d->value.i = value;
}

74
Variant::Variant (unsigned int value) : Variant(UnsignedInt) {
75
76
77
78
	L_D(Variant);
	d->value.ui = value;
}

79
80
81
82
83
84
85
86
87
88
89
Variant::Variant (short value) : Variant(Short) {
	L_D(Variant);
	d->value.s = value;
}

Variant::Variant (unsigned short value) : Variant(UnsignedShort) {
	L_D(Variant);
	d->value.us = value;
}

Variant::Variant (long value) : Variant(Long) {
90
91
92
93
	L_D(Variant);
	d->value.l = value;
}

94
Variant::Variant (unsigned long value) : Variant(UnsignedLong) {
95
96
97
98
	L_D(Variant);
	d->value.ul = value;
}

99
Variant::Variant (long long value) : Variant(LongLong) {
100
101
102
103
	L_D(Variant);
	d->value.ll = value;
}

104
Variant::Variant (unsigned long long value) : Variant(UnsignedLongLong) {
105
106
107
108
	L_D(Variant);
	d->value.ull = value;
}

109
110
111
112
113
114
Variant::Variant (char value) : Variant(Char) {
	L_D(Variant);
	d->value.c = value;
}

Variant::Variant (bool value) : Variant(Bool) {
115
116
117
118
	L_D(Variant);
	d->value.b = value;
}

119
Variant::Variant (double value) : Variant(Double) {
120
121
122
123
	L_D(Variant);
	d->value.d = value;
}

124
Variant::Variant (float value) : Variant(Float) {
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
	L_D(Variant);
	d->value.f = value;
}

Variant::Variant (const std::string &value) {
	// TODO.
}

Variant::~Variant () {
	// TODO.
}

bool Variant::operator!= (const Variant &variant) const {
	// TODO.
	return false;
}

bool Variant::operator< (const Variant &variant) const {
	// TODO.
	return false;
}

bool Variant::operator<= (const Variant &variant) const {
	// TODO.
	return false;
}

Variant &Variant::operator= (const Variant &variant) {
	// TODO.
	return *this;
}

Variant &Variant::operator= (Variant &&variant) {
	// TODO.
	return *this;
}

bool Variant::operator== (const Variant &variant) const {
	// TODO.
	return false;
}

bool Variant::operator> (const Variant &variant) const {
	// TODO.
	return false;
}

bool Variant::operator>= (const Variant &variant) const {
	// TODO.
	return false;
}

bool Variant::isValid () const {
	// TODO.
	return false;
}

void Variant::clear () {
	// TODO.
}

void Variant::swap (const Variant &variant) {
	// TODO.
}

Ronan's avatar
Ronan committed
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
// -----------------------------------------------------------------------------
// Number helpers.
// -----------------------------------------------------------------------------

static inline long long getAssumedNumber (const VariantPrivate &p) {
	L_ASSERT(p.type > Variant::Invalid && p.type < Variant::MaxDefaultTypes);

	switch (static_cast<Variant::Type>(p.type)) {
		case Variant::Int:
			return p.value.i;
		case Variant::Short:
			return p.value.s;
		case Variant::Long:
			return p.value.l;
		case Variant::LongLong:
			return p.value.ll;
		case Variant::Char:
			return p.value.c;
		case Variant::Double:
			return p.value.d;
		case Variant::Float:
			return p.value.f;

		default:
			L_ASSERT(false);
	}

	return 0;
}

static inline unsigned long long getAssumedUnsignedNumber (const VariantPrivate &p) {
	L_ASSERT(p.type > Variant::Invalid && p.type < Variant::MaxDefaultTypes);

	switch (static_cast<Variant::Type>(p.type)) {
		case Variant::UnsignedInt:
			return p.value.ui;
		case Variant::UnsignedShort:
			return p.value.us;
		case Variant::UnsignedLong:
			return p.value.ul;
		case Variant::UnsignedLongLong:
			return p.value.ull;

		default:
			L_ASSERT(false);
	}

	return 0;
}

240
// -----------------------------------------------------------------------------
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
// Number conversions.
// -----------------------------------------------------------------------------

static inline long long getValueAsNumber (const VariantPrivate &p, bool *soFarSoGood) {
	// TODO.
	return 0;
}

static inline unsigned long long getValueAsUnsignedNumber (const VariantPrivate &p, bool *soFarSoGood) {
	// TODO.
	return 0;
}

// -----------------------------------------------------------------------------
// Specific conversions.
// -----------------------------------------------------------------------------

static inline bool getValueAsBool (const VariantPrivate &p, bool *soFarSoGood) {
Ronan's avatar
Ronan committed
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
	L_ASSERT(p.type > Variant::Invalid && p.type < Variant::MaxDefaultTypes);

	*soFarSoGood = true;
	switch (static_cast<Variant::Type>(p.type)) {
		case Variant::Int:
		case Variant::Short:
		case Variant::Long:
		case Variant::LongLong:
		case Variant::Char:
		case Variant::Double:
		case Variant::Float:
			return static_cast<bool>(getAssumedNumber(p));

		case Variant::UnsignedInt:
		case Variant::UnsignedShort:
		case Variant::UnsignedLong:
		case Variant::UnsignedLongLong:
			return static_cast<bool>(getAssumedUnsignedNumber(p));

		case Variant::Bool:
			return p.value.b;

		case Variant::String:
282
			return Utils::stob(*p.value.str);
Ronan's avatar
Ronan committed
283
284
285
286
287
288
289
290
291

		case Variant::Generic:
			return static_cast<bool>(p.value.g);

		default:
			*soFarSoGood = false;
			break;
	}

292
293
294
295
	return false;
}

static inline double getValueAsDouble (const VariantPrivate &p, bool *soFarSoGood) {
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
324
325
326
327
328
329
330
	L_ASSERT(p.type > Variant::Invalid && p.type < Variant::MaxDefaultTypes);

	*soFarSoGood = true;
	switch (static_cast<Variant::Type>(p.type)) {
		case Variant::Int:
		case Variant::Short:
		case Variant::Long:
		case Variant::LongLong:
		case Variant::Char:
		case Variant::Float:
			return static_cast<double>(getAssumedNumber(p));

		case Variant::UnsignedInt:
		case Variant::UnsignedShort:
		case Variant::UnsignedLong:
		case Variant::UnsignedLongLong:
			return static_cast<double>(getAssumedUnsignedNumber(p));

		case Variant::Double:
			return p.value.d;

		case Variant::Bool:
			return static_cast<double>(p.value.b);

		case Variant::String:
			return Utils::stod(*p.value.str);

		case Variant::Generic:
			return static_cast<double>(!!p.value.g);

		default:
			*soFarSoGood = false;
			break;
	}

331
332
	return 0.0;
}
333

334
335
336
337
338
339
340
341
342
static inline float getValueAsFloat (const VariantPrivate &p, bool *soFarSoGood) {
	// TODO.
	return 0.f;
}

static inline float getValueAsString (const VariantPrivate &p, bool *soFarSoGood) {
	// TODO.
	return 0.f;
}
343

344
static inline void *getValueAsGeneric (const VariantPrivate &p, bool *soFarSoGood) {
345
	// TODO.
346
347
348
349
350
351
352
353
	return nullptr;
}

// -----------------------------------------------------------------------------

void Variant::getValue (int type, void *value, bool *soFarSoGood) const {
	L_D(const Variant);

Ronan's avatar
Ronan committed
354
	if (type <= Invalid || type >= MaxDefaultTypes) {
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
		*soFarSoGood = false;
		// Unable to get value. It will be great to support custom types.
		return;
	}

	switch (static_cast<Type>(type)) {
		// Cast as Number.
		case Int:
			*static_cast<int *>(value) = static_cast<int>(getValueAsNumber(*d, soFarSoGood));
			break;
		case Short:
			*static_cast<short *>(value) = static_cast<short>(getValueAsNumber(*d, soFarSoGood));
			break;
		case Long:
			*static_cast<long *>(value) = static_cast<long>(getValueAsNumber(*d, soFarSoGood));
			break;
		case LongLong:
			*static_cast<long long *>(value) = getValueAsNumber(*d, soFarSoGood);
			break;
		case Char:
			*static_cast<char *>(value) = static_cast<char>(getValueAsNumber(*d, soFarSoGood));
			break;

		// Cast as Unsigned number.
		case UnsignedInt:
			*static_cast<unsigned int *>(value) = static_cast<unsigned int>(getValueAsNumber(*d, soFarSoGood));
			break;
		case UnsignedShort:
			*static_cast<unsigned short *>(value) = static_cast<unsigned short>(getValueAsNumber(*d, soFarSoGood));
			break;
		case UnsignedLong:
			*static_cast<unsigned long *>(value) = static_cast<unsigned long>(getValueAsNumber(*d, soFarSoGood));
			break;
		case UnsignedLongLong:
			*static_cast<unsigned long long *>(value) = getValueAsNumber(*d, soFarSoGood);
			break;

		// Cast as specific value.
		case Bool:
			*static_cast<bool *>(value) = getValueAsBool(*d, soFarSoGood);
			break;
		case Double:
			*static_cast<double *>(value) = getValueAsDouble(*d, soFarSoGood);
			break;
		case Float:
			*static_cast<float *>(value) = getValueAsFloat(*d, soFarSoGood);
			break;
		case String:
			*static_cast<string *>(value) = getValueAsString(*d, soFarSoGood);
			break;
		case Generic:
			*static_cast<void **>(value) = getValueAsGeneric(*d, soFarSoGood);
			break;

		case Invalid:
		case MaxDefaultTypes:
			*soFarSoGood = false;
			break;
	}
414
415
416
}

LINPHONE_END_NAMESPACE