variant.cpp 10.1 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
}

Variant::Variant (const Variant &src) {
69 70 71 72 73 74 75 76 77 78 79 80
	// Don't call placement new.
	L_ASSERT(!mPrivate);
	mPrivate = new VariantPrivate();

	L_D(Variant);
	d->type = src.getPrivate()->type;

	const VariantPrivate::Value &value = src.getPrivate()->value;
	if (d->type == String)
		d->value.str = new string(*value.str);
	else
		d->value = value;
81 82 83
}

Variant::Variant (Variant &&src) {
84 85 86 87
	// Don't call placement new.
	L_ASSERT(!mPrivate);
	mPrivate = src.mPrivate;
	src.mPrivate = nullptr;
88 89 90 91 92 93 94
}

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

95
Variant::Variant (unsigned int value) : Variant(UnsignedInt) {
96 97 98 99
	L_D(Variant);
	d->value.ui = value;
}

100 101 102 103 104 105 106 107 108 109 110
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) {
111 112 113 114
	L_D(Variant);
	d->value.l = value;
}

115
Variant::Variant (unsigned long value) : Variant(UnsignedLong) {
116 117 118 119
	L_D(Variant);
	d->value.ul = value;
}

120
Variant::Variant (long long value) : Variant(LongLong) {
121 122 123 124
	L_D(Variant);
	d->value.ll = value;
}

125
Variant::Variant (unsigned long long value) : Variant(UnsignedLongLong) {
126 127 128 129
	L_D(Variant);
	d->value.ull = value;
}

130 131 132 133 134 135
Variant::Variant (char value) : Variant(Char) {
	L_D(Variant);
	d->value.c = value;
}

Variant::Variant (bool value) : Variant(Bool) {
136 137 138 139
	L_D(Variant);
	d->value.b = value;
}

140
Variant::Variant (double value) : Variant(Double) {
141 142 143 144
	L_D(Variant);
	d->value.d = value;
}

145
Variant::Variant (float value) : Variant(Float) {
146 147 148 149
	L_D(Variant);
	d->value.f = value;
}

Ronan's avatar
Ronan committed
150 151 152
Variant::Variant (const std::string &value) : Variant(String) {
	L_D(Variant);
	*d->value.str = value;
153 154 155
}

Variant::~Variant () {
Ronan's avatar
Ronan committed
156 157 158 159 160
	L_D(Variant);
	if (d->type == String)
		delete d->value.str;

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

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
204 205
	L_D(const Variant);
	return d->type != Invalid;
206 207 208 209 210 211 212 213 214 215
}

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

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

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

266
// -----------------------------------------------------------------------------
267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284
// 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
285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307
	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:
308
			return Utils::stob(*p.value.str);
Ronan's avatar
Ronan committed
309 310 311 312 313 314 315 316 317

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

		default:
			*soFarSoGood = false;
			break;
	}

318 319 320 321
	return false;
}

static inline double getValueAsDouble (const VariantPrivate &p, bool *soFarSoGood) {
322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356
	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;
	}

357 358
	return 0.0;
}
359

360 361 362 363 364 365 366 367 368
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;
}
369

370
static inline void *getValueAsGeneric (const VariantPrivate &p, bool *soFarSoGood) {
371
	// TODO.
372 373 374 375 376 377
	return nullptr;
}

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

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

380 381
	L_D(const Variant);

Ronan's avatar
Ronan committed
382
	if (type > MaxDefaultTypes) {
383
		*soFarSoGood = false;
Ronan's avatar
Ronan committed
384
		// TODO: Unable to get value. It will be great to support custom types.
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 428 429 430 431 432 433 434 435 436 437 438 439 440 441
		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;
	}
442 443
}

444 445 446 447 448 449
Variant Variant::createGeneric (void *value) {
	Variant variant(Generic);
	variant.getPrivate()->value.g = value;
	return variant;
}

450
LINPHONE_END_NAMESPACE