Ardour  9.0-pre0-582-g084a23a80d
variant.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2014-2015 David Robillard <d@drobilla.net>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program; if not, write to the Free Software Foundation, Inc.,
16  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17  */
18 
19 #pragma once
20 
21 #include <stdint.h>
22 #include <limits.h>
23 
24 #include <algorithm>
25 #include <stdexcept>
26 
28 #include "temporal/beats.h"
29 #include "pbd/compose.h"
30 
31 namespace ARDOUR {
32 
35 {
36 public:
37  enum Type {
40  BOOL,
43  INT,
44  LONG,
45  PATH,
47  URI
48  };
49 
50  Variant() : _type(NOTHING) { _long = 0; }
51 
52  explicit Variant(bool value) : _type(BOOL) { _bool = value; }
53  explicit Variant(double value) : _type(DOUBLE) { _double = value; }
54  explicit Variant(float value) : _type(FLOAT) { _float = value; }
55  explicit Variant(int32_t value) : _type(INT) { _int = value; }
56  explicit Variant(int64_t value) : _type(LONG) { _long = value; }
57 
58  explicit Variant(const Temporal::Beats& beats)
59  : _type(BEATS)
60  , _beats(beats)
61  {}
62 
64  Variant(Type type, const std::string& value)
65  : _type(type)
66  , _string(value)
67  {}
68 
73  Variant(Type type, double value)
74  : _type(type)
75  {
76  switch (type) {
77  case BOOL:
78  _bool = value != 0.0;
79  break;
80  case DOUBLE:
81  _double = (double)value;
82  break;
83  case FLOAT:
84  _float = (float)value;
85  break;
86  case INT:
87  _int = (int32_t)lrint(std::max((double)INT32_MIN,
88  std::min(value, (double)INT32_MAX)));
89  break;
90  case LONG:
91  _long = (int64_t)lrint(std::max((double)INT64_MIN,
92  std::min(value, (double)INT64_MAX)));
93  break;
94  case BEATS:
95  _beats = Temporal::Beats::from_double (value);
96  break;
97  default:
98  _type = NOTHING;
99  _long = 0;
100  }
101  }
102 
104  double to_double() const {
105  switch (_type) {
106  case BOOL: return _bool;
107  case DOUBLE: return _double;
108  case FLOAT: return _float;
109  case INT: return _int;
110  case LONG: return _long;
111  case BEATS: return Temporal::DoubleableBeats (_beats).to_double();
112  default: return 0.0;
113  }
114  }
115 
116  bool get_bool() const { ensure_type(BOOL); return _bool; }
117  double get_double() const { ensure_type(DOUBLE); return _double; }
118  float get_float() const { ensure_type(FLOAT); return _float; }
119  int get_int() const { ensure_type(INT); return _int; }
120  long get_long() const { ensure_type(LONG); return _long; }
121 
122  bool operator==(bool v) const { return _type == BOOL && _bool == v; }
123  double operator==(double v) const { return _type == DOUBLE && _double == v; }
124  float operator==(float v) const { return _type == FLOAT && _float == v; }
125  int operator==(int v) const { return _type == INT && _int == v; }
126  long operator==(long v) const { return _type == LONG && _long == v; }
127 
128  Variant& operator=(bool v) { _type = BOOL; _bool = v; return *this; }
129  Variant& operator=(double v) { _type = DOUBLE; _double = v; return *this; }
130  Variant& operator=(float v) { _type = FLOAT; _float = v; return *this; }
131  Variant& operator=(int v) { _type = INT; _int = v; return *this; }
132  Variant& operator=(long v) { _type = LONG; _long = v; return *this; }
133 
134  const std::string& get_path() const { ensure_type(PATH); return _string; }
135  const std::string& get_string() const { ensure_type(STRING); return _string; }
136  const std::string& get_uri() const { ensure_type(URI); return _string; }
137 
138  bool operator==(const Variant& v) const {
139  if (_type != v._type) {
140  return false;
141  }
142 
143  switch (_type) {
144  case NOTHING: return true;
145  case BEATS: return _beats == v._beats;
146  case BOOL: return _bool == v._bool;
147  case DOUBLE: return _double == v._double;
148  case FLOAT: return _float == v._float;
149  case INT: return _int == v._int;
150  case LONG: return _long == v._long;
151  case PATH:
152  case STRING:
153  case URI: return _string == v._string;
154  }
155 
156  return false;
157  }
158 
159  bool operator==(const Temporal::Beats& v) const {
160  return _type == BEATS && _beats == v;
161  }
162 
163  bool operator!() const { return _type == NOTHING; }
164 
166  _type = BEATS;
167  _beats = v;
168  return *this;
169  }
170 
171  const Temporal::Beats& get_beats() const {
172  ensure_type(BEATS); return _beats;
173  }
174 
175  Type type() const { return _type; }
176 
177  static bool type_is_numeric(Type type) {
178  switch (type) {
179  case BOOL: case DOUBLE: case FLOAT: case INT: case LONG: case BEATS:
180  return true;
181  default:
182  return false;
183  }
184  }
185 
186 private:
187  static const char* type_name(const Type type) {
188  static const char* names[] = {
189  "bool", "double", "float", "int", "long", "path", "string", "uri"
190  };
191 
192  return names[type];
193  }
194 
195  void ensure_type(const Type type) const {
196  if (_type != type) {
197  throw std::domain_error(
198  string_compose("get_%1 called on %2 variant",
199  type_name(type), type_name(_type)));
200  }
201  }
202 
204  std::string _string;
206 
207  // Union of all primitive numeric types
208  union {
209  bool _bool;
210  double _double;
211  float _float;
212  int32_t _int;
213  int64_t _long;
214  };
215 };
216 
217 } // namespace ARDOUR
218 
long operator==(long v) const
Definition: variant.h:126
double operator==(double v) const
Definition: variant.h:123
bool get_bool() const
Definition: variant.h:116
int get_int() const
Definition: variant.h:119
const std::string & get_path() const
Definition: variant.h:134
Variant(double value)
Definition: variant.h:53
bool operator!() const
Definition: variant.h:163
Variant & operator=(Temporal::Beats v)
Definition: variant.h:165
int32_t _int
Definition: variant.h:212
double _double
Definition: variant.h:210
int operator==(int v) const
Definition: variant.h:125
std::string _string
PATH, STRING, URI.
Definition: variant.h:204
Variant & operator=(bool v)
Definition: variant.h:128
Variant(Type type, const std::string &value)
Definition: variant.h:64
Variant & operator=(int v)
Definition: variant.h:131
const Temporal::Beats & get_beats() const
Definition: variant.h:171
bool operator==(const Variant &v) const
Definition: variant.h:138
const std::string & get_string() const
Definition: variant.h:135
const std::string & get_uri() const
Definition: variant.h:136
float get_float() const
Definition: variant.h:118
Variant(int64_t value)
Definition: variant.h:56
Type _type
Type tag.
Definition: variant.h:203
Variant & operator=(float v)
Definition: variant.h:130
Variant & operator=(long v)
Definition: variant.h:132
Variant(bool value)
Definition: variant.h:52
Variant(Type type, double value)
Definition: variant.h:73
Variant & operator=(double v)
Definition: variant.h:129
Type type() const
Definition: variant.h:175
Variant(int32_t value)
Definition: variant.h:55
Temporal::Beats _beats
BEATS.
Definition: variant.h:205
static bool type_is_numeric(Type type)
Definition: variant.h:177
int64_t _long
Definition: variant.h:213
float operator==(float v) const
Definition: variant.h:124
Variant(const Temporal::Beats &beats)
Definition: variant.h:58
double get_double() const
Definition: variant.h:117
@ BOOL
Boolean.
Definition: variant.h:40
@ LONG
Signed 64-bit int.
Definition: variant.h:44
@ STRING
Raw string (no semantics)
Definition: variant.h:46
@ FLOAT
C float (32-bit IEEE-754)
Definition: variant.h:42
@ INT
Signed 32-bit int.
Definition: variant.h:43
@ NOTHING
Nothing (void)
Definition: variant.h:38
@ DOUBLE
C double (64-bit IEEE-754)
Definition: variant.h:41
@ PATH
File path string.
Definition: variant.h:45
@ BEATS
Beats+ticks.
Definition: variant.h:39
double to_double() const
Definition: variant.h:104
bool operator==(bool v) const
Definition: variant.h:122
long get_long() const
Definition: variant.h:120
static const char * type_name(const Type type)
Definition: variant.h:187
void ensure_type(const Type type) const
Definition: variant.h:195
float _float
Definition: variant.h:211
bool operator==(const Temporal::Beats &v) const
Definition: variant.h:159
Variant(float value)
Definition: variant.h:54
static Beats from_double(double beats)
Definition: beats.h:76
double to_double() const
Definition: beats.h:260
std::string string_compose(const std::string &fmt, const T1 &o1)
Definition: compose.h:246
@ NOTHING
Definition: event.h:57
#define LIBARDOUR_API