Fix sketchy casts.
[ardour.git] / libs / ardour / ardour / midi_patch_manager.h
1 /*
2     Copyright (C) 2008 Hans Baier
3
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
18     $Id$
19 */
20
21 #ifndef MIDI_PATCH_MANAGER_H_
22 #define MIDI_PATCH_MANAGER_H_
23
24 #include "midi++/midnam_patch.h"
25
26 namespace ARDOUR {
27         class Session;
28 }
29
30 namespace MIDI
31 {
32
33 namespace Name
34 {
35
36 class MidiPatchManager
37 {
38         /// Singleton
39 private:
40         MidiPatchManager() {};
41         MidiPatchManager( const MidiPatchManager& );
42         MidiPatchManager& operator= (const MidiPatchManager&);
43
44         static MidiPatchManager* _manager;
45
46 public:
47         typedef std::map<std::string, boost::shared_ptr<MIDINameDocument> > MidiNameDocuments;
48
49         virtual ~MidiPatchManager() { _manager = 0; }
50
51         static MidiPatchManager& instance() {
52                 if (_manager == 0) {
53                         _manager = new MidiPatchManager();
54                 }
55                 return *_manager;
56         }
57
58         void set_session (ARDOUR::Session&);
59
60         boost::shared_ptr<MIDINameDocument> document_by_model(std::string model_name)
61                 { return _documents[model_name]; }
62
63         boost::shared_ptr<MasterDeviceNames> master_device_by_model(std::string model_name)
64                 { return _master_devices_by_model[model_name]; }
65
66         boost::shared_ptr<ChannelNameSet> find_channel_name_set(
67                         std::string model,
68                         std::string custom_device_mode,
69                         uint8_t channel) {
70                 boost::shared_ptr<MIDI::Name::MasterDeviceNames> master_device = master_device_by_model(model);
71
72                 if (master_device != 0 && custom_device_mode != "") {
73                         return master_device->channel_name_set_by_device_mode_and_channel(custom_device_mode, channel);
74                 } else {
75                         return boost::shared_ptr<ChannelNameSet>();
76                 }
77         }
78
79
80         boost::shared_ptr<Patch> find_patch(
81                         std::string model,
82                         std::string custom_device_mode,
83                         uint8_t channel,
84                         PatchPrimaryKey patch_key) {
85
86                 boost::shared_ptr<ChannelNameSet> channel_name_set = find_channel_name_set(model, custom_device_mode, channel);
87
88                 if (channel_name_set) {
89                         return  channel_name_set->find_patch(patch_key);
90                 } else {
91                         return boost::shared_ptr<Patch>();
92                 }
93         }
94
95         boost::shared_ptr<Patch> previous_patch(
96                         std::string model,
97                         std::string custom_device_mode,
98                         uint8_t channel,
99                         PatchPrimaryKey patch_key) {
100
101                 boost::shared_ptr<ChannelNameSet> channel_name_set = find_channel_name_set(model, custom_device_mode, channel);
102
103                 if (channel_name_set) {
104                         return  channel_name_set->previous_patch(patch_key);
105                 } else {
106                         return boost::shared_ptr<Patch>();
107                 }
108         }
109
110         boost::shared_ptr<Patch> next_patch(
111                         std::string model,
112                         std::string custom_device_mode,
113                         uint8_t channel,
114                         PatchPrimaryKey patch_key) {
115
116                 boost::shared_ptr<ChannelNameSet> channel_name_set = find_channel_name_set(model, custom_device_mode, channel);
117
118                 if (channel_name_set) {
119                         return  channel_name_set->next_patch(patch_key);
120                 } else {
121                         return boost::shared_ptr<Patch>();
122                 }
123         }
124
125         std::list<std::string> custom_device_mode_names_by_model(std::string model_name) {
126                 if (model_name != "") {
127                         return master_device_by_model(model_name)->custom_device_mode_names();
128                 } else {
129                         return std::list<std::string>();
130                 }
131         }
132
133         const MasterDeviceNames::Models& all_models() const { return _all_models; }
134
135 private:
136         void drop_session();
137         void refresh();
138
139         ARDOUR::Session*                        _session;
140         MidiNameDocuments                       _documents;
141         MIDINameDocument::MasterDeviceNamesList _master_devices_by_model;
142         MasterDeviceNames::Models               _all_models;
143 };
144
145 } // namespace Name
146
147 } // namespace MIDI
148 #endif /* MIDI_PATCH_MANAGER_H_ */