FeatherMC/src/Lockable.h

37 lines
924 B
C++

#pragma once
#include <mutex>
#include <vector>
#include <list>
#include <map>
#include <array>
namespace Feather
{
template <typename T>
class Lockable {
public:
auto borrow() { return std::pair< T&, std::lock_guard<std::mutex>>{object, mutex}; }
auto borrow() const { return std::pair<const T&, std::lock_guard<std::mutex>>{object, mutex}; }
T& steal() { return object; }
const T& steal() const { return object; }
private:
T object;
mutable std::mutex mutex;
};
template <typename T>
using LockableVector = Lockable<std::vector<T>>;
template <typename T>
using LockableList = Lockable<std::list<T>>;
template <typename Key, typename T>
using LockableMap = Lockable<std::map<Key, T>>;
template <typename T, size_t Size>
using LockableArray = Lockable<std::array<T, Size>>;
}