/****************************************************************************** * * 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 "common/callback.h" #include "os/queue.h" namespace bluetooth { namespace common { template class BidiQueueEnd : public ::bluetooth::os::IQueueEnqueue, public ::bluetooth::os::IQueueDequeue { public: using EnqueueCallback = Callback()>; using DequeueCallback = Callback; BidiQueueEnd(::bluetooth::os::IQueueEnqueue* tx, ::bluetooth::os::IQueueDequeue* rx) : tx_(tx), rx_(rx) {} void RegisterEnqueue(::bluetooth::os::Handler* handler, EnqueueCallback callback) override { tx_->RegisterEnqueue(handler, callback); } void UnregisterEnqueue() override { tx_->UnregisterEnqueue(); } void RegisterDequeue(::bluetooth::os::Handler* handler, DequeueCallback callback) override { rx_->RegisterDequeue(handler, callback); } void UnregisterDequeue() override { rx_->UnregisterDequeue(); } std::unique_ptr TryDequeue() override { return rx_->TryDequeue(); } private: ::bluetooth::os::IQueueEnqueue* tx_; ::bluetooth::os::IQueueDequeue* rx_; }; template class BidiQueue { public: explicit BidiQueue(size_t capacity) : up_queue_(capacity), down_queue_(capacity), up_end_(&down_queue_, &up_queue_), down_end_(&up_queue_, &down_queue_) {} BidiQueueEnd* GetUpEnd() { return &up_end_; } BidiQueueEnd* GetDownEnd() { return &down_end_; } private: ::bluetooth::os::Queue up_queue_; ::bluetooth::os::Queue down_queue_; BidiQueueEnd up_end_; BidiQueueEnd down_end_; }; } // namespace common } // namespace bluetooth