module-arch-POC/src/adapter_base.hpp

43 lines
1.5 KiB
C++

#pragma once
#include <boost/signals2.hpp>
#include <boost/signals2/variadic_signal.hpp>
#include <type_traits>
using namespace boost::signals2;
// Base adapter interface exposing a typed callback signal and name.
template <typename InType, typename CallbackRetTypeTag, typename... CbkAargs> class AdapterBase {
using cbk_ret_type_t_ = typename CallbackRetTypeTag::type;
// If return type is not void we combine all callback invocation results.
class CollectAllCombiner_ {
public:
using result_type = std::conditional_t<!std::is_void_v<cbk_ret_type_t_>, std::vector<cbk_ret_type_t_>, std::false_type>;
template <typename InputIterator> result_type operator()(InputIterator first, InputIterator last) const {
result_type results;
if constexpr (!std::is_same_v<result_type, std::false_type>) {
for (; first != last; ++first) {
results.push_back(*first);
}
}
return results;
}
};
public:
// Use combiner if return type is not void
using signature_t = std::conditional_t<std::is_void_v<cbk_ret_type_t_>, void(const InType &, CbkAargs &&...), cbk_ret_type_t_(const InType &, CbkAargs &&...)>;
using callback_type_t = std::conditional_t<!std::is_void_v<cbk_ret_type_t_>, signal<signature_t, CollectAllCombiner_>, signal<signature_t>>;
AdapterBase(const std::string &name) : mc_name_(name) {}
virtual callback_type_t &callback() const = 0;
inline const auto &name() const { return mc_name_; }
private:
const std::string mc_name_;
};