Newer
Older
Roman Tolchenov
committed
#ifndef MANTID_API_ICOLUMN_H_
#define MANTID_API_ICOLUMN_H_
//----------------------------------------------------------------------
// Includes
//----------------------------------------------------------------------
#include "MantidAPI/DllConfig.h"
Peterson, Peter
committed
#include <boost/shared_ptr.hpp>
#include <string>
#include <typeinfo>
#include <vector>
Roman Tolchenov
committed
namespace API
Column is the base class for columns of TableWorkspace.
\author Roman Tolchenov
\date 31/10/2008
Copyright © 2007-8 ISIS Rutherford Appleton Laboratory & NScD Oak Ridge National Laboratory
This file is part of Mantid.
Mantid is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
Mantid is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
File change history is stored at: <https://github.com/mantidproject/mantid>.
Code Documentation is available at: <http://doxygen.mantidproject.org>
*/
class MANTID_API_DLL Column
Column() : m_type("int"), m_plotType(-1000), m_isReadOnly(true) {};
/// Virtual destructor
virtual ~Column() {}
Roman Tolchenov
committed
/// Name (caption) of the column.
Roman Tolchenov
committed
const std::string& name()const{return m_name;}
Roman Tolchenov
committed
Roman Tolchenov
committed
const std::string& type()const{return m_type;}
virtual double operator[](size_t i)const{UNUSED_ARG(i); return std::numeric_limits<double>::quiet_NaN();}
/// Renames the column.
void setName(const std::string& str){m_name = str;}
Roman Tolchenov
committed
/// Number of individual elements in the column.
virtual size_t size()const = 0;
Roman Tolchenov
committed
/// Returns typeid for the data in the column
Roman Tolchenov
committed
virtual const std::type_info& get_type_info()const = 0;
Roman Tolchenov
committed
/// Returns typeid for the pointer type to the data element in the column
Roman Tolchenov
committed
virtual const std::type_info& get_pointer_type_info()const = 0;
/// Returns column read-only flag
virtual bool getReadOnly() const
{
return m_isReadOnly;
}
/// Sets column read-only flag
void setReadOnly(bool isReadOnly)
{
m_isReadOnly = isReadOnly;
}
Janik Zikovsky
committed
/// Prints out the value to a stream
virtual void print(size_t index, std::ostream& s) const = 0;
Roman Tolchenov
committed
Janik Zikovsky
committed
/// Read in a string and set the value at the given index
virtual void read(size_t index, const std::string & text )
Janik Zikovsky
committed
{ UNUSED_ARG(text);UNUSED_ARG(index); }
Janik Zikovsky
committed
/// Specialized type check
virtual bool isBool()const = 0;
/// Must return overall memory size taken by the column.
virtual long int sizeOfData()const = 0;
/// Virtual constructor. Fully clone any column.
virtual Column* clone() const = 0;
/// Cast an element to double if possible
virtual double toDouble(size_t index)const = 0;
/// Assign an element from double if possible
virtual void fromDouble(size_t index, double value) = 0;
Roman Tolchenov
committed
/// Templated method for returning a value. No type checks are done.
template<class T>
T& cell(size_t index)
Roman Tolchenov
committed
{
return *static_cast<T*>(void_pointer(index));
}
Roman Tolchenov
committed
/// Templated method for returning a value (const version). No type checks are done.
template<class T>
const T& cell(size_t index)const
Roman Tolchenov
committed
{
Anders Markvardsen
committed
return *static_cast<const T*>(void_pointer(index));
Roman Tolchenov
committed
}
/// Type check.
template<class T>
bool isType()const
{
return get_type_info() == typeid(T);
}
/// get plot type
/// @return See description of setPlotType() for the interpretation of the returned int
int getPlotType() const
{
return m_plotType;
}
/// Set plot type where
void setPlotType(int t);
/**
* Fills a std vector with values from the column if the types are compatible.
* @param vec :: The vector to fill in.
*/
template<class T>
void numeric_fill(std::vector<T>& vec) const
{
vec.resize(size());
for(size_t i = 0; i < vec.size(); ++i)
{
vec[i] = static_cast<T>(toDouble(i));
}
}
protected:
/// Sets the new column size.
virtual void resize(size_t count) = 0;
virtual void insert(size_t index) = 0;
virtual void remove(size_t index) = 0;
virtual void* void_pointer(size_t index) = 0;
Anders Markvardsen
committed
/// Pointer to a data element
virtual const void* void_pointer(size_t index) const = 0;
Janik Zikovsky
committed
std::string m_name;///< name
std::string m_type;///< type
/// plot type where
/// None = 0 (means it has specifically been set to 'no plot type')
/// NotSet = -1000 (this is the default and means plot style has not been set)
/// X = 1, Y = 2, Z = 3, xErr = 4, yErr = 5, Label = 6
int m_plotType;
/// Column read-only flag
bool m_isReadOnly;
friend class ColumnFactoryImpl;
Roman Tolchenov
committed
friend class ITableWorkspace;
template<class T> friend class ColumnVector;
Peterson, Peter
committed
/** @class Boolean
As TableColumn stores its data in a std::vector bool type cannot be used
in the same way as the other types. Class Boolean is used instead.
*/
struct MANTID_API_DLL Boolean
Peterson, Peter
committed
{
/// Default constructor
Boolean():value(false){}
/// Conversion from bool
Boolean(bool b):value(b){}
/// Returns bool
operator bool(){return value;}
/// equal to operator
bool operator==(const Boolean& b)const
{return(this->value==b.value);
}
//
operator double(void)const{return double(this->value);}
Peterson, Peter
committed
bool value;///< boolean value
};
/// Printing Boolean to an output stream
MANTID_API_DLL std::ostream& operator<<(std::ostream& ,const API::Boolean& );
/// Redaing a Boolean from an input stream
MANTID_API_DLL std::istream& operator>>(std::istream& istr, API::Boolean&);
Peterson, Peter
committed
typedef boost::shared_ptr<Column> Column_sptr;
Anders Markvardsen
committed
typedef boost::shared_ptr<const Column> Column_const_sptr;
Roman Tolchenov
committed
} // namespace API
Roman Tolchenov
committed
#endif /*MANTID_API_ICOLUMN_H_*/