Program Listing for File channel.hpp

Return to documentation for file (foxglove/include/foxglove/channel.hpp)

#pragma once

#include <foxglove-c/foxglove-c.h>
#include <foxglove/context.hpp>
#include <foxglove/error.hpp>
#include <foxglove/messages.hpp>
#include <foxglove/schema.hpp>

#include <cstdint>
#include <functional>
#include <map>
#include <memory>
#include <optional>
#include <string>
#include <type_traits>

struct foxglove_channel;
struct foxglove_channel_descriptor;

namespace foxglove {

class ChannelDescriptor {
  const foxglove_channel_descriptor* channel_descriptor_;

public:
  explicit ChannelDescriptor(const foxglove_channel_descriptor* channel_descriptor);

  [[nodiscard]] std::string_view topic() const noexcept;

  [[nodiscard]] std::string_view messageEncoding() const noexcept;

  // NOLINTNEXTLINE(readability-identifier-naming)
  [[deprecated("Use messageEncoding() instead")]] [[nodiscard]] std::string_view message_encoding(
  ) const noexcept {
    return messageEncoding();
  }

  [[nodiscard]] std::optional<std::map<std::string, std::string>> metadata() const noexcept;

  [[nodiscard]] std::optional<Schema> schema() const noexcept;
};

class SinkChannelFilterFn {
public:
  SinkChannelFilterFn() = default;

  template<
    typename F, typename = std::enable_if_t<
                  std::is_invocable_r_v<bool, F, const ChannelDescriptor&> &&
                  !std::is_same_v<std::decay_t<F>, SinkChannelFilterFn>>>
  // NOLINTNEXTLINE(google-explicit-constructor,hicpp-explicit-conversions)
  SinkChannelFilterFn(F&& fn)
      : fn_(std::forward<F>(fn)) {}

  template<
    typename F,
    typename = std::enable_if_t<
      std::is_invocable_r_v<bool, F, ChannelDescriptor&&> &&
      !std::is_invocable_v<F, const ChannelDescriptor&> &&
      !std::is_same_v<std::decay_t<F>, SinkChannelFilterFn>>,
    typename /*Disambiguate*/ = void>
  [[deprecated(
    "Use a filter function taking const ChannelDescriptor& instead of ChannelDescriptor&&"
  )]]
  // NOLINTNEXTLINE(google-explicit-constructor,hicpp-explicit-conversions)
  SinkChannelFilterFn(F&& fn)
      : fn_([f = std::forward<F>(fn)](const ChannelDescriptor& ch) mutable {
        auto copy = ch;
        return f(std::move(copy));
      }) {}

  explicit operator bool() const {
    return static_cast<bool>(fn_);
  }

  bool operator()(const ChannelDescriptor& channel) const {
    return fn_(channel);
  }

private:
  std::function<bool(const ChannelDescriptor&)> fn_;
};

class RawChannel final {
public:
  static FoxgloveResult<RawChannel> create(
    const std::string_view& topic, const std::string_view& message_encoding,
    std::optional<Schema> schema = std::nullopt, const Context& context = Context(),
    std::optional<std::map<std::string, std::string>> metadata = std::nullopt
  );

  FoxgloveError log(
    const std::byte* data, size_t data_len, std::optional<uint64_t> log_time = std::nullopt,
    std::optional<uint64_t> sink_id = std::nullopt
  ) noexcept;

  void close() noexcept;

  [[nodiscard]] uint64_t id() const noexcept;

  [[nodiscard]] std::string_view topic() const noexcept;

  [[nodiscard]] std::string_view messageEncoding() const noexcept;

  // NOLINTNEXTLINE(readability-identifier-naming)
  [[deprecated("Use messageEncoding() instead")]] [[nodiscard]] std::string_view message_encoding(
  ) const noexcept {
    return messageEncoding();
  }

  [[nodiscard]] bool hasSinks() const noexcept;

  // NOLINTNEXTLINE(readability-identifier-naming)
  [[deprecated("Use hasSinks() instead")]] [[nodiscard]] bool has_sinks() const noexcept {
    return hasSinks();
  }

  [[nodiscard]] std::optional<Schema> schema() const noexcept;

  [[nodiscard]] std::optional<std::map<std::string, std::string>> metadata() const noexcept;

  RawChannel(const RawChannel&) = delete;
  RawChannel& operator=(const RawChannel&) = delete;
  RawChannel(RawChannel&& other) noexcept = default;
  RawChannel& operator=(RawChannel&& other) noexcept = default;
  ~RawChannel() = default;

private:
  explicit RawChannel(const foxglove_channel* channel);

  messages::ChannelUniquePtr impl_;
};

}  // namespace foxglove