diff options
author | David Robillard <d@drobilla.net> | 2014-11-01 23:29:10 -0400 |
---|---|---|
committer | David Robillard <d@drobilla.net> | 2014-11-02 02:10:24 -0500 |
commit | 8a128b33d38172ae525ac798c53bc105bc4e2c64 (patch) | |
tree | 226459f2fec72a9717d12f190d354f72175607dc /libs/ardour/ardour/variant.h | |
parent | 6dfb11c2d08201f1a27818955707590b762f5a40 (diff) |
Automation of LV2 plugin properties.
Work towards ParameterDescriptor being used more universally to describe control characteristics.
Diffstat (limited to 'libs/ardour/ardour/variant.h')
-rw-r--r-- | libs/ardour/ardour/variant.h | 66 |
1 files changed, 61 insertions, 5 deletions
diff --git a/libs/ardour/ardour/variant.h b/libs/ardour/ardour/variant.h index 1e9dda179a..cc483e3cdf 100644 --- a/libs/ardour/ardour/variant.h +++ b/libs/ardour/ardour/variant.h @@ -21,7 +21,9 @@ #define __ardour_variant_h__ #include <stdint.h> +#include <limits.h> +#include <algorithm> #include <stdexcept> #include "ardour/libardour_visibility.h" @@ -45,17 +47,62 @@ public: URI ///< URI string }; - 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(int value) : _type(INT) { _int = value; } - explicit Variant(long value) : _type(LONG) { _long = value; } + 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; } @@ -68,6 +115,15 @@ public: 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[] = { |