Mantid
Loading...
Searching...
No Matches
TableRow.h
Go to the documentation of this file.
1// Mantid Repository : https://github.com/mantidproject/mantid
2//
3// Copyright © 2007 ISIS Rutherford Appleton Laboratory UKRI,
4// NScD Oak Ridge National Laboratory, European Spallation Source,
5// Institut Laue - Langevin & CSNS, Institute of High Energy Physics, CAS
6// SPDX - License - Identifier: GPL - 3.0 +
7#pragma once
8
9#include "MantidAPI/Column.h"
10
11#ifndef Q_MOC_RUN
12#include <boost/lexical_cast.hpp>
13#endif
14
15#include <ostream>
16#include <stdexcept>
17#include <vector>
18
19namespace Mantid {
20
21namespace API {
22//----------------------------------------------------------------------
23// Forward declarations
24//----------------------------------------------------------------------
25class TableRowHelper;
26
39class MANTID_API_DLL TableRow {
40public:
41 TableRow(const TableRowHelper &trh);
43 size_t row() const { return m_row; }
45 size_t size() const { return m_nrows; }
46 void row(size_t i);
47 bool next();
48 bool prev();
50 void sep(const std::string &s) { m_sep = s; }
51
61 template <class T> TableRow &operator<<(const T &t) {
62 if (m_col >= m_columns.size()) {
63 std::stringstream errss;
64 errss << "Column index " << m_col << " is out of range " << m_columns.size() << " of operator << ";
65 throw std::range_error(errss.str());
66 }
67 Column_sptr c = m_columns[m_col];
68 if (!c->isType<T>()) {
69 std::ostringstream err;
70 err << "Type mismatch: " << typeid(T).name() << " (expected " << c->get_type_info().name() << ")";
71 throw std::runtime_error(err.str());
72 }
73 c->cell<T>(m_row) = t;
74 ++m_col;
75 return *this;
76 }
77
79 TableRow &operator<<(const char *t) { return operator<<(std::string(t)); }
81 TableRow &operator<<(bool t) { return operator<<(Boolean(t)); }
82
92 template <class T> const TableRow &operator>>(T &t) const {
93 if (m_col >= m_columns.size()) {
94 std::stringstream errss;
95 errss << "Column index " << m_col << " is out of range " << m_columns.size() << " of operator >> ";
96 throw std::range_error(errss.str());
97 }
98 Column_sptr c = m_columns[m_col];
99 if (!c->isType<T>()) {
100 throw std::runtime_error("TableRow type mismatch.");
101 }
102 t = c->cell<T>(m_row);
103 ++m_col;
104 return *this;
105 }
106
108 const TableRow &operator>>(bool &t) const;
109
115 template <class T> T &cell(size_t col) {
116 if (col >= m_columns.size()) {
117 std::stringstream errss;
118 errss << "Column index " << m_col << " is out of range " << m_columns.size() << " of method cell(). ";
119 throw std::range_error(errss.str());
120 }
121 m_col = col;
122 Column_sptr c = m_columns[m_col];
123 ++m_col; // Is it right?
124 return c->cell<T>(m_row);
125 }
126
131 int &Int(size_t col) { return cell<int>(col); }
132
137 double &Double(size_t col) { return cell<double>(col); }
138
143 Boolean &Bool(size_t col) { return cell<Boolean>(col); }
144
150 std::string &String(size_t col) { return cell<std::string>(col); }
151
152private:
153 friend MANTID_API_DLL std::ostream &operator<<(std::ostream &s, const TableRow &row);
154 std::vector<Column_sptr> m_columns;
155 size_t m_row;
156 mutable size_t m_col;
157 size_t m_nrows;
158 std::string m_sep;
159};
160
161MANTID_API_DLL std::ostream &operator<<(std::ostream &s, const TableRow &row);
162
163} // namespace API
164} // namespace Mantid
std::ostream & operator<<(std::ostream &out, const MantidQt::MantidWidgets::IndexType< i > &index)
Definition: IndexTypes.h:103
Helper class used to create TableRow.
TableRow represents a row in a TableWorkspace.
Definition: TableRow.h:39
std::string & String(size_t col)
Returns a reference to the element in position col if its type is std::string.
Definition: TableRow.h:150
Boolean & Bool(size_t col)
Returns a reference to the element in position col if its type is bool.
Definition: TableRow.h:143
std::vector< Column_sptr > m_columns
Pointers to the columns in the ITableWorkspace.
Definition: TableRow.h:154
double & Double(size_t col)
Returns a reference to the element in position col if its type is double.
Definition: TableRow.h:137
size_t m_col
Current column number (for streaming operations)
Definition: TableRow.h:156
int & Int(size_t col)
Returns a reference to the element in position col if its type is int.
Definition: TableRow.h:131
size_t m_row
Row number in the TableWorkspace.
Definition: TableRow.h:155
TableRow & operator<<(bool t)
Special case of bool.
Definition: TableRow.h:81
const TableRow & operator>>(T &t) const
Output streaming operator.
Definition: TableRow.h:92
void sep(const std::string &s)
Sets a new separator character(s) between elements in a text output.
Definition: TableRow.h:50
size_t row() const
Returns the row number of the TableRow.
Definition: TableRow.h:43
TableRow & operator<<(const T &t)
Input streaming operator.
Definition: TableRow.h:61
size_t m_nrows
Number of rows in the TableWorkspace.
Definition: TableRow.h:157
T & cell(size_t col)
Templated method to access the element col in the row.
Definition: TableRow.h:115
TableRow & operator<<(const char *t)
Special case of char*.
Definition: TableRow.h:79
std::string m_sep
Separator character(s) between elements in a text output.
Definition: TableRow.h:158
size_t size() const
Returns the number of rows in the TableWorkspace.
Definition: TableRow.h:45
std::shared_ptr< Column > Column_sptr
Definition: Column.h:228
MANTID_API_DLL std::ostream & operator<<(std::ostream &, const AlgorithmHistory &)
Prints a text representation.
MANTID_API_DLL std::istream & operator>>(std::istream &istr, API::Boolean &)
Redaing a Boolean from an input stream.
Definition: Column.cpp:61
Helper class which provides the Collimation Length for SANS instruments.
As TableColumn stores its data in a std::vector bool type cannot be used in the same way as the other...
Definition: Column.h:209