Main MRPT website > C++ reference for MRPT 1.4.0
CSimpleDatabase.h
Go to the documentation of this file.
1/* +---------------------------------------------------------------------------+
2 | Mobile Robot Programming Toolkit (MRPT) |
3 | http://www.mrpt.org/ |
4 | |
5 | Copyright (c) 2005-2016, Individual contributors, see AUTHORS file |
6 | See: http://www.mrpt.org/Authors - All rights reserved. |
7 | Released under BSD License. See details in http://www.mrpt.org/License |
8 +---------------------------------------------------------------------------+ */
9#ifndef CSimpleDatabase_H
10#define CSimpleDatabase_H
11
14#include <map>
15
16namespace mrpt
17{
18namespace utils
19{
20
21 // This must be added to any CSerializable derived class:
23 // This must be added to any CSerializable derived class:
25
26/** This class implements the tables of databases.
27 * \sa CSimpleDatabase \ingroup mrpt_base_grp
28 */
29class BASE_IMPEXP CSimpleDatabaseTable : public mrpt::utils::CSerializable
30{
31 // This must be added to any CSerializable derived class:
33public:
34 /** Default constructor
35 */
37
38 /** Destructor
39 */
41
42 /** Get the count of fields.
43 */
44 size_t fieldsCount() const;
45
46 /** Append a new and empty record at the end of the table, and return the index of the newly added record.
47 * \sa deleteRecord
48 */
49 size_t appendRecord();
50
51 /** Add a new field to the table. The table is cleared in this operation. */
52 void addField(const char *fieldName);
53
54 /** Add a new field to the table. The table is cleared in this operation. */
55 void addField(const std::string &fieldName) {
56 addField(fieldName.c_str());
57 }
58
59 /** Get the name of a field by its index
60 * \exception std::exception On index out of bounds
61 */
62 std::string getFieldName(size_t fieldIndex) const;
63
64 /** Get the index for a given field name
65 * \exception std::exception On field not found
66 */
67 size_t fieldIndex(const char *fieldName) const;
68
69 /** Get the index for a given field name
70 * \exception std::exception On field not found
71 */
72 size_t fieldIndex(const std::string &fieldName) const {
73 return fieldIndex(fieldName.c_str());
74 }
75
76 /** Get the records count in the table
77 */
78 size_t getRecordCount() const;
79
80 /** Returns the cell content of the record indicates by its index, and the field indicated in "field".
81 * \exception std::exception On field or record not found
82 */
83 std::string get(size_t recordIndex, std::string field) const;
84
85 /** Returns the cell content of the record indicates by its index, and the field indicated by its index.
86 * \exception std::exception On field or record not found
87 */
88 std::string get(size_t recordIndex, size_t fieldIndex) const;
89
90 /** Sets the cell content of the record indicates by its index, and the field indicated in "field".
91 * \exception std::exception On field or record not found
92 */
93 void set(size_t recordIndex, std::string field, std::string value);
94
95 /** Sets the cell content of the record indicates by its index, and the field indicated by its index.
96 * \exception std::exception On field or record not found
97 */
98 void set(size_t recordIndex, size_t fieldIndex, std::string value);
99
100 /** 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.
101 */
102 int query(std::string field, std::string value) const;
103
104 /** Delete the record at the given index \sa appendRecord */
105 void deleteRecord(size_t recordIndex);
106
107private:
108 vector_string field_names; //!< Field names
109 std::vector<vector_string> data; //!< Data for each cell
110
111}; // end of class definition
112
113/** This class impements a very simple database system. A database is
114 * a collection of tables, each one being a CSimpleDatabaseTable object. Tables are
115 * a rectangular arrrangement of cells, organized as records of fields.
116 * There are XML export/import methods in saveAsXML, loadFromXML.
117 *
118 * \note This class is NOT safe for read/write access from different threads. If needed, use critical sections.
119 *
120 * \sa CSimpleDatabaseTable
121 */
122class BASE_IMPEXP CSimpleDatabase : public mrpt::utils::CSerializable
123{
124 // This must be added to any CSerializable derived class:
126
127public:
128 /** Default constructor
129 */
131
132 /** Destructor
133 */
135
136 /** Clears the DB.
137 */
138 void clear();
139
140 /** Creates a new table in the DB, initially empty.
141 */
142 CSimpleDatabaseTablePtr createTable(const std::string &name);
143
144 /** Returns the table with the indicated name
145 * \exception std::exception On table not found.
146 */
147 CSimpleDatabaseTablePtr getTable(const std::string &tableName);
148
149 /** Deletes the given table.
150 * \exception std::exception On table not found.
151 */
152 void dropTable(const std::string &tableName);
153
154 /** Changes the name of a given table
155 * \exception std::exception On table not found or new name already existed.
156 */
158 const std::string &tableName,
159 const std::string &newTableName );
160
161 /** Returns the table by index.
162 * \exception std::exception On index out of bounds
163 */
165
166 /** Returns the tables count in the DB.
167 */
168 size_t tablesCount() const;
169
170 /** Returns the tables names in the DB.
171 * \exception std::exception On index out of bounds
172 */
173 std::string tablesName(size_t tableIndex) const;
174
175 /** Saves this database as a XML file.
176 * \return false on any error, true if successful.
177 * \sa loadFromXML
178 */
179 bool saveAsXML( const std::string &fileName ) const;
180
181 /** Loads the content of this database from a a XML file.
182 * \return false on any error, true if successful.
183 * \sa saveAsXML
184 */
185 bool loadFromXML( const std::string &fileName );
186
187
188private:
189
190 /** The tables of the DB indexed by their names: */
191 typedef std::map<std::string, CSimpleDatabaseTablePtr> TTableList;
192 typedef std::map<std::string, CSimpleDatabaseTablePtr>::iterator iterator;
193 typedef std::map<std::string, CSimpleDatabaseTablePtr>::const_iterator const_iterator;
194
196
197
198}; // end of class definition
199
200DEFINE_SERIALIZABLE_POST_CUSTOM_BASE( CSimpleDatabase, mrpt::utils::CSerializable )
202
203
204} // End of namespace
205} // End of namespace
206
207
208#endif
#define DEFINE_SERIALIZABLE(class_name)
This declaration must be inserted in all CSerializable classes definition, within the class declarati...
#define DEFINE_SERIALIZABLE_POST_CUSTOM_BASE(class_name, base_name)
#define DEFINE_SERIALIZABLE_PRE_CUSTOM_BASE(class_name, base_name)
This declaration must be inserted in all CSerializable classes definition, before the class declarati...
This class impements a very simple database system.
bool loadFromXML(const std::string &fileName)
Loads the content of this database from a a XML file.
void clear()
Clears the DB.
std::map< std::string, CSimpleDatabaseTablePtr >::const_iterator const_iterator
bool saveAsXML(const std::string &fileName) const
Saves this database as a XML file.
std::map< std::string, CSimpleDatabaseTablePtr >::iterator iterator
virtual ~CSimpleDatabase()
Destructor.
CSimpleDatabaseTablePtr getTable(size_t tableIndex)
Returns the table by index.
CSimpleDatabaseTablePtr createTable(const std::string &name)
Creates a new table in the DB, initially empty.
CSimpleDatabaseTablePtr getTable(const std::string &tableName)
Returns the table with the indicated name.
std::map< std::string, CSimpleDatabaseTablePtr > TTableList
The tables of the DB indexed by their names:
CSimpleDatabase()
Default constructor.
void renameTable(const std::string &tableName, const std::string &newTableName)
Changes the name of a given table.
std::string tablesName(size_t tableIndex) const
Returns the tables names in the DB.
size_t tablesCount() const
Returns the tables count in the DB.
void dropTable(const std::string &tableName)
Deletes the given table.
This class implements the tables of databases.
std::string get(size_t recordIndex, size_t fieldIndex) const
Returns the cell content of the record indicates by its index, and the field indicated by its index.
size_t fieldIndex(const char *fieldName) const
Get the index for a given field name.
CSimpleDatabaseTable()
Default constructor.
void addField(const std::string &fieldName)
Add a new field to the table.
std::vector< vector_string > data
Data for each cell.
vector_string field_names
Field names.
void set(size_t recordIndex, size_t fieldIndex, std::string value)
Sets the cell content of the record indicates by its index, and the field indicated by its index.
size_t appendRecord()
Append a new and empty record at the end of the table, and return the index of the newly added record...
std::string getFieldName(size_t fieldIndex) const
Get the name of a field by its index.
virtual ~CSimpleDatabaseTable()
Destructor.
size_t fieldIndex(const std::string &fieldName) const
Get the index for a given field name.
void deleteRecord(size_t recordIndex)
Delete the record at the given index.
size_t getRecordCount() const
Get the records count in the table.
void set(size_t recordIndex, std::string field, std::string value)
Sets the cell content of the record indicates by its index, and the field indicated in "field".
size_t fieldsCount() const
Get the count of fields.
int query(std::string field, std::string value) const
Executes a query in the table, returning the record index which a given field has a given value,...
void addField(const char *fieldName)
Add a new field to the table.
std::string get(size_t recordIndex, std::string field) const
Returns the cell content of the record indicates by its index, and the field indicated in "field".
std::vector< std::string > vector_string
A type for passing a vector of strings.
Definition: types_simple.h:30
struct BASE_IMPEXP CSimpleDatabaseTablePtr
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.



Page generated by Doxygen 1.9.5 for MRPT 1.4.0 SVN: at Sun Nov 27 02:56:26 UTC 2022