From eb3a4caff766f3e9eaa8f934e811ef596b0e820f Mon Sep 17 00:00:00 2001 From: knodlseder <> Date: Fri, 5 Aug 2011 07:02:34 +0000 Subject: [PATCH] replace FITS table column() method by access operator[] --- include/GFitsTable.hpp | 24 +++++++++++++----------- src/fits/GFitsTable.cpp | 172 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++------ 2 files changed, 179 insertions(+), 17 deletions(-) diff --git a/include/GFitsTable.hpp b/include/GFitsTable.hpp index 29bd5a0..625a23e 100644 --- a/include/GFitsTable.hpp +++ b/include/GFitsTable.hpp @@ -59,22 +59,24 @@ public: virtual ~GFitsTable(void); // Operators - GFitsTable& operator= (const GFitsTable& table); + GFitsTable& operator=(const GFitsTable& table); + GFitsTableCol& operator[](const int& colnum); + const GFitsTableCol& operator[](const int& colnum) const; + GFitsTableCol& operator[](const std::string& colname); + const GFitsTableCol& operator[](const std::string& colname) const; // Pure virtual methods virtual GFitsTable* clone(void) const = 0; // Implemented Methods - void append_column(GFitsTableCol& column); - void insert_column(int colnum, GFitsTableCol& column); - void append_rows(const int& nrows); - void insert_rows(const int& rownum, const int& nrows); - void remove_rows(const int& rownum, const int& nrows); - GFitsTableCol* column(const std::string& colname); - GFitsTableCol* column(const int& colnum); - int nrows(void) const; - int ncols(void) const; - std::string print(void) const; + void append_column(GFitsTableCol& column); + void insert_column(int colnum, GFitsTableCol& column); + void append_rows(const int& nrows); + void insert_rows(const int& rownum, const int& nrows); + void remove_rows(const int& rownum, const int& nrows); + int nrows(void) const; + int ncols(void) const; + std::string print(void) const; protected: // Protected methods diff --git a/src/fits/GFitsTable.cpp b/src/fits/GFitsTable.cpp index 94f91fc..cac5447 100644 --- a/src/fits/GFitsTable.cpp +++ b/src/fits/GFitsTable.cpp @@ -50,12 +50,14 @@ #include "GFitsTableCDoubleCol.hpp" /* __ Method name definitions ____________________________________________ */ +#define G_ACCESS1 "GFitsTable::operator[](int&)" +#define G_ACCESS2 "GFitsTable::operator[](std::string&)" #define G_APPEND_COLUMN "GFitsTable::append_column(GFitsTableCol*)" #define G_INSERT_COLUMN "GFitsTable::insert_column(int, GFitsTableCol*)" #define G_INSERT_ROWS "GFitsTable::insert_rows(int&, int&)" #define G_REMOVE_ROWS "GFitsTable::remove_rows(int&, int&)" -#define G_COLUMN1 "GFitsTable::column(std::string&)" -#define G_COLUMN2 "GFitsTable::column(int&)" +//#define G_COLUMN1 "GFitsTable::column(std::string&)" +//#define G_COLUMN2 "GFitsTable::column(int&)" #define G_OPEN_DATA "GFitsTable::open_data(void*)" #define G_SAVE_DATA "GFitsTable::save_data()" #define G_GET_TFORM "GFitsTable::get_tform(int&)" @@ -149,9 +151,9 @@ GFitsTable::~GFitsTable(void) /***********************************************************************//** * @brief Assignment operator * - * @param[in] table Table which will be assigned + * @param[in] table Table. ***************************************************************************/ -GFitsTable& GFitsTable::operator= (const GFitsTable& table) +GFitsTable& GFitsTable::operator=(const GFitsTable& table) { // Execute only if object is not identical if (this != &table) { @@ -175,6 +177,162 @@ GFitsTable& GFitsTable::operator= (const GFitsTable& table) } +/***********************************************************************//** + * @brief Returns reference to table column + * + * @param[in] colnum Column number [0,...,ncols()-1]. + * + * @exception GException::fits_no_data + * No data found in table. + * @exception GException::out_of_range + * Column number is out of range. + ***************************************************************************/ +GFitsTableCol& GFitsTable::operator[](const int& colnum) +{ + // If there is no data then throw an exception + if (m_columns == NULL) { + throw GException::fits_no_data(G_ACCESS1, "No columns in table."); + } + + // Compile option: raise exception if column number is out of range + #if defined(G_RANGE_CHECK) + if (colnum < 0 || colnum >= m_cols) + throw GException::out_of_range(G_ACCESS1, colnum, 0, m_cols-1); + #endif + + // Get column pointer + GFitsTableCol* ptr = m_columns[colnum]; + if (ptr == NULL) { + throw GException::fits_no_data(G_ACCESS1, "No data for this column."); + } + + // Return reference + return *ptr; +} + + +/***********************************************************************//** + * @brief Returns reference to table column (const version) + * + * @param[in] colnum Column number [0,...,ncols()-1]. + * + * @exception GException::fits_no_data + * No data found in table. + * @exception GException::out_of_range + * Column number is out of range. + ***************************************************************************/ +const GFitsTableCol& GFitsTable::operator[](const int& colnum) const +{ + // If there is no data then throw an exception + if (m_columns == NULL) { + throw GException::fits_no_data(G_ACCESS1, "No columns in table."); + } + + // Compile option: raise exception if column number is out of range + #if defined(G_RANGE_CHECK) + if (colnum < 0 || colnum >= m_cols) + throw GException::out_of_range(G_ACCESS1, colnum, 0, m_cols-1); + #endif + + // Get column pointer + const GFitsTableCol* ptr = m_columns[colnum]; + if (ptr == NULL) { + throw GException::fits_no_data(G_ACCESS1, "No data for this column."); + } + + // Return reference + return *ptr; +} + + +/***********************************************************************//** + * @brief Returns reference to table column + * + * @param[in] colname Column name. + * + * @exception GException::fits_no_data + * No data found in table. + * @exception GException::fits_column_not_found + * Column name not found. + ***************************************************************************/ +GFitsTableCol& GFitsTable::operator[](const std::string& colname) +{ + // If there is no data then throw an exception + if (m_columns == NULL) { + throw GException::fits_no_data(G_ACCESS2, "No columns in table."); + } + + // Initialise pointer + GFitsTableCol* ptr = NULL; + + // If there are columns then search for the specified name + if (m_columns != NULL) { + for (int i = 0; i < m_cols; ++i) { + if (m_columns[i]->name() == colname) { + ptr = m_columns[i]; + if (ptr == NULL) { + throw GException::fits_no_data(G_ACCESS2, + "No data for this column"); + } + break; + } + } + } + + // If column has not been found throw an exception + if (ptr == NULL) { + throw GException::fits_column_not_found(G_ACCESS2, colname); + } + + // Return reference + return *ptr; +} + + +/***********************************************************************//** + * @brief Returns reference to table column (const version) + * + * @param[in] colname Column name. + * + * @exception GException::fits_no_data + * No data found in table. + * @exception GException::fits_column_not_found + * Column name not found. + ***************************************************************************/ +const GFitsTableCol& GFitsTable::operator[](const std::string& colname) const +{ + // If there is no data then throw an exception + if (m_columns == NULL) { + throw GException::fits_no_data(G_ACCESS2, "No columns in table."); + } + + // Initialise pointer + const GFitsTableCol* ptr = NULL; + + // If there are columns then search for the specified name + if (m_columns != NULL) { + for (int i = 0; i < m_cols; ++i) { + if (m_columns[i]->name() == colname) { + ptr = m_columns[i]; + if (ptr == NULL) { + throw GException::fits_no_data(G_ACCESS2, + "No data for this column"); + } + break; + } + } + } + + // If column has not been found throw an exception + if (ptr == NULL) { + throw GException::fits_column_not_found(G_ACCESS2, colname); + } + + // Return reference + return *ptr; +} + + /*========================================================================== = = = Public methods = @@ -399,6 +557,7 @@ void GFitsTable::remove_rows(const int& rownum, const int& nrows) * @exception GException::fits_column_not_found * Requested column has not been found in table. ***************************************************************************/ +/* GFitsTableCol* GFitsTable::column(const std::string& colname) { // If there is no data then throw an exception @@ -428,7 +587,7 @@ GFitsTableCol* GFitsTable::column(const std::string& colname) // Return column pointer return ptr; } - +*/ /***********************************************************************//** * @brief Return pointer to column with number @@ -441,6 +600,7 @@ GFitsTableCol* GFitsTable::column(const std::string& colname) * @exception GException::out_of_range * Requested column has not been found in table. ***************************************************************************/ +/* GFitsTableCol* GFitsTable::column(const int& colnum) { // If there is no data then throw an exception @@ -459,7 +619,7 @@ GFitsTableCol* GFitsTable::column(const int& colnum) // Return column pointer return ptr; } - +*/ /***********************************************************************//** * @brief Return number of rows in table -- libgit2 0.21.4