LocalParamData.hh 8.19 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 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 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460
/*
 * LocalParamData.hh
 *
 *  Created on: Nov 24, 2014
 *      Author: AKKA
 */


#ifndef LOCALPARAMDATA_H_
#define LOCALPARAMDATA_H_

#include "LocalFileInterfaceConfig.hh"

#include <cstring>
#include <cstdint>

namespace AMDA {
namespace LocalFileInterface {

/*
 * @brief Time format
 */
typedef enum {ISO, DOUBLE} LocalTimeFormat;

/*
 * @brief Container type - CONTAINER_MATRIX not tested
 */
enum LocalContainerType
{
	CONTAINER_SCALAR,
	CONTAINER_VECTOR,
	CONTAINER_MATRIX
};

/*
 * @brief Param type
 */
enum LocalParamType
{
	TYPE_UNKNOWN,
	TYPE_FLOAT,
	TYPE_DOUBLE,
	TYPE_EPOCH16,
	TYPE_SHORT,
	TYPE_INT,
	TYPE_TT2000
};

/*
 * @brief Define the maximum dimension size
 */
#define PARAMPACKET_MAX_DIMSIZE   10000

/*
 * @brief Define the maximum bytes available for one packet
 */
#define PARAMPACKET_MAX_DATABYTES 80000

/*
 * @brief Define a packet to push to a ParamData
 */
class LocalParamDataPacket
{
public:
	/*
	 * @brief Constructor
	 */
	LocalParamDataPacket(void) : _isInit(false), _containerType(CONTAINER_SCALAR),
		_paramType(TYPE_UNKNOWN), _nbData(0), _nbMaxData(0), _dims(NULL), _size(0),
		_times(NULL), _datas(NULL)
	{
	}

	/*
	 * @brief Destructor
	 */
	~LocalParamDataPacket(void)
	{
		//free allocated data if needed
		free();
	}

	/*
	 * @brief Init the packet. This function allocate all buffers used by a packet
	 */
	bool init(LocalContainerType containerType,
			LocalParamType paramType, int dim1 = 0, int dim2 = 0)
	{
		if (_isInit)
			//already init
			return false;

		_containerType = containerType;
		_paramType     = paramType;
		_size          = 0;

		//compute dimSize
		switch (_containerType)
		{
		case CONTAINER_SCALAR :
			//for a scalar, no dims defined and the size is 1
			_size = 1;
			_dims = NULL;
			break;
		case CONTAINER_VECTOR :
			//for a vector, 1 "dims" defined and the size is egal to this dimension size
			if (dim1 <= 0)
				return false;
			_size = dim1;
			_dims = new int[1];
			_dims[0] = dim1;
			break;
		case CONTAINER_MATRIX :
			//for a matrix, 2 "dims" defined and the size is egal to the multiplication of the two dimensions sizes
			if ((dim1 <= 0) || (dim2 <= 0))
				return false;
			_size = dim1 * dim2;
			_dims = new int[2];
			_dims[0] = dim1;
			_dims[1] = dim2;
			break;
		default:
			return false;
		}

		//check dimSize
		if ((_size <= 0) || (_size >= PARAMPACKET_MAX_DIMSIZE))
		{
			if (_dims != NULL)
			{
				delete[] _dims;
				_dims = NULL;
			}
			return false;
		}

		//init times and datas
		//use the PARAMPACKET_MAX_DATABYTES to determine the maximum data that's a packet can contain
		_nbData = 0;
		switch (_paramType)
		{
		case TYPE_FLOAT :
			_nbMaxData = PARAMPACKET_MAX_DATABYTES / (_size * sizeof(float));
			if (_nbMaxData < 1)
				_nbMaxData = 1;
			_times = new double[_nbMaxData];
			_datas = new float[_nbMaxData*_size];
			break;
		case TYPE_DOUBLE :
			_nbMaxData = PARAMPACKET_MAX_DATABYTES / (_size * sizeof(double));
			if (_nbMaxData < 1)
				_nbMaxData = 1;
			_times = new double[_nbMaxData];
			_datas = new double[_nbMaxData*_size];
			break;
		case TYPE_SHORT :
			_nbMaxData = PARAMPACKET_MAX_DATABYTES / (_size * sizeof(short));
			if (_nbMaxData < 1)
				_nbMaxData = 1;
			_times = new double[_nbMaxData];
			_datas = new short[_nbMaxData*_size];
			break;
		case TYPE_INT :
			_nbMaxData = PARAMPACKET_MAX_DATABYTES / (_size * sizeof(int));
			if (_nbMaxData < 1)
				_nbMaxData = 1;
			_times = new double[_nbMaxData];
			_datas = new int[_nbMaxData*_size];
			break;
		default :
			if (_dims != NULL)
			{
				delete[] _dims;
				_dims = NULL;
			}
			return false;
		}

		_isInit = true;
		return true;
	}

