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"
32 #ifdef PLATFORM_WINDOWS
38 /** A value with dynamic type (tagged union). */
39 class LIBARDOUR_API Variant
45 DOUBLE, ///< C double (64-bit IEEE-754)
46 FLOAT, ///< C float (32-bit IEEE-754)
47 INT, ///< Signed 32-bit int
48 LONG, ///< Signed 64-bit int
49 PATH, ///< File path string
50 STRING, ///< Raw string (no semantics)
54 explicit Variant() : _type(VOID) { _long = 0; }
55 explicit Variant(bool value) : _type(BOOL) { _bool = value; }
56 explicit Variant(double value) : _type(DOUBLE) { _double = value; }
57 explicit Variant(float value) : _type(FLOAT) { _float = value; }
58 explicit Variant(int32_t value) : _type(INT) { _int = value; }
59 explicit Variant(int64_t value) : _type(LONG) { _long = value; }
61 /** Make a variant of a specific string type (string types only) */
62 Variant(Type type, const std::string& value)
67 /** Make a numeric variant from a double (numeric types only).
69 * If conversion is impossible, the variant will have type VOID.
71 Variant(Type type, double value)
79 _double = (double)value;
82 _float = (float)value;
85 _int = (int32_t)lrint(std::max((double)INT32_MIN,
86 std::min(value, (double)INT32_MAX)));
89 _long = (int64_t)lrint(std::max((double)INT64_MIN,
90 std::min(value, (double)INT64_MAX)));
98 /** Convert a numeric variant to a double. */
99 double to_double() const {
101 case BOOL: return _bool;
102 case DOUBLE: return _double;
103 case FLOAT: return _float;
104 case INT: return _int;
105 case LONG: return _long;
110 bool get_bool() const { ensure_type(BOOL); return _bool; }
111 double get_double() const { ensure_type(DOUBLE); return _double; }
112 float get_float() const { ensure_type(FLOAT); return _float; }
113 int get_int() const { ensure_type(INT); return _int; }
114 long get_long() const { ensure_type(LONG); return _long; }
116 const std::string& get_path() const { ensure_type(PATH); return _string; }
117 const std::string& get_string() const { ensure_type(STRING); return _string; }
118 const std::string& get_uri() const { ensure_type(URI); return _string; }
120 Type type() const { return _type; }
122 static bool type_is_numeric(Type type) {
124 case BOOL: case DOUBLE: case FLOAT: case INT: case LONG:
132 static const char* type_name(const Type type) {
133 static const char* names[] = {
134 "bool", "double", "float", "int", "long", "path", "string", "uri"
140 void ensure_type(const Type type) const {
142 throw std::domain_error(
143 string_compose("get_%1 called on %2 variant",
144 type_name(type), type_name(_type)));
148 Type _type; ///< Type tag
149 std::string _string; ///< For all string types (PATH, STRING, URI)
151 // Union of all primitive numeric types
161 } // namespace ARDOUR
163 #endif // __ardour_variant_h__