From 323033c9eb730eb3058818f523fc4a3e835b7c0a Mon Sep 17 00:00:00 2001 From: NicolasWinckler Date: Wed, 14 Oct 2015 17:12:19 +0200 Subject: [PATCH] Add CRTP base class for the policies of the generic device --- fairmq/devices/BaseDeserializationPolicy.h | 61 +++++++++++++++ fairmq/devices/BaseProcessorTaskPolicy.h | 69 ++++++++++++++++ fairmq/devices/BaseSerializationPolicy.h | 68 ++++++++++++++++ fairmq/devices/BaseSinkPolicy.h | 41 ++++++++++ fairmq/devices/BaseSourcePolicy.h | 91 ++++++++++++++++++++++ fairmq/devices/GenericFileSink.h | 4 +- fairmq/devices/GenericMerger.h | 2 +- fairmq/devices/GenericProcessor.h | 6 +- fairmq/devices/GenericSampler.h | 3 +- fairmq/devices/GenericSampler.tpl | 2 +- fairmq/devices/README.md | 5 +- fairmq/options/FairProgOptions.h | 2 +- 12 files changed, 341 insertions(+), 13 deletions(-) create mode 100644 fairmq/devices/BaseDeserializationPolicy.h create mode 100644 fairmq/devices/BaseProcessorTaskPolicy.h create mode 100644 fairmq/devices/BaseSerializationPolicy.h create mode 100644 fairmq/devices/BaseSinkPolicy.h create mode 100644 fairmq/devices/BaseSourcePolicy.h diff --git a/fairmq/devices/BaseDeserializationPolicy.h b/fairmq/devices/BaseDeserializationPolicy.h new file mode 100644 index 00000000..e84ff34e --- /dev/null +++ b/fairmq/devices/BaseDeserializationPolicy.h @@ -0,0 +1,61 @@ +/* + * File: BaseDeserializationPolicy.h + * Author: winckler + * + * Created on October 14, 2015, 1:01 PM + */ + +#ifndef BASEDESERIALIZATIONPOLICY_H +#define BASEDESERIALIZATIONPOLICY_H + + +#include "FairMQMessage.h" + +// c++11 code +#include + + + +// CRTP base class +template +class BaseDeserializationPolicy +{ +public: + BaseDeserializationPolicy() + {} + + virtual ~BaseDeserializationPolicy() + {} + + template + auto DeserializeMsg(FairMQMessage* msg)-> decltype(static_cast(this)->DeserializeMsg(msg) ) + { + static_assert(std::is_same{}, "BaseDeserializationPolicy::DeserializeMsg hack broken"); + return static_cast(this)->DeserializeMsg(msg); + } + +}; + + +/* +// c++14 code +// CRTP base class +template +class BaseDeserializationPolicy +{ +public: + BaseDeserializationPolicy() + {} + + virtual ~BaseDeserializationPolicy() + {} + + auto DeSerializeMsg(FairMQMessage* msg) + { + return static_cast(this)->DeSerializeMsg(msg); + } + +};*/ + +#endif /* BASEDESERIALIZATIONPOLICY_H */ + diff --git a/fairmq/devices/BaseProcessorTaskPolicy.h b/fairmq/devices/BaseProcessorTaskPolicy.h new file mode 100644 index 00000000..ce4577c6 --- /dev/null +++ b/fairmq/devices/BaseProcessorTaskPolicy.h @@ -0,0 +1,69 @@ +/* + * File: BaseProcessorTaskPolicy.h + * Author: winckler + * + * Created on October 14, 2015, 1:01 PM + */ + +#ifndef BASEPROCESSORTASKPOLICY_H +#define BASEPROCESSORTASKPOLICY_H + + +#include +// CRTP base class +template +class BaseProcessorTaskPolicy +{ +public: + BaseProcessorTaskPolicy() + {} + + virtual ~BaseProcessorTaskPolicy() + {} + + template + auto GetOutputData() -> decltype(static_cast(this)->GetOutputData() ) + { + static_assert(std::is_same{}, "BaseProcessorTaskPolicy::GetOutputData hack broken"); + return static_cast(this)->GetOutputData(); + } + + template + auto ExecuteTask(CONTAINER_TYPE container) -> decltype( static_cast(this)->ExecuteTask(container) ) + { + static_assert(std::is_same{}, "BaseProcessorTaskPolicy::ExecuteTask hack broken"); + return static_cast(this)->ExecuteTask(container); + } + + +}; + + /* + +// c++14 code only +// CRTP base class +template +class BaseProcessorTaskPolicy +{ +public: + BaseProcessorTaskPolicy() + {} + + virtual ~BaseProcessorTaskPolicy() + {} + + auto GetOutputData() + { + return static_cast(this)->GetOutputData(); + } + + template + auto ExecuteTask(CONTAINER_TYPE container) + { + return static_cast(this)->ExecuteTask(container); + } + +}; +*/ +#endif /* BASEPROCESSORTASKPOLICY_H */ + diff --git a/fairmq/devices/BaseSerializationPolicy.h b/fairmq/devices/BaseSerializationPolicy.h new file mode 100644 index 00000000..88851498 --- /dev/null +++ b/fairmq/devices/BaseSerializationPolicy.h @@ -0,0 +1,68 @@ +/* + * File: BaseSerializationPolicy.h + * Author: winckler + * + * Created on October 14, 2015, 1:01 PM + */ + +#ifndef BASESERIALIZATIONPOLICY_H +#define BASESERIALIZATIONPOLICY_H + +#include "FairMQMessage.h" + +#include +// CRTP base class +template +class BaseSerializationPolicy +{ +public: + BaseSerializationPolicy() + {} + + virtual ~BaseSerializationPolicy() + {} + + template + auto SerializeMsg(CONTAINER_TYPE container) -> decltype(static_cast(this)->SerializeMsg(container) ) + { + static_assert(std::is_same{}, "BaseSerializationPolicy::SerializeMsg hack broken"); + return static_cast(this)->SerializeMsg(container); + } + + template + auto SetMessage(FairMQMessage* msg)-> decltype(static_cast(this)->SetMessage(msg) ) + { + static_assert(std::is_same{}, "BaseSerializationPolicy::SetMessage hack broken"); + return static_cast(this)->SetMessage(msg); + } + +}; + + /* +// CRTP base class +// c++14 code +template +class BaseSerializationPolicy +{ +public: + BaseSerializationPolicy() + {} + + virtual ~BaseSerializationPolicy() + {} + + template + auto SerializeMsg(CONTAINER_TYPE container) + { + return static_cast(this)->SerializeMsg(container); + } + + auto SetMessage(FairMQMessage* msg) + { + return static_cast(this)->SetMessage(msg); + } + +}; +*/ +#endif /* BASESERIALIZATIONPOLICY_H */ + diff --git a/fairmq/devices/BaseSinkPolicy.h b/fairmq/devices/BaseSinkPolicy.h new file mode 100644 index 00000000..a14b5618 --- /dev/null +++ b/fairmq/devices/BaseSinkPolicy.h @@ -0,0 +1,41 @@ +/* + * File: BaseSinkPolicy.h + * Author: winckler + * + * Created on October 14, 2015, 1:01 PM + */ + +#ifndef BASESINKPOLICY_H +#define BASESINKPOLICY_H + + +#include +// CRTP base class +template +class BaseSinkPolicy +{ +public: + BaseSinkPolicy() + {} + + virtual ~BaseSinkPolicy() + {} + + template + auto AddToFile(CONTAINER_TYPE container) -> decltype(static_cast(this)->AddToFile(container) ) + { + static_assert(std::is_same{}, "BaseSinkPolicy::AddToFile hack broken"); + return static_cast(this)->AddToFile(container); + } + + template + auto InitOutputFile() -> decltype(static_cast(this)->InitOutputFile() ) + { + static_assert(std::is_same{}, "BaseSinkPolicy::InitOutputFile hack broken"); + return static_cast(this)->InitOutputFile(); + } + +}; + +#endif /* BASESINKPOLICY_H */ + diff --git a/fairmq/devices/BaseSourcePolicy.h b/fairmq/devices/BaseSourcePolicy.h new file mode 100644 index 00000000..43223cd0 --- /dev/null +++ b/fairmq/devices/BaseSourcePolicy.h @@ -0,0 +1,91 @@ +/* + * File: BaseSourcePolicy.h + * Author: winckler + * + * Created on October 14, 2015, 1:01 PM + */ + +#ifndef BASESOURCEPOLICY_H +#define BASESOURCEPOLICY_H + +#include +// c++11 code +// CRTP base class +template +class BaseSourcePolicy +{ +public: + BaseSourcePolicy() + {} + + virtual ~BaseSourcePolicy() + {} + + template + auto InitSource()-> decltype(static_cast(this)->InitSource() ) + { + static_assert(std::is_same{}, "BaseSourcePolicy::InitSource hack broken"); + return static_cast(this)->InitSource(); + } + + template + int64_t GetNumberOfEvent()//-> decltype(static_cast(this)->GetNumberOfEvent() ) + { + static_assert(std::is_same{}, "BaseSourcePolicy::GetNumberOfEvent hack broken"); + return static_cast(this)->GetNumberOfEvent(); + } + + template + auto SetIndex(int64_t eventIdx)-> decltype(static_cast(this)->SetIndex(eventIdx) ) + { + static_assert(std::is_same{}, "BaseSourcePolicy::SetIndex hack broken"); + return static_cast(this)->SetIndex(eventIdx); + } + + template + //auto GetOutData()-> decltype(static_cast(this)->GetOutData() ) + decltype(std::declval()->GetOutData() ) GetOutData() + { + static_assert(std::is_same{}, "BaseSourcePolicy::GetOutData hack broken"); + return static_cast(this)->GetOutData(); + } + +}; + +/* +// c++14 code +// CRTP base class +template +class BaseSourcePolicy +{ +public: + BaseSourcePolicy() + {} + + virtual ~BaseSourcePolicy() + {} + + auto InitSource() + { + return static_cast(this)->InitSource(); + } + + int64_t GetNumberOfEvent() + { + return static_cast(this)->GetNumberOfEvent(); + } + + auto SetIndex(int64_t eventIdx) + { + return static_cast(this)->SetIndex(int64_t eventIdx); + } + + auto GetOutData() + { + return static_cast(this)->GetOutData(); + } + +}; +*/ +#endif /* BASESOURCEPOLICY_H */ + diff --git a/fairmq/devices/GenericFileSink.h b/fairmq/devices/GenericFileSink.h index c2aa1ad3..ea56dc38 100644 --- a/fairmq/devices/GenericFileSink.h +++ b/fairmq/devices/GenericFileSink.h @@ -28,7 +28,7 @@ * * -------- INPUT POLICY -------- * deserialization_type::InitContainer(...) - * CONTAINER_TYPE deserialization_type::DeSerializeMsg(FairMQMessage* msg) + * CONTAINER_TYPE deserialization_type::DeserializeMsg(FairMQMessage* msg) * * * -------- OUTPUT POLICY -------- @@ -82,7 +82,7 @@ class GenericFileSink : public FairMQDevice, public T, public U if (inputChannel.Receive(msg) > 0) { - sink_type::AddToFile(deserialization_type::DeSerializeMsg(msg.get())); + sink_type::AddToFile(deserialization_type::DeserializeMsg(msg.get())); receivedMsg++; } } diff --git a/fairmq/devices/GenericMerger.h b/fairmq/devices/GenericMerger.h index bd2fc37e..c5e0dcb9 100644 --- a/fairmq/devices/GenericMerger.h +++ b/fairmq/devices/GenericMerger.h @@ -55,7 +55,7 @@ class GenericMerger : public FairMQDevice, public MergerPolicy, public InputPoli received = fChannels.at("data-in").at(i).Receive(msg) if (received > 0) { - MergerPolicy::Merge(InputPolicy::DeSerializeMsg(msg)); + MergerPolicy::Merge(InputPolicy::DeserializeMsg(msg)); } } diff --git a/fairmq/devices/GenericProcessor.h b/fairmq/devices/GenericProcessor.h index ccada86b..11e79c87 100644 --- a/fairmq/devices/GenericProcessor.h +++ b/fairmq/devices/GenericProcessor.h @@ -24,7 +24,7 @@ * * -------- INPUT POLICY -------- * deserialization_type::InitContainer(...) - * CONTAINER_TYPE deserialization_type::DeSerializeMsg(FairMQMessage* msg) + * CONTAINER_TYPE deserialization_type::DeserializeMsg(FairMQMessage* msg) * deserialization_type::InitContainer(...) // if GenericProcessor::InitInputContainer(...) is used * * @@ -134,9 +134,9 @@ class GenericProcessor : public FairMQDevice, public T, public U, public V if (inputChannel.Receive(msg) > 0) { - // deserialization_type::DeSerializeMsg(msg) --> deserialize data of msg and fill output container + // deserialization_type::DeserializeMsg(msg) --> deserialize data of msg and fill output container // proc_task_type::ExecuteTask( ... ) --> process output container - proc_task_type::ExecuteTask(deserialization_type::DeSerializeMsg(msg.get())); + proc_task_type::ExecuteTask(deserialization_type::DeserializeMsg(msg.get())); // serialization_type::fMessage point to msg serialization_type::SetMessage(msg.get()); diff --git a/fairmq/devices/GenericSampler.h b/fairmq/devices/GenericSampler.h index 102ad6b1..9e6d5d63 100644 --- a/fairmq/devices/GenericSampler.h +++ b/fairmq/devices/GenericSampler.h @@ -35,12 +35,11 @@ * Function to define in (parent) policy classes : * * -------- INPUT POLICY (SAMPLER POLICY) -------- - * source_type::InitSampler() // must be there to compile + * source_type::InitSource() // must be there to compile * int64_t source_type::GetNumberOfEvent() // must be there to compile * source_type::SetIndex(int64_t eventIdx) // must be there to compile * CONTAINER_TYPE source_type::GetOutData() // must be there to compile * source_type::SetFileProperties(Args&... args) // must be there to compile - * source_type::ExecuteTasks() // must be there to compile * * void BindSendPart(std::function callback) // enabled if exists * void BindGetSocketNumber(std::function callback) // enabled if exists diff --git a/fairmq/devices/GenericSampler.tpl b/fairmq/devices/GenericSampler.tpl index 4ebfbbee..d324832b 100644 --- a/fairmq/devices/GenericSampler.tpl +++ b/fairmq/devices/GenericSampler.tpl @@ -34,7 +34,7 @@ void base_GenericSampler::InitTask() BindingGetSocketNumber(); BindingGetCurrentIndex(); - source_type::InitSampler(); + source_type::InitSource(); fNumEvents = source_type::GetNumberOfEvent(); } diff --git a/fairmq/devices/README.md b/fairmq/devices/README.md index 1a561214..66ba9983 100644 --- a/fairmq/devices/README.md +++ b/fairmq/devices/README.md @@ -56,14 +56,13 @@ The policies must have at least a couple of methods that will be called by the h ##### Input policy (Source) ``` C++ - source_type::InitSampler(); // must be there to compile + source_type::InitSource(); // must be there to compile int64_t source_type::GetNumberOfEvent(); // must be there to compile source_type::SetIndex(int64_t eventIdx); // must be there to compile CONTAINER_TYPE source_type::GetOutData(); // must be there to compile source_type::SetFileProperties(Args&... args); // if called by the host, then must be there to compile - source_type::ExecuteTasks(); // must be there to compile void source_type::BindSendPart(std::function callback); // enabled if exists void source_type::BindGetSocketNumber(std::function callback); // enabled if exists @@ -122,6 +121,6 @@ void SetFileProperties(Args&... args) ``` ### Generic Processor - +The function members required by the processor policies are : diff --git a/fairmq/options/FairProgOptions.h b/fairmq/options/FairProgOptions.h index bdff7cd3..3be63721 100644 --- a/fairmq/options/FairProgOptions.h +++ b/fairmq/options/FairProgOptions.h @@ -116,7 +116,7 @@ class FairProgOptions int ParseEnvironment(const std::function&); - virtual int ParseAll(const int argc, char** argv, bool allowUnregistered = false) = 0; + virtual int ParseAll(const int argc, char** argv, bool allowUnregistered = false) = 0;// TODO change return type to bool and propagate to executable virtual int PrintOptions(); int PrintHelp() const;