105 lines
3.6 KiB
C++
105 lines
3.6 KiB
C++
/**
|
|
* @file include/ActivMiddleware/ActivFramework/ComponentWrapper.h
|
|
*
|
|
* @brief Header file for component wrapping class.
|
|
*/
|
|
|
|
#if (!defined ACTIV_FRAMEWORK_COMPONENT_WRAPPER_H)
|
|
#define ACTIV_FRAMEWORK_COMPONENT_WRAPPER_H
|
|
|
|
#include "ActivMiddleware/ActivFramework/External.h"
|
|
#include "ActivMiddleware/ActivFramework/Component.h"
|
|
#include "ActivMiddleware/CxxYZ/functional.h"
|
|
|
|
#if (defined ACTIV_BOOST_NO_CXX11_VARIADIC_TEMPLATES)
|
|
#include ACTIV_BOOST_INCLUDE(preprocessor/arithmetic/inc.hpp)
|
|
#include ACTIV_BOOST_INCLUDE(preprocessor/punctuation/comma_if.hpp)
|
|
#include ACTIV_BOOST_INCLUDE(preprocessor/repetition/repeat_from_to.hpp)
|
|
#endif
|
|
|
|
//! Activ namespace.
|
|
namespace Activ
|
|
{
|
|
|
|
/**
|
|
* @brief Component wrapper class.
|
|
*
|
|
* This class can be used to "automatically" Enable()/Disable() a Component. Instead of:
|
|
*
|
|
* MyUdpTransmitter myUdpTransmitter(p1, p2, p3);
|
|
* Component::Enabler myUdpTransmitterEnabler(myUdpTransmitter);
|
|
*
|
|
* You can:
|
|
*
|
|
* ComponentWrapper<MyUdpTransmitter> myUdpTransmitter(CxxYZ::ref(p1), CxxYZ::cref(p2), CxxYZ::ref(p3));
|
|
*
|
|
* In C++11 or later only code, perfect forwarding is available:
|
|
*
|
|
* ComponentWrapper<MyUdpTransmitter> myUdpTransmitter(p1, p2, p3);
|
|
*
|
|
* Otherwise, all non-scalar parameters must be passed as reference wrappers (e.g. with CxxYZ::ref/cref). This is not
|
|
* perfect, as it could be valid to pass by value but isn't for the vast majority of use cases (and equally the intention
|
|
* might be to pass an integral reference that would not be flagged by these static asserts).
|
|
*/
|
|
template<typename COMPONENT>
|
|
class ComponentWrapper : public COMPONENT
|
|
{
|
|
public:
|
|
#if (defined ACTIV_BOOST_NO_CXX11_VARIADIC_TEMPLATES)
|
|
|
|
#define ACTIV_COMPONENT_WRAPPER_TEMPLATE_PARAM(z, n, data) ACTIV_BOOST_PP_COMMA_IF(n) typename T##n
|
|
|
|
#if (defined ACTIV_BOOST_NO_CXX11_RVALUE_REFERENCES)
|
|
|
|
#define ACTIV_COMPONENT_WRAPPER_CTOR_PARAM(z, n, data) ACTIV_BOOST_PP_COMMA_IF(n) T##n arg##n
|
|
#define ACTIV_COMPONENT_WRAPPER_CTOR_PARAM_PASS(z, n, data) ACTIV_BOOST_PP_COMMA_IF(n) arg##n
|
|
|
|
// NB the block scoping here is due to ACTIV_BOOST_STATIC_ASSERT using the current line number for uniqueness, which
|
|
// breaks inside a PP loop.
|
|
#define ACTIV_COMPONENT_WRAPPER_CTOR_IS_REF(z, n, data) { ACTIV_BOOST_STATIC_ASSERT(Boost::is_scalar<T##n>::value || \
|
|
CxxYZ::is_reference_wrapper<T##n>::value); }
|
|
|
|
#else
|
|
|
|
#define ACTIV_COMPONENT_WRAPPER_CTOR_PARAM(z, n, data) ACTIV_BOOST_PP_COMMA_IF(n) T##n &&arg##n
|
|
#define ACTIV_COMPONENT_WRAPPER_CTOR_PARAM_PASS(z, n, data) ACTIV_BOOST_PP_COMMA_IF(n) std::forward<T##n>(arg##n)
|
|
#define ACTIV_COMPONENT_WRAPPER_CTOR_IS_REF(z, n, data)
|
|
|
|
#endif
|
|
|
|
#define ACTIV_COMPONENT_WRAPPER_DECLARE_CTOR(z, n, data) \
|
|
template<ACTIV_BOOST_PP_REPEAT_FROM_TO(0, ACTIV_BOOST_PP_INC(n), ACTIV_COMPONENT_WRAPPER_TEMPLATE_PARAM, )> \
|
|
ComponentWrapper(ACTIV_BOOST_PP_REPEAT_FROM_TO(0, ACTIV_BOOST_PP_INC(n), ACTIV_COMPONENT_WRAPPER_CTOR_PARAM, )) : \
|
|
COMPONENT(ACTIV_BOOST_PP_REPEAT_FROM_TO(0, ACTIV_BOOST_PP_INC(n), ACTIV_COMPONENT_WRAPPER_CTOR_PARAM_PASS, )) \
|
|
{ \
|
|
ACTIV_BOOST_PP_REPEAT_FROM_TO(0, ACTIV_BOOST_PP_INC(n), ACTIV_COMPONENT_WRAPPER_CTOR_IS_REF, ) \
|
|
this->Enable(); \
|
|
}
|
|
|
|
ACTIV_BOOST_PP_REPEAT_FROM_TO(0, 10, ACTIV_COMPONENT_WRAPPER_DECLARE_CTOR, )
|
|
|
|
#else
|
|
|
|
#if (defined ACTIV_BOOST_NO_CXX11_RVALUE_REFERENCES)
|
|
#error Variadic template support but no rvalue ref support is not supported
|
|
#endif
|
|
|
|
template<typename... Ts>
|
|
ComponentWrapper(Ts &&...args) :
|
|
COMPONENT(std::forward<Ts>(args)...)
|
|
{
|
|
this->Enable();
|
|
}
|
|
|
|
#endif
|
|
|
|
~ComponentWrapper()
|
|
{
|
|
this->Disable();
|
|
}
|
|
};
|
|
|
|
} // namespace Activ
|
|
|
|
#endif // (!defined ACTIV_FRAMEWORK_COMPONENT_WRAPPER_H)
|