lasp/cpp_src/device/lasp_indatahandler.h

80 lines
2.1 KiB
C++

#pragma once
#include "lasp_types.h"
#include <atomic>
#include <functional>
#include <memory>
#include <thread>
class StreamMgr;
using SmgrHandle = std::shared_ptr<StreamMgr>;
class DaqData;
class Daq;
/** \addtogroup device
* @{
*/
/**
* @brief The function definition of callbacks with incoming DAQ data
*/
using InCallbackType = std::function<void(const DaqData &)>;
/**
* @brief Function definition for the reset callback.
*/
using ResetCallbackType = std::function<void(const Daq *)>;
class InDataHandler {
protected:
std::weak_ptr<StreamMgr> _mgr;
#if LASP_DEBUG == 1
// This is a flag to indicate whether the method stop() is called for the
// current handler. It should call the method stop() from the derived class's
// destructor.
std::atomic<bool> stopCalled{false};
#endif
public:
~InDataHandler();
const InCallbackType inCallback;
const ResetCallbackType reset;
/**
* @brief When constructed, the handler is added to the stream manager, which
* will call the handlers's inCallback() until stop() is called.
*
* @param mgr Stream manager.
* @param cb The callback that is stored, and called on new DAQ data
* @param resetfcn The callback that is stored, and called when the DAQ
* changes state.
*/
InDataHandler(SmgrHandle mgr, InCallbackType cb,
ResetCallbackType resetfcn);
/**
* @brief Adds the current InDataHandler to the list of handlers in the
* StreamMgr. After this happens, the reset() method stored in this
* object is called back. When the stream is running, right after this,
* inCallback() is called with DaqData.
*/
void start();
/**
* @brief Removes the currend InDataHandler from the list of handlers in the
* StreamMgr. From that point on, the object can be safely destroyed. Not
* calling stop() before destruction of this object is considered a BUG. I.e.
* a class which *uses* an InDataHandler should always call stop() in its
* destructor.
*/
void stop();
#if LASP_DEBUG == 1
const std::thread::id main_thread_id;
void checkRightThread() const;
#else
void checkRightThread() const {}
#endif
};
/** @} */