xref: /aosp_15_r20/hardware/libhardware_legacy/include/wakelock/wakelock.h (revision 79330504eb3d14022296e3b041867f86289dd52c)
1*79330504STreehugger Robot /*
2*79330504STreehugger Robot  * Copyright (C) 2019 The Android Open Source Project
3*79330504STreehugger Robot  *
4*79330504STreehugger Robot  * Licensed under the Apache License, Version 2.0 (the "License");
5*79330504STreehugger Robot  * you may not use this file except in compliance with the License.
6*79330504STreehugger Robot  * You may obtain a copy of the License at
7*79330504STreehugger Robot  *
8*79330504STreehugger Robot  *      http://www.apache.org/licenses/LICENSE-2.0
9*79330504STreehugger Robot  *
10*79330504STreehugger Robot  * Unless required by applicable law or agreed to in writing, software
11*79330504STreehugger Robot  * distributed under the License is distributed on an "AS IS" BASIS,
12*79330504STreehugger Robot  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*79330504STreehugger Robot  * See the License for the specific language governing permissions and
14*79330504STreehugger Robot  * limitations under the License.
15*79330504STreehugger Robot  */
16*79330504STreehugger Robot 
17*79330504STreehugger Robot #pragma once
18*79330504STreehugger Robot 
19*79330504STreehugger Robot #include <memory>
20*79330504STreehugger Robot #include <optional>
21*79330504STreehugger Robot #include <string>
22*79330504STreehugger Robot 
23*79330504STreehugger Robot namespace android {
24*79330504STreehugger Robot namespace wakelock {
25*79330504STreehugger Robot 
26*79330504STreehugger Robot // RAII-style wake lock implementation
27*79330504STreehugger Robot class WakeLock {
28*79330504STreehugger Robot   private:
29*79330504STreehugger Robot     class WakeLockImpl;
30*79330504STreehugger Robot     std::unique_ptr<WakeLockImpl> mImpl;
31*79330504STreehugger Robot 
32*79330504STreehugger Robot   public:
33*79330504STreehugger Robot     static std::optional<WakeLock> tryGet(const std::string& name);
34*79330504STreehugger Robot     // Constructor is only made public for use with std::optional.
35*79330504STreehugger Robot     // It is not intended to be and cannot be invoked from public context,
36*79330504STreehugger Robot     // since private WakeLockImpl prevents calling the constructor directly.
37*79330504STreehugger Robot     WakeLock(std::unique_ptr<WakeLockImpl> wlImpl);
38*79330504STreehugger Robot     ~WakeLock();
39*79330504STreehugger Robot };
40*79330504STreehugger Robot 
41*79330504STreehugger Robot }  // namespace wakelock
42*79330504STreehugger Robot }  // namespace android
43