2013-08-10 23:38:57 -04:00
|
|
|
/*
|
|
|
|
* Copyright 2013 (c) Anna Schumaker.
|
|
|
|
*/
|
|
|
|
#ifndef OCARINA_DATABASE_H
|
|
|
|
#define OCARINA_DATABASE_H
|
|
|
|
|
|
|
|
#include <file.h>
|
2013-08-14 21:09:11 -04:00
|
|
|
#include <print.h>
|
2013-08-10 23:38:57 -04:00
|
|
|
|
2013-11-27 16:34:31 -05:00
|
|
|
#include <map>
|
2013-08-11 10:41:22 -04:00
|
|
|
#include <vector>
|
|
|
|
|
2013-08-11 20:18:51 -04:00
|
|
|
|
|
|
|
class DatabaseEntry {
|
|
|
|
public:
|
2014-03-16 18:32:20 -04:00
|
|
|
unsigned int id;
|
2013-08-11 20:18:51 -04:00
|
|
|
|
|
|
|
DatabaseEntry();
|
2014-03-25 10:57:09 -04:00
|
|
|
virtual ~DatabaseEntry() = 0;
|
2014-03-12 20:23:34 -04:00
|
|
|
virtual const std::string primary_key() = 0;
|
2013-08-14 21:09:11 -04:00
|
|
|
virtual void write(File &) = 0;
|
|
|
|
virtual void read(File &) = 0;
|
2013-08-11 20:18:51 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2013-12-01 22:31:35 -05:00
|
|
|
|
2013-12-30 18:18:23 -05:00
|
|
|
template <class T>
|
2013-08-10 23:38:57 -04:00
|
|
|
class Database {
|
2013-08-11 10:41:22 -04:00
|
|
|
private:
|
2014-03-25 10:57:09 -04:00
|
|
|
std::vector<T *> _db;
|
2014-03-09 14:58:06 -04:00
|
|
|
std::map<const std::string, unsigned int> _keys;
|
2013-08-11 10:41:22 -04:00
|
|
|
unsigned int _size;
|
2014-03-09 14:58:06 -04:00
|
|
|
bool _autosave;
|
|
|
|
File _file;
|
|
|
|
|
2013-08-11 10:41:22 -04:00
|
|
|
public:
|
2014-03-25 10:57:09 -04:00
|
|
|
typedef typename std::vector<T *>::iterator iterator;
|
|
|
|
typedef typename std::vector<T *>::const_iterator const_iterator;
|
2014-03-09 14:58:06 -04:00
|
|
|
|
|
|
|
Database(std::string, bool);
|
2013-08-11 10:41:22 -04:00
|
|
|
~Database();
|
2013-08-14 21:09:11 -04:00
|
|
|
void save();
|
2014-03-12 20:23:34 -04:00
|
|
|
void autosave();
|
2013-08-14 21:09:11 -04:00
|
|
|
void load();
|
2013-08-10 23:38:57 -04:00
|
|
|
|
2014-03-24 19:51:13 -04:00
|
|
|
T *insert(T);
|
2013-08-11 21:09:11 -04:00
|
|
|
void remove(unsigned int);
|
2013-08-11 20:18:51 -04:00
|
|
|
unsigned int size();
|
2014-03-09 14:58:06 -04:00
|
|
|
unsigned int actual_size();
|
|
|
|
|
|
|
|
iterator begin();
|
|
|
|
iterator end();
|
|
|
|
iterator next(iterator &);
|
|
|
|
|
2014-03-24 19:51:13 -04:00
|
|
|
T *at(unsigned int);
|
|
|
|
T *find(const std::string &);
|
2013-08-10 23:38:57 -04:00
|
|
|
};
|
|
|
|
|
2013-08-11 10:41:22 -04:00
|
|
|
#include "database.hpp"
|
|
|
|
|
2013-08-10 23:38:57 -04:00
|
|
|
#endif /* OCARINA_DATABASE_H */
|