Alexandria 2.31.0
SDC-CH common library for the Euclid project
Loading...
Searching...
No Matches
Row.cpp
Go to the documentation of this file.
1
25#include "Table/Row.h"
28#include <algorithm>
29#include <boost/algorithm/string/join.hpp>
30
31#if BOOST_VERSION < 105600
32#include <boost/units/detail/utility.hpp>
33using boost::units::detail::demangle;
34#else
35using boost::core::demangle;
36#endif
37
38namespace Euclid {
39namespace Table {
40
41struct StreamCellVisitor : public boost::static_visitor<void> {
42
44
45 template <typename T>
46 void operator()(const std::vector<T>& v) const {
47 auto it = v.begin();
48 if (it != v.end()) {
49 m_stream << *it;
50 ++it;
51 }
52 while (it != v.end()) {
53 m_stream << ',' << *it;
54 ++it;
55 }
56 }
57
58 template <typename T>
59 void operator()(const T& val) const {
60 m_stream << val;
61 }
62
64};
65
66std::ostream& operator<<(std::ostream& s, const cell_stream_adaptor& cell) {
67 StreamCellVisitor visitor{s};
68 boost::apply_visitor(visitor, cell.m_cell);
69 return s;
70}
71
73 : m_values(std::move(values)), m_column_info{column_info} {
74 if (!m_column_info) {
75 throw Elements::Exception() << "Row construction with nullptr column_info";
76 }
77 if (m_values.size() != m_column_info->size()) {
78 throw Elements::Exception() << "Wrong number of row values (" << m_values.size() << " instead of "
79 << m_column_info->size();
80 }
81 for (std::size_t i = 0; i < m_values.size(); ++i) {
82 auto& value_type = m_values[i].type();
83 auto& column_type = column_info->getDescription(i).type;
84 if (std::type_index{value_type} != column_type) {
85 auto& column_name = column_info->getDescription(i).name;
86 throw Elements::Exception() << "Incompatible cell type for " << column_name << ": expected "
87 << demangle(column_type.name()) << ", got " << demangle(value_type.name());
88 }
89 }
90 static const regex::regex vertical_whitespace{".*[\\n\\v\\f\\r].*"}; // Checks if input contains any whitespace
91 // characters
92 for (auto cell : m_values) {
93 if (cell.type() == typeid(std::string)) {
94 std::string value = boost::get<std::string>(cell);
95 if (value.empty()) {
96 throw Elements::Exception() << "Empty string cell values are not allowed";
97 }
98 if (regex_match(value, vertical_whitespace)) {
99 throw Elements::Exception() << "Cell value '" << value << "' contains "
100 << "vertical whitespace characters";
101 }
102 }
103 }
104}
105
109
110size_t Row::size() const {
111 return m_values.size();
112}
113
114const Row::cell_type& Row::operator[](const size_t index) const {
115 if (index >= m_values.size()) {
116 throw Elements::Exception("Index out of bounds");
117 }
118 return m_values[index];
119}
120
121const Row::cell_type& Row::operator[](const std::string& column) const {
122 auto index = m_column_info->find(column);
123 if (!index) {
124 throw Elements::Exception() << "Row does not contain column with name " << column;
125 }
126 return m_values[*index];
127}
128
130 return m_values.cbegin();
131}
132
134 return m_values.cend();
135}
136
137} // namespace Table
138} // end of namespace Euclid
T begin(T... args)
const_iterator end() const
Returns a const iterator to the past-the-end cell of the row.
Definition Row.cpp:133
std::vector< cell_type > m_values
Definition Row.h:150
std::shared_ptr< ColumnInfo > getColumnInfo() const
Returns a ColumnInfo object describing the columns of the Row.
Definition Row.cpp:106
const_iterator begin() const
Returns a const iterator to the first cell of the row.
Definition Row.cpp:129
size_t size() const
Returns the number of cells in the row.
Definition Row.cpp:110
std::shared_ptr< ColumnInfo > m_column_info
Definition Row.h:151
Row(std::vector< cell_type > values, std::shared_ptr< ColumnInfo > column_info)
Constructs a Row with the given cell values and column info descriptor.
Definition Row.cpp:72
std::vector< cell_type >::const_iterator const_iterator
Definition Row.h:66
boost::variant< bool, int32_t, int64_t, float, double, std::string, std::vector< bool >, std::vector< int32_t >, std::vector< int64_t >, std::vector< float >, std::vector< double >, NdArray::NdArray< int32_t >, NdArray::NdArray< int64_t >, NdArray::NdArray< float >, NdArray::NdArray< double > > cell_type
The possible cell types.
Definition Row.h:64
const cell_type & operator[](const size_t index) const
Returns the value of the column with the given index (zero based)
Definition Row.cpp:114
T empty(T... args)
T end(T... args)
STL namespace.
T size(T... args)
void operator()(const std::vector< T > &v) const
Definition Row.cpp:46
StreamCellVisitor(std::ostream &s)
Definition Row.cpp:43
void operator()(const T &val) const
Definition Row.cpp:59
const Row::cell_type & m_cell
Definition Row.h:163