2 Copyright (C) 2014 Paul Davis
3 Author: David Robillard
5 This program is free software; you can redistribute it and/or modify it
6 under the terms of the GNU General Public License as published by the Free
7 Software Foundation; either version 2 of the License, or (at your option)
10 This program is distributed in the hope that it will be useful, but WITHOUT
11 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 You should have received a copy of the GNU General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 675 Mass Ave, Cambridge, MA 02139, USA.
20 #ifndef __ardour_variant_h__
21 #define __ardour_variant_h__
29 #include "ardour/libardour_visibility.h"
30 #include "pbd/compose.h"
34 /** A value with dynamic type (tagged union). */
35 class LIBARDOUR_API Variant
39 NOTHING, ///< Nothing (void)
41 DOUBLE, ///< C double (64-bit IEEE-754)
42 FLOAT, ///< C float (32-bit IEEE-754)
43 INT, ///< Signed 32-bit int
44 LONG, ///< Signed 64-bit int
45 PATH, ///< File path string
46 STRING, ///< Raw string (no semantics)
50 explicit Variant() : _type(NOTHING) { _long = 0; }
51 explicit Variant(bool value) : _type(BOOL) { _bool = value; }
52 explicit Variant(double value) : _type(DOUBLE) { _double = value; }
53 explicit Variant(float value) : _type(FLOAT) { _float = value; }
54 explicit Variant(int32_t value) : _type(INT) { _int = value; }
55 explicit Variant(int64_t value) : _type(LONG) { _long = value; }
57 /** Make a variant of a specific string type (string types only) */
58 Variant(Type type, const std::string& value)
63 /** Make a numeric variant from a double (numeric types only).
65 * If conversion is impossible, the variant will have type NOTHING.
67 Variant(Type type, double value)
75 _double = (double)value;
78 _float = (float)value;
81 _int = (int32_t)lrint(std::max((double)INT32_MIN,
82 std::min(value, (double)INT32_MAX)));
85 _long = (int64_t)lrint(std::max((double)INT64_MIN,
86 std::min(value, (double)INT64_MAX)));
94 /** Convert a numeric variant to a double. */
95 double to_double() const {
97 case BOOL: return _bool;
98 case DOUBLE: return _double;
99 case FLOAT: return _float;
100 case INT: return _int;
101 case LONG: return _long;
106 bool get_bool() const { ensure_type(BOOL); return _bool; }
107 double get_double() const { ensure_type(DOUBLE); return _double; }
108 float get_float() const { ensure_type(FLOAT); return _float; }
109 int get_int() const { ensure_type(INT); return _int; }
110 long get_long() const { ensure_type(LONG); return _long; }
112 const std::string& get_path() const { ensure_type(PATH); return _string; }
113 const std::string& get_string() const { ensure_type(STRING); return _string; }
114 const std::string& get_uri() const { ensure_type(URI); return _string; }
116 Type type() const { return _type; }
118 static bool type_is_numeric(Type type) {
120 case BOOL: case DOUBLE: case FLOAT: case INT: case LONG:
128 static const char* type_name(const Type type) {
129 static const char* names[] = {
130 "bool", "double", "float", "int", "long", "path", "string", "uri"
136 void ensure_type(const Type type) const {
138 throw std::domain_error(
139 string_compose("get_%1 called on %2 variant",
140 type_name(type), type_name(_type)));
144 Type _type; ///< Type tag
145 std::string _string; ///< For all string types (PATH, STRING, URI)
147 // Union of all primitive numeric types
157 } // namespace ARDOUR
159 #endif // __ardour_variant_h__