Fixed performance (assert/branching/call overhead) issue with DataType.
[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         DataType(const Symbol& symbol)
52         : _symbol(symbol)
53         {}
54
55         /** Construct from a string (Used for loading from XML and Ports)
56          * The string can be as in an XML file (eg "audio" or "midi"), or a
57          * Jack type string (from jack_port_type) */
58         DataType(const std::string& str) {
59                 if (str == "audio")
60                         _symbol = AUDIO;
61                 else if (str == JACK_DEFAULT_AUDIO_TYPE)
62                         _symbol = AUDIO;
63                 else if (str == "midi")
64                         _symbol = MIDI;
65                 else if (str == JACK_DEFAULT_MIDI_TYPE)
66                         _symbol = MIDI;
67                 else
68                         _symbol = NIL;
69         }
70
71         /** Get the Jack type this DataType corresponds to */
72         const char* to_jack_type() const {
73                 switch (_symbol) {
74                         case AUDIO: return JACK_DEFAULT_AUDIO_TYPE;
75                         case MIDI:  return JACK_DEFAULT_MIDI_TYPE;
76                         default:    return "";
77                 }
78         }
79         
80         /** Inverse of the from-string constructor */
81         const char* to_string() const {
82                 switch (_symbol) {
83                         case AUDIO: return "audio";
84                         case MIDI:  return "midi";
85                         default:    return "unknown"; // reeeally shouldn't ever happen
86                 }
87         }
88
89         //Symbol        to_symbol() const { return _symbol; }
90         inline size_t to_index() const { return (size_t)_symbol - 1; }
91
92         /** DataType iterator, for writing generic loops that iterate over all
93          * available types.
94          */
95         class iterator {
96         public:
97
98                 iterator(size_t index) : _index(index) {}
99
100                 DataType  operator*()  { return DataType((Symbol)_index); }
101                 iterator& operator++() { ++_index; return *this; } // yes, prefix only
102                 bool operator==(const iterator& other) { return (_index == other._index); }
103                 bool operator!=(const iterator& other) { return (_index != other._index); }
104
105         private:
106                 friend class DataType;
107
108                 size_t _index;
109         };
110
111         static iterator begin() { return iterator(1); }
112         static iterator end()   { return iterator(num_types+1); }
113         
114         bool operator==(const Symbol symbol) { return (_symbol == symbol); }
115         bool operator!=(const Symbol symbol) { return (_symbol != symbol); }
116         
117         bool operator==(const DataType other) { return (_symbol == other._symbol); }
118         bool operator!=(const DataType other) { return (_symbol != other._symbol); }
119
120 private:
121         Symbol _symbol;
122 };
123
124
125
126 } // namespace ARDOUR
127
128 #endif // __ardour_data_type_h__
129