fix conflicts after merge with master
[ardour.git] / libs / ardour / ardour / data_type.h
1 /*
2     Copyright (C) 2006 Paul Davis
3     Author: David 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 <stdint.h>
25 #include <glib.h>
26
27 namespace ARDOUR {
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         /** Numeric symbol for this DataType.
39          *
40          * Castable to int for use as an array index (e.g. by ChanCount).
41          * Note this means NIL is (ntypes-1) and guaranteed to change when
42          * types are added, so this number is NOT suitable for serialization,
43          * network, or binary anything.
44          *
45          * WARNING: The number of non-NIL entries here must match num_types.
46          */
47         enum Symbol {
48                 AUDIO = 0,
49                 MIDI = 1,
50                 NIL = 2,
51         };
52
53         /** Number of types (not including NIL).
54          * WARNING: make sure this matches Symbol!
55          */
56         static const uint32_t num_types = 2;
57
58         DataType(const Symbol& symbol)
59         : _symbol(symbol)
60         {}
61
62         /** Construct from a string (Used for loading from XML and Ports)
63          * The string can be as in an XML file (eg "audio" or "midi"), or a
64          */
65         DataType(const std::string& str)
66         : _symbol(NIL) {
67                 if (!g_ascii_strncasecmp(str.c_str(), "audio", str.length())) {
68                         _symbol = AUDIO;
69                 } else if (!g_ascii_strncasecmp(str.c_str(), "midi", str.length())) {
70                         _symbol = MIDI;
71                 }
72         }
73
74         /** Inverse of the from-string constructor */
75         const char* to_string() const {
76                 switch (_symbol) {
77                 case AUDIO: return "audio";
78                 case MIDI:  return "midi";
79                 default:    return "unknown"; // reeeally shouldn't ever happen
80                 }
81         }
82     
83         const char* to_i18n_string () const;
84
85         inline operator uint32_t() const { return (uint32_t)_symbol; }
86
87         /** DataType iterator, for writing generic loops that iterate over all
88          * available types.
89          */
90         class iterator {
91         public:
92
93                 iterator(uint32_t index) : _index(index) {}
94
95                 DataType  operator*()  { return DataType((Symbol)_index); }
96                 iterator& operator++() { ++_index; return *this; } // yes, prefix only
97                 bool operator==(const iterator& other) { return (_index == other._index); }
98                 bool operator!=(const iterator& other) { return (_index != other._index); }
99
100         private:
101                 friend class DataType;
102
103                 uint32_t _index;
104         };
105
106         static iterator begin() { return iterator(0); }
107         static iterator end()   { return iterator(num_types); }
108
109         bool operator==(const Symbol symbol) { return (_symbol == symbol); }
110         bool operator!=(const Symbol symbol) { return (_symbol != symbol); }
111
112         bool operator==(const DataType other) { return (_symbol == other._symbol); }
113         bool operator!=(const DataType other) { return (_symbol != other._symbol); }
114
115 private:
116         Symbol _symbol; // could be const if not for the string constructor
117 };
118
119
120 } // namespace ARDOUR
121
122 #endif // __ardour_data_type_h__
123