]> git.sesse.net Git - nageru/blob - shared/midi_mapper_util.h
Move another helper into midi_mapper_util.h.
[nageru] / shared / midi_mapper_util.h
1 #ifndef _MIDI_MAPPER_UTIL_H
2 #define _MIDI_MAPPER_UTIL_H 1
3
4 #include "midi_mapping.pb.h"
5
6 #include <google/protobuf/descriptor.h>
7
8 template <class Proto>
9 inline bool match_controller_helper(const Proto &msg, int field_number, int controller)
10 {
11         using namespace google::protobuf;
12         const FieldDescriptor *descriptor = msg.GetDescriptor()->FindFieldByNumber(field_number);
13         const Reflection *reflection = msg.GetReflection();
14         if (!reflection->HasField(msg, descriptor)) {
15                 return false;
16         }
17         const MIDIControllerProto &controller_proto =
18                 static_cast<const MIDIControllerProto &>(reflection->GetMessage(msg, descriptor));
19         return (controller_proto.controller_number() == controller);
20 }
21
22 template <class Proto>
23 inline bool match_button_helper(const Proto &msg, int field_number, int note)
24 {
25         using namespace google::protobuf;
26         const FieldDescriptor *descriptor = msg.GetDescriptor()->FindFieldByNumber(field_number);
27         const Reflection *reflection = msg.GetReflection();
28         if (!reflection->HasField(msg, descriptor)) {
29                 return false;
30         }
31         const MIDIButtonProto &button_proto =
32                 static_cast<const MIDIButtonProto &>(reflection->GetMessage(msg, descriptor));
33         return (button_proto.note_number() == note);
34 }
35
36 template <class Proto>
37 inline bool match_bank_helper(const Proto &msg, int bank_field_number, int bank)
38 {
39         using namespace google::protobuf;
40         const FieldDescriptor *bank_descriptor = msg.GetDescriptor()->FindFieldByNumber(bank_field_number);
41         const Reflection *reflection = msg.GetReflection();
42         if (!reflection->HasField(msg, bank_descriptor)) {
43                 // No bank set => in all banks.
44                 return true;
45         }
46         return reflection->GetInt32(msg, bank_descriptor) == bank;
47 }
48
49 // Find what MIDI note the given light (as given by field_number) is mapped to, and enable it.
50 template <class Proto>
51 void activate_mapped_light(const Proto &msg, int field_number, std::set<unsigned> *active_lights)
52 {
53         using namespace google::protobuf;
54         const FieldDescriptor *descriptor = msg.GetDescriptor()->FindFieldByNumber(field_number);
55         const Reflection *reflection = msg.GetReflection();
56         if (!reflection->HasField(msg, descriptor)) {
57                 return;
58         }
59         const MIDILightProto &light_proto =
60                 static_cast<const MIDILightProto &>(reflection->GetMessage(msg, descriptor));
61         active_lights->insert(light_proto.note_number());
62 }
63
64 #endif  // !defined(_MIDI_MAPPER_UTIL_H)