summaryrefslogtreecommitdiff
path: root/libs/ardour/ardour/variant.h
blob: 712d559c94c924c0921b2b43cf98245cfcd5f996 (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
/*
    Copyright (C) 2014 Paul Davis
    Author: David Robillard

    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.,
    675 Mass Ave, Cambridge, MA 02139, USA.
*/

#ifndef __ardour_variant_h__
#define __ardour_variant_h__

#include <stdint.h>
#include <limits.h>

#include <algorithm>
#include <stdexcept>

#include "ardour/libardour_visibility.h"
#include "pbd/compose.h"

#ifdef COMPILER_MSVC
#undef VOID
#endif

namespace ARDOUR {

/** A value with dynamic type (tagged union). */
class LIBARDOUR_API Variant
{
public:
	enum Type {
		VOID,    ///< Nothing
		BOOL,    ///< Boolean
		DOUBLE,  ///< C double (64-bit IEEE-754)
		FLOAT,   ///< C float (32-bit IEEE-754)
		INT,     ///< Signed 32-bit int
		LONG,    ///< Signed 64-bit int
		PATH,    ///< File path string
		STRING,  ///< Raw string (no semantics)
		URI      ///< URI string
	};

	explicit Variant()              : _type(VOID)   { _long   = 0;     }
	explicit Variant(bool    value) : _type(BOOL)   { _bool   = value; }
	explicit Variant(double  value) : _type(DOUBLE) { _double = value; }
	explicit Variant(float   value) : _type(FLOAT)  { _float  = value; }
	explicit Variant(int32_t value) : _type(INT)    { _int    = value; }
	explicit Variant(int64_t value) : _type(LONG)   { _long   = value; }

	/** Make a variant of a specific string type (string types only) */
	Variant(Type type, const std::string& value)
		: _type(type)
		, _string(value)
	{}

	/** Make a numeric variant from a double (numeric types only).
	 *
	 * If conversion is impossible, the variant will have type VOID.
	 */
	Variant(Type type, double value)
		: _type(type)
	{
		switch (type) {
		case BOOL:
			_bool = value != 0.0;
			break;
		case DOUBLE:
			_double = (double)value;
			break;
		case FLOAT:
			_float = (float)value;
			break;
		case INT:
			_int = (int32_t)lrint(std::max((double)INT32_MIN,
			                               std::min(value, (double)INT32_MAX)));
			break;
		case LONG:
			_long = (int64_t)lrint(std::max((double)INT64_MIN,
			                                std::min(value, (double)INT64_MAX)));
			break;
		default:
			_type = VOID;
			_long = 0;
		}
	}

	/** Convert a numeric variant to a double. */
	double to_double() const {
		switch (_type) {
		case BOOL:   return _bool;
		case DOUBLE: return _double;
		case FLOAT:  return _float;
		case INT:    return _int;
		case LONG:   return _long;
		default:     return 0.0;
		}
	}

	bool   get_bool()   const { ensure_type(BOOL);   return _bool;   }
	double get_double() const { ensure_type(DOUBLE); return _double; }
	float  get_float()  const { ensure_type(FLOAT);  return _float;  }
	int    get_int()    const { ensure_type(INT);    return _int;    }
	long   get_long()   const { ensure_type(LONG);   return _long;   }

	const std::string& get_path()   const { ensure_type(PATH);   return _string; }
	const std::string& get_string() const { ensure_type(STRING); return _string; }
	const std::string& get_uri()    const { ensure_type(URI);    return _string; }

	Type type() const { return _type; }

	static bool type_is_numeric(Type type) {
		switch (type) {
		case BOOL: case DOUBLE: case FLOAT: case INT: case LONG:
			return true;
		default:
			return false;
		}
	}

private:
	static const char* type_name(const Type type) {
		static const char* names[] = {
			"bool", "double", "float", "int", "long", "path", "string", "uri"
		};

		return names[type];
	}

	void ensure_type(const Type type) const {
		if (_type != type) {
			throw std::domain_error(
				string_compose("get_%1 called on %2 variant",
				               type_name(type), type_name(_type)));
		}
	}

	Type        _type;    ///< Type tag
	std::string _string;  ///< For all string types (PATH, STRING, URI)

	// Union of all primitive numeric types
	union {
		bool    _bool;
		double  _double;
		float   _float;
		int32_t _int;
		int64_t _long;
	};
};

} // namespace ARDOUR

#endif // __ardour_variant_h__