comparison base/Event.h @ 1866:b4b11af915f4

If duration < 0, swap start time and duration rather than throwing an exception - this is too deep to be throwing an exception here, we end up with bugs like #1989 (Crash when trying to import CSV file with certain unexpected data in it) - and the meaning of negative duration is not actually ambiguous
author Chris Cannam
date Thu, 11 Jun 2020 14:07:56 +0100
parents 21c792334c2e
children
comparison
equal deleted inserted replaced
1863:14bf9bf5ac28 1866:b4b11af915f4
82 Event(sv_frame_t frame, float value, sv_frame_t duration, QString label) : 82 Event(sv_frame_t frame, float value, sv_frame_t duration, QString label) :
83 m_haveValue(true), m_haveLevel(false), 83 m_haveValue(true), m_haveLevel(false),
84 m_haveDuration(true), m_haveReferenceFrame(false), 84 m_haveDuration(true), m_haveReferenceFrame(false),
85 m_value(value), m_level(0.f), m_frame(frame), 85 m_value(value), m_level(0.f), m_frame(frame),
86 m_duration(duration), m_referenceFrame(0), m_label(label) { 86 m_duration(duration), m_referenceFrame(0), m_label(label) {
87 if (m_duration < 0) throw std::logic_error("duration must be >= 0"); 87 if (m_duration < 0) {
88 m_frame += m_duration;
89 m_duration = -m_duration;
90 }
88 } 91 }
89 92
90 Event(sv_frame_t frame, float value, sv_frame_t duration, 93 Event(sv_frame_t frame, float value, sv_frame_t duration,
91 float level, QString label) : 94 float level, QString label) :
92 m_haveValue(true), m_haveLevel(true), 95 m_haveValue(true), m_haveLevel(true),
93 m_haveDuration(true), m_haveReferenceFrame(false), 96 m_haveDuration(true), m_haveReferenceFrame(false),
94 m_value(value), m_level(level), m_frame(frame), 97 m_value(value), m_level(level), m_frame(frame),
95 m_duration(duration), m_referenceFrame(0), m_label(label) { 98 m_duration(duration), m_referenceFrame(0), m_label(label) {
96 if (m_duration < 0) throw std::logic_error("duration must be >= 0"); 99 if (m_duration < 0) {
100 m_frame += m_duration;
101 m_duration = -m_duration;
102 }
97 } 103 }
98 104
99 Event(const Event &event) =default; 105 Event(const Event &event) =default;
100 106
101 // We would ideally like Event to be immutable - but we have to 107 // We would ideally like Event to be immutable - but we have to
133 139
134 Event withDuration(sv_frame_t duration) const { 140 Event withDuration(sv_frame_t duration) const {
135 Event p(*this); 141 Event p(*this);
136 p.m_duration = duration; 142 p.m_duration = duration;
137 p.m_haveDuration = true; 143 p.m_haveDuration = true;
138 if (duration < 0) throw std::logic_error("duration must be >= 0"); 144 if (p.m_duration < 0) {
145 p.m_frame += p.m_duration;
146 p.m_duration = -p.m_duration;
147 }
139 return p; 148 return p;
140 } 149 }
141 Event withoutDuration() const { 150 Event withoutDuration() const {
142 Event p(*this); 151 Event p(*this);
143 p.m_haveDuration = false; 152 p.m_haveDuration = false;