#include "CandleCache.h" void CandleCache::get(int possibleRow, bool &inCache, SingleCandle &value) const { if (SingleCandle const *candle = getProperty(_values, possibleRow)) { inCache = true; value = *candle; } else inCache = false; } CandleCache::CandleCache(Requests const &requests, SingleCandle::ByStartTime const &byStartTime, void const *owner) : _owner(owner) { for (auto it = requests.begin(); it != requests.end(); it++) { const time_t startTime = it->first; const int possibleRow = it->second; SingleCandle const *const candle = getProperty(byStartTime, startTime); if (candle) _values[possibleRow] = *candle; else _values[possibleRow].clear(); } } void CandleCache::getAllRows(GridDataProviderContext *context, Requests &requests, AllRowTimes &allRowTimes) { requests.clear(); allRowTimes.clear(); const CandleTimer::Ref timer = context->getPrototype()->getCandleTimer(); const int count = timer->getPreviewRowCount(time(NULL)); const int start = context->getCalculationRowPossible(0); if (start >= count) return; allRowTimes.reserve(count - start); for (int i = start; i < count; i++) { RowTimes rowTimes; timer->getTimes(i, rowTimes.start, rowTimes.end); allRowTimes.push_back(rowTimes); requests[rowTimes.start] = i; } } void CandleCache::start(Requests const &requests, SingleCandle::ByStartTime const &data, void const *owner) { assert(!_instance); _instance = new CandleCache(requests, data, owner); } void CandleCache::get(void const *owner, int possibleRow, bool &inCache, SingleCandle &value) { if (_instance&& (_instance->_owner == owner)) _instance->get(possibleRow, inCache, value); else inCache = false; } void CandleCache::release(void const *owner) { if (_instance && (_instance->_owner == owner)) { delete _instance; _instance = NULL; } } __thread CandleCache *CandleCache::_instance = NULL;