2015-12-08 04:51:55 +08:00
|
|
|
#pragma once
|
2015-07-31 18:31:08 +08:00
|
|
|
|
|
|
|
#include <atomic>
|
|
|
|
|
2015-09-13 17:34:17 +08:00
|
|
|
namespace mvcc
|
|
|
|
{
|
|
|
|
|
2015-07-31 18:31:08 +08:00
|
|
|
template <class T>
|
|
|
|
class Version
|
|
|
|
{
|
|
|
|
public:
|
2015-12-06 23:42:47 +08:00
|
|
|
Version() = default;
|
|
|
|
Version(T* older) : older(older) {}
|
2015-07-31 18:31:08 +08:00
|
|
|
|
2015-10-08 06:58:29 +08:00
|
|
|
~Version()
|
|
|
|
{
|
2015-12-06 23:42:47 +08:00
|
|
|
delete older.load(std::memory_order_seq_cst);
|
2015-10-08 06:58:29 +08:00
|
|
|
}
|
|
|
|
|
2015-12-06 23:42:47 +08:00
|
|
|
// return a pointer to an older version stored in this record
|
|
|
|
T* next(std::memory_order order = std::memory_order_seq_cst)
|
|
|
|
{
|
|
|
|
return older.load(order);
|
|
|
|
}
|
2015-07-31 18:31:08 +08:00
|
|
|
|
2015-12-06 23:42:47 +08:00
|
|
|
const T* next(std::memory_order order = std::memory_order_seq_cst) const
|
2015-07-31 18:31:08 +08:00
|
|
|
{
|
2015-12-06 23:42:47 +08:00
|
|
|
return older.load(order);
|
2015-07-31 18:31:08 +08:00
|
|
|
}
|
|
|
|
|
2015-12-06 23:42:47 +08:00
|
|
|
// set the older version of this record
|
|
|
|
void next(T* value, std::memory_order order = std::memory_order_seq_cst)
|
2015-07-31 18:31:08 +08:00
|
|
|
{
|
2015-12-06 23:42:47 +08:00
|
|
|
older.store(value, order);
|
2015-07-31 18:31:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2015-12-06 23:42:47 +08:00
|
|
|
std::atomic<T*> older {nullptr};
|
2015-07-31 18:31:08 +08:00
|
|
|
};
|
|
|
|
|
2015-09-13 17:34:17 +08:00
|
|
|
}
|