Add CRTP base class for the policies of the generic device

This commit is contained in:
NicolasWinckler 2015-10-14 17:12:19 +02:00
parent 559c7babca
commit 323033c9eb
12 changed files with 341 additions and 13 deletions

View File

@ -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 <type_traits>
// CRTP base class
template <typename TDerived >
class BaseDeserializationPolicy
{
public:
BaseDeserializationPolicy()
{}
virtual ~BaseDeserializationPolicy()
{}
template<typename C = TDerived>
auto DeserializeMsg(FairMQMessage* msg)-> decltype(static_cast<C*>(this)->DeserializeMsg(msg) )
{
static_assert(std::is_same<C, TDerived>{}, "BaseDeserializationPolicy::DeserializeMsg hack broken");
return static_cast<TDerived*>(this)->DeserializeMsg(msg);
}
};
/*
// c++14 code
// CRTP base class
template <typename TDerived >
class BaseDeserializationPolicy
{
public:
BaseDeserializationPolicy()
{}
virtual ~BaseDeserializationPolicy()
{}
auto DeSerializeMsg(FairMQMessage* msg)
{
return static_cast<TDerived*>(this)->DeSerializeMsg(msg);
}
};*/
#endif /* BASEDESERIALIZATIONPOLICY_H */

View File

@ -0,0 +1,69 @@
/*
* File: BaseProcessorTaskPolicy.h
* Author: winckler
*
* Created on October 14, 2015, 1:01 PM
*/
#ifndef BASEPROCESSORTASKPOLICY_H
#define BASEPROCESSORTASKPOLICY_H
#include <type_traits>
// CRTP base class
template <typename TDerived >
class BaseProcessorTaskPolicy
{
public:
BaseProcessorTaskPolicy()
{}
virtual ~BaseProcessorTaskPolicy()
{}
template<typename C = TDerived>
auto GetOutputData() -> decltype(static_cast<C*>(this)->GetOutputData() )
{
static_assert(std::is_same<C, TDerived>{}, "BaseProcessorTaskPolicy::GetOutputData hack broken");
return static_cast<TDerived*>(this)->GetOutputData();
}
template<typename CONTAINER_TYPE, typename C = TDerived>
auto ExecuteTask(CONTAINER_TYPE container) -> decltype( static_cast<C*>(this)->ExecuteTask(container) )
{
static_assert(std::is_same<C, TDerived>{}, "BaseProcessorTaskPolicy::ExecuteTask hack broken");
return static_cast<TDerived*>(this)->ExecuteTask(container);
}
};
/*
// c++14 code only
// CRTP base class
template <typename TDerived >
class BaseProcessorTaskPolicy
{
public:
BaseProcessorTaskPolicy()
{}
virtual ~BaseProcessorTaskPolicy()
{}
auto GetOutputData()
{
return static_cast<TDerived*>(this)->GetOutputData();
}
template<typename CONTAINER_TYPE>
auto ExecuteTask(CONTAINER_TYPE container)
{
return static_cast<TDerived*>(this)->ExecuteTask(container);
}
};
*/
#endif /* BASEPROCESSORTASKPOLICY_H */

View File

@ -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 <type_traits>
// CRTP base class
template <typename TDerived >
class BaseSerializationPolicy
{
public:
BaseSerializationPolicy()
{}
virtual ~BaseSerializationPolicy()
{}
template<typename CONTAINER_TYPE, typename C = TDerived>
auto SerializeMsg(CONTAINER_TYPE container) -> decltype(static_cast<C*>(this)->SerializeMsg(container) )
{
static_assert(std::is_same<C, TDerived>{}, "BaseSerializationPolicy::SerializeMsg hack broken");
return static_cast<TDerived*>(this)->SerializeMsg(container);
}
template<typename C = TDerived>
auto SetMessage(FairMQMessage* msg)-> decltype(static_cast<C*>(this)->SetMessage(msg) )
{
static_assert(std::is_same<C, TDerived>{}, "BaseSerializationPolicy::SetMessage hack broken");
return static_cast<TDerived*>(this)->SetMessage(msg);
}
};
/*
// CRTP base class
// c++14 code
template <typename TDerived >
class BaseSerializationPolicy
{
public:
BaseSerializationPolicy()
{}
virtual ~BaseSerializationPolicy()
{}
template<typename CONTAINER_TYPE>
auto SerializeMsg(CONTAINER_TYPE container)
{
return static_cast<TDerived*>(this)->SerializeMsg(container);
}
auto SetMessage(FairMQMessage* msg)
{
return static_cast<TDerived*>(this)->SetMessage(msg);
}
};
*/
#endif /* BASESERIALIZATIONPOLICY_H */

View File

