07ba7c60ef3dd45e351c5d5104b04713aa7f3582
[ardour.git] / libs / ardour / ardour / data_type.h
1 /*
2     Copyright (C) 2006 Paul Davis
3     Author: Dave Robillard
4
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)
8     any later version.
9
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
13     for more details.
14
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.
18 */
19
20 #ifndef __ardour_data_type_h__
21 #define __ardour_data_type_h__
22
23 #include <string>
24 #include <jack/jack.h>
25
26 #include "i18n.h"
27
28 namespace ARDOUR {
29
30
31 /** A type of Data Ardour is capable of processing.
32  *
33  * The majority of this class is dedicated to conversion to and from various
34  * other type representations, simple comparison between then, etc.  This code
35  * is deliberately 'ugly' so other code doesn't have to be.
36  */
37 class DataType
38 {
39 public:
40         /** Numeric symbol for this DataType.
41          *
42          * Castable to int for use as an array index (e.g. by ChanCount).
43          * Note this means NIL is (ntypes-1) and guaranteed to change when
44          * types are added, so this number is NOT suitable for serialization,
45          * network, or binary anything.
46          *
47          * WARNING: The number of non-NIL entries here must match num_types.
48          */
49         enum Symbol {
50                 AUDIO = 0,
51                 MIDI = 1,
52                 NIL = 2,
53         };
54
55         /** Number of types (not including NIL).
56          * WARNING: make sure this matches Symbol!
57          */
58         static const uint32_t num_types = 2;
59
60         DataType(const Symbol& symbol)
61         : _symbol(symbol)
62         {}
63
64         /** Construct from a string (Used for loading from XML and Ports)
65          * The string can be as in an XML file (eg "audio" or "midi"), or a
66          * Jack type string (from jack_port_type) */
67         DataType(const std::string& str)
68         : _symbol(NIL) {
69                 if (str == "audio" || str == JACK_DEFAULT_AUDIO_TYPE)
70                         _symbol = AUDIO;
71                 else if (str == "midi" || str == JACK_DEFAULT_MIDI_TYPE)
72                         _symbol = MIDI;
73         }
74
75         /** Get the Jack type this DataType corresponds to */
76         const char* to_jack_type() const {
77                 switch (_symbol) {
78                         case AUDIO: return JACK_DEFAULT_AUDIO_TYPE;
79                         case MIDI:  return JACK_DEFAULT_MIDI_TYPE;
80                         default:    return "";
81                 }
82         }
83
84         /** Inverse of the from-string constructor */
85         const char* to_string() const {
86                 switch (_symbol) {
87                         case AUDIO: return "audio";
88                         case MIDI:  return "midi";
89                         default:    return "unknown"; // reeeally shouldn't ever happen
90                 }
91         }
92
93         const char* to_i18n_string() const {
94                 switch (_symbol) {
95                         case AUDIO: return _("audio");
96                         case MIDI: return _("MIDI");
97                         default: return _("unknown");
98                 }
99         }
100
101         inline operator uint32_t() const { return (uint32_t)_symbol; }
102
103         /** DataType iterator, for writing generic loops that iterate over all
104          * available types.
105          */
106         class iterator {
107         public:
108
109                 iterator(uint32_t index) : _index(index) {}
110
111                 DataType  operator*()  { return DataType((Symbol)_index); }
112                 iterator& operator++() { ++_index; return *this; } // yes, prefix only
113                 bool operator==(const iterator& other) { return (_index == other._index); }
114                 bool operator!=(const iterator& other) { return (_index != other._index); }
115
116         private:
117                 friend class DataType;
118
119                 uint32_t _index;
120         };
121
122         static iterator begin() { return iterator(0); }
123         static iterator end()   { return iterator(num_types); }
124
125         bool operator==(const Symbol symbol) { return (_symbol == symbol); }
126         bool operator!=(const Symbol symbol) { return (_symbol != symbol); }
127
128         bool operator==(const DataType other) { return (_symbol == other._symbol); }
129         bool operator!=(const DataType other) { return (_symbol != other._symbol); }
130
131 private:
132         Symbol _symbol; // could be const if not for the string constructor
133 };
134
135
136
137 } // namespace ARDOUR
138
139 #endif // __ardour_data_type_h__
140