	/*
	 * @brief Free all allocated buffers. This function is called in the destructor
	 */
	void free()
	{
		if (!_isInit)
			return;

		//free all allocated buffers
		if (_dims != NULL)
		{
			delete[] _dims;
			_dims = NULL;
		}

		if (_times != NULL)
		{
			delete[] _times;
			_times = NULL;
		}

		if (_datas != NULL)
		{
			switch (_paramType)
			{
			case TYPE_FLOAT :
				delete[] (float*)_datas;
				_datas = NULL;
				break;
			case TYPE_DOUBLE :
				delete[] (double*)_datas;
				_datas = NULL;
				break;
			case TYPE_SHORT :
				delete[] (short*)_datas;
				_datas = NULL;
				break;
			case TYPE_INT :
				delete[] (int*)_datas;
				_datas = NULL;
				break;
			default :
				throw;
			}
		}

		_isInit = false;
	}

	/*
	 * @brief Add one record (a pair of one time and a data)
	 * If the result is false and "full" parameter is true => the packet is full
	 */
	bool addData(double time, void* data, bool& full)
	{
		//add a record in the packet
		full = false;

		if (!_isInit)
			return false;

		full = (_nbData >= _nbMaxData);
		if (full)
			//cannot add more data in the packet
			return false;

		//set data
		void *pos = _datas;
		int sizeToCopy;

		switch (_paramType)
		{
		case TYPE_FLOAT :
			sizeToCopy = _size * sizeof(float);
			break;
		case TYPE_DOUBLE :
			sizeToCopy = _size * sizeof(double);
			break;
		case TYPE_SHORT :
			sizeToCopy = _size * sizeof(short);
			break;
		case TYPE_INT :
			sizeToCopy = _size * sizeof(int);
			break;
		default:
			return false;
		}
		pos = (void*)((intptr_t)pos + (_nbData * sizeToCopy));

		memcpy(pos,data,sizeToCopy);

		//set time
		_times[_nbData] = time;

		++_nbData;

		full = (_nbData >= _nbMaxData);
		return true;
	}

	/*
	 * @brief Get number of record contained by the packet
	 */
	int getNbData(void)
	{
		if (!_isInit)
			return 0;
		return _nbData;
	}

	/*
	 * @brief Get one time by record index
	 */
	double getTime(int index)
	{
		if (!_isInit)
			return 0.;
		if (index >= _nbData)
			return 0.;
		return _times[index];
	}

	/*
	 * @brief Get the first dimension size.
	 */
	int getDim1Size(void)
	{
		if (!_isInit)
			return 0;
		switch (_containerType)
		{
		case CONTAINER_VECTOR :
		case CONTAINER_MATRIX :
			if (_dims != NULL)
				return _dims[0];
			break;
		default:
			return 0;
		}
		return 0;
	}

	/*
	 * @brief Get the second dimension size.
	 */
	int getDim2Size(void)
	{
		if (!_isInit)
			return 0;
		switch (_containerType)
		{
		case CONTAINER_MATRIX :
			if (_dims != NULL)
				return _dims[1];
			break;
		default:
			return 0;
		}
		return 0;
	}

	/*
	 * @brief Get the full size of a data of a record
	 */
	int getDimsSize(void)
	{
		return _size;
	}

	/*
	 * @brief Get data type for a record
	 */
	LocalParamType getType()
	{
		return _paramType;
	}

	/*
	 * @brief Get one data value from record with the index "index"
	 * And from the dimensions indexes
	 */
	bool getDataValue(void *val, int index, int dim1Index = 0, int dim2Index = 0)
	{
		if (!_isInit)
			return false;
		void *pos = NULL;

		int valueSize = 0;
		switch (_paramType)
		{
			case TYPE_FLOAT :
				valueSize = sizeof(float);
				break;
			case TYPE_DOUBLE :
				valueSize = sizeof(double);
				break;
			case TYPE_SHORT :
				valueSize = sizeof(short);
				break;
			case TYPE_INT :
				valueSize = sizeof(int);
				break;
			default:
				return false;
		}

		switch (_containerType)
		{
		case CONTAINER_SCALAR :
			pos = (void*)((intptr_t)_datas + (valueSize * _size * index));
			break;
		case CONTAINER_VECTOR :
			pos = (void*)((intptr_t)_datas + (valueSize * _size * index));
			pos = (void*)((intptr_t)pos + (dim1Index * valueSize));
			break;
		case CONTAINER_MATRIX :
			pos = (void*)((intptr_t)_datas + (valueSize * _size * index));
			pos = (void*)((intptr_t)pos + (dim1Index * _dims[1] * valueSize));
			pos = (void*)((intptr_t)pos + (dim2Index * valueSize));
			break;
		default :
			return false;
		}
		memcpy(val,pos,valueSize);
		return true;
	}

private:
	/*
	 * @brief Flag to know if the packet is init
	 */
	bool _isInit;

	/*
	 * @brief Container type for data of the packet
	 */
	LocalContainerType _containerType;

	/*
	 * @brief Param type for data of the packet
	 */
	LocalParamType     _paramType;

	/*
	 * @brief Number of record defined in the packet
	 */
	int                _nbData;

	/*
	 * @brief Maximum number of record that's can be defined in the packet
	 */
	int                _nbMaxData;

	/*
	 * @brief Dimensions definition
	 */
	int*               _dims;

	/*
	 * @brief Full size of a data of a record
	 */
	int                _size;

	/*
	 * @brief times buffer
	 */
	double*            _times;

	/*
	 * @brief record data buffer
	 */
	void*              _datas;
};

} /* LocalFileInterface */
} /* AMDA */

#endif