//---------------------------------------------------------------------------------------------------------------------- // Structures //---------------------------------------------------------------------------------------------------------------------- template struct RandomTimeAccessValue::TimesliceEntry { TimesliceType timesliceLength; bool committed; }; //---------------------------------------------------------------------------------------------------------------------- template struct RandomTimeAccessValue::WriteEntry { WriteEntry() { } WriteEntry(const DataType& defaultValue) :newValue(defaultValue) { } WriteEntry(TimesliceType awriteTime, const DataType& anewValue, const Timeslice& acurrentTimeslice) :writeTime(awriteTime), newValue(anewValue), currentTimeslice(acurrentTimeslice) { } TimesliceType writeTime; DataType newValue; Timeslice currentTimeslice; }; //---------------------------------------------------------------------------------------------------------------------- template struct RandomTimeAccessValue::WriteInfo { WriteInfo() { } WriteInfo(const DataType& defaultValue) :newValue(defaultValue) { } bool exists; TimesliceType writeTime; DataType newValue; }; //---------------------------------------------------------------------------------------------------------------------- template struct RandomTimeAccessValue::TimesliceSaveEntry { TimesliceSaveEntry(const typename std::list::const_iterator& atimeslice, unsigned int aid) :timeslice(atimeslice), id(aid) { } TimesliceSaveEntry(unsigned int aid, TimesliceType atimesliceLength) :id(aid), timesliceLength(atimesliceLength) { } typename std::list::const_iterator timeslice; Timeslice timesliceLoad; unsigned int id; TimesliceType timesliceLength; }; //---------------------------------------------------------------------------------------------------------------------- template struct RandomTimeAccessValue::WriteSaveEntry { WriteSaveEntry(TimesliceType awriteTime, const DataType& aoldValue, unsigned int acurrentTimeslice) :writeTime(awriteTime), oldValue(aoldValue), currentTimeslice(acurrentTimeslice) { } TimesliceType writeTime; DataType oldValue; unsigned int currentTimeslice; }; //---------------------------------------------------------------------------------------------------------------------- // Constructors //---------------------------------------------------------------------------------------------------------------------- template RandomTimeAccessValue::RandomTimeAccessValue() { } //---------------------------------------------------------------------------------------------------------------------- template RandomTimeAccessValue::RandomTimeAccessValue(const DataType& defaultValue) :_value(defaultValue) { } //---------------------------------------------------------------------------------------------------------------------- // Dereference operators //---------------------------------------------------------------------------------------------------------------------- template const DataType& RandomTimeAccessValue::operator*() const { return _value; } //---------------------------------------------------------------------------------------------------------------------- template DataType& RandomTimeAccessValue::operator*() { return _value; } //---------------------------------------------------------------------------------------------------------------------- // Access functions //---------------------------------------------------------------------------------------------------------------------- template DataType RandomTimeAccessValue::Read(TimesliceType readTime) const { std::unique_lock lock(_accessLock); // Search for written values in the current timeslice typename std::list::const_reverse_iterator i = _writeList.rbegin(); while ((i != _writeList.rend()) && (i->currentTimeslice == _latestTimeslice)) { if (i->writeTime <= readTime) { return i->newValue; } ++i; } if (i != _writeList.rend()) { // Return the latest write from a previous timeslice return i->newValue; } else { // Default to the committed value return _value; } } //---------------------------------------------------------------------------------------------------------------------- template void RandomTimeAccessValue::Write(TimesliceType writeTime, const DataType& data) { std::unique_lock lock(_accessLock); WriteEntry entry(writeTime, data, _latestTimeslice); // Find the correct location in the list to insert the new write entry. The writeList // must be sorted from earliest to latest write by time. typename std::list::reverse_iterator i = _writeList.rbegin(); while ((i != _writeList.rend()) && (i->currentTimeslice == _latestTimeslice) && (i->writeTime > writeTime)) { ++i; } _writeList.insert(i.base(), entry); } //---------------------------------------------------------------------------------------------------------------------- template DataType RandomTimeAccessValue::ReadCommitted() const { return _value; } //---------------------------------------------------------------------------------------------------------------------- template DataType RandomTimeAccessValue::ReadCommitted(TimesliceType readTime) const { std::unique_lock lock(_accessLock); TimesliceType currentTimeBase = 0; // Default to the committed value DataType foundValue = _value; // Search for any buffered writes before the read time typename std::list::const_iterator currentTimeslice = _timesliceList.begin(); typename std::list::const_iterator i = _writeList.begin(); bool done = false; while ((i != _writeList.end()) && !done) { // Advance through the timeslice list until we find the timeslice matching the // next buffered write, or we pass the end of this time step. while ((currentTimeslice != i->currentTimeslice) && (((currentTimeBase + currentTimeslice->timesliceLength) - _currentTimeOffset) <= readTime)) { currentTimeBase += currentTimeslice->timesliceLength; ++currentTimeslice; } // If the next buffered write is outside the time step, terminate processing of // buffered writes. if ((currentTimeslice != i->currentTimeslice) || (((currentTimeBase + i->writeTime) - _currentTimeOffset) > readTime)) { done = true; continue; } // Set the found write as the value at the time of the read, and advance to the // next buffered write. foundValue = i->newValue; ++i; } return foundValue; } //---------------------------------------------------------------------------------------------------------------------- template void RandomTimeAccessValue::WriteCommitted(const DataType& data) { _value = data; } //---------------------------------------------------------------------------------------------------------------------- template DataType RandomTimeAccessValue::ReadLatest() const { std::unique_lock lock(_accessLock); // Search for written values in any timeslice typename std::list::const_reverse_iterator i = _writeList.rbegin(); if (i != _writeList.rend()) { // Return the latest write from any timeslice return i->newValue; } else { // Default to the committed value return _value; } } //---------------------------------------------------------------------------------------------------------------------- template void RandomTimeAccessValue::WriteLatest(const DataType& data) { std::unique_lock lock(_accessLock); // Erase any write entries to this address in any timeslice. We do this to prevent // uncommitted writes from overwriting this change. This write function should make // the new value visible from all access functions. _writeList.clear(); // Write the new value directly to the committed state _value = data; } //---------------------------------------------------------------------------------------------------------------------- // Time management functions //---------------------------------------------------------------------------------------------------------------------- template void RandomTimeAccessValue::Initialize() { _value = DataType(); // Initialize buffers _writeList.clear(); _timesliceList.clear(); _currentTimeOffset = 0; _latestTimeslice = _timesliceList.end(); } //---------------------------------------------------------------------------------------------------------------------- template bool RandomTimeAccessValue::DoesLatestTimesliceExist() const { std::unique_lock lock(_accessLock); return !_timesliceList.empty(); } //---------------------------------------------------------------------------------------------------------------------- template typename RandomTimeAccessValue::Timeslice RandomTimeAccessValue::GetLatestTimeslice() { std::unique_lock lock(_accessLock); if (_timesliceList.empty()) { return _timesliceList.end(); } else { return (++_timesliceList.rbegin()).base(); } } //---------------------------------------------------------------------------------------------------------------------- template void RandomTimeAccessValue::AdvancePastTimeslice(const Timeslice& targetTimeslice) { std::unique_lock lock(_accessLock); // Commit buffered writes which we have passed in this step typename std::list::iterator currentTimeslice = _timesliceList.begin(); typename std::list::iterator i = _writeList.begin(); bool done = false; while ((i != _writeList.end()) && !done) { // Advance through the timeslice list until we find the timeslice matching the // next buffered write, or we pass the end of this time step. while ((currentTimeslice != i->currentTimeslice) && (currentTimeslice != targetTimeslice)) { ++currentTimeslice; } // If the next buffered write is outside the time step, terminate processing of // buffered writes. if (currentTimeslice != i->currentTimeslice) { done = true; continue; } _value = i->newValue; ++i; } // Check for any remaining timeslices which have expired while (currentTimeslice != targetTimeslice) { ++currentTimeslice; } // Set our current time offset to the end of the target timeslice _currentTimeOffset = targetTimeslice->timesliceLength; // Erase buffered writes which have been committed, and timeslices which have expired. _writeList.erase(_writeList.begin(), i); _timesliceList.erase(_timesliceList.begin(), targetTimeslice); } //---------------------------------------------------------------------------------------------------------------------- template void RandomTimeAccessValue::AdvanceToTimeslice(const Timeslice& targetTimeslice) { std::unique_lock lock(_accessLock); // Commit buffered writes which we have passed in this step typename std::list::iterator currentTimeslice = _timesliceList.begin(); typename std::list::iterator i = _writeList.begin(); bool done = false; while ((i != _writeList.end()) && !done) { // Advance through the timeslice list until we find the timeslice matching the // next buffered write, or we pass the end of this time step. while ((currentTimeslice != i->currentTimeslice) && (currentTimeslice != targetTimeslice)) { ++currentTimeslice; } // If the next buffered write is outside the time step, terminate processing of // buffered writes. if ((currentTimeslice != i->currentTimeslice) || (currentTimeslice == targetTimeslice)) { done = true; continue; } _value = i->newValue; ++i; } // Check for any remaining timeslices which have expired while (currentTimeslice != targetTimeslice) { ++currentTimeslice; } // Set our current time offset to the start of the target timeslice _currentTimeOffset = 0; // Erase buffered writes which have been committed, and timeslices which have expired. _writeList.erase(_writeList.begin(), i); _timesliceList.erase(_timesliceList.begin(), targetTimeslice); } //---------------------------------------------------------------------------------------------------------------------- template void RandomTimeAccessValue::AdvanceByTime(TimesliceType step, const Timeslice& targetTimeslice) { std::unique_lock lock(_accessLock); TimesliceType currentTimeBase = 0; // Commit buffered writes which we have passed in this step typename std::list::iterator currentTimeslice = _timesliceList.begin(); typename std::list::iterator i = _writeList.begin(); bool done = false; while ((i != _writeList.end()) && !done) { // Advance through the timeslice list until we find the timeslice matching the // next buffered write, or we pass the end of this time step. while ((currentTimeslice != i->currentTimeslice) && (currentTimeslice != targetTimeslice) && (((currentTimeBase + currentTimeslice->timesliceLength) - _currentTimeOffset) <= step)) { currentTimeBase += currentTimeslice->timesliceLength; ++currentTimeslice; } // If the next buffered write is outside the time step, terminate processing of // buffered writes. if ((currentTimeslice != i->currentTimeslice) || (((currentTimeBase + i->writeTime) - _currentTimeOffset) > step)) { done = true; continue; } _value = i->newValue; ++i; } // Check for any remaining timeslices which have expired while ((currentTimeslice != targetTimeslice) && (((currentTimeBase + currentTimeslice->timesliceLength) - _currentTimeOffset) <= step)) { currentTimeBase += currentTimeslice->timesliceLength; ++currentTimeslice; } // Set the amount of the current timeslice which has been stepped through as the time // offset for the next step operation. _currentTimeOffset = (_currentTimeOffset + step) - currentTimeBase; // Erase buffered writes which have been committed, and timeslices which have expired. _writeList.erase(_writeList.begin(), i); _timesliceList.erase(_timesliceList.begin(), currentTimeslice); } //---------------------------------------------------------------------------------------------------------------------- template bool RandomTimeAccessValue::AdvanceByStep(const Timeslice& targetTimeslice) { std::unique_lock lock(_accessLock); TimesliceType currentTimeBase = 0; TimesliceType writeTime = targetTimeslice->timesliceLength; bool foundWrite = false; // Commit buffered writes which we have passed in this step typename std::list::iterator currentTimeslice = _timesliceList.begin(); typename std::list::iterator i = _writeList.begin(); if (!_writeList.empty()) { // Advance through the timeslice list until we find the timeslice matching the // next buffered write, or we pass the end of this time step. while ((currentTimeslice != i->currentTimeslice) && (currentTimeslice != targetTimeslice)) { currentTimeBase += currentTimeslice->timesliceLength; ++currentTimeslice; } // If the next buffered write is within the time step, save the write time and // commit the data. if (currentTimeslice == i->currentTimeslice) { foundWrite = true; writeTime = i->writeTime; _value = i->newValue; ++i; } } // Check for any remaining timeslices which have expired. if (!foundWrite) { while (currentTimeslice != targetTimeslice) { currentTimeBase += currentTimeslice->timesliceLength; ++currentTimeslice; } } // Set the amount of the current timeslice which has been stepped through as the time // offset for the next step operation. _currentTimeOffset = writeTime; // Erase buffered writes which have been committed, and timeslices which have expired. _writeList.erase(_writeList.begin(), i); _timesliceList.erase(_timesliceList.begin(), currentTimeslice); return foundWrite; } //---------------------------------------------------------------------------------------------------------------------- template TimesliceType RandomTimeAccessValue::GetNextWriteTime(const Timeslice& targetTimeslice) { std::unique_lock lock(_accessLock); bool foundWrite = false; TimesliceType nextWriteTime = 0; TimesliceType currentTimeBase = 0; // Search the write list for the next buffered write inside this time step typename std::list::iterator currentTimeslice = _timesliceList.begin(); typename std::list::iterator i = _writeList.begin(); if (!_writeList.empty()) { // Advance through the timeslice list until we find the timeslice matching the // next buffered write, or we pass the end of this time step. while ((currentTimeslice != i->currentTimeslice) && (currentTimeslice != targetTimeslice)) { currentTimeBase += currentTimeslice->timesliceLength; ++currentTimeslice; } // If the next buffered write is within the time step, save the write time. if (currentTimeslice == i->currentTimeslice) { foundWrite = true; nextWriteTime = ((currentTimeBase + i->writeTime) - _currentTimeOffset); } } // If no write is pending within the time step, output the end of the target timeslice // as the next write time. if (!foundWrite) { while (currentTimeslice != targetTimeslice) { currentTimeBase += currentTimeslice->timesliceLength; ++currentTimeslice; } nextWriteTime = ((currentTimeBase + currentTimeslice->timesliceLength) - _currentTimeOffset); } return nextWriteTime; } //---------------------------------------------------------------------------------------------------------------------- template typename RandomTimeAccessValue::WriteInfo RandomTimeAccessValue::GetWriteInfo(unsigned int index, const Timeslice& targetTimeslice) { std::unique_lock lock(_accessLock); TimesliceType currentTimeBase = 0; unsigned int currentIndex = 0; WriteInfo writeInfo; writeInfo.exists = false; // Search the write list for the next buffered write inside this time step typename std::list::iterator currentTimeslice = _timesliceList.begin(); typename std::list::iterator i = _writeList.begin(); bool done = false; while ((i != _writeList.end()) && !done) { // Advance through the timeslice list until we find the timeslice matching the // next buffered write, or we pass the end of this time step. while ((currentTimeslice != i->currentTimeslice) && (currentTimeslice != targetTimeslice)) { currentTimeBase += currentTimeslice->timesliceLength; ++currentTimeslice; } // If the next buffered write is within the time step, output its data if (currentTimeslice == i->currentTimeslice) { if (currentIndex == index) { writeInfo.exists = true; writeInfo.newValue = i->newValue; writeInfo.writeTime = ((currentTimeBase + i->writeTime) - _currentTimeOffset); } else { ++currentIndex; continue; } } done = true; } return writeInfo; } //---------------------------------------------------------------------------------------------------------------------- template void RandomTimeAccessValue::Commit() { std::unique_lock lock(_accessLock); // Flag all timeslices as committed typename std::list::reverse_iterator i = _timesliceList.rbegin(); while ((i != _timesliceList.rend()) && (!i->committed)) { i->committed = true; ++i; } } //---------------------------------------------------------------------------------------------------------------------- template void RandomTimeAccessValue::Rollback() { std::unique_lock lock(_accessLock); // Erase non-committed memory writes typename std::list::reverse_iterator i = _writeList.rbegin(); while ((i != _writeList.rend()) && (!i->currentTimeslice->committed)) { ++i; } _writeList.erase(i.base(), _writeList.end()); // Erase non-committed timeslice entries typename std::list::reverse_iterator j = _timesliceList.rbegin(); while ((j != _timesliceList.rend()) && (!j->committed)) { ++j; } _timesliceList.erase(j.base(), _timesliceList.end()); // Recalculate the latest timeslice if (_timesliceList.empty()) { _latestTimeslice = _timesliceList.end(); } else { _latestTimeslice = (++_timesliceList.rbegin()).base(); } } //---------------------------------------------------------------------------------------------------------------------- template void RandomTimeAccessValue::AddTimeslice(TimesliceType timeslice) { std::unique_lock lock(_accessLock); // Add the new timeslice entry to the list TimesliceEntry entry; entry.timesliceLength = timeslice; entry.committed = false; _timesliceList.push_back(entry); // Select the new timeslice entry as the latest timeslice _latestTimeslice = (++_timesliceList.rbegin()).base(); } //---------------------------------------------------------------------------------------------------------------------- // Savestate functions //---------------------------------------------------------------------------------------------------------------------- template bool RandomTimeAccessValue::LoadState(IHierarchicalStorageNode& node) { std::list timesliceSaveList; std::list writeSaveList; // Load the current time offset node.ExtractAttribute(L"CurrentTimeOffset", _currentTimeOffset); // Read saved data from the XML tree std::list childList = node.GetChildList(); for (std::list::iterator i = childList.begin(); i != childList.end(); ++i) { if ((*i)->GetName() == L"TimesliceList") { if (!LoadTimesliceEntries(*(*i), timesliceSaveList)) { return false; } } else if ((*i)->GetName() == L"WriteList") { if (!LoadWriteEntries(*(*i), writeSaveList)) { return false; } } } // Load timeslice list _timesliceList.clear(); for (typename std::list::iterator i = timesliceSaveList.begin(); i != timesliceSaveList.end(); ++i) { TimesliceEntry timesliceEntry; timesliceEntry.timesliceLength = i->timesliceLength; _timesliceList.push_back(timesliceEntry); i->timesliceLoad = (++_timesliceList.rbegin()).base(); } _latestTimeslice = GetLatestTimeslice(); // Load memory buffer IHierarchicalStorageAttribute* valueAttribute = node.GetAttribute(L"Value"); if (valueAttribute != 0) { valueAttribute->ExtractValue(_value); } // Load write list, and rebuild memory buffer _writeList.clear(); for (typename std::list::reverse_iterator i = writeSaveList.rbegin(); i != writeSaveList.rend(); ++i) { WriteEntry writeEntry(_value); writeEntry.writeTime = i->writeTime; typename std::list::iterator currentTimeslice = timesliceSaveList.begin(); while ((currentTimeslice != timesliceSaveList.end()) && (currentTimeslice->id != i->currentTimeslice)) { ++currentTimeslice; } if (currentTimeslice != timesliceSaveList.end()) { writeEntry.currentTimeslice = currentTimeslice->timesliceLoad; writeEntry.newValue = _value; _value = i->oldValue; _writeList.push_front(writeEntry); } } return true; } //---------------------------------------------------------------------------------------------------------------------- template bool RandomTimeAccessValue::LoadTimesliceEntries(IHierarchicalStorageNode& node, std::list& timesliceSaveList) { std::list childList = node.GetChildList(); for (std::list::iterator i = childList.begin(); i != childList.end(); ++i) { if ((*i)->GetName() == L"Timeslice") { IHierarchicalStorageAttribute* timesliceID = (*i)->GetAttribute(L"TimesliceID"); IHierarchicalStorageAttribute* timesliceLength = (*i)->GetAttribute(L"TimesliceLength"); if ((timesliceID != 0) && (timesliceLength != 0)) { TimesliceSaveEntry entry(timesliceID->ExtractValue(), timesliceLength->ExtractValue()); // Find the correct location in the list to insert the timeslice entry. The // timeslice list must be sorted from earliest to latest by id. typename std::list::reverse_iterator j = timesliceSaveList.rbegin(); while ((j != timesliceSaveList.rend()) && (j->id > entry.id)) { ++j; } timesliceSaveList.insert(j.base(), entry); } } } return true; } //---------------------------------------------------------------------------------------------------------------------- template bool RandomTimeAccessValue::LoadWriteEntries(IHierarchicalStorageNode& node, std::list& writeSaveList) { std::list childList = node.GetChildList(); for (std::list::iterator i = childList.begin(); i != childList.end(); ++i) { if ((*i)->GetName() == L"Write") { IHierarchicalStorageAttribute* writeTime = (*i)->GetAttribute(L"WriteTime"); IHierarchicalStorageAttribute* oldValue = (*i)->GetAttribute(L"OldValue"); IHierarchicalStorageAttribute* timesliceID = (*i)->GetAttribute(L"TimesliceID"); if ((writeTime != 0) && (oldValue != 0) && (timesliceID != 0)) { // Extract the write entry from the XML stream WriteSaveEntry entry(writeTime->ExtractValue(), _value, timesliceID->ExtractValue()); oldValue->ExtractValue(entry.oldValue); // Find the correct location in the list to insert the write entry. The // write list must be sorted from earliest to latest. typename std::list::reverse_iterator j = writeSaveList.rbegin(); while ((j != writeSaveList.rend()) && ((j->currentTimeslice > entry.currentTimeslice) || ((j->currentTimeslice == entry.currentTimeslice) && (j->writeTime > entry.writeTime)))) { ++j; } writeSaveList.insert(j.base(), entry); } } } return true; } //---------------------------------------------------------------------------------------------------------------------- template bool RandomTimeAccessValue::SaveState(IHierarchicalStorageNode& node) const { std::list timesliceSaveList; DataType saveValue(_value); // Save the current time offset node.CreateAttribute(L"CurrentTimeOffset", _currentTimeOffset); // Take a copy of the current memory buffer. We need to copy the contents rather than // directly dumping the current memory buffer, because we change the direction of the // write history when we save it. Rather than saving the committed state of the // memory, we save the latest volatile state of the memory, and each writeEntry // contains the previous data that was in the memory before that write occurred, // rather than the new value which is being written. We do this to allow easy // modification of the saved data, without having to worry about uncommitted writes // overwriting changes. saveValue = _value; // Build numeric ID's to identify each timeslice, and save the timeslice list state IHierarchicalStorageNode& timesliceListState = node.CreateChild(L"TimesliceList"); unsigned int id = 0; for (typename std::list::const_iterator i = _timesliceList.begin(); i != _timesliceList.end(); ++i) { timesliceSaveList.push_back(TimesliceSaveEntry(i, id)); IHierarchicalStorageNode& timesliceEntry = timesliceListState.CreateChild(L"Timeslice"); timesliceEntry.CreateAttribute(L"TimesliceID", id); timesliceEntry.CreateAttribute(L"TimesliceLength", i->timesliceLength); ++id; } // Save the writeList state IHierarchicalStorageNode& writeListState = node.CreateChild(L"WriteList"); typename std::list::iterator currentTimeslice = timesliceSaveList.begin(); for (typename std::list::const_iterator i = _writeList.begin(); i != _writeList.end(); ++i) { IHierarchicalStorageNode& writeEntry = writeListState.CreateChild(L"Write"); while (currentTimeslice->timeslice != i->currentTimeslice) { ++currentTimeslice; } writeEntry.CreateAttribute(L"TimesliceID", currentTimeslice->id); writeEntry.CreateAttribute(L"WriteTime", i->writeTime); writeEntry.CreateAttribute(L"OldValue", saveValue); saveValue = i->newValue; } // Save the latest value node.CreateAttribute(L"Value", saveValue); return true; }