Stroika Library 3.0d23
 
Loading...
Searching...
No Matches
VirtualLockable.h
1/*
2 * Copyright(c) Sophist Solutions, Inc. 1990-2026. All rights reserved
3 */
4#ifndef _Stroika_Foundation_Execution_VirtualLockable_h_
5#define _Stroika_Foundation_Execution_VirtualLockable_h_ 1
6
7#include "Stroika/Foundation/StroikaPreComp.h"
8
9#include <mutex>
10
11#include "Stroika/Foundation/Common/Common.h"
13
14/*
15 *
16 * \note Code-Status: <a href="Code-Status.md#Alpha">Alpha</a>
17 */
18
20
21 /**
22 * This class follows the Mutex concept and indirects to argument mutex impl.
23 *
24 * \par Example Usage
25 * \code
26 * // in class declaration where you sometimes want real locking, and somtimes just assert fake locking
27 * mutable Execution::VirtualLockable fMaybeLock_; // either Debug::AssertExternallySyncrhonized or std::recursive_mutex
28 *
29 * // in constructor, based on config parameter passed in
30 * , fMaybeLock_{options.fInternallySyncrhonized == eInternallySynchronized ? VirtualLockable::Make<recursive_mutex> ()
31 * : VirtualLockable::Make<Debug::AssertExternallySynchronizedMutex> ()}
32 * // then use as regular mutex
33 * scoped_lock critSec{fMaybeLock_};
34 * \endcode
35 *
36 * \note Satisfies Concepts:
37 * o movable<VirtualLockable>;
38 * o not copyable<VirtualLockable>);
39 * o Common::StdCompat::Lockable<VirtualLockable>;
40 */
42 public:
43 /**
44 * Because we need a templated type for construction, and cannot template constructor called with explicit template arguments
45 */
46 template <Common::StdCompat::Lockable REAL_MUTEX>
47 static VirtualLockable Make ();
48
49 public:
50 /**
51 */
52 VirtualLockable () = delete;
53 VirtualLockable (const VirtualLockable& src) = delete;
54 VirtualLockable (VirtualLockable&& src) noexcept = default;
55
56 private:
57 struct IRep_;
58 VirtualLockable (unique_ptr<IRep_>&& rep);
59
60 public:
61 VirtualLockable& operator= (VirtualLockable&& rhs) noexcept = default;
62
63 public:
64 nonvirtual void lock ();
65 nonvirtual bool try_lock ();
66 nonvirtual void unlock ();
67
68 private:
69 struct IRep_ {
70 virtual ~IRep_ () = default;
71 virtual void lock () = 0;
72 virtual bool try_lock () = 0;
73 virtual void unlock () = 0;
74 };
75 unique_ptr<IRep_> fRep_;
76 };
77 static_assert (movable<VirtualLockable>); // see Satisfies Concepts
78 static_assert (not copyable<VirtualLockable>);
80
81}
82
83/*
84 ********************************************************************************
85 ***************************** Implementation Details ***************************
86 ********************************************************************************
87 */
88#include "VirtualLockable.inl"
89
90#endif /*_Stroika_Foundation_Execution_VirtualLockable_h_*/
Logically the C++ standard Lockable named requirement, but that was not included in std c++ library.
Definition StdCompat.h:77