summaryrefslogtreecommitdiff
path: root/libs/surfaces/websockets/typed_value.cc
blob: 8e01da9bf92367508eb5a93ea5ad59ebb168cc80 (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
/*
 * Copyright (C) 2020 Luciano Iam <lucianito@gmail.com>
 *
 * 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 2 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, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

#include <boost/lexical_cast.hpp>
#include <cmath>
#include <limits>
#include <string>

#include "typed_value.h"

#define DBL_TOLERANCE 0.001

TypedValue::TypedValue ()
    : _type (Empty)
    , _b (false)
    , _i (0)
    , _d (0)
{
}

TypedValue::TypedValue (bool value)
    : _type (Bool)
    , _b (value)
    , _i (0)
    , _d (0)
{
}

TypedValue::TypedValue (int value)
    : _type (Int)
    , _b (false)
    , _i (value)
    , _d (0)
{
}

TypedValue::TypedValue (double value)
    : _type (Double)
    , _b (false)
    , _i (0)
    , _d (value)
{
}

TypedValue::TypedValue (std::string value)
    : _type (String)
    , _b (false)
    , _i (0)
    , _d (0)
    , _s (value)
{
}

TypedValue::operator bool () const
{
	switch (_type) {
		case Bool:
			return _b;
		case Int:
			return _i != 0;
		case Double:
			return _d != 0;
		case String:
			return _s == "true";
		default:
			return false;
	}
}

TypedValue::operator int () const
{
	switch (_type) {
		case Int:
			return _i;
		case Bool:
			return _b ? 1 : 0;
		case Double:
			return static_cast<int> (_d);
		case String:
			try {
				return boost::lexical_cast<int> (_s);
			} catch (const boost::bad_lexical_cast&) {
				return 0;
			}
		default:
			return 0;
	}
}

TypedValue::operator double () const
{
	switch (_type) {
		case Double:
			return _d;
		case Bool:
			return _b ? 1.f : 0;
		case Int:
			return static_cast<double> (_i);
		case String:
			try {
				return boost::lexical_cast<double> (_s);
			} catch (const boost::bad_lexical_cast&) {
				return 0;
			}
		default:
			return 0;
	}
}

TypedValue::operator std::string () const
{
	switch (_type) {
		case String:
			return _s;
		case Bool:
			return _b ? "true" : "false";
		case Int:
			return boost::lexical_cast<std::string> (_i);
		case Double:
			return boost::lexical_cast<std::string> (_d);
		default:
			return "";
	}
}

bool
TypedValue::operator== (const TypedValue& other) const
{
	if (_type != other._type) {
		/* make an exception when comparing doubles and ints
		 * for example browser json implementations will send
		 * 1 instead of 1.0 removing any type hint
		 */
		if ((_type == Int) && (other._type == Double)) {
			return fabs (static_cast<double> (_i) - other._d) < DBL_TOLERANCE;
		} else if ((_type == Double) && (other._type == Int)) {
			return fabs (_d - static_cast<double> (other._i)) < DBL_TOLERANCE;
		}

		return false;
	}

	switch (_type) {
		case Bool:
			return _b == other._b;
		case Int:
			return _i == other._i;
		case Double: {
			double inf = std::numeric_limits<double>::infinity ();
			return ((_d == inf) && (other._d == inf)) || ((_d == -inf) && (other._d == -inf)) || (fabs (_d - other._d) < DBL_TOLERANCE);
		}
		case String:
			return _s == other._s;
		default:
			return false;
	}
}

bool
TypedValue::operator!= (const TypedValue& other) const
{
	return !(*this == other);
}

std::string
TypedValue::debug_str () const
{
	char s[256];

	sprintf (s, "type = %d; b = %d; i = %d; d = %f; s = \"%s\"",
	         _type, _b, _i, _d, _s.c_str ());

	return s;
}