Mantid
Loading...
Searching...
No Matches
ITableWorkspace.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//----------------------------------------------------------------------
10// Includes
11//----------------------------------------------------------------------
12#include "MantidAPI/Column.h"
13#include "MantidAPI/DllConfig.h"
16#include "MantidAPI/Workspace.h"
19
20#ifndef Q_MOC_RUN
21#include <boost/lexical_cast.hpp>
22#include <memory>
23#endif
24
25#include <sstream>
26#include <utility>
27
28namespace Mantid {
29
30//----------------------------------------------------------------------
31// Forward declarations
32//----------------------------------------------------------------------
33
34namespace API {
35
38public:
40 TableColumnHelper(ITableWorkspace *tw, const std::string &name) : m_workspace(tw), m_name(name) {}
42 std::string m_name;
43};
44
47public:
49 TableConstColumnHelper(const ITableWorkspace *tw, const std::string &name) : m_workspace(tw), m_name(name) {}
51 std::string m_name;
52};
53
56public:
58 TableRowHelper(ITableWorkspace *tw, size_t row) : m_workspace(tw), m_row(row) {}
60 size_t m_row;
61};
62
101// =====================================================================================
102class MANTID_API_DLL ITableWorkspace : public API::Workspace {
103public:
106
108 ITableWorkspace_uptr clone() const { return std::unique_ptr<ITableWorkspace>(doClone()); }
109
111 ITableWorkspace_uptr cloneEmpty() const { return ITableWorkspace_uptr(doCloneEmpty()); }
112
113 ITableWorkspace_uptr cloneColumns(const std::vector<std::string> &colNames) const;
114
117 const std::string id() const override { return "ITableWorkspace"; }
118 const std::string toString() const override;
124 virtual Column_sptr addColumn(const std::string &type, const std::string &name) = 0;
126 virtual bool addColumns(const std::string &type, const std::string &name, size_t n);
131
133 virtual void removeColumn(const std::string &name) = 0;
134
136 virtual size_t columnCount() const = 0;
137
139 virtual Column_sptr getColumn(const std::string &name) = 0;
140
142 virtual Column_const_sptr getColumn(const std::string &name) const = 0;
143
145 virtual Column_sptr getColumn(size_t index) = 0;
146
149 virtual Column_const_sptr getColumn(size_t index) const = 0;
150
152 virtual std::vector<std::string> getColumnNames() const = 0;
153
155 virtual size_t rowCount() const = 0;
156
158 virtual void setRowCount(size_t count) = 0;
159
162 virtual size_t insertRow(size_t index) = 0;
163
165 virtual void removeRow(size_t index) = 0;
166
168 TableRowHelper appendRow();
169
173 virtual bool customSort() const { return false; }
174
176 virtual void sort(std::vector<std::pair<std::string, bool>> &criteria);
177
179 TableColumnHelper getVector(const std::string &name);
180
182 TableConstColumnHelper getVector(const std::string &name) const;
183 template <class T>
192 T &getRef(const std::string &name, size_t index) {
193 std::shared_ptr<Column> c = getColumn(name);
194 if (!c->isType<T>()) {
195 std::string str =
196 std::string("getRef: Type mismatch. ") + typeid(T).name() + " != " + c->get_type_info().name() + '\n';
197 throw std::runtime_error(str);
198 }
199 return *(static_cast<T *>(c->void_pointer(index)));
200 }
201
210 template <class T> T &cell(size_t row, size_t col) {
211 Column_sptr c = getColumn(col);
212 if (!c->isType<T>()) {
213 std::ostringstream ostr;
214 ostr << "cell: Type mismatch:\n" << typeid(T).name() << " != \n" << c->get_type_info().name() << '\n';
215 throw std::runtime_error(ostr.str());
216 }
217 if (row >= this->rowCount()) {
218 throw std::range_error("Table does not have row " + std::to_string(row));
219 }
220 return *(static_cast<T *>(c->void_pointer(row)));
221 }
222
231 int &Int(size_t row, size_t col) { return cell<int>(row, col); }
240 double &Double(size_t row, size_t col) { return cell<double>(row, col); }
249 Boolean &Bool(size_t row, size_t col) { return cell<Boolean>(row, col); }
258 std::string &String(size_t row, size_t col) { return cell<std::string>(row, col); }
259
264 TableRowHelper getRow(size_t row) { return TableRowHelper(this, row); }
268
270 virtual void find(size_t value, size_t &row, size_t col) = 0;
272 virtual void find(double value, size_t &row, size_t col) = 0;
274 virtual void find(float value, size_t &row, size_t col) = 0;
277 virtual void find(API::Boolean value, size_t &row, size_t col) = 0;
280 virtual void find(const std::string &value, size_t &row, size_t col) = 0;
283 virtual void find(const Mantid::Kernel::V3D &value, size_t &row, size_t col) = 0;
284
285 void modified();
286
287protected:
290
295 void resizeColumn(Column *c, size_t size) { c->resize(size); }
296
302 void insertInColumn(Column *c, size_t index) { c->insert(index); }
303
308 void removeFromColumn(Column *c, size_t index) { c->remove(index); }
309
310private:
311 ITableWorkspace *doClone() const override { return doCloneColumns(std::vector<std::string>()); }
312 ITableWorkspace *doCloneEmpty() const override = 0;
313 virtual ITableWorkspace *doCloneColumns(const std::vector<std::string> &colNames) const = 0;
314};
315
316// =====================================================================================
322template <class T> class ColumnVector {
323public:
325 ColumnVector(const TableColumnHelper &th) : m_column(th.m_workspace->getColumn(th.m_name)) {
326 if (!m_column->isType<T>()) {
327 std::stringstream mess;
328 mess << "Type mismatch when creating a ColumnVector<" << typeid(T).name() << ">.";
329 throw std::runtime_error(mess.str());
330 }
331 }
333 ColumnVector(Column_sptr column) : m_column(std::move(column)) {
334 if (!m_column->isType<T>()) {
335 std::stringstream mess;
336 mess << "Type mismatch when creating a ColumnVector<" << typeid(T).name() << ">.";
337 throw std::runtime_error(mess.str());
338 }
339 }
344 T &operator[](size_t i) { return m_column->cell<T>(i); }
346 size_t size() { return (m_column->size()); }
347
348private:
350};
351
357template <class T> class ConstColumnVector {
358public:
360 ConstColumnVector(const TableConstColumnHelper &th) : m_column(th.m_workspace->getColumn(th.m_name)) {
361 if (!m_column->isType<T>()) {
362 std::stringstream mess;
363 mess << "Type mismatch when creating a ColumnVector<" << typeid(T).name() << ">.";
364 throw std::runtime_error(mess.str());
365 }
366 }
369 if (!m_column->isType<T>()) {
370 std::stringstream mess;
371 mess << "Type mismatch when creating a ColumnVector<" << typeid(T).name() << ">.";
372 throw std::runtime_error(mess.str());
373 }
374 }
379 const T &operator[](size_t i) { return m_column->cell<T>(i); }
381 size_t size() { return m_column->size(); }
382
383private:
385};
386
387} // namespace API
388} // Namespace Mantid
389
390// Instantiated and exported by API/src/PropertyWithValue.cpp
391MANTID_API_EXTERN_TEMPLATE(Mantid::Kernel::PropertyWithValue<std::shared_ptr<Mantid::API::ITableWorkspace>>);
std::string name
Definition Run.cpp:60
double value
The value of the point.
Definition FitMW.cpp:51
MANTID_API_EXTERN_TEMPLATE(Mantid::Kernel::PropertyWithValue< std::shared_ptr< Mantid::API::ITableWorkspace > >)
std::map< DeltaEMode::Type, std::string > index
int count
counter
Definition Matrix.cpp:37
ColumnVector gives access to the column elements without alowing its resizing.
ColumnVector(Column_sptr column)
Construct directly from column.
ColumnVector(const TableColumnHelper &th)
Constructor.
T & operator[](size_t i)
Get the element.
size_t size()
Size of the vector.
Column_sptr m_column
Pointer to the underlying column.
Column is the base class for columns of TableWorkspace.
Definition Column.h:35
virtual void remove(size_t index)=0
Removes an item.
virtual void resize(size_t count)=0
Sets the new column size.
virtual void insert(size_t index)=0
Inserts an item.
ConstColumnVector gives const access to the column elements without alowing its resizing.
Column_const_sptr m_column
Pointer to the underlying column.
size_t size()
Size of the vector.
const T & operator[](size_t i)
Get the const reference to element.
ConstColumnVector(Column_const_sptr column)
Construct directly from column.
ConstColumnVector(const TableConstColumnHelper &th)
Constructor.
ITableWorkspace is an implementation of Workspace in which the data are organised in columns of same ...
virtual ITableWorkspace * doCloneColumns(const std::vector< std::string > &colNames) const =0
const std::string id() const override
Return the workspace typeID.
void removeFromColumn(Column *c, size_t index)
Remove an element from a column.
virtual void find(const Mantid::Kernel::V3D &value, size_t &row, size_t col)=0
find method to get the index of Mantid::Kernel::V3D cell value in a table workspace
virtual void removeColumn(const std::string &name)=0
Removes a column.
virtual API::LogManager_const_sptr getLogs() const =0
Get constant access to shared pointer containing workspace properties.
T & getRef(const std::string &name, size_t index)
Get a reference to a data element.
virtual void find(size_t value, size_t &row, size_t col)=0
find method to get the index of integer cell value in a table workspace
ITableWorkspace(const ITableWorkspace &)=default
Protected copy constructor. May be used by childs for cloning.
virtual Column_const_sptr getColumn(size_t index) const =0
Gets the shared pointer to a column by index - return none-modifyable column.
virtual bool customSort() const
Does this type of TableWorkspace need a custom sorting call (e.g.
virtual void find(const std::string &value, size_t &row, size_t col)=0
find method to get the index of cellstd::string value in a table workspace
ITableWorkspace * doCloneEmpty() const override=0
Virtual cloneEmpty method.
T & cell(size_t row, size_t col)
Get the reference to the element in row row and column col.
virtual API::LogManager_sptr logs()=0
Get access to shared pointer containing workspace properties.
virtual Column_const_sptr getColumn(const std::string &name) const =0
Gets the shared pointer to a column by name.
virtual void setRowCount(size_t count)=0
Resizes the workspace.
ITableWorkspace_uptr cloneEmpty() const
Returns a default-initialized clone of the workspace.
TableRowHelper getFirstRow()
Creates a TableRow object for the first row (row == 0).
virtual void find(double value, size_t &row, size_t col)=0
find method to get the index of double cell value in a table workspace
int & Int(size_t row, size_t col)
Get the reference to the element in row row and column col if its type is int.
virtual Column_sptr getColumn(const std::string &name)=0
Gets the shared pointer to a column by name.
std::string & String(size_t row, size_t col)
Get the reference to the element in row row and column col if its type is std::string.
double & Double(size_t row, size_t col)
Get the reference to the element in row row and column col if its type is double.
Boolean & Bool(size_t row, size_t col)
Get the reference to the element in row row and column col if its type is bool.
virtual void find(API::Boolean value, size_t &row, size_t col)=0
find method to get the index of API::Boolean value cell in a table workspace
virtual size_t insertRow(size_t index)=0
Inserts a row before row pointed to by index and fills it with default vales.
virtual void removeRow(size_t index)=0
Delets a row if it exists.
virtual Column_sptr addColumn(const std::string &type, const std::string &name)=0
Creates a new column.
ITableWorkspace_uptr clone() const
Returns a clone of the workspace.
void insertInColumn(Column *c, size_t index)
Insert a new element into a column.
TableRowHelper getRow(size_t row)
Creates a TableRow object for row row.
virtual size_t columnCount() const =0
Number of columns in the workspace.
virtual void find(float value, size_t &row, size_t col)=0
find method to get the index of float cell value in a table workspace
virtual Column_sptr getColumn(size_t index)=0
Gets the shared pointer to a column by index.
virtual std::vector< std::string > getColumnNames() const =0
Returns a vector of all column names.
virtual size_t rowCount() const =0
Number of rows in the workspace.
void resizeColumn(Column *c, size_t size)
Resize a column.
ITableWorkspace & operator=(const ITableWorkspace &)=delete
ITableWorkspace * doClone() const override
Virtual clone method. Not implemented to force implementation in children.
Helper class used to create ColumnVector.
ITableWorkspace * m_workspace
Pointer to the TableWorkspace.
std::string m_name
column namae
TableColumnHelper(ITableWorkspace *tw, const std::string &name)
Constructor.
Helper class used to create ConstColumnVector.
TableConstColumnHelper(const ITableWorkspace *tw, const std::string &name)
Constructor.
const ITableWorkspace * m_workspace
Pointer to the TableWorkspace.
Helper class used to create TableRow.
TableRowHelper(ITableWorkspace *tw, size_t row)
Constructor.
ITableWorkspace * m_workspace
Pointer to the TableWorkspace.
Base Workspace Abstract Class.
Definition Workspace.h:30
The concrete, templated class for properties.
Class for 3D vectors.
Definition V3D.h:35
std::shared_ptr< const LogManager > LogManager_const_sptr
shared pointer to the logManager base class (const version)
Definition LogManager.h:228
std::shared_ptr< Column > Column_sptr
Definition Column.h:229
std::shared_ptr< LogManager > LogManager_sptr
shared pointer to the logManager base class
Definition LogManager.h:226
std::unique_ptr< ITableWorkspace > ITableWorkspace_uptr
unique pointer to Mantid::API::ITableWorkspace
std::shared_ptr< const Column > Column_const_sptr
Definition Column.h:230
std::string toString(const T &value)
Convert values to strings.
Helper class which provides the Collimation Length for SANS instruments.
STL namespace.
std::string to_string(const wide_integer< Bits, Signed > &n)
As TableColumn stores its data in a std::vector bool type cannot be used in the same way as the other...
Definition Column.h:210