Alexandria  2.19
Please provide a description of the project.
Row.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2012-2021 Euclid Science Ground Segment
3  *
4  * This library is free software; you can redistribute it and/or modify it under
5  * the terms of the GNU Lesser General Public License as published by the Free
6  * Software Foundation; either version 3.0 of the License, or (at your option)
7  * any later version.
8  *
9  * This library is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11  * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12  * details.
13  *
14  * You should have received a copy of the GNU Lesser General Public License
15  * along with this library; if not, write to the Free Software Foundation, Inc.,
16  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17  */
18 
25 #include <algorithm>
26 // The std regex library is not fully implemented in GCC 4.8. The following lines
27 // make use of the BOOST library and can be modified if GCC 4.9 will be used in
28 // the future.
29 // #include <regex>
30 #include <boost/regex.hpp>
31 using boost::regex;
32 using boost::regex_match;
34 #include "Table/Row.h"
35 #include <boost/algorithm/string/join.hpp>
36 
37 #if BOOST_VERSION < 105600
38 #include <boost/units/detail/utility.hpp>
39 using boost::units::detail::demangle;
40 #else
41 using boost::core::demangle;
42 #endif
43 
44 namespace std {
45 
46 template <typename T>
48  auto it = v.begin();
49  if (it != v.end()) {
50  s << *it;
51  }
52  while (++it != v.end()) {
53  s << ',' << *it;
54  }
55  return s;
56 }
57 
60 template std::ostream& operator<<<std::int64_t>(std::ostream& s, const std::vector<std::int64_t>& v);
61 template std::ostream& operator<<<std::int32_t>(std::ostream& s, const std::vector<std::int32_t>& v);
63 
64 } // namespace std
65 
66 namespace Euclid {
67 namespace Table {
68 
70  : m_values(std::move(values)), m_column_info{column_info} {
71  if (!m_column_info) {
72  throw Elements::Exception() << "Row construction with nullptr column_info";
73  }
74  if (m_values.size() != m_column_info->size()) {
75  throw Elements::Exception() << "Wrong number of row values (" << m_values.size() << " instead of " << m_column_info->size();
76  }
77  for (std::size_t i = 0; i < m_values.size(); ++i) {
78  auto& value_type = m_values[i].type();
79  auto& column_type = column_info->getDescription(i).type;
80  auto& column_name = column_info->getDescription(i).name;
81  if (std::type_index{value_type} != column_type) {
82  throw Elements::Exception() << "Incompatible cell type for " << column_name << ": expected " << demangle(column_type.name())
83  << ", got " << demangle(value_type.name());
84  }
85  }
86  regex vertical_whitespace{".*\\v.*"}; // Checks if input contains any vertical whitespace characters
87  for (auto cell : m_values) {
88  if (cell.type() == typeid(std::string)) {
89  std::string value = boost::get<std::string>(cell);
90  if (value.empty()) {
91  throw Elements::Exception() << "Empty string cell values are not allowed";
92  }
93  if (regex_match(value, vertical_whitespace)) {
94  throw Elements::Exception() << "Cell value '" << value << "' contains "
95  << "vertical whitespace characters";
96  }
97  }
98  }
99 }
100 
102  return m_column_info;
103 }
104 
105 size_t Row::size() const {
106  return m_values.size();
107 }
108 
109 const Row::cell_type& Row::operator[](const size_t index) const {
110  if (index >= m_values.size()) {
111  throw Elements::Exception("Index out of bounds");
112  }
113  return m_values[index];
114 }
115 
116 const Row::cell_type& Row::operator[](const std::string& column) const {
117  auto index = m_column_info->find(column);
118  if (!index) {
119  throw Elements::Exception() << "Row does not contain column with name " << column;
120  }
121  return m_values[*index];
122 }
123 
125  return m_values.cbegin();
126 }
127 
129  return m_values.cend();
130 }
131 
132 } // namespace Table
133 } // end of namespace Euclid
Euclid::Table::Row::operator[]
const cell_type & operator[](const size_t index) const
Returns the value of the column with the given index (zero based)
Definition: Row.cpp:109
std::string
STL class.
std::shared_ptr
STL class.
Euclid::Table::Row::getColumnInfo
std::shared_ptr< ColumnInfo > getColumnInfo() const
Returns a ColumnInfo object describing the columns of the Row.
Definition: Row.cpp:101
Euclid::Table::Row::cell_type
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:71
std::vector
STL class.
std::vector::size
T size(T... args)
std::type_index
std::regex_match
T regex_match(T... args)
Row.h
std::ostream
STL class.
Exception.h
Elements::Exception
Euclid::Table::Row::m_values
std::vector< cell_type > m_values
Definition: Row.h:157
Euclid::Table::Row::end
const_iterator end() const
Returns a const iterator to the past-the-end cell of the row.
Definition: Row.cpp:128
std::operator<<<double >
template std::ostream & operator<<<double >(std::ostream &s, const std::vector< double > &v)
Euclid::Table::Row::size
size_t size() const
Returns the number of cells in the row.
Definition: Row.cpp:105
std::operator<<<bool >
template std::ostream & operator<<<bool >(std::ostream &s, const std::vector< bool > &v)
std::vector::begin
T begin(T... args)
Euclid::Table::Row::const_iterator
std::vector< cell_type >::const_iterator const_iterator
Definition: Row.h:73
std
STL namespace.
s
constexpr double s
std::string::empty
T empty(T... args)
Euclid::Table::Row::begin
const_iterator begin() const
Returns a const iterator to the first cell of the row.
Definition: Row.cpp:124
std::size_t
Euclid::Table::Row::m_column_info
std::shared_ptr< ColumnInfo > m_column_info
Definition: Row.h:158
std::vector::end
T end(T... args)
std::operator<<<float >
template std::ostream & operator<<<float >(std::ostream &s, const std::vector< float > &v)
Euclid
Definition: InstOrRefHolder.h:29
std::operator<<
std::ostream & operator<<(std::ostream &s, const std::vector< T > &v)
Definition: Row.cpp:47