Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit b830711a authored by Chris Manton's avatar Chris Manton Committed by Automerger Merge Worker
Browse files

Extract bindable context from postable model am: a229634d

parents f4a44ed1 a229634d
Loading
Loading
Loading
Loading
+3 −0
Original line number Diff line number Diff line
@@ -17,6 +17,7 @@
#pragma once

#include "btif/include/btif_common.h"
#include "common/postable_context.h"
#include "include/hardware/bluetooth.h"

void jni_thread_startup();
@@ -55,3 +56,5 @@ bt_status_t do_in_jni_thread(base::OnceClosure task);
bool is_on_jni_thread();

void post_on_bt_jni(BtJniClosure closure);

bluetooth::common::PostableContext* get_jni();
+3 −0
Original line number Diff line number Diff line
@@ -26,6 +26,7 @@
#include <utility>

#include "common/message_loop_thread.h"
#include "common/postable_context.h"
#include "include/hardware/bluetooth.h"
#include "osi/include/allocator.h"
#include "stack/include/bt_types.h"
@@ -124,3 +125,5 @@ void post_on_bt_jni(BtJniClosure closure) {
                                                    std::move(closure))) ==
         BT_STATUS_SUCCESS);
}

bluetooth::common::PostableContext* get_jni() { return jni_thread.Postable(); }
+4 −0
Original line number Diff line number Diff line
@@ -29,6 +29,8 @@
#include <string>
#include <thread>

#include "common/postable_context.h"

namespace bluetooth {
namespace common {

@@ -215,5 +217,7 @@ void MessageLoopThread::Post(base::OnceClosure closure) {
  DoInThread(FROM_HERE, std::move(closure));
}

PostableContext* MessageLoopThread::Postable() { return this; }

}  // namespace common
}  // namespace bluetooth
+6 −34
Original line number Diff line number Diff line
@@ -27,8 +27,7 @@
#include <thread>

#include "abstract_message_loop.h"
#include "common/contextual_callback.h"
#include "common/i_postable_context.h"
#include "common/postable_context.h"

namespace bluetooth {

@@ -37,7 +36,7 @@ namespace common {
/**
 * An interface to various thread related functionality
 */
class MessageLoopThread final : public IPostableContext {
class MessageLoopThread final : public PostableContext {
 public:
  /**
   * Create a message loop thread with name. Thread won't be running until
@@ -173,37 +172,10 @@ class MessageLoopThread final : public IPostableContext {
   */
  void Post(base::OnceClosure closure) override;

  template <typename Functor, typename... Args>
  auto BindOnce(Functor&& functor, Args&&... args) {
    return common::ContextualOnceCallback(
        common::BindOnce(std::forward<Functor>(functor),
                         std::forward<Args>(args)...),
        this);
  }

  template <typename Functor, typename T, typename... Args>
  auto BindOnceOn(T* obj, Functor&& functor, Args&&... args) {
    return common::ContextualOnceCallback(
        common::BindOnce(std::forward<Functor>(functor),
                         common::Unretained(obj), std::forward<Args>(args)...),
        this);
  }

  template <typename Functor, typename... Args>
  auto Bind(Functor&& functor, Args&&... args) {
    return common::ContextualCallback(
        common::Bind(std::forward<Functor>(functor),
                     std::forward<Args>(args)...),
        this);
  }

  template <typename Functor, typename T, typename... Args>
  auto BindOn(T* obj, Functor&& functor, Args&&... args) {
    return common::ContextualCallback(
        common::Bind(std::forward<Functor>(functor), common::Unretained(obj),
                     std::forward<Args>(args)...),
        this);
  }
  /**
   * Returns a postable object
   */
  PostableContext* Postable();

 private:
  /**
+58 −0
Original line number Diff line number Diff line
/*
 * Copyright 2024 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#pragma once

#include "common/bind.h"
#include "common/contextual_callback.h"
#include "common/i_postable_context.h"

namespace bluetooth::common {

class PostableContext : public IPostableContext {
 public:
  virtual ~PostableContext() = default;

  template <typename Functor, typename... Args>
  auto BindOnce(Functor&& functor, Args&&... args) {
    return common::ContextualOnceCallback(
        common::BindOnce(std::forward<Functor>(functor), std::forward<Args>(args)...), this);
  }

  template <typename Functor, typename T, typename... Args>
  auto BindOnceOn(T* obj, Functor&& functor, Args&&... args) {
    return common::ContextualOnceCallback(
        common::BindOnce(
            std::forward<Functor>(functor), common::Unretained(obj), std::forward<Args>(args)...),
        this);
  }

  template <typename Functor, typename... Args>
  auto Bind(Functor&& functor, Args&&... args) {
    return common::ContextualCallback(
        common::Bind(std::forward<Functor>(functor), std::forward<Args>(args)...), this);
  }

  template <typename Functor, typename T, typename... Args>
  auto BindOn(T* obj, Functor&& functor, Args&&... args) {
    return common::ContextualCallback(
        common::Bind(
            std::forward<Functor>(functor), common::Unretained(obj), std::forward<Args>(args)...),
        this);
  }
};

}  // namespace bluetooth::common
Loading