You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Blockio currently allows to create a block with N inputs and M outputs of type Tin and Tout. However, it does not allow to mix types (e.g. have two inputs - one being an int and the other a double). We could use parameter packs to implement this and make the blockio class more versatile.
#include<tuple>
#include<utility>
#include<iostream>// Helper metafunction to extract input and output tuples from Types...template <size_t N, size_t M, typename... Types>
structSplitTypes;
template <size_t N, size_t M, typename... Tin, typename... Tout>
structSplitTypes<N, M, std::tuple<Tin...>, std::tuple<Tout...>> {
using Inputs = std::tuple<Tin...>;
using Outputs = std::tuple<Tout...>;
};
template <size_t N, size_t M, typename First, typename... Rest, typename... Tin, typename... Tout>
structSplitTypes<N, M, std::tuple<Tin...>, std::tuple<Tout...>, First, Rest...>
: SplitTypes<N - 1, M, std::tuple<Tin..., First>, std::tuple<Tout...>, Rest...> {};
template <size_t M, typename First, typename... Rest, typename... Tin, typename... Tout>
structSplitTypes<0, M, std::tuple<Tin...>, std::tuple<Tout...>, First, Rest...>
: SplitTypes<0, M - 1, std::tuple<Tin...>, std::tuple<Tout..., First>, Rest...> {};
template <typename... Tin, typename... Tout>
structSplitTypes<0, 0, std::tuple<Tin...>, std::tuple<Tout...>> {
using Inputs = std::tuple<Tin...>;
using Outputs = std::tuple<Tout...>;
};
template <size_t N, size_t M, typename... Types>
using SplitTypes_t = SplitTypes<N, M, std::tuple<>, std::tuple<>, Types...>;
// Blockio demo classtemplate <size_t N, size_t M, typename... Types>
classBlockio {
static_assert(sizeof...(Types) == N + M, "Total number of types must match N + M");
using Split = SplitTypes_t<N, M, Types...>;
using InputTuple = typename Split::Inputs;
using OutputTuple = typename Split::Outputs;
InputTuple inputs;
OutputTuple outputs;
public:Blockio() = default;
// Function to set inputstemplate<typename... Tin>
voidsetInputs(Tin... in) {
static_assert(sizeof...(Tin) == N, "Number of inputs must match N");
inputs = std::make_tuple(in...);
}
// Function to get inputs
InputTuple getInputs() const {
return inputs;
}
// Function to set outputstemplate<typename... Tout>
voidsetOutputs(Tout... out) {
static_assert(sizeof...(Tout) == M, "Number of outputs must match M");
outputs = std::make_tuple(out...);
}
// Function to get outputs
OutputTuple getOutputs() const {
return outputs;
}
// Example method to display the inputs and outputsvoiddisplay() const {
std::cout << "Inputs: ";
std::apply([](auto&&... args) {
((std::cout << args << ""), ...);
}, inputs);
std::cout << "\nOutputs: ";
std::apply([](auto&&... args) {
((std::cout << args << ""), ...);
}, outputs);
std::cout << std::endl;
}
};
intmain() {
// Define the Blockio class with 3 inputs and 2 outputs
Blockio<3, 2, int, double, float, char, bool> block;
// Set the inputs and outputs
block.setInputs(1, 2.3, 4.5f);
block.setOutputs('a', true);
// Display the inputs and outputs
block.display();
return0;
}
The text was updated successfully, but these errors were encountered:
Blockio currently allows to create a block with N inputs and M outputs of type Tin and Tout. However, it does not allow to mix types (e.g. have two inputs - one being an int and the other a double). We could use parameter packs to implement this and make the blockio class more versatile.
The text was updated successfully, but these errors were encountered: