From f015c08dd96f4510a0efe3b8ecfc3139a411ad1e Mon Sep 17 00:00:00 2001 From: Paul Davis Date: Mon, 28 Dec 2020 13:24:24 -0700 Subject: [PATCH] libtemporal: move Beats operator<< and operator>> to .cc and add exceptions for input --- libs/temporal/beats.cc | 38 ++++++++++++++++++++++++++++++++++ libs/temporal/temporal/beats.h | 18 ++-------------- 2 files changed, 40 insertions(+), 16 deletions(-) diff --git a/libs/temporal/beats.cc b/libs/temporal/beats.cc index ab8d527248..3e48f477bc 100644 --- a/libs/temporal/beats.cc +++ b/libs/temporal/beats.cc @@ -16,6 +16,8 @@ Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ +#include + #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; +} diff --git a/libs/temporal/temporal/beats.h b/libs/temporal/temporal/beats.h index e39b6234e1..39a4868f58 100644 --- a/libs/temporal/temporal/beats.h +++ b/libs/temporal/temporal/beats.h @@ -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