]> git.tdb.fi Git - r2c2.git/commitdiff
Make sure TrackOffsetIters make sense
authorMikko Rasa <tdb@tdb.fi>
Thu, 10 Apr 2014 19:02:39 +0000 (22:02 +0300)
committerMikko Rasa <tdb@tdb.fi>
Thu, 10 Apr 2014 19:29:09 +0000 (22:29 +0300)
Disallow creation of iterators that have a path not connected to the
entry endpoint, or an out of range offset.

source/libr2c2/trackoffsetiter.cpp
source/libr2c2/trackoffsetiter.h

index f8e8a2d2a4c54a5270c85a41fd5955011c2f9641..9676985b5b971a659bb6918c95c40f3d1f4bc5e5 100644 (file)
@@ -24,25 +24,50 @@ TrackOffsetIter::TrackOffsetIter(Track *t, unsigned e, float o):
        _track(t, e),
        _offset(_track ? o : 0),
        _path((_track && _offset) ? _track->get_active_path() : -1)
-{ }
+{
+       validate();
+}
 
 TrackOffsetIter::TrackOffsetIter(Track *t, unsigned e, unsigned p, float o):
        _track(t, e),
        _offset(o),
        _path(p)
-{ }
+{
+       validate();
+}
 
 TrackOffsetIter::TrackOffsetIter(const TrackIter &t, float o):
        _track(t),
        _offset(_track ? o : 0),
        _path((_track && _offset) ? _track->get_active_path() : -1)
-{ }
+{
+       validate();
+}
 
 TrackOffsetIter::TrackOffsetIter(const TrackIter &t, unsigned p, float o):
        _track(t),
        _offset(o),
        _path(p)
-{ }
+{
+       validate();
+}
+
+void TrackOffsetIter::validate()
+{
+       if(!_track)
+               return;
+
+       if(_offset<0)
+               throw invalid_argument("TrackOffsetIter::TrackOffsetIter");
+
+       if(_path>=0)
+       {
+               if(!_track.endpoint().has_path(_path))
+                       throw invalid_argument("TrackOffsetIter::TrackOffsetIter");
+               if(_offset>_track->get_type().get_path_length(_path))
+                       throw invalid_argument("TrackOffsetIter::TrackOffsetIter");
+       }
+}
 
 BlockIter TrackOffsetIter::block_iter() const
 {
index 776f7a26d26367ff29afe7b5cb7b686c3b7091e6..3156c08c61c33b14bc79e27a6910021c39bc5aff 100644 (file)
@@ -29,6 +29,10 @@ public:
        TrackOffsetIter(const TrackIter &, float = 0);
        TrackOffsetIter(const TrackIter &, unsigned, float);
 
+private:
+       void validate();
+
+public:
        Track *track() const { return _track.track(); }
        unsigned entry() const { return _track.entry(); }
        const TrackIter &track_iter() const { return _track; }