summaryrefslogtreecommitdiff
path: root/src/util.hpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/util.hpp')
-rw-r--r--src/util.hpp53
1 files changed, 53 insertions, 0 deletions
diff --git a/src/util.hpp b/src/util.hpp
index f7254e8..d5e2482 100644
--- a/src/util.hpp
+++ b/src/util.hpp
@@ -2,7 +2,9 @@
#include <chrono>
#include <command.h>
+#include <filesystem>
#include <scn/scan.h>
+#include <shared_mutex>
#include <tuple>
namespace WebStat {
@@ -95,4 +97,55 @@ namespace WebStat {
}
return false;
}
+
+ template<typename ValueType, typename MutexType = std::shared_mutex> class ThreadSafeT {
+ public:
+ template<typename... P> ThreadSafeT(P &&... params) : value {std::forward<P>(params)...} { }
+
+ template<typename LockedValueType, typename LockType> class Locked {
+ public:
+ Locked(LockedValueType & valueRef, MutexType & mutex) : value {valueRef}, lock {mutex} { }
+
+ LockedValueType *
+ operator->()
+ {
+ return &value;
+ }
+
+ private:
+ LockedValueType & value;
+ LockType lock;
+ };
+
+ Locked<const ValueType, std::shared_lock<MutexType>>
+ shared() const
+ {
+ return {value, mutex};
+ }
+
+ Locked<ValueType, std::lock_guard<MutexType>>
+ unique()
+ {
+ return {value, mutex};
+ }
+
+ Locked<const ValueType, std::shared_lock<MutexType>>
+ operator->() const
+ {
+ return shared();
+ }
+
+ Locked<ValueType, std::lock_guard<MutexType>>
+ operator()()
+ {
+ return unique();
+ }
+
+ private:
+ ValueType value;
+ mutable MutexType mutex;
+ };
+
+ std::error_code renameNoExcept(
+ const std::filesystem::path & path, const std::filesystem::path & finalPath) noexcept;
}