summaryrefslogtreecommitdiff
path: root/libs/ardour/ardour/data_type.h
blob: cbb88afccf89c417b7dc8880ec53ff927ea6d5fa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
/*
    Copyright (C) 2006 Paul Davis 
    
    This program is free software; you can redistribute it and/or modify it
    under the terms of the GNU General Public License as published by the Free
    Software Foundation; either version 2 of the License, or (at your option)
    any later version.
    
    This program is distributed in the hope that it will be useful, but WITHOUT
    ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    for more details.
    
    You should have received a copy of the GNU General Public License along
    with this program; if not, write to the Free Software Foundation, Inc.,
    675 Mass Ave, Cambridge, MA 02139, USA.
*/

#ifndef __ardour_data_type_h__
#define __ardour_data_type_h__

#include <string>
#include <ardour/data_type.h>
#include <jack/jack.h>

namespace ARDOUR {


/** A type of Data Ardour is capable of processing.
 *
 * The majority of this class is dedicated to conversion to and from various
 * other type representations, simple comparison between then, etc.  This code
 * is deliberately 'ugly' so other code doesn't have to be.
 */
class DataType
{
public:
	/// WARNING: make REALLY sure you don't mess up indexes if you change this
	enum Symbol {
		NIL = 0,
		AUDIO,
		MIDI
	};
	
	/** Number of types (not including NIL).
	 * WARNING: make sure this matches Symbol!
	 */
	static const size_t num_types = 2;


	/** Helper for collections that store typed things by index (BufferSet, PortList).
	 * Guaranteed to be a valid index from 0 to (the number of available types - 1),
	 * because NIL is not included.  No, this isn't pretty - purely for speed.
	 * See DataType::to_index().
	 */
	inline static size_t symbol_index(const Symbol symbol)
		{ return (size_t)symbol - 1; }

	DataType(const Symbol& symbol)
	: _symbol(symbol)
	{}

	/** Construct from a string (Used for loading from XML and Ports)
	 * The string can be as in an XML file (eg "audio" or "midi"), or a
	 * Jack type string (from jack_port_type) */
	DataType(const std::string& str) {
		if (str == "audio")
			_symbol = AUDIO;
		else if (str == JACK_DEFAULT_AUDIO_TYPE)
			_symbol = AUDIO;
		else if (str == "midi")
			_symbol = MIDI;
		else if (str == JACK_DEFAULT_MIDI_TYPE)
			_symbol = MIDI;
		else
			_symbol = NIL;
	}

	/** Get the Jack type this DataType corresponds to */
	const char* to_jack_type() const {
		switch (_symbol) {
			case AUDIO: return JACK_DEFAULT_AUDIO_TYPE;
			case MIDI:  return JACK_DEFAULT_MIDI_TYPE;
			default:    return "";
		}
	}
	
	/** Inverse of the from-string constructor */
	const char* to_string() const {
		switch (_symbol) {
			case AUDIO: return "audio";
			case MIDI:  return "midi";
			default:    return "unknown"; // reeeally shouldn't ever happen
		}
	}

	Symbol        to_symbol() const { return _symbol; }
	inline size_t to_index() const  { return symbol_index(_symbol); }

	/** DataType iterator, for writing generic loops that iterate over all
	 * available types.
	 */
	class iterator {
	public:

		iterator(size_t index) : _index(index) {}

		DataType  operator*()  { return DataType((Symbol)_index); }
		iterator& operator++() { ++_index; return *this; } // yes, prefix only
		bool operator==(const iterator& other) { return (_index == other._index); }
		bool operator!=(const iterator& other) { return (_index != other._index); }

	private:
		friend class DataType;

		size_t _index;
	};

	static iterator begin() { return iterator(1); }
	static iterator end()   { return iterator(num_types+1); }
	
	bool operator==(const Symbol symbol) { return (_symbol == symbol); }
	bool operator!=(const Symbol symbol) { return (_symbol != symbol); }
	
	bool operator==(const DataType other) { return (_symbol == other._symbol); }
	bool operator!=(const DataType other) { return (_symbol != other._symbol); }

private:
	Symbol _symbol;
};



} // namespace ARDOUR

#endif // __ardour_data_type_h__