Skip to content

Commit

Permalink
Refactoring of constants
Browse files Browse the repository at this point in the history
  • Loading branch information
mauer committed Apr 8, 2024
1 parent 7ca781c commit 936c99e
Show file tree
Hide file tree
Showing 21 changed files with 321 additions and 275 deletions.
10 changes: 5 additions & 5 deletions src/common/map.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -274,19 +274,19 @@ void map::read_data_1(text_logger& in_log, toml::value& in_data)
m_data_1_type = map_data_1_type::note;

in_log.debug_param(in_data.location().line(), c_cfg_note, std::to_string(m_data_1));
} else if (in_data.contains(CFG_KEY_PITCH_BEND)) {
} else if (in_data.contains(c_cfg_pitch.data())) {
m_data_1 = 0; // fixed value for pitch bend messages
m_data_1_type = map_data_1_type::pitch_bend;

in_log.debug_line(in_data.location().line(),
fmt::format("Parameter '{}' = '{}' (fixed value for pitch bend)",
CFG_KEY_PITCH_BEND,
c_cfg_pitch,
std::to_string(m_data_1)));
} else if (in_data.contains(CFG_KEY_PROGRAM_CHANGE)) {
m_data_1 = static_cast<char>(in_data[CFG_KEY_PROGRAM_CHANGE].as_integer());
} else if (in_data.contains(c_cfg_program_change.data())) {
m_data_1 = static_cast<char>(in_data[c_cfg_program_change.data()].as_integer());
m_data_1_type = map_data_1_type::program_change;

in_log.debug_param(in_data.location().line(), CFG_KEY_PROGRAM_CHANGE, std::to_string(m_data_1));
in_log.debug_param(in_data.location().line(), c_cfg_program_change, std::to_string(m_data_1));
} else {
in_log.error_line(in_data.location().line(), "Parameter for MIDI type is missing");
}
Expand Down
2 changes: 2 additions & 0 deletions src/common/map.h
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,8 @@ class map {
static constexpr std::string_view c_cfg_ch {"ch"};
static constexpr std::string_view c_cfg_cc {"cc"};
static constexpr std::string_view c_cfg_note {"note"};
static constexpr std::string_view c_cfg_pitch {"pitch"};
static constexpr std::string_view c_cfg_program_change {"prg"};
static constexpr std::string_view c_cfg_sl {"sl"};

unsigned int m_no {0};
Expand Down
38 changes: 0 additions & 38 deletions src/common/types.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,52 +77,14 @@ const char* const COMMAND_RELOAD_PROFILE = "RELOAD_PROFILE";
const char* const COMMAND_TOGGLE_SUBLAYER = "TOGGLE_SUBLAYER";

// Keys for the config files
const char* const CFG_KEY_COMMAND_DOWN = "command_down";
const char* const CFG_KEY_COMMAND_FAST_DOWN = "command_fast_down";
const char* const CFG_KEY_COMMAND_FAST_UP = "command_fast_up";
const char* const CFG_KEY_COMMAND_UP = "command_up";
const char* const CFG_KEY_COMMON_PROFILE = "common_profile";
const char* const CFG_KEY_DATAREF = "dataref";
const char* const CFG_KEY_DATAREF_PUSH = "dataref_push";
const char* const CFG_KEY_DATAREF_PULL = "dataref_pull";
const char* const CFG_KEY_DEBUG_MODE = "debug_mode";
const char* const CFG_KEY_DEFAULT_MIDI_SORT = "default_midi_sort";
const char* const CFG_KEY_DEFAULT_OUTBOUND_DELAY = "default_outbound_delay";
const char* const CFG_KEY_DEFAULT_TEXT_SORT = "default_text_sort";
const char* const CFG_KEY_DELAY = "delay";
const char* const CFG_KEY_DEVICE = "device";
const char* const CFG_KEY_LOG_MIDI = "log_midi";
const char* const CFG_KEY_MAPPING_IN = "mapping_in";
const char* const CFG_KEY_MAPPING_INIT = "mapping_init";
const char* const CFG_KEY_MAPPING_OUT = "mapping_out";
const char* const CFG_KEY_MODE = "mode";
const char* const CFG_KEY_MODE_NOTE = "mode_note";
const char* const CFG_KEY_MODE_OUT = "mode_out";
const char* const CFG_KEY_MODIFIER_DOWN = "modifier_down";
const char* const CFG_KEY_MODIFIER_FAST_DOWN = "modifier_fast_down";
const char* const CFG_KEY_MODIFIER_FAST_UP = "modifier_fast_up";
const char* const CFG_KEY_MODIFIER_UP = "modifier_up";
const char* const CFG_KEY_NAME = "name";
const char* const CFG_KEY_NOTE_NAME = "note";
const char* const CFG_KEY_OUTBOUND_DELAY = "outbound_delay";
const char* const CFG_KEY_PITCH_BEND = "pitch";
const char* const CFG_KEY_PROGRAM_CHANGE = "prg";
const char* const CFG_KEY_PORT_IN = "port_in";
const char* const CFG_KEY_PORT_OUT = "port_out";
const char* const CFG_KEY_SHOW_ERRORS = "show_message_dialog";
const char* const CFG_KEY_SL_DATAREF = "sl_dataref";
const char* const CFG_KEY_TEXT = "text";
const char* const CFG_KEY_VALUE = "value";
const char* const CFG_KEY_VALUES = "values";
const char* const CFG_KEY_VALUES_PUSH = "values_push";
const char* const CFG_KEY_VALUES_PULL = "values_pull";
const char* const CFG_KEY_VALUE_MIN = "value_min";
const char* const CFG_KEY_VALUE_MAX = "value_max";
const char* const CFG_KEY_VALUE_WRAP = "value_wrap";
const char* const CFG_KEY_VALUE_ON = "value_on";
const char* const CFG_KEY_VALUE_OFF = "value_off";
const char* const CFG_KEY_VIRTUAL_CHANNEL = "virtual_channel";
const char* const CFG_KEY_VIRTUAL_DEVICE = "virtual_device";
const char* const CFG_KEY_INFO_DISABLED = "info_disabled";
const char* const CFG_KEY_INFO_OFFSET_X = "info_offset_x";
const char* const CFG_KEY_INFO_OFFSET_Y = "info_offset_y";
Expand Down
22 changes: 11 additions & 11 deletions src/device/map_in/map_in_drf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,24 +61,24 @@ void map_in_drf::read_config(text_logger& in_log, toml::value& in_data, toml::va
map_in_label::read_config(in_log, in_data, in_config);

// read mode
m_mode = dataref_mode_from_code(toml_utils::read_string(in_log, in_data, CFG_KEY_MODE));
m_mode = dataref_mode_from_code(toml_utils::read_string(in_log, in_data, c_cfg_mode));

// read dataref
m_dataref = toml_utils::read_string(in_log, in_data, CFG_KEY_DATAREF);
m_dataref = toml_utils::read_string(in_log, in_data, c_cfg_dataref);

// check if a values array has been defined
m_values = toml_utils::read_str_vector_array(in_log, in_data, CFG_KEY_VALUES);
m_values = toml_utils::read_str_vector_array(in_log, in_data, c_cfg_values);

if (m_values.empty()) {
// read value on
std::string value = toml_utils::read_string(in_log, in_data, CFG_KEY_VALUE_ON);
std::string value = toml_utils::read_string(in_log, in_data, c_cfg_value_on);

if (!value.empty())
m_values.push_back(value);

// read value off
if (toml_utils::contains(in_log, in_data, CFG_KEY_VALUE_OFF))
value = toml_utils::read_string(in_log, in_data, CFG_KEY_VALUE_OFF);
if (toml_utils::contains(in_log, in_data, c_cfg_value_off))
value = toml_utils::read_string(in_log, in_data, c_cfg_value_off);

if (!value.empty())
m_values.push_back(value);
Expand All @@ -98,13 +98,13 @@ bool map_in_drf::check(text_logger& in_log, const device_settings& in_dev_settin

if (m_dataref.empty()) {
in_log.error(source_line());
in_log.error(" --> Parameter '" + std::string(CFG_KEY_DATAREF) + "' is empty");
in_log.error(fmt::format(" --> Parameter '{}' is empty", c_cfg_dataref));
result = false;
}

if (!env().drf().check(m_dataref)) {
in_log.error(source_line());
in_log.error(" --> Dataref '" + m_dataref + "' not found");
in_log.error(fmt::format(" --> Dataref '{}' not found", m_dataref));
result = false;
}

Expand All @@ -116,8 +116,7 @@ bool map_in_drf::check(text_logger& in_log, const device_settings& in_dev_settin

if (m_mode == dataref_mode::momentary && m_values.size() != 2) {
in_log.error(source_line());
in_log.error(" --> When parameter '" + std::string(CFG_KEY_MODE)
+ "' is 'momentary', two values (on/off) are expected");
in_log.error(fmt::format(" --> When parameter '{}' is 'momentary', two values (on/off) are expected", c_cfg_mode));
result = false;
}

Expand Down Expand Up @@ -235,7 +234,8 @@ std::string map_in_drf::build_mapping_text(bool in_short)

map_str.append("Values = [");

for (const auto& value: m_values) map_str.append(" '" + value + "', ");
for (const auto& value: m_values)
map_str.append(" '" + value + "', ");

map_str.append("]" + sep_str);

Expand Down
11 changes: 9 additions & 2 deletions src/device/map_in/map_in_drf.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,15 +56,22 @@ class map_in_drf : public map_in_label {
std::string build_mapping_text(bool in_short) override;

private:
// functions
dataref_mode dataref_mode_from_code(std::string_view in_mode) const;
// constants
static constexpr std::string_view c_cfg_dataref {"dataref"};
static constexpr std::string_view c_cfg_mode {"mode"};
static constexpr std::string_view c_cfg_values {"values"};
static constexpr std::string_view c_cfg_value_on {"value_on"};
static constexpr std::string_view c_cfg_value_off {"value_off"};

// members
dataref_mode m_mode {dataref_mode::toggle};

std::string m_dataref {};

std::vector<std::string> m_values {};

// functions
[[nodiscard]] dataref_mode dataref_mode_from_code(std::string_view in_mode) const;
};

} // Namespace xmidictrl
Expand Down
52 changes: 26 additions & 26 deletions src/device/map_in/map_in_enc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,73 +62,73 @@ void map_in_enc::read_config(text_logger& in_log, toml::value& in_data, toml::va
map_in_label::read_config(in_log, in_data, in_config);

// read the mode
if (toml_utils::contains(in_log, in_data, CFG_KEY_MODE))
m_enc_mode = encoder_mode_from_code(toml_utils::read_string(in_log, in_data, CFG_KEY_MODE));
if (toml_utils::contains(in_log, in_data, c_cfg_mode))
m_enc_mode = encoder_mode_from_code(toml_utils::read_string(in_log, in_data, c_cfg_mode));

// read the delay (if defined)
if (toml_utils::contains(in_log, in_data, CFG_KEY_DELAY))
m_delay = toml_utils::read_int(in_log, in_data, CFG_KEY_DELAY);
if (toml_utils::contains(in_log, in_data, c_cfg_delay))
m_delay = toml_utils::read_int(in_log, in_data, c_cfg_delay);

// check if dataref was defined
if (toml_utils::contains(in_log, in_data, CFG_KEY_DATAREF)) {
if (toml_utils::contains(in_log, in_data, c_cfg_dataref)) {
in_log.debug_line(in_data.location().line(), "Use 'dataref' mode for encoder mapping");
m_enc_map_type = encoder_map_type::dataref;

// read dataref
m_dataref = toml_utils::read_string(in_log, in_data, CFG_KEY_DATAREF);
m_dataref = toml_utils::read_string(in_log, in_data, c_cfg_dataref);

// read modifier up
m_modifier_up = toml_utils::read_float(in_log, in_data, CFG_KEY_MODIFIER_UP);
m_modifier_up = toml_utils::read_float(in_log, in_data, c_cfg_modifier_up);

// read modifier down
m_modifier_down = toml_utils::read_float(in_log, in_data, CFG_KEY_MODIFIER_DOWN);
m_modifier_down = toml_utils::read_float(in_log, in_data, c_cfg_modifier_down);

// read modifier fast up
if (toml_utils::contains(in_log, in_data, CFG_KEY_MODIFIER_FAST_UP))
m_modifier_fast_up = toml_utils::read_float(in_log, in_data, CFG_KEY_MODIFIER_FAST_UP);
if (toml_utils::contains(in_log, in_data, c_cfg_modifier_fast_up))
m_modifier_fast_up = toml_utils::read_float(in_log, in_data, c_cfg_modifier_fast_up);
else
m_modifier_fast_up = m_modifier_up;

// read modifier fast down
if (toml_utils::contains(in_log, in_data, CFG_KEY_MODIFIER_FAST_DOWN))
m_modifier_fast_down = toml_utils::read_float(in_log, in_data, CFG_KEY_MODIFIER_FAST_DOWN);
if (toml_utils::contains(in_log, in_data, c_cfg_modifier_fast_down))
m_modifier_fast_down = toml_utils::read_float(in_log, in_data, c_cfg_modifier_fast_down);
else
m_modifier_fast_down = m_modifier_down;

// read value min
if (toml_utils::contains(in_log, in_data, CFG_KEY_VALUE_MIN)) {
m_value_min = toml_utils::read_float(in_log, in_data, CFG_KEY_VALUE_MIN, false);
if (toml_utils::contains(in_log, in_data, c_cfg_value_min)) {
m_value_min = toml_utils::read_float(in_log, in_data, c_cfg_value_min, false);
m_value_min_defined = true;
}

// read value max
if (toml_utils::contains(in_log, in_data, CFG_KEY_VALUE_MAX)) {
m_value_max = toml_utils::read_float(in_log, in_data, CFG_KEY_VALUE_MAX, false);
if (toml_utils::contains(in_log, in_data, c_cfg_value_max)) {
m_value_max = toml_utils::read_float(in_log, in_data, c_cfg_value_max, false);
m_value_max_defined = true;
}

// read value wrap
if (toml_utils::contains(in_log, in_data, CFG_KEY_VALUE_WRAP))
m_value_wrap = toml_utils::read_bool(in_log, in_data, CFG_KEY_VALUE_WRAP);
if (toml_utils::contains(in_log, in_data, c_cfg_value_wrap))
m_value_wrap = toml_utils::read_bool(in_log, in_data, c_cfg_value_wrap);
} else {
in_log.debug_line(in_data.location().line(), "Use 'command' mode for encoder mapping");
m_enc_map_type = encoder_map_type::command;

// read command up
m_command_up = toml_utils::read_string(in_log, in_data, CFG_KEY_COMMAND_UP);
m_command_up = toml_utils::read_string(in_log, in_data, c_cfg_command_up);

// read command down
m_command_down = toml_utils::read_string(in_log, in_data, CFG_KEY_COMMAND_DOWN);
m_command_down = toml_utils::read_string(in_log, in_data, c_cfg_command_down);

// read fast command up
if (toml_utils::contains(in_log, in_data, CFG_KEY_COMMAND_FAST_UP))
m_command_fast_up = toml_utils::read_string(in_log, in_data, CFG_KEY_COMMAND_FAST_UP);
if (toml_utils::contains(in_log, in_data, c_cfg_command_fast_up))
m_command_fast_up = toml_utils::read_string(in_log, in_data, c_cfg_command_fast_up);
else
m_command_fast_up = m_command_up;

// read fast command down
if (toml_utils::contains(in_log, in_data, CFG_KEY_COMMAND_FAST_DOWN))
m_command_fast_down = toml_utils::read_string(in_log, in_data, CFG_KEY_COMMAND_FAST_DOWN);
if (toml_utils::contains(in_log, in_data, c_cfg_command_fast_down))
m_command_fast_down = toml_utils::read_string(in_log, in_data, c_cfg_command_fast_down);
else
m_command_fast_down = m_command_down;
}
Expand Down Expand Up @@ -184,8 +184,8 @@ bool map_in_enc::check(text_logger& in_log, const device_settings& in_dev_settin
if (m_value_min_defined && m_value_max_defined && m_value_min >= m_value_max) {
in_log.error(source_line());
in_log.error(fmt::format(" --> Parameter '{}' needs to be less than Parameter '{}'",
CFG_KEY_VALUE_MIN,
CFG_KEY_VALUE_MAX));
c_cfg_value_min,
c_cfg_value_max));
result = false;
}

Expand Down
24 changes: 24 additions & 0 deletions src/device/map_in/map_in_enc.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,36 @@ class map_in_enc : public map_in_label {

private:
// constants

static constexpr std::string_view c_cfg_command_down {"command_down"};
static constexpr std::string_view c_cfg_command_fast_down {"command_fast_down"};

static constexpr std::string_view c_cfg_command_up {"command_up"};
static constexpr std::string_view c_cfg_command_fast_up {"command_fast_up"};

static constexpr std::string_view c_cfg_dataref {"dataref"};

static constexpr std::string_view c_cfg_data_2_up {"data_2_up"};
static constexpr std::string_view c_cfg_data_2_down {"data_2_down"};

static constexpr std::string_view c_cfg_data_2_min {"data_2_min"};
static constexpr std::string_view c_cfg_data_2_max {"data_2_max"};

static constexpr std::string_view c_cfg_delay {"delay"};

static constexpr std::string_view c_cfg_mode {"mode"};

static constexpr std::string_view c_cfg_modifier_down {"modifier_down"};
static constexpr std::string_view c_cfg_modifier_fast_down {"modifier_fast_down"};

static constexpr std::string_view c_cfg_modifier_up {"modifier_up"};
static constexpr std::string_view c_cfg_modifier_fast_up {"modifier_fest_up"};

static constexpr std::string_view c_cfg_value_min {"value_min"};
static constexpr std::string_view c_cfg_value_max {"value_max"};

static constexpr std::string_view c_cfg_value_wrap {"value_wrap"};

// enumerations
enum class encoder_map_type
{
Expand Down
Loading

0 comments on commit 936c99e

Please sign in to comment.