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