Program Listing for File wait_set.hpp

Return to documentation for file (include/email/wait_set.hpp)

// Copyright 2021 Christophe Bedard
//
// 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.

#ifndef EMAIL__WAIT_SET_HPP_
#define EMAIL__WAIT_SET_HPP_

#include <chrono>
#include <memory>
#include <vector>

#include "email/log.hpp"
#include "email/guard_condition.hpp"
#include "email/service_client.hpp"
#include "email/service_server.hpp"
#include "email/subscription.hpp"
#include "email/macros.hpp"
#include "email/visibility_control.hpp"

namespace email
{


class WaitSet
{
public:
  EMAIL_PUBLIC
  WaitSet();

  EMAIL_PUBLIC
  ~WaitSet();


  EMAIL_PUBLIC
  bool
  wait(const std::chrono::milliseconds timeout = std::chrono::milliseconds(-1));


  EMAIL_PUBLIC
  void
  add_subscription(Subscription * subscription);


  EMAIL_PUBLIC
  void
  add_client(ServiceClient * client);


  EMAIL_PUBLIC
  void
  add_server(ServiceServer * server);


  EMAIL_PUBLIC
  void
  add_guard_condition(GuardCondition * guard_condition);

  EMAIL_PUBLIC
  const std::vector<Subscription *> &
  get_subscriptions() const;

  EMAIL_PUBLIC
  const std::vector<ServiceClient *> &
  get_clients() const;

  EMAIL_PUBLIC
  const std::vector<ServiceServer *> &
  get_servers() const;

  EMAIL_PUBLIC
  const std::vector<GuardCondition *> &
  get_guard_conditions() const;


  EMAIL_PUBLIC
  void
  clear();

private:
  EMAIL_DISABLE_COPY(WaitSet)


  static
  void
  check_guard_condition(GuardCondition * guard_condition);


  static
  void
  release_guard_conditions(std::vector<GuardCondition *> & guard_condition);


  template<class T>
  void
  apply_status(std::vector<T> & objects, const std::vector<bool> & ready)
  {
    for (std::size_t i = 0u; i < objects.size(); i++) {
      if (!ready[i]) {
        objects[i] = nullptr;
      }
    }
  }

  std::function<bool()>
  get_loop_predicate(
    std::chrono::nanoseconds timeout_ns,
    std::chrono::steady_clock::time_point start);

  std::shared_ptr<Logger> logger_;
  std::vector<Subscription *> subscriptions_;
  std::vector<ServiceClient *> clients_;
  std::vector<ServiceServer *> servers_;
  std::vector<GuardCondition *> guard_conditions_;
};

}  // namespace email

#endif  // EMAIL__WAIT_SET_HPP_