summaryrefslogtreecommitdiff
path: root/libs/midi++2
diff options
context:
space:
mode:
authorDavid Robillard <d@drobilla.net>2014-12-17 19:43:09 -0500
committerDavid Robillard <d@drobilla.net>2014-12-17 19:43:09 -0500
commitd2cafbe95a5784b7c306c24b0008379a41a909f7 (patch)
tree03216b0bd933f4d774634a7f8e14965fcf79c990 /libs/midi++2
parent6e912a0aa31313636e2957ec0aa97d4103ee117f (diff)
Remove some aborts that don't really need to be.
Enforce PatchPrimaryKey sanity at the type level rather than attempting to check for it everywhere. Remove dead file.
Diffstat (limited to 'libs/midi++2')
-rw-r--r--libs/midi++2/midi++/midnam_patch.h54
-rw-r--r--libs/midi++2/midnam_patch.cc16
2 files changed, 35 insertions, 35 deletions
diff --git a/libs/midi++2/midi++/midnam_patch.h b/libs/midi++2/midi++/midnam_patch.h
index 4e5bb29f55..5902bd5c05 100644
--- a/libs/midi++2/midi++/midnam_patch.h
+++ b/libs/midi++2/midi++/midnam_patch.h
@@ -21,6 +21,7 @@
#define MIDNAM_PATCH_H_
#include <algorithm>
+#include <cassert>
#include <iostream>
#include <string>
#include <list>
@@ -43,41 +44,41 @@ namespace Name
struct LIBMIDIPP_API PatchPrimaryKey
{
public:
- int bank_number;
- int program_number;
-
- PatchPrimaryKey (uint8_t a_program_number = 0, uint16_t a_bank_number = 0) {
- bank_number = std::min (a_bank_number, (uint16_t) 16384);
- program_number = std::min (a_program_number, (uint8_t) 127);
- }
-
- bool is_sane() const {
- return ((bank_number >= 0) && (bank_number <= 16384) &&
- (program_number >=0 ) && (program_number <= 127));
- }
+ PatchPrimaryKey (int program_num = 0, int bank_num = 0)
+ : _bank(std::max(0, std::min(bank_num, 16383)))
+ , _program(std::max(0, std::min(program_num, 127)))
+ {}
inline PatchPrimaryKey& operator=(const PatchPrimaryKey& id) {
- bank_number = id.bank_number;
- program_number = id.program_number;
+ _bank = id._bank;
+ _program = id._program;
return *this;
}
inline bool operator==(const PatchPrimaryKey& id) const {
- return (bank_number == id.bank_number && program_number == id.program_number);
+ return (_bank == id._bank &&
+ _program == id._program);
}
- /**
- * obey strict weak ordering or crash in STL containers
- */
+ /** Strict weak ordering. */
inline bool operator<(const PatchPrimaryKey& id) const {
- if (bank_number < id.bank_number) {
+ if (_bank < id._bank) {
return true;
- } else if (bank_number == id.bank_number && program_number < id.program_number) {
+ } else if (_bank == id._bank && _program < id._program) {
return true;
}
-
return false;
}
+
+ void set_bank(int bank) { _bank = std::max(0, std::min(bank, 16383)); }
+ void set_program(int program) { _program = std::max(0, std::min(program, 127)); }
+
+ inline uint16_t bank() const { return _bank; }
+ inline uint8_t program() const { return _program; }
+
+private:
+ uint16_t _bank;
+ uint8_t _program;
};
class PatchBank;
@@ -94,11 +95,11 @@ public:
const std::string& note_list_name() const { return _note_list_name; }
- uint8_t program_number() const { return _id.program_number; }
- void set_program_number(uint8_t n) { _id.program_number = n; }
+ uint8_t program_number() const { return _id.program(); }
+ void set_program_number(uint8_t n) { _id.set_program(n); }
- uint16_t bank_number() const { return _id.bank_number; }
- void set_bank_number (uint16_t n) { _id.bank_number = n; }
+ uint16_t bank_number() const { return _id.bank(); }
+ void set_bank_number (uint16_t n) { _id.set_bank(n); }
const PatchPrimaryKey& patch_primary_key() const { return _id; }
@@ -161,12 +162,10 @@ public:
}
boost::shared_ptr<Patch> find_patch(const PatchPrimaryKey& key) {
- assert(key.is_sane());
return _patch_map[key];
}
boost::shared_ptr<Patch> previous_patch(const PatchPrimaryKey& key) {
- assert(key.is_sane());
for (PatchList::const_iterator i = _patch_list.begin();
i != _patch_list.end();
++i) {
@@ -182,7 +181,6 @@ public:
}
boost::shared_ptr<Patch> next_patch(const PatchPrimaryKey& key) {
- assert(key.is_sane());
for (PatchList::const_iterator i = _patch_list.begin();
i != _patch_list.end();
++i) {
diff --git a/libs/midi++2/midnam_patch.cc b/libs/midi++2/midnam_patch.cc
index 77e13f29e1..e69842e6cd 100644
--- a/libs/midi++2/midnam_patch.cc
+++ b/libs/midi++2/midnam_patch.cc
@@ -58,7 +58,8 @@ static int
initialize_primary_key_from_commands (
const XMLTree& tree, PatchPrimaryKey& id, const XMLNode* node)
{
- id.bank_number = 0;
+ uint16_t bank = 0;
+ uint8_t program = 0;
const XMLNodeList events = node->children();
for (XMLNodeList::const_iterator i = events.begin(); i != events.end(); ++i) {
@@ -69,18 +70,19 @@ initialize_primary_key_from_commands (
const string& value = node->property("Value")->value();
if (control == "0") {
- id.bank_number |= string_to_int(tree, value) << 7;
+ bank |= string_to_int(tree, value) << 7;
} else if (control == "32") {
- id.bank_number |= string_to_int(tree, value);
+ bank |= string_to_int(tree, value);
}
} else if (node->name() == "ProgramChange") {
const string& number = node->property("Number")->value();
assert(number != "");
- id.program_number = string_to_int(tree, number);
+ program = string_to_int(tree, number);
}
}
+ id = PatchPrimaryKey(program, bank);
return 0;
}
@@ -91,7 +93,7 @@ Patch::get_state (void)
/* XXX this is totally wrong */
- node->add_property("Number", string_compose ("%1", _id.program_number));
+ node->add_property("Number", string_compose ("%1", _id.program()));
node->add_property("Name", _name);
/*
@@ -120,7 +122,7 @@ Patch::set_state (const XMLTree& tree, const XMLNode& node)
const XMLProperty* program_change = node.property("ProgramChange");
if (program_change) {
- _id.program_number = string_to_int(tree, program_change->value());
+ _id = PatchPrimaryKey(string_to_int(tree, program_change->value()), _id.bank());
}
const XMLProperty* name = node.property("Name");
@@ -426,7 +428,7 @@ PatchBank::set_state (const XMLTree& tree, const XMLNode& node)
if (initialize_primary_key_from_commands (tree, id, commands)) {
return -1;
}
- _number = id.bank_number;
+ _number = id.bank();
}
XMLNode* patch_name_list = node.child("PatchNameList");