|
- #pragma once
-
- #include <mutex>
- #include <memory>
- #include <chrono>
-
- namespace cppcore
- {
-
- template<
- typename T_value,
- typename T_lock = std::mutex>
- struct locked
- {
- public:
- template<typename T_locked_value>
- struct locked_ref;
-
- using value_type = T_value;
- using lock_type = T_lock;
- using guard_type = std::unique_lock<lock_type>;
- using locked_ref_type = locked_ref<value_type>;
- using locked_ref_ptr_u = std::unique_ptr<locked_ref_type>;
- using locked_const_ref_type = locked_ref<const value_type>;
- using locked_const_ref_ptr_u = std::unique_ptr<const locked_ref_type>;
-
- template<typename T_locked_value>
- struct locked_ref
- {
- public:
- using value_type = T_locked_value;
- using reference_type = value_type&;
- using pointer_type = value_type*;
-
- private:
- guard_type _guard;
-
- public:
- value_type& value;
-
- public:
- /**
- * @brief Constructor.
- */
- inline locked_ref(
- guard_type&& p_guard,
- value_type& p_value);
-
- inline pointer_type operator->();
- inline reference_type operator*();
- };
-
- private:
- mutable lock_type _lock;
- value_type _value;
-
- public:
- /**
- * @brief Constructor.
- */
- template<typename... X_args>
- inline locked(X_args&&... p_args);
-
- public:
- /**
- * @brief Lock the resource stored in this object and return the locked reference.
- */
- inline locked_ref_type lock();
-
- /**
- * @brief Try to lock the resource stored in this object and return the locked reference.
- */
- inline locked_ref_ptr_u try_lock();
-
- /**
- * @brief Try to lock the resource stored in this object and return the locked reference.
- */
- template<typename X_value, typename X_ratio>
- inline locked_ref_ptr_u try_lock_for(
- const std::chrono::duration<X_value, X_ratio>& timeout);
-
- /**
- * @brief Try to lock the resource stored in this object and return the locked reference.
- */
- template<typename X_clock, typename X_duration>
- inline locked_ref_ptr_u try_lock_until(
- const std::chrono::time_point<X_clock, X_duration>& timeout);
-
- public:
- /**
- * @brief Lock the resource stored in this object and return the locked reference.
- */
- inline locked_const_ref_type lock() const;
-
- /**
- * @brief Try to lock the resource stored in this object and return the locked reference.
- */
- inline locked_const_ref_ptr_u try_lock() const;
-
- /**
- * @brief Try to lock the resource stored in this object and return the locked reference.
- */
- template<typename X_value, typename X_ratio>
- inline locked_const_ref_ptr_u try_lock_for(
- const std::chrono::duration<X_value, X_ratio>& timeout) const;
-
- /**
- * @brief Try to lock the resource stored in this object and return the locked reference.
- */
- template<typename X_clock, typename X_duration>
- inline locked_const_ref_ptr_u try_lock_until(
- const std::chrono::time_point<X_clock, X_duration>& timeout) const;
- };
-
- /**
- * @brief Construct a locked object with the passed arguments.
- */
- template<
- typename T_value,
- typename... X_args>
- inline locked<T_value> make_locked(X_args&&... p_args);
-
- }
-
- #include "locked.inl"
|