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:
|
private:
|
||||||
bool valid;
|
bool valid;
|
||||||
public:
|
public:
|
||||||
virtual File &operator>>(File &) = 0;
|
virtual void write(File &) = 0;
|
||||||
virtual File &operator<<(File &) = 0;
|
virtual void read(File &) = 0;
|
||||||
friend class Database;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
File << <CHILD_CLASS_DATA>
|
File << <CHILD_CLASS_DATA>
|
||||||
|
|
|
@ -5,6 +5,7 @@
|
||||||
#define OCARINA_DATABASE_H
|
#define OCARINA_DATABASE_H
|
||||||
|
|
||||||
#include <file.h>
|
#include <file.h>
|
||||||
|
#include <print.h>
|
||||||
|
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
|
@ -14,6 +15,8 @@ public:
|
||||||
bool valid;
|
bool valid;
|
||||||
|
|
||||||
DatabaseEntry();
|
DatabaseEntry();
|
||||||
|
virtual void write(File &) = 0;
|
||||||
|
virtual void read(File &) = 0;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
@ -27,6 +30,8 @@ private:
|
||||||
public:
|
public:
|
||||||
Database(std::string);
|
Database(std::string);
|
||||||
~Database();
|
~Database();
|
||||||
|
void save();
|
||||||
|
void load();
|
||||||
|
|
||||||
unsigned int insert(T);
|
unsigned int insert(T);
|
||||||
void remove(unsigned int);
|
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>
|
template <class T>
|
||||||
unsigned int Database<T> :: insert(T val)
|
unsigned int Database<T> :: insert(T val)
|
||||||
{
|
{
|
||||||
|
|
|
@ -7,15 +7,34 @@
|
||||||
class DBTest : public DatabaseEntry {
|
class DBTest : public DatabaseEntry {
|
||||||
public:
|
public:
|
||||||
unsigned int value;
|
unsigned int value;
|
||||||
|
|
||||||
|
DBTest();
|
||||||
DBTest(unsigned int);
|
DBTest(unsigned int);
|
||||||
|
void write(File &);
|
||||||
|
void read(File &);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
DBTest :: DBTest()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
DBTest :: DBTest(unsigned int val)
|
DBTest :: DBTest(unsigned int val)
|
||||||
{
|
{
|
||||||
value = val;
|
value = val;
|
||||||
valid = true;
|
valid = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void DBTest :: write(File &file)
|
||||||
|
{
|
||||||
|
file << value;
|
||||||
|
}
|
||||||
|
|
||||||
|
void DBTest :: read(File &file)
|
||||||
|
{
|
||||||
|
file >> value;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
void print_db(Database<DBTest> &db)
|
void print_db(Database<DBTest> &db)
|
||||||
{
|
{
|
||||||
|
@ -30,7 +49,7 @@ void print_db(Database<DBTest> &db)
|
||||||
|
|
||||||
void test_insertion(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));
|
db.insert(DBTest(i));
|
||||||
print_db(db);
|
print_db(db);
|
||||||
}
|
}
|
||||||
|
@ -44,7 +63,10 @@ void test_deletion(Database<DBTest> &db)
|
||||||
print_db(db);
|
print_db(db);
|
||||||
}
|
}
|
||||||
|
|
||||||
int main(int argc, char **argv)
|
/*
|
||||||
|
* Test everything except reloading.
|
||||||
|
*/
|
||||||
|
void test_0()
|
||||||
{
|
{
|
||||||
Database<DBTest> db("test.db");
|
Database<DBTest> db("test.db");
|
||||||
|
|
||||||
|
@ -52,5 +74,23 @@ int main(int argc, char **argv)
|
||||||
test_deletion(db);
|
test_deletion(db);
|
||||||
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;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
File diff suppressed because it is too large
Load Diff
Loading…
Reference in New Issue