variant.cpp 9.76 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
	Value value = {};
};

Variant::Variant () {
Ronan's avatar
Ronan committed
54
55
	if (!mPrivate)
		mPrivate = new VariantPrivate();
56
57
}

Ronan's avatar
Ronan committed
58
59
60
61
62
63
64
65
Variant::Variant (Type type) : Variant() {
	if (type != String)
		return;

	L_D(Variant);

	if (!d->value.str)
		d->value.str = new string();
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
}

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

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

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

81
Variant::Variant (unsigned int value) : Variant(UnsignedInt) {
82
83
84
85
	L_D(Variant);
	d->value.ui = value;
}

86
87
88
89
90
91
92
93
94
95
96
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) {
97
98
99
100
	L_D(Variant);
	d->value.l = value;
}

101
Variant::Variant (unsigned long value) : Variant(UnsignedLong) {
102
103
104
105
	L_D(Variant);
	d->value.ul = value;
}

106
Variant::Variant (long long value) : Variant(LongLong) {
107
108
109
110
	L_D(Variant);
	d->value.ll = value;
}

111
Variant::Variant (unsigned long long value) : Variant(UnsignedLongLong) {
112
113
114
115
	L_D(Variant);
	d->value.ull = value;
}

116
117
118
119
120
121
Variant::Variant (char value) : Variant(Char) {
	L_D(Variant);
	d->value.c = value;
}

Variant::Variant (bool value) : Variant(Bool) {
122
123
124
125
	L_D(Variant);
	d->value.b = value;
}

126
Variant::Variant (double value) : Variant(Double) {
127
128
129
130
	L_D(Variant);
	d->value.d = value;
}

131
Variant::Variant (float value) : Variant(Float) {
132
133
134
135
	L_D(Variant);
	d->value.f = value;
}

Ronan's avatar
Ronan committed
136
137
138
Variant::Variant (const std::string &value) : Variant(String) {
	L_D(Variant);
	*d->value.str = value;
139
140
141
}

Variant::~Variant () {
Ronan's avatar
Ronan committed
142
143
144
145
146
	L_D(Variant);
	if (d->type == String)
		delete d->value.str;

	delete mPrivate;
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
}

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 {
Ronan's avatar
Ronan committed
190
191
	L_D(const Variant);
	return d->type != Invalid;
192
193
194
195
196
197
198
199
200
201
}

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

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

Ronan's avatar
Ronan committed
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
// -----------------------------------------------------------------------------
// 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;
}

252
// -----------------------------------------------------------------------------
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
// 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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
	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:
294
			return Utils::stob(*p.value.str);
Ronan's avatar
Ronan committed
295
296
297
298
299
300
301
302
303

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

		default:
			*soFarSoGood = false;
			break;
	}

304
305
306
307
	return false;
}

static inline double getValueAsDouble (const VariantPrivate &p, bool *soFarSoGood) {
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
	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;
	}

343
344
	return 0.0;
}
345

346
347
348
349
350
351
352
353
354
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;
}
355

356
static inline void *getValueAsGeneric (const VariantPrivate &p, bool *soFarSoGood) {
357
	// TODO.
358
359
360
361
362
363
	return nullptr;
}

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

void Variant::getValue (int type, void *value, bool *soFarSoGood) const {
Ronan's avatar
Ronan committed
364
365
	L_ASSERT(type > Invalid && type != MaxDefaultTypes);

366
367
	L_D(const Variant);

Ronan's avatar
Ronan committed
368
	if (type > MaxDefaultTypes) {
369
		*soFarSoGood = false;
Ronan's avatar
Ronan committed
370
		// TODO: Unable to get value. It will be great to support custom types.
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
414
415
416
417
418
419
420
421
422
423
424
425
426
427
		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;
	}
428
429
}

430
431
432
433
434
435
Variant Variant::createGeneric (void *value) {
	Variant variant(Generic);
	variant.getPrivate()->value.g = value;
	return variant;
}

436
LINPHONE_END_NAMESPACE