database: Implement insert() and size()
I don't have the DatabaseEntry base class yet, but I will soon! Signed-off-by: Anna Schumaker <schumaker.anna@gmail.com>
This commit is contained in:
parent
f7a3eec35c
commit
b3f5363445
|
@ -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 <class T>
|
||||
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 <class T>
|
||||
T &Database.operator[unsigned int index]
|
||||
T &Database : operator[unsigned int index]
|
||||
Return a reference to db[index]
|
||||
|
|
|
@ -6,13 +6,23 @@
|
|||
|
||||
#include <file.h>
|
||||
|
||||
class Database {
|
||||
private:
|
||||
File file;
|
||||
#include <vector>
|
||||
|
||||
public:
|
||||
Database(std::string);
|
||||
~Database();
|
||||
template <class T>
|
||||
class Database {
|
||||
private:
|
||||
std::vector<T> 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 */
|
||||
|
|
|
@ -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 <class T>
|
||||
Database<T> :: Database(std::string filepath)
|
||||
: _size(0), file(filepath, FILE_TYPE_DATA)
|
||||
{
|
||||
}
|
||||
|
||||
template <class T>
|
||||
Database<T> :: ~Database()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
template <class T>
|
||||
unsigned int Database<T> :: insert(T val)
|
||||
{
|
||||
db.push_back(val);
|
||||
_size++;
|
||||
return db.size() - 1;
|
||||
}
|
||||
|
||||
template <class T>
|
||||
const unsigned int &Database<T> :: size()
|
||||
{
|
||||
return _size;
|
||||
}
|
||||
|
||||
#endif /* OCARINA_DATABASE_HPP */
|
|
@ -3,11 +3,3 @@
|
|||
*/
|
||||
#include <database.h>
|
||||
|
||||
Database :: Database(std::string filepath)
|
||||
: file(filepath, FILE_TYPE_DATA)
|
||||
{
|
||||
}
|
||||
|
||||
Database :: ~Database()
|
||||
{
|
||||
}
|
||||
|
|
|
@ -2,9 +2,26 @@
|
|||
* Copyright 2013 (c) Anna Schumaker.
|
||||
*/
|
||||
#include <database.h>
|
||||
#include <print.h>
|
||||
|
||||
void test_insertion(Database<int> &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<int> db("test.db");
|
||||
|
||||
test_insertion(db);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue