annotate Source/Utility/Types.h @ 16:61e3c9df4674

Fix bug where TouchKeys standalone mode turns off when mode is changed.
author Andrew McPherson <andrewm@eecs.qmul.ac.uk>
date Mon, 25 Nov 2013 21:36:02 +0000
parents 3580ffe87dc8
children dfff66c07936
rev   line source
andrewm@0 1 /*
andrewm@0 2 TouchKeys: multi-touch musical keyboard control software
andrewm@0 3 Copyright (c) 2013 Andrew McPherson
andrewm@0 4
andrewm@0 5 This program is free software: you can redistribute it and/or modify
andrewm@0 6 it under the terms of the GNU General Public License as published by
andrewm@0 7 the Free Software Foundation, either version 3 of the License, or
andrewm@0 8 (at your option) any later version.
andrewm@0 9
andrewm@0 10 This program is distributed in the hope that it will be useful,
andrewm@0 11 but WITHOUT ANY WARRANTY; without even the implied warranty of
andrewm@0 12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
andrewm@0 13 GNU General Public License for more details.
andrewm@0 14
andrewm@0 15 You should have received a copy of the GNU General Public License
andrewm@0 16 along with this program. If not, see <http://www.gnu.org/licenses/>.
andrewm@0 17
andrewm@0 18 =====================================================================
andrewm@0 19
andrewm@0 20 Types.h: basic types used throughout the program
andrewm@0 21 */
andrewm@0 22
andrewm@0 23 #ifndef KEYCONTROL_TYPES_H
andrewm@0 24 #define KEYCONTROL_TYPES_H
andrewm@0 25
andrewm@0 26 #include <limits>
andrewm@0 27 #include <cstdlib>
andrewm@0 28 #include <cmath>
andrewm@0 29 #include <utility>
andrewm@0 30
andrewm@0 31 #undef FIXED_POINT_TIME
andrewm@0 32
andrewm@0 33 // The following template specializations give the "missing" values for each kind of data that can be used in a Node.
andrewm@0 34 // If an unknown type is added, its "missing" value is whatever comes back from the default constructor. Generally speaking, new
andrewm@0 35 // types should be added to this list as they are used
andrewm@0 36
andrewm@0 37 template<typename T>
andrewm@0 38 struct missing_value {
andrewm@0 39 static const T missing() { return T(); }
andrewm@0 40 static const bool isMissing(T val) { return val == missing(); }
andrewm@0 41 };
andrewm@0 42
andrewm@0 43 template<> struct missing_value<short> {
andrewm@0 44 static const short missing() { return std::numeric_limits<short>::max(); }
andrewm@0 45 static const bool isMissing(short val) { return (val == missing()); }
andrewm@0 46 };
andrewm@0 47 template<> struct missing_value<unsigned short> {
andrewm@0 48 static const unsigned short missing() { return std::numeric_limits<unsigned short>::max(); }
andrewm@0 49 static const bool isMissing(unsigned short val) { return (val == missing()); }
andrewm@0 50 };
andrewm@0 51 template<> struct missing_value<int> {
andrewm@0 52 static const int missing() { return std::numeric_limits<int>::max(); }
andrewm@0 53 static const bool isMissing(int val) { return (val == missing()); }
andrewm@0 54 };
andrewm@0 55 template<> struct missing_value<unsigned int> {
andrewm@0 56 static const unsigned int missing() { return std::numeric_limits<unsigned int>::max(); }
andrewm@0 57 static const bool isMissing(unsigned int val) { return (val == missing()); }
andrewm@0 58 };
andrewm@0 59 template<> struct missing_value<long> {
andrewm@0 60 static const long missing() { return std::numeric_limits<long>::max(); }
andrewm@0 61 static const bool isMissing(long val) { return (val == missing()); }
andrewm@0 62 };
andrewm@0 63 template<> struct missing_value<unsigned long> {
andrewm@0 64 static const unsigned long missing() { return std::numeric_limits<unsigned long>::max(); }
andrewm@0 65 static const bool isMissing(unsigned long val) { return (val == missing()); }
andrewm@0 66 };
andrewm@0 67 template<> struct missing_value<long long> {
andrewm@0 68 static const long long missing() { return std::numeric_limits<long long>::max(); }
andrewm@0 69 static const bool isMissing(long long val) { return (val == missing()); }
andrewm@0 70 };
andrewm@0 71 template<> struct missing_value<unsigned long long> {
andrewm@0 72 static const unsigned long long missing() { return std::numeric_limits<unsigned long long>::max(); }
andrewm@0 73 static const bool isMissing(unsigned long long val) { return (val == missing()); }
andrewm@0 74 };
andrewm@0 75 template<> struct missing_value<float> {
andrewm@0 76 static const float missing() { return std::numeric_limits<float>::quiet_NaN(); }
andrewm@0 77 static const bool isMissing(float val) { return std::isnan(val); }
andrewm@0 78 };
andrewm@0 79 template<> struct missing_value<double> {
andrewm@0 80 static const double missing() { return std::numeric_limits<double>::quiet_NaN(); }
andrewm@0 81 static const bool isMissing(double val) { return std::isnan(val); }
andrewm@0 82 };
andrewm@0 83 template<typename T1, typename T2>
andrewm@0 84 struct missing_value<std::pair<T1,T2> > {
andrewm@0 85 static const std::pair<T1,T2> missing() { return std::pair<T1,T2>(missing_value<T1>::missing(), missing_value<T2>::missing()); }
andrewm@0 86 static const bool isMissing(std::pair<T1,T2> val) {
andrewm@0 87 return missing_value<T1>::isMissing(val.first) && missing_value<T2>::isMissing(val.second);
andrewm@0 88 }
andrewm@0 89 };
andrewm@0 90
andrewm@0 91
andrewm@0 92 // Globally-defined types: these types must be shared by all active units
andrewm@0 93
andrewm@0 94 #ifdef FIXED_POINT_TIME
andrewm@0 95 typedef unsigned long long timestamp_type;
andrewm@0 96 typedef long long timestamp_diff_type;
andrewm@0 97
andrewm@0 98 #define timestamp_abs(x) std::llabs(x)
andrewm@0 99 #define ptime_to_timestamp(x) (x).total_microseconds()
andrewm@0 100 #define timestamp_to_ptime(x) microseconds(x)
andrewm@0 101 #define timestamp_to_milliseconds(x) ((x)/1000ULL)
andrewm@0 102 #define microseconds_to_timestamp(x) (x)
andrewm@0 103 #define milliseconds_to_timestamp(x) ((x)*1000ULL)
andrewm@0 104 #define seconds_to_timestamp(x) ((x)*1000000ULL)
andrewm@0 105
andrewm@0 106 #else /* Floating point time */
andrewm@0 107 typedef double timestamp_type;
andrewm@0 108 typedef double timestamp_diff_type;
andrewm@0 109
andrewm@0 110 #define timestamp_abs(x) std::fabs(x)
andrewm@0 111 #define ptime_to_timestamp(x) ((timestamp_type)(x).total_microseconds()/1000000.0)
andrewm@0 112 #define timestamp_to_ptime(x) microseconds((x)*1000000.0)
andrewm@0 113 #define timestamp_to_milliseconds(x) ((x)*1000.0)
andrewm@0 114 #define microseconds_to_timestamp(x) ((double)(x)/1000000.0)
andrewm@0 115 #define milliseconds_to_timestamp(x) ((double)(x)/1000.0)
andrewm@0 116 #define seconds_to_timestamp(x) (x)
andrewm@0 117
andrewm@0 118 #endif /* FIXED_POINT_TIME */
andrewm@0 119
andrewm@0 120
andrewm@0 121 #endif /* KEYCONTROL_TYPES_H */