diff options
| author | Carl Hetherington <cth@carlh.net> | 2025-01-14 23:50:38 +0100 |
|---|---|---|
| committer | Carl Hetherington <cth@carlh.net> | 2025-01-19 22:48:28 +0100 |
| commit | eb28997f188c905af40054e4139251ebf1756ae4 (patch) | |
| tree | 91214593633d2a391587e46e2f2375d2fc47ad3b /src/main_sound_configuration.cc | |
| parent | d4024848dfe293b06440d20a9f48894b2b008316 (diff) | |
Make MainSoundConfiguration behave "correctly" with badly-formatted strings.
Add some documentation for a design "principle" when handling malformatted
data, and make MainSoundConfiguration adhere to that.
Diffstat (limited to 'src/main_sound_configuration.cc')
| -rw-r--r-- | src/main_sound_configuration.cc | 43 |
1 files changed, 29 insertions, 14 deletions
diff --git a/src/main_sound_configuration.cc b/src/main_sound_configuration.cc index 1a081321..a48ff88d 100644 --- a/src/main_sound_configuration.cc +++ b/src/main_sound_configuration.cc @@ -47,11 +47,13 @@ using namespace dcp; MainSoundConfiguration::MainSoundConfiguration (string s) + : _configuration(s) { vector<string> parts; boost::split (parts, s, boost::is_any_of("/")); if (parts.empty()) { - throw MainSoundConfigurationError(s); + _valid = false; + return; } if (parts[0] == "51") { @@ -73,14 +75,19 @@ MainSoundConfiguration::MainSoundConfiguration (string s) boost::split (channels, parts[1], boost::is_any_of(",")); if (channels.size() > 16) { - throw MainSoundConfigurationError (s); + _valid = false; + return; } for (auto i: channels) { if (i == "-") { _channels.push_back(optional<Channel>()); } else { - _channels.push_back(mca_id_to_channel(i)); + try { + _channels.push_back(mca_id_to_channel(i)); + } catch (UnknownChannelIdError&) { + _valid = false; + } } } } @@ -90,19 +97,23 @@ MainSoundConfiguration::MainSoundConfiguration (MCASoundField field, int channel : _field (field) { _channels.resize (channels); + update_string(); } -string -MainSoundConfiguration::to_string () const +void +MainSoundConfiguration::update_string() { - string c; + if (!_valid) { + return; + } + switch (_field) { case MCASoundField::FIVE_POINT_ONE: - c = "51/"; + _configuration = "51/"; break; case MCASoundField::SEVEN_POINT_ONE: - c = "71/"; + _configuration = "71/"; break; default: DCP_ASSERT(false); @@ -110,23 +121,23 @@ MainSoundConfiguration::to_string () const for (auto i: _channels) { if (!i) { - c += "-,"; + _configuration += "-,"; } else { - c += channel_to_mca_id(*i, _field) + ","; + _configuration += channel_to_mca_id(*i, _field) + ","; } } - if (c.length() > 0) { - c = c.substr(0, c.length() - 1); + if (!_configuration.empty() > 0) { + _configuration = _configuration.substr(0, _configuration.length() - 1); } - - return c; } optional<Channel> MainSoundConfiguration::mapping (int index) const { + throw_if_invalid(); + DCP_ASSERT (static_cast<size_t>(index) < _channels.size()); return _channels[index]; } @@ -135,8 +146,12 @@ MainSoundConfiguration::mapping (int index) const void MainSoundConfiguration::set_mapping (int index, Channel c) { + throw_if_invalid(); + DCP_ASSERT (static_cast<size_t>(index) < _channels.size()); _channels[index] = c; + + update_string(); } |
