Scheduler.hh 15.2 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 461 462 463 464 465 466 467 468 469
/*
 * Scheduler.hh
 *
 *  Created on: Dec 3, 2012
 *      Author: f.casimir
 */

#ifndef Scheduler_HH_
#define Scheduler_HH_

#include "vector"

#include <boost/algorithm/string.hpp>

#include "AMDA_exception.hh"
#include "DicError.hh"

#include "Parameter.hh"
#include "ParamData.hh"
#include "Operation.hh"

using namespace std;
using namespace boost;
using namespace AMDA::Parameters;

namespace AMDA {
	namespace Merge {

	/**
	 * name space of base class, implementation are template class
	 */
		namespace Base {

		/**
		 * @brief abstract class can write in paramDatas output
		 * @details is the strategy interface in the Strategy pattern
		 * the implementation arc made by template Pusher class
		 */
		class Pusher {
			public:
				virtual ~Pusher() {}
				virtual void  write(unsigned int /*index*/) {
					 BOOST_THROW_EXCEPTION(AMDA::AMDA_exception() << AMDA::errno_code(AMDA_ERROR_UNKNOWN) << AMDA::ex_msg("Pusher operation not supported"));
				}
			};

		/**
		 * @brief the operation of MergeProcess
		 * @detail write one line by paramData input
		 * The implementation are made by template class.
		 */
		 class Scheduler : public Operation {
			public:
			     /**
			     * @brief Constructor
			    */
				Scheduler(Process& pProcess) : Operation(pProcess) {}
				/**
				 * @brief Destructor
				 */
				virtual ~Scheduler() { for(auto operation : _operationList) { delete operation; } }
				/**
				 * @brief add an input paramData
				 */
				virtual void push_back(ParamData*) = 0;

			protected:
				/**
				 * @brief store a Base::Pusher by ParamDataInput
				 */
				  std::vector<Base::Pusher*> _operationList;
			};

		} // Base;


		/**
		 * Definition of Pucher class template
		 */
		template<typename ParamOutput, typename ParamInput>
		class Pusher : public Base::Pusher {
		public:

		     /**
		     * @brief Constructor
		    */
			Pusher(ParamOutput& pParamOutput, ParamInput&  pParamInput) : _paramOutput(pParamOutput), _paramInput(pParamInput) {}

		private:
			/**
			 * @brief paramData Output
			 */
			ParamOutput& _paramOutput;

			/**
			 * @brief paramData Input
			 */
			ParamInput&  _paramInput;
		};

		/**
		 * @brief vector implementation of Pusher template
		 * @detail ParamOutput and ParamInput are ParamDataSpec<std::vector<ElType> with ElType are standard C++ type as float
		 */
		template<typename ElType1, typename ElType2>
		class Pusher<ParamDataSpec<std::vector<ElType1> >,ParamDataSpec<std::vector<ElType2> > > : public Base::Pusher {
		public:
			typedef	ParamDataSpec<std::vector<ElType1> > ParamOutput;
			typedef	ParamDataSpec<std::vector<ElType2> > ParamInput;

			/**
			 * @brief Constructor
			 */
			Pusher(ParamOutput& pParamOutput, ParamInput&  pParamInput) : _paramOutput(pParamOutput), _paramInput(pParamInput) {}

			/**
			 * @overload operation::write(unsigned int index)
			 */
			void  write(unsigned int index) {
				  _paramOutput.pushTime(_paramInput.getTime(index));
				  const std::vector<ElType2>& lData = _paramInput.getDataList()[index];
				  _paramOutput.getDataList().push_back(std::vector<ElType1>(lData.begin(),lData.end()));
			}

		private:
			ParamOutput& _paramOutput;
			ParamInput&  _paramInput;
		};
		/**
		 * @brief vector implementation of Pusher template
		 * @detail ParamOutput and ParamInput are ParamDataSpec<std::vector<LogicalData>
		 */
		template<>
		class Pusher<ParamDataSpec<std::vector<LogicalData> >,ParamDataSpec<std::vector<LogicalData> > > : public Base::Pusher {
		public:
			typedef	ParamDataSpec<std::vector<LogicalData> > ParamOutput;
			typedef	ParamDataSpec<std::vector<LogicalData> > ParamInput;

			/**
			 * @brief Constructor
			 */
			Pusher(ParamOutput& pParamOutput, ParamInput&  pParamInput) : _paramOutput(pParamOutput), _paramInput(pParamInput) {}

			/**
			 * @overload operation::write(unsigned int index)
			 */
			void  write(unsigned int index) {
				  _paramOutput.pushTime(_paramInput.getTime(index));
				  _paramOutput.getDataList().push_back(_paramInput.getDataList()[index]);
			  }
			

