3818a8d74fc26527211621fe568caf3ecbdbd139
[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 <string>
23 #include <ardour/data_type.h>
24 #include <jack/jack.h>
25
26 namespace ARDOUR {
27
28
29 /** A type of Data Ardour is capable of processing.
30  *
31  * The majority of this class is dedicated to conversion to and from various
32  * other type representations, simple comparison between then, etc.  This code
33  * is deliberately 'ugly' so other code doesn't have to be.
34  */
35 class DataType
36 {
37 public:
38         /// WARNING: make REALLY sure you don't mess up indexes if you change this
39         enum Symbol {
40                 NIL = 0,
41                 AUDIO,
42                 MIDI
43         };
44         
45         /** Number of types (not including NIL).
46          * WARNING: make sure this matches Symbol!
47          */
48         static const size_t num_types = 2;
49
50         DataType(const Symbol& symbol)
51         : _symbol(symbol)
52         {}
53
54         /** Construct from a string (Used for loading from XML and Ports)
55          * The string can be as in an XML file (eg "audio" or "midi"), or a
56          * Jack type string (from jack_port_type) */
57         DataType(const std::string& str) {
58                 if (str == "audio")
59                         _symbol = AUDIO;
60                 else if (str == JACK_DEFAULT_AUDIO_TYPE)
61                         _symbol = AUDIO;
62                 else if (str == "midi")
63                         _symbol = MIDI;
64                 else if (str == JACK_DEFAULT_MIDI_TYPE)
65                         _symbol = MIDI;
66                 else
67                         _symbol = NIL;
68         }
69
70         /** Get the Jack type this DataType corresponds to */
71         const char* to_jack_type() const {
72                 switch (_symbol) {
73                         case AUDIO: return JACK_DEFAULT_AUDIO_TYPE;
74                         case MIDI:  return JACK_DEFAULT_MIDI_TYPE;
75                         default:    return "";
76                 }
77         }
78         
79         /** Inverse of the from-string constructor */
80         const char* to_string() const {
81                 switch (_symbol) {
82                         case AUDIO: return "audio";
83                         case MIDI:  return "midi";
84                         default:    return "unknown"; // reeeally shouldn't ever happen
85                 }
86         }
87
88         //Symbol        to_symbol() const { return _symbol; }
89         inline size_t to_index() const { return (size_t)_symbol - 1; }
90
91         /** DataType iterator, for writing generic loops that iterate over all
92          * available types.
93          */
94         class iterator {
95         public:
96
97                 iterator(size_t index) : _index(index) {}
98
99                 DataType  operator*()  { return DataType((Symbol)_index); }
100                 iterator& operator++() { ++_index; return *this; } // yes, prefix only
101                 bool operator==(const iterator& other) { return (_index == other._index); }
102                 bool operator!=(const iterator& other) { return (_index != other._index); }
103
104         private:
105                 friend class DataType;
106
107                 size_t _index;
108         };
109
110         static iterator begin() { return iterator(1); }
111         static iterator end()   { return iterator(num_types+1); }
112         
113         bool operator==(const Symbol symbol) { return (_symbol == symbol); }
114         bool operator!=(const Symbol symbol) { return (_symbol != symbol); }
115         
116         bool operator==(const DataType other) { return (_symbol == other._symbol); }
117         bool operator!=(const DataType other) { return (_symbol != other._symbol); }
118
119 private:
120         Symbol _symbol;
121 };
122
123
124
125 } // namespace ARDOUR
126
127 #endif // __ardour_data_type_h__
128