database: Make read() and write() functions for database entries
I had planned on using the stream operator for my database class but this created some crazy compiler error that I was having difficulty figuring out. I decided to take the easy route for now and instead create read() and write() functions that do exactly the same thing. Signed-off-by: Anna Schumaker <schumaker.anna@gmail.com>
This commit is contained in:
parent
a5891a3338
commit
13313482d4
|
@ -34,9 +34,8 @@ Database: (lib/database.cpp)
|
|||
private:
|
||||
bool valid;
|
||||
public:
|
||||
virtual File &operator>>(File &) = 0;
|
||||
virtual File &operator<<(File &) = 0;
|
||||
friend class Database;
|
||||
virtual void write(File &) = 0;
|
||||
virtual void read(File &) = 0;
|
||||
};
|
||||
|
||||
File << <CHILD_CLASS_DATA>
|
||||
|
|
|
@ -5,6 +5,7 @@
|
|||
#define OCARINA_DATABASE_H
|
||||
|
||||
#include <file.h>
|
||||
#include <print.h>
|
||||
|
||||
#include <vector>
|
||||
|
||||
|
@ -14,6 +15,8 @@ public:
|
|||
bool valid;
|
||||
|
||||
DatabaseEntry();
|
||||
virtual void write(File &) = 0;
|
||||
virtual void read(File &) = 0;
|
||||
};
|
||||
|
||||
|
||||
|
@ -27,6 +30,8 @@ private:
|
|||
public:
|
||||
Database(std::string);
|
||||
~Database();
|
||||
void save();
|
||||
void load();
|
||||
|
||||
unsigned int insert(T);
|
||||
void remove(unsigned int);
|
||||
|
|
|
@ -19,6 +19,41 @@ Database<T> :: ~Database()
|
|||
|
||||
}
|
||||
|
||||
template <class T>
|
||||
void Database<T> :: save()
|
||||
{
|
||||
file.open(OPEN_WRITE);
|
||||
|
||||
file << db.size() << std::endl;
|
||||
for (unsigned int i = 0; i < db.size(); i++) {
|
||||
file << db[i].valid << " ";
|
||||
if (db[i].valid == true)
|
||||
db[i].write(file);
|
||||
file << std::endl;
|
||||
}
|
||||
|
||||
file.close();
|
||||
}
|
||||
|
||||
template <class T>
|
||||
void Database<T> :: load()
|
||||
{
|
||||
unsigned int db_size;
|
||||
file.open(OPEN_READ);
|
||||
|
||||
file >> db_size;
|
||||
db.resize(db_size);
|
||||
for (unsigned int i = 0; i < db_size; i++) {
|
||||
file >> db[i].valid;
|
||||
if (db[i].valid == true) {
|
||||
db[i].read(file);
|
||||
_size++;
|
||||
}
|
||||
}
|
||||
|
||||
file.close();
|
||||
}
|
||||
|
||||
template <class T>
|
||||
unsigned int Database<T> :: insert(T val)
|
||||
{
|
||||
|
|
|
@ -7,15 +7,34 @@
|
|||
class DBTest : public DatabaseEntry {
|
||||
public:
|
||||
unsigned int value;
|
||||
|
||||
DBTest();
|
||||
DBTest(unsigned int);
|
||||
void write(File &);
|
||||
void read(File &);
|
||||
};
|
||||
|
||||
DBTest :: DBTest()
|
||||
{
|
||||
}
|
||||
|
||||
DBTest :: DBTest(unsigned int val)
|
||||
{
|
||||
value = val;
|
||||
valid = true;
|
||||
}
|
||||
|
||||
void DBTest :: write(File &file)
|
||||
{
|
||||
file << value;
|
||||
}
|
||||
|
||||
void DBTest :: read(File &file)
|
||||
{
|
||||
file >> value;
|
||||
}
|
||||
|
||||
|
||||
|
||||
void print_db(Database<DBTest> &db)
|
||||
{
|
||||
|
@ -30,7 +49,7 @@ void print_db(Database<DBTest> &db)
|
|||
|
||||
void test_insertion(Database<DBTest> &db)
|
||||
{
|
||||
for (unsigned int i = 1; i <= 1000; i++)
|
||||
for (unsigned int i = 1; i <= 100000; i++)
|
||||
db.insert(DBTest(i));
|
||||
print_db(db);
|
||||
}
|
||||
|
@ -44,7 +63,10 @@ void test_deletion(Database<DBTest> &db)
|
|||
print_db(db);
|
||||
}
|
||||
|
||||
int main(int argc, char **argv)
|
||||
/*
|
||||
* Test everything except reloading.
|
||||
*/
|
||||
void test_0()
|
||||
{
|
||||
Database<DBTest> db("test.db");
|
||||
|
||||
|
@ -52,5 +74,23 @@ int main(int argc, char **argv)
|
|||
test_deletion(db);
|
||||
test_deletion(db);
|
||||
|
||||
db.save();
|
||||
}
|
||||
|
||||
/*
|
||||
* Reload the database from disk.
|
||||
*/
|
||||
void test_1()
|
||||
{
|
||||
Database<DBTest> db("test.db");
|
||||
db.load();
|
||||
print_db(db);
|
||||
}
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
test_0();
|
||||
test_1();
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
|
File diff suppressed because it is too large
Load Diff
Loading…
Reference in New Issue