1 /*
2  * Copyright 2024 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #pragma once
18 
19 #include "common/bind.h"
20 #include "common/contextual_callback.h"
21 #include "common/i_postable_context.h"
22 
23 namespace bluetooth::common {
24 
25 class PostableContext : public IPostableContext {
26  public:
27   virtual ~PostableContext() = default;
28 
29   template <typename Functor, typename... Args>
BindOnce(Functor && functor,Args &&...args)30   auto BindOnce(Functor&& functor, Args&&... args) {
31     return common::ContextualOnceCallback(
32         common::BindOnce(std::forward<Functor>(functor), std::forward<Args>(args)...), this);
33   }
34 
35   template <typename Functor, typename T, typename... Args>
BindOnceOn(T * obj,Functor && functor,Args &&...args)36   auto BindOnceOn(T* obj, Functor&& functor, Args&&... args) {
37     return common::ContextualOnceCallback(
38         common::BindOnce(
39             std::forward<Functor>(functor), common::Unretained(obj), std::forward<Args>(args)...),
40         this);
41   }
42 
43   template <typename Functor, typename... Args>
Bind(Functor && functor,Args &&...args)44   auto Bind(Functor&& functor, Args&&... args) {
45     return common::ContextualCallback(
46         common::Bind(std::forward<Functor>(functor), std::forward<Args>(args)...), this);
47   }
48 
49   template <typename Functor, typename T, typename... Args>
BindOn(T * obj,Functor && functor,Args &&...args)50   auto BindOn(T* obj, Functor&& functor, Args&&... args) {
51     return common::ContextualCallback(
52         common::Bind(
53             std::forward<Functor>(functor), common::Unretained(obj), std::forward<Args>(args)...),
54         this);
55   }
56 };
57 
58 }  // namespace bluetooth::common
59