13
0

libtemporal: move Beats operator<< and operator>> to .cc and add exceptions for input

This commit is contained in:
Paul Davis 2020-12-28 13:24:24 -07:00
parent af2474ab80
commit f015c08dd9
2 changed files with 40 additions and 16 deletions

View File

@ -16,6 +16,8 @@
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include <stdexcept>
#include "pbd/integer_division.h"
#include "temporal/beats.h"
@ -116,3 +118,39 @@ Beats::round_to_subdivision (int subdivision, RoundMode dir) const {
DEBUG_TRACE (DEBUG::SnapBBT, string_compose ("return %1 from %2 : %3\n", Beats (beats, ticks), beats, ticks));
return Beats (beats, ticks);
}
std::istream&
Temporal::operator>>(std::istream& istr, Beats& b)
{
int32_t beats, ticks;
char d; /* delimiter, whatever it is */
istr >> beats;
if (!istr) {
throw std::invalid_argument (_("illegal or missing value for beat count"));
}
istr >> d; /* we don't care what the delimiter is */
if (!istr) {
throw std::invalid_argument (_("illegal or missing delimiter for beat value"));
}
istr >> ticks;
if (!istr) {
throw std::invalid_argument (_("illegal or missing delimiter for tick count"));
}
b = Beats (beats, ticks);
return istr;
}
std::ostream&
Temporal::operator<<(std::ostream& os, const Beats& t)
{
os << t.get_beats() << ':' << t.get_ticks();
return os;
}

View File

@ -311,22 +311,8 @@ class DoubleableBeats : public Beats
virtual-method-in-a-template will bite you.
*/
inline std::ostream&
operator<<(std::ostream& os, const Beats& t)
{
os << t.get_beats() << ':' << t.get_ticks();
return os;
}
inline std::istream&
operator>>(std::istream& istr, Beats& b)
{
int32_t beats, ticks;
char d; /* delimiter, whatever it is */
istr >> beats >> d >> ticks;
b = Beats (beats, ticks);
return istr;
}
std::ostream& operator<<(std::ostream& ostream, const Temporal::Beats& t);
std::istream& operator>>(std::istream& istream, Temporal::Beats& b);
} // namespace Temporal