Towards MIDI:
[ardour.git] / libs / ardour / ardour / data_type.h
1 /*
2     Copyright (C) 2006 Paul Davis 
3     
4     This program is free software; you can redistribute it and/or modify it
5     under the terms of the GNU General Public License as published by the Free
6     Software Foundation; either version 2 of the License, or (at your option)
7     any later version.
8     
9     This program is distributed in the hope that it will be useful, but WITHOUT
10     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11     FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12     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     675 Mass Ave, Cambridge, MA 02139, USA.
17 */
18
19 #ifndef __ardour_data_type_h__
20 #define __ardour_data_type_h__
21
22 #include <cassert>
23 #include <string>
24 #include <ardour/data_type.h>
25 #include <jack/jack.h>
26
27 namespace ARDOUR {
28
29
30 /** A type of Data Ardour is capable of processing.
31  *
32  * The majority of this class is dedicated to conversion to and from various
33  * other type representations, simple comparison between then, etc.  This code
34  * is deliberately 'ugly' so other code doesn't have to be.
35  */
36 class DataType
37 {
38 public:
39         /// WARNING: make REALLY sure you don't mess up indexes if you change this
40         enum Symbol {
41                 NIL = 0,
42                 AUDIO,
43                 MIDI
44         };
45         
46         /** Number of types (not including NIL).
47          * WARNING: make sure this matches Symbol!
48          */
49         static const size_t num_types = 2;
50
51
52         /** Helper for collections that store typed things by index (BufferSet, PortList).
53          * Guaranteed to be a valid index from 0 to (the number of available types - 1),
54          * because NIL is not included.  No, this isn't pretty - purely for speed.
55          * See DataType::to_index().
56          */
57         inline static size_t symbol_index(const Symbol symbol)
58                 { return (size_t)symbol - 1; }
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                 if (str == "audio")
69                         _symbol = AUDIO;
70                 else if (str == JACK_DEFAULT_AUDIO_TYPE)
71                         _symbol = AUDIO;
72                 else if (str == "midi")
73                         _symbol = MIDI;
74                 else if (str == JACK_DEFAULT_MIDI_TYPE)
75                         _symbol = MIDI;
76                 else
77                         _symbol = NIL;
78         }
79
80         /** Get the Jack type this DataType corresponds to */
81         const char* to_jack_type() const {
82                 switch (_symbol) {
83                         case AUDIO: return JACK_DEFAULT_AUDIO_TYPE;
84                         case MIDI:  return JACK_DEFAULT_MIDI_TYPE;
85                         default:    return "";
86                 }
87         }
88         
89         /** Inverse of the from-string constructor */
90         const char* to_string() const {
91                 switch (_symbol) {
92                         case AUDIO: return "audio";
93                         case MIDI:  return "midi";
94                         default:    return "unknown"; // reeeally shouldn't ever happen
95                 }
96         }
97
98         Symbol        to_symbol() const { return _symbol; }
99         inline size_t to_index() const  { assert(_symbol != NIL); return symbol_index(_symbol); }
100
101         /** DataType iterator, for writing generic loops that iterate over all
102          * available types.
103          */
104         class iterator {
105         public:
106
107                 iterator(size_t index) : _index(index) {}
108
109                 DataType  operator*()  { return DataType((Symbol)_index); }
110                 iterator& operator++() { ++_index; return *this; } // yes, prefix only
111                 bool operator==(const iterator& other) { return (_index == other._index); }
112                 bool operator!=(const iterator& other) { return (_index != other._index); }
113
114         private:
115                 friend class DataType;
116
117                 size_t _index;
118         };
119
120         static iterator begin() { return iterator(1); }
121         static iterator end()   { return iterator(num_types+1); }
122         
123         bool operator==(const Symbol symbol) { return (_symbol == symbol); }
124         bool operator!=(const Symbol symbol) { return (_symbol != symbol); }
125         
126         bool operator==(const DataType other) { return (_symbol == other._symbol); }
127         bool operator!=(const DataType other) { return (_symbol != other._symbol); }
128
129 private:
130         Symbol _symbol;
131 };
132
133
134
135 } // namespace ARDOUR
136
137 #endif // __ardour_data_type_h__
138