From b3f5363445548bee9ad1e2f84803b716e9d4ad3b Mon Sep 17 00:00:00 2001 From: Anna Schumaker Date: Sun, 11 Aug 2013 10:41:22 -0400 Subject: [PATCH] database: Implement insert() and size() I don't have the DatabaseEntry base class yet, but I will soon! Signed-off-by: Anna Schumaker --- design/database.txt | 20 +++++++++----------- include/database.h | 22 ++++++++++++++++------ include/database.hpp | 36 ++++++++++++++++++++++++++++++++++++ lib/database.cpp | 8 -------- tests/database/database.cpp | 19 ++++++++++++++++++- 5 files changed, 79 insertions(+), 26 deletions(-) create mode 100644 include/database.hpp diff --git a/design/database.txt b/design/database.txt index 01ffba57..cfd8cd19 100644 --- a/design/database.txt +++ b/design/database.txt @@ -6,7 +6,7 @@ database.cpp == Depends == -idle file +file Database: (lib/database.cpp) Ocarina 5.x created a different save file format for each type of @@ -64,27 +64,25 @@ Database: (lib/database.cpp) ... - API: - Database.Database(filename); + Database : Database(filename); Initializes database to use ~/.ocarina{-debug}/filename. - void Database.load(); - Reads data from file. Call after static initialization of - Ocarina to ensure idle tasks are configured so loading can - happen at a later time. + void Database : load(); + Reads data from file. - void Database.save(); + void Database : save(); Saves data to file. template - unsigned int Database.insert(T &); + unsigned int Database : insert(T &); Adds a new item to the db, returns the id of the item - void Database.delete(unsigned int index); + void Database : delete(unsigned int index); Mark db[index] as invalid (quick deletion) - unsigned Database.size(); + unsigned Database : size(); Returns number of valid rows in the database template - T &Database.operator[unsigned int index] + T &Database : operator[unsigned int index] Return a reference to db[index] diff --git a/include/database.h b/include/database.h index 2c351e4c..3b79c977 100644 --- a/include/database.h +++ b/include/database.h @@ -6,13 +6,23 @@ #include -class Database { - private: - File file; +#include - public: - Database(std::string); - ~Database(); +template +class Database { +private: + std::vector db; + unsigned int _size; + File file; + +public: + Database(std::string); + ~Database(); + + unsigned int insert(T); + const unsigned int &size(); }; +#include "database.hpp" + #endif /* OCARINA_DATABASE_H */ diff --git a/include/database.hpp b/include/database.hpp new file mode 100644 index 00000000..475236e5 --- /dev/null +++ b/include/database.hpp @@ -0,0 +1,36 @@ +/* + * Copyright 2013 (c) Anna Schumaker. + * + * DO NOT INCLUDE THIS FILE DIRECTLY. THIS IS A TEMPLATE DEFINITION FILE + * AND ONLY MEANT TO BE INCLUDED BY include/database.h! + */ +#ifndef OCARINA_DATABASE_HPP +#define OCARINA_DATABASE_HPP + +template +Database :: Database(std::string filepath) + : _size(0), file(filepath, FILE_TYPE_DATA) +{ +} + +template +Database :: ~Database() +{ + +} + +template +unsigned int Database :: insert(T val) +{ + db.push_back(val); + _size++; + return db.size() - 1; +} + +template +const unsigned int &Database :: size() +{ + return _size; +} + +#endif /* OCARINA_DATABASE_HPP */ diff --git a/lib/database.cpp b/lib/database.cpp index f0aa094a..9a4b06ec 100644 --- a/lib/database.cpp +++ b/lib/database.cpp @@ -3,11 +3,3 @@ */ #include -Database :: Database(std::string filepath) - : file(filepath, FILE_TYPE_DATA) -{ -} - -Database :: ~Database() -{ -} diff --git a/tests/database/database.cpp b/tests/database/database.cpp index 4903fd79..16c3e6d9 100644 --- a/tests/database/database.cpp +++ b/tests/database/database.cpp @@ -2,9 +2,26 @@ * Copyright 2013 (c) Anna Schumaker. */ #include +#include + +void test_insertion(Database &db) +{ + unsigned int id; + + print("\n"); + for (unsigned int i = 1; i <= 10; i++) { + id = db.insert(i); + print("db[%u] = %u\n", id, i); + } + + print("Database size: %u\n", db.size()); +} int main(int argc, char **argv) { - Database db("test.db"); + Database db("test.db"); + + test_insertion(db); + return 0; }