Alexandria  2.14.1
Please provide a description of the project.
Row.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2012-2020 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;
33 #include <boost/algorithm/string/join.hpp>
35 #include "Table/Row.h"
36 
37 namespace std {
38 
39 template <typename T>
41  auto it = v.begin();
42  if (it != v.end()) {
43  s << *it;
44  }
45  while (++it != v.end()) {
46  s << ',' << *it;
47  }
48  return s;
49 }
50 
53 template std::ostream& operator<< <std::int64_t> (std::ostream& s, const std::vector<std::int64_t>& v);
54 template std::ostream& operator<< <std::int32_t> (std::ostream& s, const std::vector<std::int32_t>& v);
56 
57 }
58 
59 namespace Euclid {
60 namespace Table {
61 
63  : m_values(std::move(values)), m_column_info{column_info} {
64  if (!m_column_info) {
65  throw Elements::Exception() << "Row construction with nullptr column_info";
66  }
67  if (m_values.size() != m_column_info->size()) {
68  throw Elements::Exception() << "Wrong number of row values (" << m_values.size()
69  << " instead of " << m_column_info->size();
70  }
71  for (std::size_t i=0; i<m_values.size(); ++i) {
72  if (std::type_index{m_values[i].type()} != column_info->getDescription(i).type) {
73  throw Elements::Exception() << "Incompatible cell type";
74  }
75  }
76  regex whitespace {".*\\s.*"}; // Checks if input contains any whitespace characters
77  for (auto cell : m_values) {
78  if (cell.type() == typeid(std::string)) {
79  std::string value = boost::get<std::string>(cell);
80  if (value.empty()) {
81  throw Elements::Exception() << "Empty string cell values are not allowed";
82  }
83  if (regex_match(value, whitespace)) {
84  throw Elements::Exception() << "Cell value '" << value << "' contains "
85  << "whitespace characters";
86  }
87  }
88  }
89 }
90 
92  return m_column_info;
93 }
94 
95 size_t Row::size() const {
96  return m_values.size();
97 }
98 
99 const Row::cell_type& Row::operator [](const size_t index) const {
100  if (index >= m_values.size()) {
101  throw Elements::Exception("Index out of bounds");
102  }
103  return m_values[index];
104 }
105 
106 const Row::cell_type& Row::operator [](const std::string& column) const {
107  auto index = m_column_info->find(column);
108  if (!index) {
109  throw Elements::Exception() << "Row does not contain column with name " << column;
110  }
111  return m_values[*index];
112 }
113 
115  return m_values.cbegin();
116 }
117 
119  return m_values.cend();
120 }
121 
122 }
123 } // 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:99
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:91
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::operator<<< float >
template std::ostream & operator<<< float >(std::ostream &s, const std::vector< float > &v)
std::ostream
STL class.
Exception.h
Elements::Exception
Euclid::Table::Row::m_values
std::vector< cell_type > m_values
Definition: Row.h:170
Euclid::Table::Row::end
const_iterator end() const
Returns a const iterator to the past-the-end cell of the row.
Definition: Row.cpp:118
std::operator<<< double >
template std::ostream & operator<<< double >(std::ostream &s, const std::vector< double > &v)
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< bool >, NdArray::NdArray< int32_t >, NdArray::NdArray< int64_t >, NdArray::NdArray< float >, NdArray::NdArray< double > > cell_type
The possible cell types.
Definition: Row.h:84
Euclid::Table::Row::size
size_t size() const
Returns the number of cells in the row.
Definition: Row.cpp:95
std::vector::begin
T begin(T... args)
Euclid::Table::Row::const_iterator
std::vector< cell_type >::const_iterator const_iterator
Definition: Row.h:86
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:114
std::size_t
Euclid::Table::Row::m_column_info
std::shared_ptr< ColumnInfo > m_column_info
Definition: Row.h:171
std::vector::end
T end(T... args)
Euclid
Definition: InstOrRefHolder.h:29
std::operator<<< bool >
template std::ostream & operator<<< bool >(std::ostream &s, const std::vector< bool > &v)
std::operator<<
std::ostream & operator<<(std::ostream &s, const std::vector< T > &v)
Definition: Row.cpp:40