It seems to me that I am breaking the contract mutablethat I use to cache information in a data model that executes requests on demand asynchronously. The data model is Qt, although this is not a particularly important fact.
class MyDataModel : public QAbstractItemModel
{
public:
QVariant data( const QModelIndex & index, int role ) const override;
private:
void SignalRowDataUpdated( int row ) const;
mutable SimpleRowCache mCache;
};
When called data(), I check the cache to see if it has it. If not, I immediately return null data (to avoid blocking the user interface), and also send an asynchronous API request to fill the cache. Since it data()must be const, this requires that it be mCachemodified. Feelings data()look like this:
RowData row_data = mCache.Get( row );
if( !row_data )
{
mCache.Set( row, RowData() );
auto data_callback = [this, row]( RowData data )
{
mCache.Set( row, std::move(data) );
SignalRowDataUpdated( row );
};
DataApi::GetRowData( row, data_callback );
return QVariant::Invalid;
}
return row_data[ column ];
, . data() , .
? / ?
: SignalRowDataUpdated(). Qt: emit dataChanged( from, to ), . this , const. = (