Skip to content

Commit

Permalink
refactored port<T> interface - WIP
Browse files Browse the repository at this point in the history
... in view of #148

tackled items:
 *

Signed-off-by: Ralph J. Steinhagen <[email protected]>
  • Loading branch information
RalphSteinhagen committed Sep 18, 2023
1 parent a8d7c24 commit f22ace8
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 40 deletions.
30 changes: 11 additions & 19 deletions include/port.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,21 +21,13 @@ using supported_type = std::variant<uint8_t, uint32_t, int8_t, int16_t, int32_t,

enum class port_direction_t { INPUT, OUTPUT, ANY }; // 'ANY' only for query and not to be used for port declarations

template<typename... Args>
constexpr port_direction_t
find_port_direction(fair::meta::typelist<Args...>, port_direction_t default_value = port_direction_t::ANY) {
if constexpr ((... || fair::meta::is_any_of_v<Args, port_direction_t>) ) {
return (... ? (air::meta::is_any_of_v<Args, port_direction_t> ? static_cast<port_direction_t>(Args::value) : default_value) : default_value);
} else {
return default_value;
}
}

enum class connection_result_t { SUCCESS, FAILED };

enum class port_type_t {
STREAM, /*!< used for single-producer-only ond usually synchronous one-to-one or one-to-many communications */
MESSAGE /*!< used for multiple-producer one-to-one, one-to-many, many-to-one, or many-to-many communications */
};

enum class port_domain_t { CPU, GPU, NET, FPGA, DSP, MLU };

template<class T>
Expand Down Expand Up @@ -64,16 +56,16 @@ struct internal_port_buffers {
};

template<std::size_t MIN_SAMPLES = std::dynamic_extent, std::size_t MAX_SAMPLES = std::dynamic_extent>
struct BufferSize {
struct RequiredSamples {
static constexpr std::size_t MinSamples = MIN_SAMPLES;
static constexpr std::size_t MaxSamples = MAX_SAMPLES;
};

template<typename T>
struct is_buffer_size : std::false_type {};
struct is_required_samples : std::false_type {};

template<std::size_t MIN_SAMPLES, std::size_t MAX_SAMPLES>
struct is_buffer_size<BufferSize<MIN_SAMPLES, MAX_SAMPLES>> : std::true_type {};
struct is_required_samples<RequiredSamples<MIN_SAMPLES, MAX_SAMPLES>> : std::true_type {};

/**
* @brief 'ports' are interfaces that allows data to flow between blocks in a graph, similar to RF connectors.
Expand Down Expand Up @@ -107,13 +99,13 @@ template<typename T, fixed_string PortName, port_type_t PortType, port_direction
gr::Buffer TagBufferType = gr::circular_buffer<tag_t>, typename... Arguments>
class port {
public:
using BufferSize = typename fair::meta::typelist<Arguments...>::template find_or_default<is_buffer_size, BufferSize<>>;
static_assert(PortDirection != port_direction_t::ANY, "ANY reserved for queries and not port direction declarations");

using value_type = T;

static constexpr bool IS_INPUT = PortDirection == port_direction_t::INPUT;
static constexpr bool IS_OUTPUT = PortDirection == port_direction_t::OUTPUT;
using value_type = T;
using Required = typename fair::meta::typelist<Arguments...>::template find_or_default<is_required_samples, RequiredSamples<MIN_SAMPLES, MAX_SAMPLES>>;
static constexpr port_direction_t Direction = PortDirection;
static constexpr bool IS_INPUT = PortDirection == port_direction_t::INPUT;
static constexpr bool IS_OUTPUT = PortDirection == port_direction_t::OUTPUT;

template<fixed_string NewName>
using with_name = port<T, NewName, PortType, PortDirection, PortDomain, MIN_SAMPLES, MAX_SAMPLES, BufferType>;
Expand Down Expand Up @@ -523,7 +515,7 @@ class dynamic_port {
}

~wrapper() override = default;

// TODO revisit: constexpr was removed because emscripten does not support constexpr function for non literal type, like DataSet<T>
#if defined(__EMSCRIPTEN__)
[[nodiscard]] supported_type
Expand Down
28 changes: 9 additions & 19 deletions include/port_traits.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ namespace fair::graph::traits::port {

template<typename T>
concept has_fixed_info_v = requires {
typename T::value_type;
{ T::static_name() };
{ T::direction() } -> std::same_as<port_direction_t>;
{ T::type() } -> std::same_as<port_type_t>;
};
typename T::value_type;
{ T::static_name() };
{ T::direction() } -> std::same_as<port_direction_t>;
{ T::type() } -> std::same_as<port_type_t>;
};

template<typename T>
using has_fixed_info = std::integral_constant<bool, has_fixed_info_v<T>>;
Expand Down Expand Up @@ -43,25 +43,15 @@ using is_output = std::integral_constant<bool, Port::direction() == port_directi
template<typename Port>
concept is_output_v = is_output<Port>::value;

template <typename Type>
template<typename Type>
concept is_port_v = is_output_v<Type> || is_input_v<Type>;

template<typename... Ports>
struct min_samples : std::integral_constant<std::size_t, std::max({ min_samples<Ports>::value... })> {};

template<typename T, fixed_string PortName, port_type_t PortType, port_direction_t PortDirection,
std::size_t MIN_SAMPLES, std::size_t MAX_SAMPLES, gr::Buffer BufferType>
struct min_samples<fair::graph::port<T, PortName, PortType, PortDirection, MIN_SAMPLES, MAX_SAMPLES, BufferType>>
: std::integral_constant<std::size_t, MIN_SAMPLES> {};
struct min_samples : std::integral_constant<std::size_t, std::max({ Ports::RequiredSamples::MinSamples... })> {};

template<typename... Ports>
struct max_samples : std::integral_constant<std::size_t, std::min({ max_samples<Ports>::value... })> {};

template<typename T, fixed_string PortName, port_type_t PortType, port_direction_t PortDirection,
std::size_t MIN_SAMPLES, std::size_t MAX_SAMPLES, gr::Buffer BufferType>
struct max_samples<fair::graph::port<T, PortName, PortType, PortDirection, MIN_SAMPLES, MAX_SAMPLES, BufferType>>
: std::integral_constant<std::size_t, MAX_SAMPLES> {};
struct max_samples : std::integral_constant<std::size_t, std::max({ Ports::RequiredSamples::MaxSamples... })> {};

} // namespace port
} // namespace fair::graph::traits::port

#endif // include guard
6 changes: 4 additions & 2 deletions test/qa_dynamic_port.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,10 @@ const boost::ut::suite PortApiTests = [] {
static_assert(Port<IN_MSG<float, 0, 0, "in_msg">>);
static_assert(Port<OUT_MSG<float, 0, 0, "out_msg">>);

static_assert(IN<float, 0, 0, "in">::static_name() == fixed_string("in"));
static_assert(requires { IN<float>("in").name; });
static_assert(IN<float, 1, 2, "in">::Required::MinSamples == 1);
static_assert(IN<float, 1, 2, "in">::Required::MaxSamples == 2);
static_assert(IN<float, 1, 2, "in">::direction() == port_direction_t::INPUT);
static_assert(OUT<float, 1, 2, "in">::direction() == port_direction_t::OUTPUT);
};

"PortBufferApi"_test = [] {
Expand Down

0 comments on commit f22ace8

Please sign in to comment.