		private:
			ParamOutput& _paramOutput;
			ParamInput&  _paramInput;
		};

		/**
		 * @brief vector implementation of Pusher template
		 * @detail ParamOutput are ParamDataSpec<std::vector<LogicalData>
		 * and ParamInput are ParamDataSpec<std::vector<ElType> with ElType are standard C++ type as float
		 * throw an exception if use @see Base::Pusher::write()
		 */
		template<typename ElType2>
		class Pusher<ParamDataSpec<std::vector<LogicalData> >,ParamDataSpec<std::vector<ElType2> > > : public Base::Pusher {
		public:
			typedef	ParamDataSpec<std::vector<LogicalData> > ParamOutput;
			typedef	ParamDataSpec<std::vector<ElType2> > ParamInput;

			/**
			 * @brief Constructor
			 */
			Pusher(ParamOutput& pParamOutput, ParamInput&  pParamInput) : _paramOutput(pParamOutput), _paramInput(pParamInput) {}

		private:
			ParamOutput& _paramOutput;
			ParamInput&  _paramInput;
		};

		/**
		 * @brief vector implementation of Pusher template
		 * @detail ParamOutput are ParamDataSpec<std::vector<ElType> with ElType are standard C++ type as float
		 * and ParamInput are  ParamDataSpec<std::vector<LogicalData>
		 * throw an exception if use @see Base::Pusher::write()
		 */
		template<typename ElType1>
		class Pusher<ParamDataSpec<std::vector<ElType1> >,ParamDataSpec<std::vector<LogicalData> > > : public Base::Pusher {
		public:
			typedef	ParamDataSpec<std::vector<ElType1> > ParamOutput;
			typedef	ParamDataSpec<std::vector<LogicalData> > ParamInput;

			Pusher(ParamOutput& pParamOutput, ParamInput&  pParamInput) : _paramOutput(pParamOutput), _paramInput(pParamInput) {}

		private:
			ParamOutput& _paramOutput;
			ParamInput&  _paramInput;
		};


		/**
		 * @brief vector implementation of Pusher template
		 * @detail ParamOutput and ParamInput are ParamDataSpec<std::vector<ElType> with ElType are standard C++ type as float
		 */
		template<typename ElType>
		class Pusher<ParamDataSpec<std::vector<ElType> >,ParamDataSpec<std::vector<ElType> > > : public Base::Pusher {
		public:
			typedef	ParamDataSpec<std::vector<ElType> > ParamOutput;
			typedef	ParamDataSpec<std::vector<ElType> > ParamInput;

			/**
			 * @brief Constructor
			 */
			Pusher(ParamOutput& pParamOutput, ParamInput&  pParamInput) : _paramOutput(pParamOutput), _paramInput(pParamInput) {}

			/**
			 * @overload operation::write(unsigned int index)
			 */
			void  write(unsigned int index) {
				_paramOutput.pushTime(_paramInput.getTime(index));
				_paramOutput.getDataList().push_back(_paramInput.getDataList()[index]);
			}

		private:
			ParamOutput& _paramOutput;
			ParamInput&  _paramInput;
		};

		/**
		 * @brief vector implementation of Pusher template
		 * @detail ParamOutput and ParamInput are ParamDataSpec<ElType> with ElType are standard C++ type as float
		 */
		template<typename ElType>
		class Pusher<ParamDataSpec<ElType>,ParamDataSpec<ElType> > : public Base::Pusher {
		public:
			typedef	ParamDataSpec<ElType> ParamOutput;
			typedef	ParamDataSpec<ElType> ParamInput;

			Pusher(ParamOutput& pParamOutput, ParamInput&  pParamInput) : _paramOutput(pParamOutput), _paramInput(pParamInput) {}

			/**
			 * @overload operation::write(unsigned int index)
			 */
			void  write(unsigned int index) {
				_paramOutput.pushTime(_paramInput.getTime(index));
				_paramOutput.getDataList().push_back(_paramInput.getDataList()[index]);
			  }			

		private:
			ParamOutput& _paramOutput;
			ParamInput&  _paramInput;
		};

		/**
		 * @brief vector implementation of Pusher template
		 * @detail  ParamOutput is is ParamDataSpec<LogicalData>
		 * and ParamInput is any
		 * throw an exception if use @see Base::Pusher::write()
		 */
		template<typename TParamInput>
		class Pusher<ParamDataSpec<LogicalData>,TParamInput> : public Base::Pusher {
		public:
			typedef	ParamDataSpec<LogicalData> ParamOutput;
			typedef	TParamInput ParamInput;

