C/C++ API Reference
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
27
34template <typename GuardedType,
35 typename Lock = pw::sync::VirtualMutex,
36 typename LockInterface = Lock>
37class InlineBorrowable : private internal::BorrowableStorage<GuardedType, Lock>,
38 public Borrowable<GuardedType, LockInterface> {
39 using Storage = internal::BorrowableStorage<GuardedType, Lock>;
41
42 public:
44 constexpr InlineBorrowable()
45 : Storage(std::in_place), Base(Storage::object_, Storage::lock_) {}
46
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
81 template <typename... ObjectArgs, typename... LockArgs>
82 constexpr explicit InlineBorrowable(
83 std::tuple<ObjectArgs...>&& object_args,
84 std::tuple<LockArgs...>&& lock_args = std::make_tuple())
85 : Storage(std::forward<std::tuple<ObjectArgs...>>(object_args),
86 std::forward<std::tuple<LockArgs...>>(lock_args)),
87 Base(Storage::object_, Storage::lock_) {}
88
102 template <typename ObjectConstructor,
103 typename LockConstructor = Lock(),
104 typename = std::enable_if_t<
105 std::is_invocable_r_v<GuardedType&&, ObjectConstructor>>,
106 typename = std::enable_if_t<
107 std::is_invocable_r_v<Lock&&, LockConstructor>>>
108 constexpr explicit InlineBorrowable(
109 const ObjectConstructor& object_ctor,
110 const LockConstructor& lock_ctor = internal::DefaultConstruct<Lock>)
111 : Storage(object_ctor, lock_ctor),
112 Base(Storage::object_, Storage::lock_) {}
113
114 template <typename ObjectConstructor,
115 typename LockConstructor = Lock(),
116 typename = std::enable_if_t<
117 std::is_invocable_r_v<GuardedType&&, ObjectConstructor>>,
118 typename = std::enable_if_t<
119 std::is_invocable_r_v<Lock&&, LockConstructor>>>
120 constexpr explicit InlineBorrowable(
121 ObjectConstructor& object_ctor,
122 const LockConstructor& lock_ctor = internal::DefaultConstruct<Lock>)
123 : Storage(object_ctor, lock_ctor),
124 Base(Storage::object_, Storage::lock_) {}
125
126 template <typename ObjectConstructor,
127 typename LockConstructor = Lock(),
128 typename = std::enable_if_t<
129 std::is_invocable_r_v<GuardedType&&, ObjectConstructor>>,
130 typename = std::enable_if_t<
131 std::is_invocable_r_v<Lock&&, LockConstructor>>>
132 constexpr explicit InlineBorrowable(const ObjectConstructor& object_ctor,
133 LockConstructor& lock_ctor)
134 : Storage(object_ctor, lock_ctor),
135 Base(Storage::object_, Storage::lock_) {}
136
137 template <typename ObjectConstructor,
138 typename LockConstructor = Lock(),
139 typename = std::enable_if_t<
140 std::is_invocable_r_v<GuardedType&&, ObjectConstructor>>,
141 typename = std::enable_if_t<
142 std::is_invocable_r_v<Lock&&, LockConstructor>>>
143 constexpr explicit InlineBorrowable(ObjectConstructor& object_ctor,
144 LockConstructor& lock_ctor)
145 : Storage(object_ctor, lock_ctor),
146 Base(Storage::object_, Storage::lock_) {}
147};
148
150
151} // namespace pw::sync
Definition: borrow.h:164
Definition: inline_borrowable.h:38
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:82
constexpr InlineBorrowable(const ObjectConstructor &object_ctor, const LockConstructor &lock_ctor=internal::DefaultConstruct< Lock >)
Definition: inline_borrowable.h:108
constexpr InlineBorrowable()
Constructs the guarded object and lock using their default constructors.
Definition: inline_borrowable.h:44
Definition: mutex.h:84
Definition: binary_semaphore.h:26