summaryrefslogtreecommitdiff
path: root/src/core/common/Variant.cpp
blob: b1a65eab033144ae35139281a9369b2c101be93f (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
/*
    Ousía
    Copyright (C) 2014, 2015  Benjamin Paaßen, Andreas Stöckel

    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/>.
*/

#include <sstream>

#include <core/managed/Managed.hpp>

#include "Logger.hpp"
#include "Utils.hpp"
#include "Variant.hpp"
#include "VariantConverter.hpp"
#include "VariantWriter.hpp"

namespace ousia {

/* Class Variant::TypeException */

Variant::TypeException::TypeException(Type actualType, Type requestedType)
    : OusiaException(std::string("Variant: Requested \"") +
                     Variant::getTypeName(requestedType) +
                     std::string("\" but is \"") +
                     Variant::getTypeName(actualType) + std::string("\"")),
      actualType(actualType),
      requestedType(requestedType)
{
}

/* Class Variant */

const char *Variant::getTypeName(Type type)
{
	switch (type) {
		case Type::NULLPTR:
			return "null";
		case Type::BOOL:
			return "boolean";
		case Type::INT:
			return "integer";
		case Type::DOUBLE:
			return "double";
		case Type::STRING:
			return "string";
		case Type::MAGIC:
			return "magic";
		case Type::ARRAY:
			return "array";
		case Type::MAP:
			return "map";
		case Type::OBJECT:
			return "object";
		case Type::FUNCTION:
			return "function";
	}
	return "unknown";
}

Variant::boolType Variant::toBool() const
{
	ExceptionLogger logger;
	Variant res{*this};
	VariantConverter::toBool(res, logger, VariantConverter::Mode::ALL);
	return res.asBool();
}

Variant::intType Variant::toInt() const
{
	ExceptionLogger logger;
	Variant res{*this};
	VariantConverter::toInt(res, logger, VariantConverter::Mode::ALL);
	return res.asInt();
}

Variant::doubleType Variant::toDouble() const
{
	ExceptionLogger logger;
	Variant res{*this};
	VariantConverter::toDouble(res, logger, VariantConverter::Mode::ALL);
	return res.asDouble();
}

Variant::stringType Variant::toString(bool escape) const
{
	ExceptionLogger logger;
	Variant res{*this};
	VariantConverter::toString(res, logger, VariantConverter::Mode::ALL);
	return res.asString();
}

/* Output stream operators */

std::ostream &operator<<(std::ostream &os, const Variant &v)
{
	VariantWriter::writeJson(v, os, true);
	return os;
}

/* Comparison operators */

bool operator<(const Variant &lhs, const Variant &rhs)
{
	// If the types do not match, we can not do a meaningful comparison.
	if (lhs.getType() != rhs.getType()) {
		throw Variant::TypeException(lhs.getType(), rhs.getType());
	}
	switch (lhs.getType()) {
		case Variant::Type::NULLPTR:
			return false;
		case Variant::Type::BOOL:
			return lhs.boolVal < rhs.boolVal;
		case Variant::Type::INT:
			return lhs.intVal < rhs.intVal;
		case Variant::Type::DOUBLE:
			return lhs.doubleVal < rhs.doubleVal;
		case Variant::Type::MAGIC:
		case Variant::Type::STRING:
			return lhs.asString() < rhs.asString();
		case Variant::Type::ARRAY:
			return lhs.asArray() < rhs.asArray();
		case Variant::Type::MAP:
			return lhs.asMap() < rhs.asMap();
		case Variant::Type::OBJECT:
			return lhs.asObject().get() < rhs.asObject().get();
		case Variant::Type::FUNCTION:
			return lhs.asFunction() < rhs.asFunction();
	}
	throw OusiaException("Internal Error! Unknown type!");
}

bool operator>(const Variant &lhs, const Variant &rhs)
{
	return rhs < lhs;
}

bool operator<=(const Variant &lhs, const Variant &rhs)
{
	return !(lhs > rhs);
}

bool operator>=(const Variant &lhs, const Variant &rhs)
{
	return !(lhs < rhs);
}

bool operator==(const Variant &lhs, const Variant &rhs)
{
	if (lhs.getType() != rhs.getType()) {
		return false;
	}
	switch (lhs.getType()) {
		case Variant::Type::NULLPTR:
			return true;
		case Variant::Type::BOOL:
			return lhs.boolVal == rhs.boolVal;
		case Variant::Type::INT:
			return lhs.intVal == rhs.intVal;
		case Variant::Type::DOUBLE:
			return lhs.doubleVal == rhs.doubleVal;
		case Variant::Type::STRING:
		case Variant::Type::MAGIC:
			return lhs.asString() == rhs.asString();
		case Variant::Type::ARRAY:
			return lhs.asArray() == rhs.asArray();
		case Variant::Type::MAP:
			return lhs.asMap() == rhs.asMap();
		case Variant::Type::OBJECT:
			return lhs.asObject() == rhs.asObject();
		case Variant::Type::FUNCTION:
			return lhs.asFunction() == rhs.asFunction();
	}
	throw OusiaException("Internal Error! Unknown type!");
}

bool operator!=(const Variant &lhs, const Variant &rhs)
{
	return !(lhs == rhs);
}

}