			Pusher(ParamOutput& pParamOutput, ParamInput&  pParamInput) : _paramOutput(pParamOutput), _paramInput(pParamInput) {}

		private:
			ParamOutput& _paramOutput;
			ParamInput&  _paramInput;
		};

		/**
		 * @brief vector implementation of Pusher template
		 * @detail ParamOutput is any
		 * and ParamInput are ParamDataSpec<LogicalData>
		 * throw an exception if use @see Base::Pusher::write()
		 */
		template<typename TParamOutput>
		class Pusher<TParamOutput,ParamDataSpec<LogicalData> > : public Base::Pusher {
		public:
			typedef	TParamOutput ParamOutput;
			typedef	ParamDataSpec<LogicalData> ParamInput;

			Pusher(ParamOutput& pParamOutput, ParamInput&  pParamInput) : _paramOutput(pParamOutput), _paramInput(pParamInput) {}

		private:
			ParamOutput& _paramOutput;
			ParamInput&  _paramInput;
		};

		/**
		 * @brief vector implementation of Pusher template
		 * @detail ParamOutput and ParamInput are ParamDataSpec<LogicalData>
		 * throw an exception if use @see Base::Pusher::write()
		 */
		template<>
		class Pusher<ParamDataSpec<LogicalData>,ParamDataSpec<LogicalData> > : public Base::Pusher {
		public:
			typedef	ParamDataSpec<LogicalData> ParamOutput;
			typedef	ParamDataSpec<LogicalData> ParamInput;

			Pusher(ParamOutput& pParamOutput, ParamInput&  pParamInput) : _paramOutput(pParamOutput), _paramInput(pParamInput) {}

			/**
			 * @overload operation::write(unsigned int index)
			 */
			void  write(unsigned int index) {
				_paramOutput.pushTime(_paramInput.getTime(index));
				_paramOutput.getDataList().push_back(_paramInput.getDataList()[index]);
			  }
			

		private:
			ParamOutput& _paramOutput;
			ParamInput&  _paramInput;
		};

		/**
		 * @brief Create the specific Pusher class
		 * @details used the visitor pattern for find the good Pusher in function of ParamInput type
		 */
		template<typename TParamOutput>
		class PusherCreator : public VisitorOfParamData {
		public:
			/**
			 * @brief Constructor.
			 */
			PusherCreator(TParamOutput& pOutputParamData,ParamData& pInputParamData)
				: _outputParamData(pOutputParamData), _operation(nullptr) {

				pInputParamData.accept(*this);
			}

			/**
			 * @overload VisitorOfParamData::visit(ParamDataScalaireShort *)
			 */
			void visit(ParamDataScalaireShort *pInputParamData) { _operation = new Pusher<TParamOutput,ParamDataScalaireShort>( _outputParamData,*pInputParamData); }

			/**
			 * @overload VisitorOfParamData::visit(ParamDataScalaireFloat *)
			 */
			void visit(ParamDataScalaireFloat *pInputParamData) { _operation = new Pusher<TParamOutput,ParamDataScalaireFloat>( _outputParamData,*pInputParamData); }

			/**
			 * @overload VisitorOfParamData::visit(ParamDataScalaireDouble *)
			 */
			void visit(ParamDataScalaireDouble *pInputParamData) { _operation = new Pusher<TParamOutput,ParamDataScalaireDouble>( _outputParamData,*pInputParamData); }

			/**
			 * @overload VisitorOfParamData::visit(ParamDataScalaireLongDouble *)
			 */
			void visit(ParamDataScalaireLongDouble *pInputParamData) { _operation = new Pusher<TParamOutput,ParamDataScalaireLongDouble>( _outputParamData,*pInputParamData); }

			/**
			 * @overload VisitorOfParamData::visit(ParamDataScalaireInt *)
			 */
			void visit(ParamDataScalaireInt *pInputParamData) { _operation = new Pusher<TParamOutput,ParamDataScalaireInt>( _outputParamData,*pInputParamData); }

			/**
			 * @overload VisitorOfParamData::visit(ParamDataLogicalData *)
			 */
			void visit(ParamDataLogicalData *pInputParamData) { _operation = new Pusher<TParamOutput,ParamDataLogicalData>( _outputParamData,*pInputParamData); }

			/**
			 * @overload VisitorOfParamData::visit(ParamDataTab1DShort *)
			 */
			void visit(ParamDataTab1DShort *pInputParamData) { _operation = new Pusher<TParamOutput,ParamDataTab1DShort>( _outputParamData,*pInputParamData); }

