2017-09-23 02:46:31 +00:00
|
|
|
//
|
|
|
|
// DiskImageImplementation.hpp
|
|
|
|
// Clock Signal
|
|
|
|
//
|
|
|
|
// Created by Thomas Harte on 22/09/2017.
|
2018-05-13 19:19:52 +00:00
|
|
|
// Copyright 2017 Thomas Harte. All rights reserved.
|
2017-09-23 02:46:31 +00:00
|
|
|
//
|
|
|
|
|
2018-05-07 03:17:36 +00:00
|
|
|
template <typename T> HeadPosition DiskImageHolder<T>::get_maximum_head_position() {
|
|
|
|
return disk_image_.get_maximum_head_position();
|
2017-09-23 02:46:31 +00:00
|
|
|
}
|
|
|
|
|
2017-10-07 01:45:12 +00:00
|
|
|
template <typename T> int DiskImageHolder<T>::get_head_count() {
|
2017-09-23 02:46:31 +00:00
|
|
|
return disk_image_.get_head_count();
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T> bool DiskImageHolder<T>::get_is_read_only() {
|
|
|
|
return disk_image_.get_is_read_only();
|
|
|
|
}
|
|
|
|
|
2017-10-07 01:45:12 +00:00
|
|
|
template <typename T> void DiskImageHolder<T>::flush_tracks() {
|
2017-10-07 02:07:42 +00:00
|
|
|
if(!unwritten_tracks_.empty()) {
|
2022-07-16 18:41:04 +00:00
|
|
|
if(!update_queue_) update_queue_ = std::make_unique<Concurrency::AsyncTaskQueue<true>>();
|
2017-10-07 02:07:42 +00:00
|
|
|
|
|
|
|
using TrackMap = std::map<Track::Address, std::shared_ptr<Track>>;
|
|
|
|
std::shared_ptr<TrackMap> track_copies(new TrackMap);
|
2018-05-01 02:23:57 +00:00
|
|
|
for(const auto &address : unwritten_tracks_) {
|
2017-11-11 23:22:41 +00:00
|
|
|
track_copies->insert(std::make_pair(address, std::shared_ptr<Track>(cached_tracks_[address]->clone())));
|
2017-10-07 02:07:42 +00:00
|
|
|
}
|
|
|
|
unwritten_tracks_.clear();
|
|
|
|
|
|
|
|
update_queue_->enqueue([this, track_copies]() {
|
2017-10-07 23:37:36 +00:00
|
|
|
disk_image_.set_tracks(*track_copies);
|
2017-10-07 02:07:42 +00:00
|
|
|
});
|
|
|
|
}
|
2017-10-07 01:45:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T> void DiskImageHolder<T>::set_track_at_position(Track::Address address, const std::shared_ptr<Track> &track) {
|
2017-09-23 02:46:31 +00:00
|
|
|
if(disk_image_.get_is_read_only()) return;
|
|
|
|
|
2017-10-07 02:07:42 +00:00
|
|
|
unwritten_tracks_.insert(address);
|
2017-09-23 02:46:31 +00:00
|
|
|
cached_tracks_[address] = track;
|
|
|
|
}
|
|
|
|
|
2017-10-07 01:45:12 +00:00
|
|
|
template <typename T> std::shared_ptr<Track> DiskImageHolder<T>::get_track_at_position(Track::Address address) {
|
|
|
|
if(address.head >= get_head_count()) return nullptr;
|
2018-05-07 03:17:36 +00:00
|
|
|
if(address.position >= get_maximum_head_position()) return nullptr;
|
2017-09-23 02:46:31 +00:00
|
|
|
|
2017-10-07 01:45:12 +00:00
|
|
|
auto cached_track = cached_tracks_.find(address);
|
2017-09-23 02:46:31 +00:00
|
|
|
if(cached_track != cached_tracks_.end()) return cached_track->second;
|
|
|
|
|
2017-10-07 01:45:12 +00:00
|
|
|
std::shared_ptr<Track> track = disk_image_.get_track_at_position(address);
|
2017-09-23 02:46:31 +00:00
|
|
|
if(!track) return nullptr;
|
|
|
|
cached_tracks_[address] = track;
|
|
|
|
return track;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T> DiskImageHolder<T>::~DiskImageHolder() {
|
|
|
|
if(update_queue_) update_queue_->flush();
|
|
|
|
}
|
2020-07-18 02:09:21 +00:00
|
|
|
|
|
|
|
template <typename T> bool DiskImageHolder<T>::tracks_differ(Track::Address lhs, Track::Address rhs) {
|
|
|
|
return disk_image_.tracks_differ(lhs, rhs);
|
|
|
|
}
|