ParamFlow.hh 3.91 KB
/*
 * ParamFlow.hh
 *
 *  Created on: Nov 25, 2014
 *      Author: AKKA
 */

#ifndef PARAMFLOW_HH_
#define PARAMFLOW_HH_

#include "LocalParamData.hh"

#include <boost/shared_ptr.hpp>

#include <CThreadDeque.hh>

namespace AMDA {

namespace LocalFileInterface {

class VirtualInstrumentInterval;

/**
 * @class ParamFlow.
 * @brief Represent a flow of a couple time and data asked for one Parameter of a Virtual Instrument.
 * @details This flow is implement with a thread safe dequeue of Packet.
 * This class is a listener of the class VirtualInstrumentInterval.
 */
class ParamFlow {
public:
	/*
	 * @ brief Param info associated to a ParamFlow
	 */
	class ParamInfo
	{
	public:
		ParamInfo(LocalParamType paramType, int paramDim1Size, int paramDim2Size) :
			_paramType(paramType), _paramDim1Size(paramDim1Size), _paramDim2Size(paramDim2Size)
		{

		}

		~ParamInfo()
		{
			//remove not treated packets (this can occured after an exception)
			while (!_packetList.empty())
			{
				LocalParamDataPacket* packet = _packetList.pop();
				if (packet != NULL)
					delete packet;
			}
		}

		LocalParamType getType()
		{
			return _paramType;
		}

		int getDim1Size()
		{
			return _paramDim1Size;
		}

		int getDim2Size()
		{
			return _paramDim2Size;
		}

		CThreadDeque<LocalParamDataPacket*>& getPacketList()
		{
			return _packetList;
		}

	private:
		/*
		 * @brief Param type
		 */
		LocalParamType _paramType;

		/*
		 * @brief Param dim1 size
		 */
		int _paramDim1Size;

		/*
		 * @brief Param dim2 size
		 */
		int _paramDim2Size;

		/*
		 * @brief List of Packet for this param
		 */
		CThreadDeque<LocalParamDataPacket*> _packetList;
	};

	/**
	 * @brief Constructor.
	 */
	ParamFlow(VirtualInstrumentInterval&);

	/*
	 * @brief Destructor.
	 */
	virtual ~ParamFlow();

	/*
	 * @brief Add associated param to this param flow
	 */
	void addAssociatedParam(std::string paramId, LocalParamType paramType, int dim1Size, int dim2Size)
	{
		//try to find param
		auto lIt = _paramInfoMap.find(paramId);
		if (lIt == _paramInfoMap.end())
			_paramInfoMap[paramId].reset(new ParamInfo(paramType,dim1Size,dim2Size));
	}

	/**
	 * @brief This method waits for the arrival of a new pack.
	 * @return Packet or null.
	 */
	LocalParamDataPacket* get(std::string paramId);

	/**
	 * @return the Packet already in the dequeue or null.
	 */
	LocalParamDataPacket* tryGet(std::string pParamId)
	{
		//try to find param
		auto lIt = _paramInfoMap.find(pParamId);
		if (lIt == _paramInfoMap.end())
			return NULL;
		if (_paramInfoMap[pParamId] == nullptr)
			return NULL;
		return _paramInfoMap[pParamId]->getPacketList().tryToPop(nullptr);
	}

	/**
	 * @brief Push Packet to the internal dequeue.
	 */
	void push(std::string pParamId, LocalParamDataPacket* pPacket)
	{
		//try to find param
		auto lIt = _paramInfoMap.find(pParamId);
		if (lIt == _paramInfoMap.end())
			return;
		if (_paramInfoMap[pParamId] == nullptr)
			return;
		return _paramInfoMap[pParamId]->getPacketList().push(pPacket);
	}

	/**
	 * @brief Identify at any time if a TimeInterval is not processed.
	 * @note This value must be retrieved from VirtualInstrumentInterval instance.
	 */
	bool isTimeIntToProcessChanged (std::string pParamId);

	/**
	 * @brief Identify at any time if the lat insterval is reached.
	 * @note This value must be retrieved from VirtualInstrumentInterval instance.
	 */
	bool isNoMoreTimeInt(std::string pParamId);

	/*
	 * @brief Get list of param info associated to this param flow
	 */
	std::map<std::string,std::shared_ptr<ParamInfo> >& getParamInfoMap()
	{
		return _paramInfoMap;
	}

	private:

	/*
	 * @brief Reference to the VirtualInstrumentInterval
	 */
	VirtualInstrumentInterval&      _vi;

	/*
	 * @brief Map of ParamInfo associaited to this ParamFlow. The key is the param Id
	 */
	std::map<std::string,std::shared_ptr<ParamInfo>> _paramInfoMap;
};

typedef boost::shared_ptr<ParamFlow>  ParamFlowSPtr;

} /* namespace LocalFileInterface */

} /* namespace AMDA */
#endif /* PARAMFLOW_HH_ */