			/**
			 * @overload VisitorOfParamData::visit(ParamDataTab1DFloat *)
			 */
			void visit(ParamDataTab1DFloat *pInputParamData) { _operation = new Pusher<TParamOutput,ParamDataTab1DFloat>( _outputParamData,*pInputParamData); }

			/**
			 * @overload VisitorOfParamData::visit(ParamDataTab1DDouble *)
			 */
			void visit(ParamDataTab1DDouble *pInputParamData) { _operation = new Pusher<TParamOutput,ParamDataTab1DDouble>( _outputParamData,*pInputParamData); }

			/**
			 * @overload VisitorOfParamData::visit(ParamDataTab1DLongDouble *)
			 */
			void visit(ParamDataTab1DLongDouble *pInputParamData) { _operation = new Pusher<TParamOutput,ParamDataTab1DLongDouble>( _outputParamData,*pInputParamData); }

			/**
			 * @overload VisitorOfParamData::visit(ParamDataTab1DInt *)
			 */
			void visit(ParamDataTab1DInt *pInputParamData) { _operation = new Pusher<TParamOutput,ParamDataTab1DInt>( _outputParamData,*pInputParamData); }

			/**
			 * @overload VisitorOfParamData::visit(ParamDataTab1DLogicalData *)
			 */
			void visit(ParamDataTab1DLogicalData *pInputParamData) { _operation = new Pusher<TParamOutput,ParamDataTab1DLogicalData>( _outputParamData,*pInputParamData); }

			/**
			 * @overload VisitorOfParamData::visit(ParamDataTab2DShort *)
			 */
			void visit(ParamDataTab2DShort *pInputParamData) { _operation = new Pusher<TParamOutput,ParamDataTab2DShort>( _outputParamData,*pInputParamData); }

			/**
			 * @overload VisitorOfParamData::visit(ParamDataTab2DFloat *)
			 */
			void visit(ParamDataTab2DFloat *pInputParamData) { _operation = new Pusher<TParamOutput,ParamDataTab2DFloat>( _outputParamData,*pInputParamData); }

			/**
			 * @overload VisitorOfParamData::visit(ParamDataTab2DDouble *)
			 */
			void visit(ParamDataTab2DDouble *pInputParamData) { _operation = new Pusher<TParamOutput,ParamDataTab2DDouble>( _outputParamData,*pInputParamData); }

			/**
			 * @overload VisitorOfParamData::visit(ParamDataTab2DLongDouble *)
			 */
			void visit(ParamDataTab2DLongDouble *pInputParamData) { _operation = new Pusher<TParamOutput,ParamDataTab2DLongDouble>( _outputParamData,*pInputParamData); }

			/**
			 * @overload VisitorOfParamData::visit(ParamDataTab2DInt *)
			 */
			void visit(ParamDataTab2DInt *pInputParamData) { _operation = new Pusher<TParamOutput,ParamDataTab2DInt>( _outputParamData,*pInputParamData); }

			/**
			 * @overload VisitorOfParamData::visit(ParamDataTab2DLogicalData *)
			 */
			void visit(ParamDataTab2DLogicalData *pInputParamData) { _operation = new Pusher<TParamOutput,ParamDataTab2DLogicalData>( _outputParamData,*pInputParamData); }

			Base::Pusher *getPusher() { return _operation; }

		private:
			TParamOutput &_outputParamData;
			Base::Pusher *_operation;
		};


		/**
		 * @brief Base::Scheduler implementation
		 */
	template<typename TParamOutput>
	class Scheduler : public Base::Scheduler {
		public:
			Scheduler(Process& pProcess) : Base::Scheduler(pProcess), _paramOutput(new TParamOutput()) {
				_paramDataOutput=_paramOutput;
			}
			/**
			* @overload  Base::Scheduler::write(ParamDataIndexInfo &pParamDataIndexInfo)
			 */
			void push_back(ParamData* pInputParam) {
				PusherCreator<TParamOutput> lPusherCreator(*_paramOutput,*pInputParam);
				_operationList.push_back(lPusherCreator.getPusher());
			}

		  /**
			 * @overload Operation::write(ParamDataIndexInfo &pParamDataIndexInfo)
			 */
		  void  write(ParamDataIndexInfo &pParamDataIndexInfo) {
		   unsigned int index = pParamDataIndexInfo._startIndex;
			  for (; index< pParamDataIndexInfo._startIndex + pParamDataIndexInfo._nbDataToProcess; index++) {
			     for(auto operation : _operationList) { operation->write(index); }
			  }
		  }

		protected:
			/**
			 * @brief It is the channel of the data shifted.
			 */
		  TParamOutput *_paramOutput;
		};


} /* namespace Merge */
} /* namespace AMDA */
#endif /* Scheduler_HH_ */