11cb0ef41Sopenharmony_ci#ifndef SRC_CALLBACK_QUEUE_INL_H_
21cb0ef41Sopenharmony_ci#define SRC_CALLBACK_QUEUE_INL_H_
31cb0ef41Sopenharmony_ci
41cb0ef41Sopenharmony_ci#if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
51cb0ef41Sopenharmony_ci
61cb0ef41Sopenharmony_ci#include "callback_queue.h"
71cb0ef41Sopenharmony_ci
81cb0ef41Sopenharmony_cinamespace node {
91cb0ef41Sopenharmony_ci
101cb0ef41Sopenharmony_citemplate <typename R, typename... Args>
111cb0ef41Sopenharmony_citemplate <typename Fn>
121cb0ef41Sopenharmony_cistd::unique_ptr<typename CallbackQueue<R, Args...>::Callback>
131cb0ef41Sopenharmony_ciCallbackQueue<R, Args...>::CreateCallback(Fn&& fn, CallbackFlags::Flags flags) {
141cb0ef41Sopenharmony_ci  return std::make_unique<CallbackImpl<Fn>>(std::move(fn), flags);
151cb0ef41Sopenharmony_ci}
161cb0ef41Sopenharmony_ci
171cb0ef41Sopenharmony_citemplate <typename R, typename... Args>
181cb0ef41Sopenharmony_cistd::unique_ptr<typename CallbackQueue<R, Args...>::Callback>
191cb0ef41Sopenharmony_ciCallbackQueue<R, Args...>::Shift() {
201cb0ef41Sopenharmony_ci  std::unique_ptr<Callback> ret = std::move(head_);
211cb0ef41Sopenharmony_ci  if (ret) {
221cb0ef41Sopenharmony_ci    head_ = ret->get_next();
231cb0ef41Sopenharmony_ci    if (!head_)
241cb0ef41Sopenharmony_ci      tail_ = nullptr;  // The queue is now empty.
251cb0ef41Sopenharmony_ci    size_--;
261cb0ef41Sopenharmony_ci  }
271cb0ef41Sopenharmony_ci  return ret;
281cb0ef41Sopenharmony_ci}
291cb0ef41Sopenharmony_ci
301cb0ef41Sopenharmony_citemplate <typename R, typename... Args>
311cb0ef41Sopenharmony_civoid CallbackQueue<R, Args...>::Push(std::unique_ptr<Callback> cb) {
321cb0ef41Sopenharmony_ci  Callback* prev_tail = tail_;
331cb0ef41Sopenharmony_ci
341cb0ef41Sopenharmony_ci  size_++;
351cb0ef41Sopenharmony_ci  tail_ = cb.get();
361cb0ef41Sopenharmony_ci  if (prev_tail != nullptr)
371cb0ef41Sopenharmony_ci    prev_tail->set_next(std::move(cb));
381cb0ef41Sopenharmony_ci  else
391cb0ef41Sopenharmony_ci    head_ = std::move(cb);
401cb0ef41Sopenharmony_ci}
411cb0ef41Sopenharmony_ci
421cb0ef41Sopenharmony_citemplate <typename R, typename... Args>
431cb0ef41Sopenharmony_civoid CallbackQueue<R, Args...>::ConcatMove(CallbackQueue<R, Args...>&& other) {
441cb0ef41Sopenharmony_ci  size_ += other.size_;
451cb0ef41Sopenharmony_ci  if (tail_ != nullptr)
461cb0ef41Sopenharmony_ci    tail_->set_next(std::move(other.head_));
471cb0ef41Sopenharmony_ci  else
481cb0ef41Sopenharmony_ci    head_ = std::move(other.head_);
491cb0ef41Sopenharmony_ci  tail_ = other.tail_;
501cb0ef41Sopenharmony_ci  other.tail_ = nullptr;
511cb0ef41Sopenharmony_ci  other.size_ = 0;
521cb0ef41Sopenharmony_ci}
531cb0ef41Sopenharmony_ci
541cb0ef41Sopenharmony_citemplate <typename R, typename... Args>
551cb0ef41Sopenharmony_cisize_t CallbackQueue<R, Args...>::size() const {
561cb0ef41Sopenharmony_ci  return size_.load();
571cb0ef41Sopenharmony_ci}
581cb0ef41Sopenharmony_ci
591cb0ef41Sopenharmony_citemplate <typename R, typename... Args>
601cb0ef41Sopenharmony_ciCallbackQueue<R, Args...>::Callback::Callback(CallbackFlags::Flags flags)
611cb0ef41Sopenharmony_ci  : flags_(flags) {}
621cb0ef41Sopenharmony_ci
631cb0ef41Sopenharmony_citemplate <typename R, typename... Args>
641cb0ef41Sopenharmony_ciCallbackFlags::Flags CallbackQueue<R, Args...>::Callback::flags() const {
651cb0ef41Sopenharmony_ci  return flags_;
661cb0ef41Sopenharmony_ci}
671cb0ef41Sopenharmony_ci
681cb0ef41Sopenharmony_citemplate <typename R, typename... Args>
691cb0ef41Sopenharmony_cistd::unique_ptr<typename CallbackQueue<R, Args...>::Callback>
701cb0ef41Sopenharmony_ciCallbackQueue<R, Args...>::Callback::get_next() {
711cb0ef41Sopenharmony_ci  return std::move(next_);
721cb0ef41Sopenharmony_ci}
731cb0ef41Sopenharmony_ci
741cb0ef41Sopenharmony_citemplate <typename R, typename... Args>
751cb0ef41Sopenharmony_civoid CallbackQueue<R, Args...>::Callback::set_next(
761cb0ef41Sopenharmony_ci    std::unique_ptr<Callback> next) {
771cb0ef41Sopenharmony_ci  next_ = std::move(next);
781cb0ef41Sopenharmony_ci}
791cb0ef41Sopenharmony_ci
801cb0ef41Sopenharmony_citemplate <typename R, typename... Args>
811cb0ef41Sopenharmony_citemplate <typename Fn>
821cb0ef41Sopenharmony_ciCallbackQueue<R, Args...>::CallbackImpl<Fn>::CallbackImpl(
831cb0ef41Sopenharmony_ci    Fn&& callback, CallbackFlags::Flags flags)
841cb0ef41Sopenharmony_ci  : Callback(flags),
851cb0ef41Sopenharmony_ci    callback_(std::move(callback)) {}
861cb0ef41Sopenharmony_ci
871cb0ef41Sopenharmony_citemplate <typename R, typename... Args>
881cb0ef41Sopenharmony_citemplate <typename Fn>
891cb0ef41Sopenharmony_ciR CallbackQueue<R, Args...>::CallbackImpl<Fn>::Call(Args... args) {
901cb0ef41Sopenharmony_ci  return callback_(std::forward<Args>(args)...);
911cb0ef41Sopenharmony_ci}
921cb0ef41Sopenharmony_ci
931cb0ef41Sopenharmony_ci}  // namespace node
941cb0ef41Sopenharmony_ci
951cb0ef41Sopenharmony_ci#endif  // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
961cb0ef41Sopenharmony_ci
971cb0ef41Sopenharmony_ci#endif  // SRC_CALLBACK_QUEUE_INL_H_
98