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

Commit f680bd26 authored by Hansong Zhang's avatar Hansong Zhang
Browse files

Replace std::function with base::Closure

std::function doesn't support non-copyable arguments. Use base::Closure
and base::OnceClosure from libchrome instead.

Test: atest --host bluetooth_test_gd
Change-Id: Ic18bbc000730e8b0d7acff0097870bd258ee6150
parent 3bd06b7f
Loading
Loading
Loading
Loading
+11 −0
Original line number Diff line number Diff line
@@ -124,6 +124,9 @@ cc_library {
    generated_headers: [
        "BluetoothGeneratedPackets_h",
    ],
    shared_libs: [
        "libchrome",
    ],
}

cc_binary {
@@ -149,6 +152,7 @@ cc_binary {
        "libbluetooth_gd",
    ],
    shared_libs: [
        "libchrome",
        "libgrpc++_unsecure",
        "libprotobuf-cpp-full",
    ],
@@ -191,6 +195,7 @@ cc_binary {
        "libbluetooth_gd",
    ],
    shared_libs: [
        "libchrome",
        "libgrpc++_unsecure",
        "libprotobuf-cpp-full",
    ],
@@ -257,6 +262,9 @@ cc_test {
    static_libs: [
        "libbluetooth_gd",
    ],
    shared_libs: [
        "libchrome",
    ],
    sanitize: {
        address: true,
    },
@@ -294,6 +302,9 @@ cc_benchmark {
    static_libs: [
        "libbluetooth_gd",
    ],
    shared_libs: [
        "libchrome",
    ],
}

genrule {
+3 −2
Original line number Diff line number Diff line
@@ -18,6 +18,7 @@

#pragma once

#include "common/callback.h"
#include "os/queue.h"

namespace bluetooth {
@@ -28,8 +29,8 @@ class BidiQueueEnd
    : public ::bluetooth::os::IQueueEnqueue<TENQUEUE>,
      public ::bluetooth::os::IQueueDequeue<TDEQUEUE> {
 public:
  using EnqueueCallback = std::function<std::unique_ptr<TENQUEUE>()>;
  using DequeueCallback = std::function<void()>;
  using EnqueueCallback = Callback<std::unique_ptr<TENQUEUE>()>;
  using DequeueCallback = Callback<void()>;

  BidiQueueEnd(::bluetooth::os::IQueueEnqueue<TENQUEUE>* tx, ::bluetooth::os::IQueueDequeue<TDEQUEUE>* rx)
      : tx_(tx), rx_(rx) {
+27 −15
Original line number Diff line number Diff line
@@ -18,9 +18,10 @@

#include <future>

#include "common/bind.h"
#include "gtest/gtest.h"
#include "os/thread.h"
#include "os/handler.h"
#include "os/thread.h"

using ::bluetooth::os::Thread;
using ::bluetooth::os::Handler;
@@ -69,27 +70,38 @@ class TestBidiQueueEnd {

  std::promise<void>* Send(TA* value) {
    std::promise<void>* promise = new std::promise<void>();
    handler_->Post([this, value, promise] {
      end_->RegisterEnqueue(handler_, [this, value, promise]() -> std::unique_ptr<TA> {
    handler_->Post(BindOnce(&TestBidiQueueEnd<TA, TB>::handle_send, common::Unretained(this), common::Unretained(value),
                            common::Unretained(promise)));
    return promise;
  }

  std::promise<TB*>* Receive() {
    std::promise<TB*>* promise = new std::promise<TB*>();
    handler_->Post(
        BindOnce(&TestBidiQueueEnd<TA, TB>::handle_receive, common::Unretained(this), common::Unretained(promise)));

    return promise;
  }

  void handle_send(TA* value, std::promise<void>* promise) {
    end_->RegisterEnqueue(handler_, Bind(&TestBidiQueueEnd<TA, TB>::handle_register_enqueue, common::Unretained(this),
                                         common::Unretained(value), common::Unretained(promise)));
  }

  std::unique_ptr<TA> handle_register_enqueue(TA* value, std::promise<void>* promise) {
    end_->UnregisterEnqueue();
    promise->set_value();
    return std::unique_ptr<TA>(value);
      });
    });
  }

    return promise;
  void handle_receive(std::promise<TB*>* promise) {
    end_->RegisterDequeue(handler_, Bind(&TestBidiQueueEnd<TA, TB>::handle_register_dequeue, common::Unretained(this),
                                         common::Unretained(promise)));
  }

  std::promise<TB*>* Receive() {
    std::promise<TB*>* promise = new std::promise<TB*>();
    handler_->Post([this, promise] {
      end_->RegisterDequeue(handler_, [this, promise] {
  void handle_register_dequeue(std::promise<TB*>* promise) {
    end_->UnregisterDequeue();
    promise->set_value(end_->TryDequeue().get());
      });
    });

    return promise;
  }

 private:
+34 −0
Original line number Diff line number Diff line
/*
 * Copyright 2019 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 "base/bind.h"

namespace bluetooth {
namespace common {

using base::Bind;
using base::BindOnce;
using base::ConstRef;
using base::IgnoreResult;
using base::Owned;
using base::Passed;
using base::RetainedRef;
using base::Unretained;

}  // namespace common
}  // namespace bluetooth
+30 −0
Original line number Diff line number Diff line
/*
 * Copyright 2019 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 "base/callback.h"

namespace bluetooth {
namespace common {

using base::Callback;
using base::Closure;
using base::OnceCallback;
using base::OnceClosure;

}  // namespace common
}  // namespace bluetooth
Loading