Pigweed
 
Loading...
Searching...
No Matches
inline_borrowable.h
1// Copyright 2022 The Pigweed Authors
2//
3// Licensed under the Apache License, Version 2.0 (the "License"); you may not
4// use this file except in compliance with the License. You may obtain a copy of
5// the License at
6//
7// https://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12// License for the specific language governing permissions and limitations under
13// the License.
14#pragma once
15
16#include <tuple>
17#include <utility>
18
19#include "pw_sync/borrow.h"
20#include "pw_sync/internal/borrowable_storage.h"
21#include "pw_sync/mutex.h"
22#include "pw_sync/virtual_basic_lockable.h"
23
24namespace pw::sync {
25
33template <typename GuardedType,
34 typename Lock = pw::sync::VirtualMutex,
35 typename LockInterface = pw::sync::VirtualBasicLockable>
36class InlineBorrowable : private internal::BorrowableStorage<GuardedType, Lock>,
37 public Borrowable<GuardedType, LockInterface> {
38 using Storage = internal::BorrowableStorage<GuardedType, Lock>;
40
41 public:
43 constexpr InlineBorrowable()
44 : Storage(std::in_place), Base(Storage::object_, Storage::lock_) {}
45
60 template <typename... Args>
61 constexpr explicit InlineBorrowable(std::in_place_t, Args&&... args)
62 : Storage(std::in_place, std::forward<Args>(args)...),
63 Base(Storage::object_, Storage::lock_) {}
64
82 template <typename... ObjectArgs, typename... LockArgs>
83 constexpr explicit InlineBorrowable(
84 std::tuple<ObjectArgs...>&& object_args,
85 std::tuple<LockArgs...>&& lock_args = std::make_tuple())
86 : Storage(std::forward<std::tuple<ObjectArgs...>>(object_args),
87 std::forward<std::tuple<LockArgs...>>(lock_args)),
88 Base(Storage::object_, Storage::lock_) {}
89
104 template <typename ObjectConstructor,
105 typename LockConstructor = Lock(),
106 typename = std::enable_if_t<
107 std::is_invocable_r_v<GuardedType&&, ObjectConstructor>>,
108 typename = std::enable_if_t<
109 std::is_invocable_r_v<Lock&&, LockConstructor>>>
110 constexpr explicit InlineBorrowable(
111 const ObjectConstructor& object_ctor,
112 const LockConstructor& lock_ctor = internal::DefaultConstruct<Lock>)
113 : Storage(object_ctor, lock_ctor),
114 Base(Storage::object_, Storage::lock_) {}
115
116 template <typename ObjectConstructor,
117 typename LockConstructor = Lock(),
118 typename = std::enable_if_t<
119 std::is_invocable_r_v<GuardedType&&, ObjectConstructor>>,
120 typename = std::enable_if_t<
121 std::is_invocable_r_v<Lock&&, LockConstructor>>>
122 constexpr explicit InlineBorrowable(
123 ObjectConstructor& object_ctor,
124 const LockConstructor& lock_ctor = internal::DefaultConstruct<Lock>)
125 : Storage(object_ctor, lock_ctor),
126 Base(Storage::object_, Storage::lock_) {}
127
128 template <typename ObjectConstructor,
129 typename LockConstructor = Lock(),
130 typename = std::enable_if_t<
131 std::is_invocable_r_v<GuardedType&&, ObjectConstructor>>,
132 typename = std::enable_if_t<
133 std::is_invocable_r_v<Lock&&, LockConstructor>>>
134 constexpr explicit InlineBorrowable(const ObjectConstructor& object_ctor,
135 LockConstructor& lock_ctor)
136 : Storage(object_ctor, lock_ctor),
137 Base(Storage::object_, Storage::lock_) {}
138
139 template <typename ObjectConstructor,
140 typename LockConstructor = Lock(),
141 typename = std::enable_if_t<
142 std::is_invocable_r_v<GuardedType&&, ObjectConstructor>>,
143 typename = std::enable_if_t<
144 std::is_invocable_r_v<Lock&&, LockConstructor>>>
145 constexpr explicit InlineBorrowable(ObjectConstructor& object_ctor,
146 LockConstructor& lock_ctor)
147 : Storage(object_ctor, lock_ctor),
148 Base(Storage::object_, Storage::lock_) {}
149};
150
151} // namespace pw::sync
Definition: borrow.h:164
Definition: inline_borrowable.h:37
constexpr InlineBorrowable(std::in_place_t, Args &&... args)
Definition: inline_borrowable.h:61
constexpr InlineBorrowable(std::tuple< ObjectArgs... > &&object_args, std::tuple< LockArgs... > &&lock_args=std::make_tuple())
Definition: inline_borrowable.h:83
constexpr InlineBorrowable(const ObjectConstructor &object_ctor, const LockConstructor &lock_ctor=internal::DefaultConstruct< Lock >)
Definition: inline_borrowable.h:110
constexpr InlineBorrowable()
Construct the guarded object and lock using their default constructors.
Definition: inline_borrowable.h:43
Definition: virtual_basic_lockable.h:29
Definition: mutex.h:89