DSMBCTrigger.h 1.86 KB
Newer Older
Jonas Stienen's avatar
Jonas Stienen committed
1
/*
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
 * ----------------------------------------------------------------
 *
 *		ITA core libs
 *		(c) Copyright Institute of Technical Acoustics (ITA)
 *		RWTH Aachen University, Germany, 2015-2017
 *
 * ----------------------------------------------------------------
 *				    ____  __________  _______
 *				   //  / //__   ___/ //  _   |
 *				  //  /    //  /    //  /_|  |
 *				 //  /    //  /    //  ___   |
 *				//__/    //__/    //__/   |__|
 *
 * ----------------------------------------------------------------
 *
 */
Jonas Stienen's avatar
Jonas Stienen committed
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75

#ifndef INCLUDE_WATCHER_DSMBC_TRIGGER
#define INCLUDE_WATCHER_DSMBC_TRIGGER

#include <ITAConvolutionDefinitions.h>

#include <ITAUncopyable.h>

// Lightweight atomic trigger for synchronized filter exchanges
class ITA_CONVOLUTION_API DSMBCTrigger : public ITAUncopyable
{
public:
	DSMBCTrigger() : m_iState(0) {};

	// Returns the current state
	int getState() const { return m_iState; };

	// Pull the trigger. Update/alter the state.
	void trigger() { ++m_iState; };
	
private:
	volatile int m_iState;
};

// Simple watcher class, realization update checks on a trigger
class ITA_CONVOLUTION_API DSMBCTriggerWatch
{
public:
	DSMBCTriggerWatch() : m_pTrigger(0), bFirst(true), m_iPrevTriggerState(0) {};

	const DSMBCTrigger* getWatchedTrigger() const { return m_pTrigger; };
	
	void setWatchedTrigger(const DSMBCTrigger* pNewTrigger) {
		m_pTrigger = pNewTrigger;
		bFirst = true;
	};

	// Logic: We always fire, if there is no trigger associated. This is needed in the convolver.
	bool fire() {
		if (!m_pTrigger) return true;

		int iNewTriggerState = m_pTrigger->getState();
		if ((iNewTriggerState != m_iPrevTriggerState) || bFirst) {
			m_iPrevTriggerState = iNewTriggerState;
			bFirst = false;
			return true;
		}

		return false;
	};

private:
	const DSMBCTrigger* m_pTrigger;
	bool bFirst;
	int m_iPrevTriggerState;
};

#endif // INCLUDE_WATCHER_DSMBC_TRIGGER