From 8263c48a1d491107b54dd92ca1fc9d69fe5ed05f Mon Sep 17 00:00:00 2001 From: Thomas Harte Date: Fri, 18 May 2018 23:03:28 -0400 Subject: [PATCH] Added a guarantee that the TrackSerialiser won't modify tracks it receives. --- Storage/Disk/Track/PCMPatchedTrack.cpp | 2 +- Storage/Disk/Track/PCMPatchedTrack.hpp | 6 +++--- Storage/Disk/Track/PCMTrack.cpp | 2 +- Storage/Disk/Track/PCMTrack.hpp | 6 +++--- Storage/Disk/Track/Track.hpp | 2 +- Storage/Disk/Track/TrackSerialiser.cpp | 11 +++++++---- Storage/Disk/Track/TrackSerialiser.hpp | 2 +- Storage/Disk/Track/UnformattedTrack.cpp | 2 +- Storage/Disk/Track/UnformattedTrack.hpp | 6 +++--- 9 files changed, 21 insertions(+), 18 deletions(-) diff --git a/Storage/Disk/Track/PCMPatchedTrack.cpp b/Storage/Disk/Track/PCMPatchedTrack.cpp index d845749b0..7c4275171 100644 --- a/Storage/Disk/Track/PCMPatchedTrack.cpp +++ b/Storage/Disk/Track/PCMPatchedTrack.cpp @@ -27,7 +27,7 @@ PCMPatchedTrack::PCMPatchedTrack(const PCMPatchedTrack &original) { active_period_ = periods_.begin(); } -Track *PCMPatchedTrack::clone() { +Track *PCMPatchedTrack::clone() const { return new PCMPatchedTrack(*this); } diff --git a/Storage/Disk/Track/PCMPatchedTrack.hpp b/Storage/Disk/Track/PCMPatchedTrack.hpp index 62d5b2130..710926c77 100644 --- a/Storage/Disk/Track/PCMPatchedTrack.hpp +++ b/Storage/Disk/Track/PCMPatchedTrack.hpp @@ -43,9 +43,9 @@ class PCMPatchedTrack: public Track { void add_segment(const Time &start_time, const PCMSegment &segment, bool clamp_to_index_hole); // To satisfy Storage::Disk::Track - Event get_next_event(); - Time seek_to(const Time &time_since_index_hole); - Track *clone(); + Event get_next_event() override; + Time seek_to(const Time &time_since_index_hole) override; + Track *clone() const override; private: std::shared_ptr underlying_track_; diff --git a/Storage/Disk/Track/PCMTrack.cpp b/Storage/Disk/Track/PCMTrack.cpp index 93da03f17..9252ef643 100644 --- a/Storage/Disk/Track/PCMTrack.cpp +++ b/Storage/Disk/Track/PCMTrack.cpp @@ -46,7 +46,7 @@ PCMTrack::PCMTrack(const PCMTrack &original) : PCMTrack() { segment_event_sources_ = original.segment_event_sources_; } -Track *PCMTrack::clone() { +Track *PCMTrack::clone() const { return new PCMTrack(*this); } diff --git a/Storage/Disk/Track/PCMTrack.hpp b/Storage/Disk/Track/PCMTrack.hpp index 9ec932175..e14f3fd3e 100644 --- a/Storage/Disk/Track/PCMTrack.hpp +++ b/Storage/Disk/Track/PCMTrack.hpp @@ -42,9 +42,9 @@ class PCMTrack: public Track { PCMTrack(const PCMTrack &); // as per @c Track - Event get_next_event(); - Time seek_to(const Time &time_since_index_hole); - Track *clone(); + Event get_next_event() override; + Time seek_to(const Time &time_since_index_hole) override; + Track *clone() const override; private: // storage for the segments that describe this track diff --git a/Storage/Disk/Track/Track.hpp b/Storage/Disk/Track/Track.hpp index 10df77135..bbc0fa96b 100644 --- a/Storage/Disk/Track/Track.hpp +++ b/Storage/Disk/Track/Track.hpp @@ -114,7 +114,7 @@ class Track { /*! The virtual copy constructor pattern; returns a copy of the Track. */ - virtual Track *clone() = 0; + virtual Track *clone() const = 0; }; } diff --git a/Storage/Disk/Track/TrackSerialiser.cpp b/Storage/Disk/Track/TrackSerialiser.cpp index 143dcde1c..4de6a6885 100644 --- a/Storage/Disk/Track/TrackSerialiser.cpp +++ b/Storage/Disk/Track/TrackSerialiser.cpp @@ -8,11 +8,14 @@ #include "TrackSerialiser.hpp" +#include + // TODO: if this is a PCMTrack with only one segment and that segment's bit rate is within tolerance, // just return a copy of that segment. -Storage::Disk::PCMSegment Storage::Disk::track_serialisation(Track &track, Time length_of_a_bit) { +Storage::Disk::PCMSegment Storage::Disk::track_serialisation(const Track &track, Time length_of_a_bit) { unsigned int history_size = 16; DigitalPhaseLockedLoop pll(100, history_size); + std::unique_ptr track_copy(track.clone()); struct ResultAccumulator: public DigitalPhaseLockedLoop::Delegate { PCMSegment result; @@ -29,12 +32,12 @@ Storage::Disk::PCMSegment Storage::Disk::track_serialisation(Track &track, Time length_multiplier.simplify(); // start at the index hole - track.seek_to(Time(0)); + track_copy->seek_to(Time(0)); // grab events until the next index hole Time time_error = Time(0); while(true) { - Track::Event next_event = track.get_next_event(); + Track::Event next_event = track_copy->get_next_event(); if(next_event.type == Track::Event::IndexHole) break; Time extended_length = next_event.length * length_multiplier + time_error; @@ -47,7 +50,7 @@ Storage::Disk::PCMSegment Storage::Disk::track_serialisation(Track &track, Time if(history_size) { history_size--; if(!history_size) { - track.seek_to(Time(0)); + track_copy->seek_to(Time(0)); time_error.set_zero(); pll.set_delegate(&result_accumulator); } diff --git a/Storage/Disk/Track/TrackSerialiser.hpp b/Storage/Disk/Track/TrackSerialiser.hpp index f1d865b37..85b025cfb 100644 --- a/Storage/Disk/Track/TrackSerialiser.hpp +++ b/Storage/Disk/Track/TrackSerialiser.hpp @@ -30,7 +30,7 @@ namespace Disk { @param length_of_a_bit The expected length of a single bit, as a proportion of the track length. */ -PCMSegment track_serialisation(Track &track, Time length_of_a_bit); +PCMSegment track_serialisation(const Track &track, Time length_of_a_bit); } } diff --git a/Storage/Disk/Track/UnformattedTrack.cpp b/Storage/Disk/Track/UnformattedTrack.cpp index d61db09e8..ed0e1669d 100644 --- a/Storage/Disk/Track/UnformattedTrack.cpp +++ b/Storage/Disk/Track/UnformattedTrack.cpp @@ -21,6 +21,6 @@ Storage::Time UnformattedTrack::seek_to(const Time &time_since_index_hole) { return Time(0); } -Track *UnformattedTrack::clone() { +Track *UnformattedTrack::clone() const { return new UnformattedTrack; } diff --git a/Storage/Disk/Track/UnformattedTrack.hpp b/Storage/Disk/Track/UnformattedTrack.hpp index 77b645782..6d66dc82b 100644 --- a/Storage/Disk/Track/UnformattedTrack.hpp +++ b/Storage/Disk/Track/UnformattedTrack.hpp @@ -19,9 +19,9 @@ namespace Disk { */ class UnformattedTrack: public Track { public: - Event get_next_event(); - Time seek_to(const Time &time_since_index_hole); - Track *clone(); + Event get_next_event() override; + Time seek_to(const Time &time_since_index_hole) override; + Track *clone() const override; }; }