Program Listing for File data_sink.hpp

Return to documentation for file (/tmp/ws/src/data_tamer/data_tamer_cpp/include/data_tamer/data_sink.hpp)

#pragma once

#include "data_tamer/types.hpp"

#include <chrono>
#include <cstdint>
#include <memory>
#include <string>
#include <string_view>
#include <vector>

namespace DataTamer
{

using ActiveMask = std::vector<uint8_t>;
using PayloadVector = std::vector<uint8_t>;

bool GetBit(const ActiveMask& mask, size_t index);
void SetBit(ActiveMask& mask, size_t index, bool val);

struct Snapshot
{
  std::string_view channel_name;

  std::size_t schema_hash;

  std::chrono::nanoseconds timestamp;

  ActiveMask active_mask;

  PayloadVector payload;
};

using DataSnapshot = std::vector<uint8_t>;

class DataSinkBase
{
public:
  DataSinkBase();

  DataSinkBase(const DataSinkBase& other) = delete;
  DataSinkBase& operator=(const DataSinkBase& other) = delete;

  DataSinkBase(DataSinkBase&& other) = delete;
  DataSinkBase& operator=(DataSinkBase&& other) = delete;

  virtual ~DataSinkBase();

  virtual void addChannel(const std::string& name, const Schema& schema) = 0;

  virtual bool pushSnapshot(const Snapshot& snapshot);

protected:
  virtual bool storeSnapshot(const Snapshot& snapshot) = 0;

  void stopThread();

private:
  struct Pimpl;
  std::unique_ptr<Pimpl> _p;
};

//--------------------------------------------------

inline bool GetBit(const ActiveMask& mask, size_t index)
{
  const uint8_t& byte = mask[index >> 3];
  return 0 != (byte & uint8_t(1 << (index % 8)));
}

inline void SetBit(ActiveMask& mask, size_t index, bool value)
{
  if (!value)
  {
    mask[index >> 3] &= uint8_t(~(1 << (index % 8)));
  }
  else
  {
    mask[index >> 3] |= uint8_t(1 << (index % 8));
  }
}

}   // namespace DataTamer