@ -0,0 +1,41 @@
/*
* File: BaseSinkPolicy.h
* Author: winckler
*
* Created on October 14, 2015, 1:01 PM
*/
#ifndef BASESINKPOLICY_H
#define BASESINKPOLICY_H
#include <type_traits>
// CRTP base class
template <typename TDerived >
class BaseSinkPolicy
{
public:
BaseSinkPolicy()
{}
virtual ~BaseSinkPolicy()
{}
template<typename CONTAINER_TYPE, typename C = TDerived>
auto AddToFile(CONTAINER_TYPE container) -> decltype(static_cast<C*>(this)->AddToFile(container) )
{
static_assert(std::is_same<C, TDerived>{}, "BaseSinkPolicy::AddToFile hack broken");
return static_cast<TDerived*>(this)->AddToFile(container);
}
template<typename C = TDerived>
auto InitOutputFile() -> decltype(static_cast<C*>(this)->InitOutputFile() )
{
static_assert(std::is_same<C, TDerived>{}, "BaseSinkPolicy::InitOutputFile hack broken");
return static_cast<TDerived*>(this)->InitOutputFile();
}
};
#endif /* BASESINKPOLICY_H */

View File

@ -0,0 +1,91 @@
/*
* File: BaseSourcePolicy.h
* Author: winckler
*
* Created on October 14, 2015, 1:01 PM
*/
#ifndef BASESOURCEPOLICY_H
#define BASESOURCEPOLICY_H
#include <type_traits>
// c++11 code
// CRTP base class
template <typename TDerived >
class BaseSourcePolicy
{
public:
BaseSourcePolicy()
{}
virtual ~BaseSourcePolicy()
{}
template<typename C = TDerived>
auto InitSource()-> decltype(static_cast<C*>(this)->InitSource() )
{
static_assert(std::is_same<C, TDerived>{}, "BaseSourcePolicy::InitSource hack broken");
return static_cast<TDerived*>(this)->InitSource();
}
template<typename C = TDerived>
int64_t GetNumberOfEvent()//-> decltype(static_cast<C*>(this)->GetNumberOfEvent() )
{
static_assert(std::is_same<C, TDerived>{}, "BaseSourcePolicy::GetNumberOfEvent hack broken");
return static_cast<TDerived*>(this)->GetNumberOfEvent();
}
template<typename C = TDerived>
auto SetIndex(int64_t eventIdx)-> decltype(static_cast<C*>(this)->SetIndex(eventIdx) )
{
static_assert(std::is_same<C, TDerived>{}, "BaseSourcePolicy::SetIndex hack broken");
return static_cast<TDerived*>(this)->SetIndex(eventIdx);
}
template<typename C = TDerived>
//auto GetOutData()-> decltype(static_cast<C*>(this)->GetOutData() )
decltype(std::declval<C*>()->GetOutData() ) GetOutData()
{
static_assert(std::is_same<C, TDerived>{}, "BaseSourcePolicy::GetOutData hack broken");
return static_cast<TDerived*>(this)->GetOutData();
}
};
/*
// c++14 code
// CRTP base class
template <typename TDerived >
class BaseSourcePolicy
{
public:
BaseSourcePolicy()
{}
virtual ~BaseSourcePolicy()
{}
auto InitSource()
{
return static_cast<TDerived*>(this)->InitSource();
}
int64_t GetNumberOfEvent()
{
return static_cast<TDerived*>(this)->GetNumberOfEvent();
}
auto SetIndex(int64_t eventIdx)
{
return static_cast<TDerived*>(this)->SetIndex(int64_t eventIdx);
}
auto GetOutData()
{
return static_cast<TDerived*>(this)->GetOutData();
}
};
*/
#endif /* BASESOURCEPOLICY_H */

View File

@ -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++;
}
}

View File

@ -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));
}
}

View File

@ -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());

View File

@ -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<void(int)> callback) // enabled if exists
* void BindGetSocketNumber(std::function<int()> callback) // enabled if exists

View File

@ -34,7 +34,7 @@ void base_GenericSampler<T,U,K,L>::InitTask()
BindingGetSocketNumber();
BindingGetCurrentIndex();
source_type::InitSampler();
source_type::InitSource();
fNumEvents = source_type::GetNumberOfEvent();
}

View File

@ -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<void(int)> callback); // enabled if exists
void source_type::BindGetSocketNumber(std::function<int()> callback); // enabled if exists
@ -122,6 +121,6 @@ void SetFileProperties(Args&... args)
```
### Generic Processor
The function members required by the processor policies are :

View File

@ -116,7 +116,7 @@ class FairProgOptions
int ParseEnvironment(const std::function<std::string(std::string)>&);
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;