Main MRPT website > C++ reference for MRPT 1.9.9
test.cpp
Go to the documentation of this file.
1 /* +------------------------------------------------------------------------+
2  | Mobile Robot Programming Toolkit (MRPT) |
3  | http://www.mrpt.org/ |
4  | |
5  | Copyright (c) 2005-2018, 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 
11 #include <mrpt/io/CFileStream.h>
12 #include <mrpt/system/CTicTac.h>
14 #include <cstdio>
15 #include <iostream>
16 
17 // ------------------------------------------------------
18 // TestDB
19 // ------------------------------------------------------
20 void TestDB()
21 {
22  using namespace mrpt::db;
23  using namespace mrpt::io;
24  using namespace mrpt::serialization;
25 
26  CSimpleDatabase db, db2, db3;
28  size_t i;
29  mrpt::system::CTicTac tictac;
30 
31  table = db.createTable("table1");
32 
33  table->addField("name");
34  table->addField("value");
35 
36  i = table->appendRecord();
37  table->set(i, "name", "cell_11");
38  table->set(i, "value", "cell_12");
39 
40  i = table->appendRecord();
41  table->set(i, "name", "cell_21");
42  table->set(i, "value", "cell_22");
43 
44  i = table->appendRecord();
45  table->set(i, "name", "another cell");
46  table->set(
47  i, "value",
48  "And this is an\n example of a multi-line \n \t\t and formated "
49  "string.");
50 
51  // Save/load as binary:
52  // ------------------------------
53  tictac.Tic();
54  {
55  CFileStream f("test_db.bin", fomWrite);
56  archiveFrom(f) << db;
57  }
58  {
59  CFileStream f("test_db.bin", fomRead);
60  archiveFrom(f) >> db2;
61  }
62  double t_bin = tictac.Tac();
63 
64  printf(
65  "Read test: %u tables in DB. table[0]=%s, records=%u\n",
66  (unsigned int)db2.tablesCount(), db2.tablesName(0).c_str(),
67  (unsigned int)db2.getTable("table1")->getRecordCount());
68 
69  int query = db2.getTable("table1")->query("name", "cell_11");
70  printf("query=%i,", query);
71  printf(" '%s'\n", db2.getTable("table1")->get(query, "value").c_str());
72 
73  // Save/load as XML:
74  // ------------------------------
75  tictac.Tic();
76  db.saveAsXML("test_db.xml");
77  db3.loadFromXML("test_db.xml");
78  double t_xml = tictac.Tac();
79  db3.saveAsXML("test_db_out.xml");
80 
81  printf("Binary write+read: %f ms\n", t_bin * 1e3);
82  printf("XML write+read: %f ms\n", t_xml * 1e3);
83 }
84 
85 // ------------------------------------------------------
86 // MAIN
87 // ------------------------------------------------------
88 int main()
89 {
90  try
91  {
92  TestDB();
93 
94  return 0;
95  }
96  catch (std::exception& e)
97  {
98  std::cout << "MRPT exception caught: " << e.what() << std::endl;
99  return -1;
100  }
101  catch (...)
102  {
103  printf("Untyped exception!!");
104  return -1;
105  }
106 }
mrpt::io::CFileStream
This CStream derived class allow using a file as a read/write binary stream, creating it if the file ...
Definition: CFileStream.h:39
mrpt::io
Definition: img/CImage.h:22
mrpt::db::CSimpleDatabase::getTable
CSimpleDatabaseTable::Ptr getTable(const std::string &tableName)
Returns the table with the indicated name.
Definition: CSimpleDatabase.cpp:134
mrpt::system::CTicTac
A high-performance stopwatch, with typical resolution of nanoseconds.
Definition: system/CTicTac.h:19
TestDB
void TestDB()
Definition: vision_stereo_rectify/test.cpp:20
mrpt::db::CSimpleDatabase::saveAsXML
bool saveAsXML(const std::string &fileName) const
Saves this database as a XML file.
Definition: CSimpleDatabase.cpp:353
mrpt::db::CSimpleDatabase::tablesName
std::string tablesName(size_t tableIndex) const
Returns the tables names in the DB.
Definition: CSimpleDatabase.cpp:169
mrpt::system::CTicTac::Tac
double Tac() noexcept
Stops the stopwatch.
Definition: CTicTac.cpp:90
main
int main()
Definition: vision_stereo_rectify/test.cpp:78
CSimpleDatabase.h
mrpt::io::fomWrite
@ fomWrite
Definition: CFileStream.h:28
mrpt::db::CSimpleDatabaseTable::Ptr
std::shared_ptr< CSimpleDatabaseTable > Ptr
Definition: CSimpleDatabase.h:23
mrpt::db
Definition: CSimpleDatabase.h:16
mrpt::system::CTicTac::Tic
void Tic() noexcept
Starts the stopwatch.
Definition: CTicTac.cpp:79
mrpt::db::CSimpleDatabase::tablesCount
size_t tablesCount() const
Returns the tables count in the DB.
Definition: CSimpleDatabase.cpp:165
mrpt::serialization
Definition: aligned_serialization.h:14
mrpt::db::CSimpleDatabase::loadFromXML
bool loadFromXML(const std::string &fileName)
Loads the content of this database from a a XML file.
Definition: CSimpleDatabase.cpp:411
mrpt::serialization::archiveFrom
CArchiveStreamBase< STREAM > archiveFrom(STREAM &s)
Helper function to create a templatized wrapper CArchive object for a: MRPT's CStream,...
Definition: CArchive.h:561
table
GLenum GLsizei GLenum GLenum const GLvoid * table
Definition: glext.h:3532
CTicTac.h
mrpt::db::CSimpleDatabase
This class impements a very simple database system.
Definition: CSimpleDatabase.h:121
mrpt::io::fomRead
@ fomRead
Definition: CFileStream.h:27
CFileStream.h
CArchive.h
mrpt::db::CSimpleDatabase::createTable
CSimpleDatabaseTable::Ptr createTable(const std::string &name)
Creates a new table in the DB, initially empty.
Definition: CSimpleDatabase.cpp:184



Page generated by Doxygen 1.8.17 for MRPT 1.9.9 Git: ad3a9d8ae Tue May 1 23:10:22 2018 -0700 at miƩ 12 jul 2023 10:03:34 CEST