Main MRPT website > C++ reference
MRPT logo

CSimpleDatabase.h

Go to the documentation of this file.
00001 /* +---------------------------------------------------------------------------+
00002    |          The Mobile Robot Programming Toolkit (MRPT) C++ library          |
00003    |                                                                           |
00004    |                   http://mrpt.sourceforge.net/                            |
00005    |                                                                           |
00006    |   Copyright (C) 2005-2011  University of Malaga                           |
00007    |                                                                           |
00008    |    This software was written by the Machine Perception and Intelligent    |
00009    |      Robotics Lab, University of Malaga (Spain).                          |
00010    |    Contact: Jose-Luis Blanco  <jlblanco@ctima.uma.es>                     |
00011    |                                                                           |
00012    |  This file is part of the MRPT project.                                   |
00013    |                                                                           |
00014    |     MRPT is free software: you can redistribute it and/or modify          |
00015    |     it under the terms of the GNU General Public License as published by  |
00016    |     the Free Software Foundation, either version 3 of the License, or     |
00017    |     (at your option) any later version.                                   |
00018    |                                                                           |
00019    |   MRPT is distributed in the hope that it will be useful,                 |
00020    |     but WITHOUT ANY WARRANTY; without even the implied warranty of        |
00021    |     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         |
00022    |     GNU General Public License for more details.                          |
00023    |                                                                           |
00024    |     You should have received a copy of the GNU General Public License     |
00025    |     along with MRPT.  If not, see <http://www.gnu.org/licenses/>.         |
00026    |                                                                           |
00027    +---------------------------------------------------------------------------+ */
00028 #ifndef CSimpleDatabase_H
00029 #define CSimpleDatabase_H
00030 
00031 #include <mrpt/utils/utils_defs.h>
00032 #include <mrpt/utils/CSerializable.h>
00033 
00034 namespace mrpt
00035 {
00036 namespace utils
00037 {
00038 
00039         // This must be added to any CSerializable derived class:
00040         DEFINE_SERIALIZABLE_PRE_CUSTOM_BASE( CSimpleDatabase, mrpt::utils::CSerializable )
00041         // This must be added to any CSerializable derived class:
00042         DEFINE_SERIALIZABLE_PRE_CUSTOM_BASE( CSimpleDatabaseTable, mrpt::utils::CSerializable )
00043 
00044 /**  This class implements the tables of databases.
00045  * \sa CSimpleDatabase
00046  */
00047 class BASE_IMPEXP CSimpleDatabaseTable : public mrpt::utils::CSerializable
00048 {
00049         // This must be added to any CSerializable derived class:
00050         DEFINE_SERIALIZABLE( CSimpleDatabaseTable )
00051 public:
00052         /** Default constructor
00053           */
00054         CSimpleDatabaseTable( );
00055 
00056         /** Destructor
00057           */
00058         virtual ~CSimpleDatabaseTable();
00059 
00060         /** Get the count of fields.
00061           */
00062         size_t fieldsCount() const;
00063 
00064         /** Append a new and empty record at the end of the table, and return the index of the newly added record.
00065           * \sa deleteRecord
00066           */
00067         size_t appendRecord();
00068 
00069         /** Add a new field to the table. The table is cleared in this operation.  */
00070         void  addField(const char *fieldName);
00071 
00072         /** Add a new field to the table. The table is cleared in this operation.  */
00073         void  addField(const std::string &fieldName) {
00074                 addField(fieldName.c_str());
00075         }
00076 
00077         /** Get the name of a field by its index
00078           * \exception std::exception On index out of bounds
00079           */
00080         std::string  getFieldName(size_t fieldIndex) const;
00081 
00082         /** Get the index for a given field name
00083           * \exception std::exception On field not found
00084           */
00085         size_t fieldIndex(const char *fieldName) const;
00086         
00087         /** Get the index for a given field name
00088           * \exception std::exception On field not found
00089           */
00090         size_t fieldIndex(const std::string &fieldName) const {
00091                 return fieldIndex(fieldName.c_str());
00092         }
00093 
00094         /** Get the records count in the table
00095           */
00096         size_t getRecordCount() const;
00097 
00098         /**  Returns the cell content of the record indicates by its index, and the field indicated in "field".
00099           * \exception std::exception On field or record not found
00100           */
00101         std::string  get(size_t recordIndex, std::string field) const;
00102 
00103         /**  Returns the cell content of the record indicates by its index, and the field indicated by its index.
00104           * \exception std::exception On field or record not found
00105           */
00106         std::string  get(size_t recordIndex, size_t fieldIndex) const;
00107 
00108         /**  Sets the cell content of the record indicates by its index, and the field indicated in "field".
00109           * \exception std::exception On field or record not found
00110           */
00111         void  set(size_t recordIndex, std::string field, std::string value);
00112 
00113         /**  Sets the cell content of the record indicates by its index, and the field indicated by its index.
00114           * \exception std::exception On field or record not found
00115           */
00116         void  set(size_t recordIndex, size_t fieldIndex, std::string value);
00117 
00118         /**  Executes a query in the table, returning the record index which a given field has a given value, case insensitive, or -1 if not found.
00119           */
00120         int  query(std::string field, std::string value) const;
00121 
00122         /** Delete the record at the given index \sa appendRecord */
00123         void deleteRecord(size_t recordIndex);
00124 
00125 private:
00126         vector_string                           field_names;    //!< Field names
00127         std::vector<vector_string>      data;   //!< Data for each cell
00128 
00129 }; // end of class definition
00130 
00131 /**  This class impements a very simple database system. A database is
00132   *   a collection of tables, each one being a CSimpleDatabaseTable object. Tables are
00133   *   a rectangular arrrangement of cells, organized as records of fields.
00134   *  There are XML export/import methods in saveAsXML, loadFromXML.
00135   *
00136   * \note This class is NOT safe for read/write access from different threads. If needed, use critical sections.
00137   *
00138   * \sa CSimpleDatabaseTable
00139   */
00140 class BASE_IMPEXP CSimpleDatabase  : public mrpt::utils::CSerializable
00141 {
00142         // This must be added to any CSerializable derived class:
00143         DEFINE_SERIALIZABLE( CSimpleDatabase )
00144 
00145 public:
00146         /** Default constructor
00147           */
00148         CSimpleDatabase( );
00149 
00150         /** Destructor
00151           */
00152         virtual ~CSimpleDatabase( );
00153 
00154         /** Clears the DB.
00155           */
00156         void  clear();
00157 
00158         /** Creates a new table in the DB, initially empty.
00159           */
00160         CSimpleDatabaseTablePtr  createTable(const std::string &name);
00161 
00162         /** Returns the table with the indicated name
00163           * \exception std::exception On table not found.
00164           */
00165         CSimpleDatabaseTablePtr getTable(const std::string &tableName);
00166 
00167         /** Deletes the given table.
00168           * \exception std::exception On table not found.
00169           */
00170         void dropTable(const std::string &tableName);
00171 
00172         /** Changes the name of a given table 
00173           * \exception std::exception On table not found or new name already existed.
00174           */
00175         void renameTable(
00176                 const std::string &tableName,
00177                 const std::string &newTableName );
00178 
00179         /** Returns the table by index.
00180           * \exception std::exception On index out of bounds
00181           */
00182         CSimpleDatabaseTablePtr getTable(size_t tableIndex);
00183 
00184         /** Returns the tables count in the DB.
00185           */
00186         size_t   tablesCount() const;
00187 
00188         /** Returns the tables names in the DB.
00189           * \exception std::exception On index out of bounds
00190           */
00191         std::string      tablesName(size_t tableIndex) const;
00192 
00193         /** Saves this database as a XML file.
00194           * \return false on any error, true if successful.
00195           * \sa loadFromXML
00196           */
00197         bool saveAsXML( const std::string &fileName ) const;
00198 
00199         /** Loads the content of this database from a a XML file.
00200           * \return false on any error, true if successful.
00201           * \sa saveAsXML
00202           */
00203         bool loadFromXML( const std::string &fileName );
00204 
00205 
00206 private:
00207 
00208         /** The tables of the DB indexed by their names: */
00209         typedef std::map<std::string, CSimpleDatabaseTablePtr>                                  TTableList;
00210         typedef std::map<std::string, CSimpleDatabaseTablePtr>::iterator                iterator;
00211         typedef std::map<std::string, CSimpleDatabaseTablePtr>::const_iterator  const_iterator;
00212 
00213         TTableList      m_tables;
00214 
00215 
00216 }; // end of class definition
00217 
00218 
00219 } // End of namespace
00220 } // End of namespace
00221 
00222 
00223 #endif



Page generated by Doxygen 1.7.3 for MRPT 0.9.4 SVN: at Sat Mar 26 06:16:28 UTC 2011