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 */
37 public:
38 /**
39 * Because we need a templated type for construction, and cannot template constructor called with explicit template arguments
40 */
41 template <Common::StdCompat::Lockable REAL_MUTEX>
42 static VirtualLockable Make ();
43
44 public:
45 /**
46 */
47 VirtualLockable () = delete;
48 VirtualLockable (const VirtualLockable& src) = delete;
49 VirtualLockable (VirtualLockable&& src) noexcept = default;
50
51 private:
52 struct IRep_;
53 VirtualLockable (unique_ptr<IRep_>&& rep);
54
55 public:
56 VirtualLockable& operator= (VirtualLockable&& rhs) noexcept = default;
57
58 public:
59 nonvirtual void lock ();
60 nonvirtual bool try_lock ();
61 nonvirtual void unlock ();
62
63 private:
64 struct IRep_ {
65 virtual ~IRep_ () = default;
66 virtual void lock () = 0;
67 virtual bool try_lock () = 0;
68 virtual void unlock () = 0;
69 };
70 unique_ptr<IRep_> fRep_;
71 };
72 static_assert (movable<VirtualLockable>);
73 static_assert (not copyable<VirtualLockable>);
75
76}
77
78/*
79 ********************************************************************************
80 ***************************** Implementation Details ***************************
81 ********************************************************************************
82 */
83#include "VirtualLockable.inl"
84
85#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