12 #include <hipipe/build_config.hpp>
14 #ifdef HIPIPE_BUILD_PYTHON
15 #include <hipipe/core/python/utility/ndim_vector_converter.hpp>
18 #include <initializer_list>
24 namespace hipipe::stream {
28 class abstract_column {
33 template<
typename Column>
34 std::runtime_error extraction_error()
const
36 return std::runtime_error{
37 std::string{
"Trying to extract column `"} + Column{}.name()
38 +
"` from a column of type `" + this->
name() +
"`."};
57 template<
typename Column>
58 typename Column::data_type&
extract()
61 return dynamic_cast<Column&>(*this).data();
62 }
catch (
const std::bad_cast&) {
63 throw extraction_error<Column>();
70 template<
typename Column>
71 const typename Column::data_type&
extract()
const
74 return dynamic_cast<const Column&>(*this).data();
75 }
catch (
const std::bad_cast&) {
76 throw extraction_error<Column>();
86 virtual std::string
name()
const = 0;
91 virtual std::size_t
size()
const = 0;
96 virtual void push_back(std::unique_ptr<abstract_column> rhs) = 0;
101 virtual std::unique_ptr<abstract_column>
take(std::size_t n) = 0;
105 #ifdef HIPIPE_BUILD_PYTHON
106 virtual boost::python::object to_python() = 0;
114 virtual ~abstract_column() =
default;
120 template <
typename ColumnName,
typename ExampleType>
121 class column_base :
public abstract_column {
127 using data_type = std::vector<example_type>;
145 template <
typename... Args>
147 : data_{std::forward<Args>(args)...}
153 std::size_t
size()
const override
176 std::unique_ptr<abstract_column>
take(std::size_t n)
override
178 if (n > data_.size()) {
179 throw std::runtime_error{
"hipipe: Attempting to take "
184 std::move(data_.begin(), data_.begin() + n, taken_examples.begin());
185 data_.erase(data_.begin(), data_.begin() + n);
186 return std::make_unique<ColumnName>(std::move(taken_examples));
205 void push_back(std::unique_ptr<abstract_column> rhs)
override
208 ColumnName& typed_rhs = dynamic_cast<ColumnName&>(*rhs);
209 data_.reserve(data_.size() + typed_rhs.data_.size());
211 data_.push_back(std::move(example));
213 }
catch (
const std::bad_cast&) {
214 throw std::runtime_error{
"hipipe: Attempting to push back "
215 "column `" + rhs->name() +
"` to column `" +
name() +
"."};
236 #ifdef HIPIPE_BUILD_PYTHON
237 boost::python::object to_python()
override
239 return hipipe::python::utility::to_python(std::move(data_));
251 #define HIPIPE_DEFINE_COLUMN(column_name_, example_type_) \
252 struct column_name_ : hipipe::stream::column_base<column_name_, example_type_> { \
253 using hipipe::stream::column_base<column_name_, example_type_>::column_base; \
254 std::string name() const override { return #column_name_; } \