2014-10-17 17:15:02 -04:00
|
|
|
/**
|
|
|
|
* @file
|
2013-08-10 23:38:57 -04:00
|
|
|
* Copyright 2013 (c) Anna Schumaker.
|
|
|
|
*/
|
2014-06-21 09:37:47 -04:00
|
|
|
#ifndef OCARINA_CORE_DATABASE_H
|
|
|
|
#define OCARINA_CORE_DATABASE_H
|
2013-08-10 23:38:57 -04:00
|
|
|
|
2014-06-05 10:19:22 -04:00
|
|
|
#include <core/file.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
|
|
|
|
2014-10-17 17:15:02 -04:00
|
|
|
/**
|
|
|
|
* Class representing a single item in a Database.
|
|
|
|
*/
|
2013-08-11 20:18:51 -04:00
|
|
|
class DatabaseEntry {
|
|
|
|
public:
|
2014-10-17 17:15:02 -04:00
|
|
|
/** The index of this item */
|
2014-03-16 18:32:20 -04:00
|
|
|
unsigned int id;
|
2013-08-11 20:18:51 -04:00
|
|
|
|
2014-10-17 17:15:02 -04:00
|
|
|
/**
|
|
|
|
* Constructor
|
|
|
|
*/
|
2013-08-11 20:18:51 -04:00
|
|
|
DatabaseEntry();
|
2014-10-17 17:15:02 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Destructor
|
|
|
|
*/
|
2014-03-25 10:57:09 -04:00
|
|
|
virtual ~DatabaseEntry() = 0;
|
2014-10-17 17:15:02 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Used to find the primary key of a database item.
|
|
|
|
* @return A uniqueue string identifying a single item in the database.
|
|
|
|
*/
|
2014-03-25 17:51:37 -04:00
|
|
|
virtual const std::string primary_key() const = 0;
|
2014-10-17 17:15:02 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Called to write a DatabaseItem to disk.
|
|
|
|
* @param file File to use when writing data.
|
|
|
|
*/
|
2013-08-14 21:09:11 -04:00
|
|
|
virtual void write(File &) = 0;
|
2014-10-17 17:15:02 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Called to read a DatabaseItem from disk.
|
|
|
|
* @param file File to use when reading data.
|
|
|
|
*/
|
2013-08-14 21:09:11 -04:00
|
|
|
virtual void read(File &) = 0;
|
2013-08-11 20:18:51 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2014-10-17 17:15:02 -04:00
|
|
|
/**
|
|
|
|
* Our custom database class.
|
|
|
|
*/
|
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-10-17 17:15:02 -04:00
|
|
|
/** Iterator access for our backing std::vector */
|
2014-03-25 10:57:09 -04:00
|
|
|
typedef typename std::vector<T *>::iterator iterator;
|
2014-10-17 17:15:02 -04:00
|
|
|
/** Const iterator access for our backing std::vector */
|
2014-03-25 10:57:09 -04:00
|
|
|
typedef typename std::vector<T *>::const_iterator const_iterator;
|
2014-03-09 14:58:06 -04:00
|
|
|
|
2014-10-17 17:15:02 -04:00
|
|
|
/**
|
|
|
|
* Database constructor.
|
|
|
|
* @param filepath File on disk that will be written to.
|
|
|
|
* @param autosave True if database should be saved on every modification.
|
|
|
|
*/
|
2014-03-09 14:58:06 -04:00
|
|
|
Database(std::string, bool);
|
2014-10-17 17:15:02 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Database destructor.
|
|
|
|
*/
|
2013-08-11 10:41:22 -04:00
|
|
|
~Database();
|
2014-10-17 17:15:02 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Called to write the database to disk.
|
|
|
|
*/
|
2013-08-14 21:09:11 -04:00
|
|
|
void save();
|
2014-10-17 17:15:02 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Called to save the database after modifications.
|
|
|
|
*/
|
2014-03-12 20:23:34 -04:00
|
|
|
void autosave();
|
2014-10-17 17:15:02 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Called to load the database from disk.
|
|
|
|
*/
|
2013-08-14 21:09:11 -04:00
|
|
|
void load();
|
2013-08-10 23:38:57 -04:00
|
|
|
|
2014-10-17 17:15:02 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Add a new item to the database.
|
|
|
|
* @param item The new item to be added.
|
|
|
|
* @return A pointer to the item in the database.
|
|
|
|
*/
|
2014-03-25 17:51:37 -04:00
|
|
|
T *insert(const T &);
|
2014-10-17 17:15:02 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove an item from the database.
|
|
|
|
* @param index The index of the item that should be removed.
|
|
|
|
*/
|
2013-08-11 21:09:11 -04:00
|
|
|
void remove(unsigned int);
|
2014-10-17 17:15:02 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Find the size of the database.
|
|
|
|
* @return The number of valid items in the database.
|
|
|
|
*/
|
2013-08-11 20:18:51 -04:00
|
|
|
unsigned int size();
|
2014-10-17 17:15:02 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Find the actual size of the backing vector.
|
|
|
|
* @return The number valid and invalid items in the database.
|
|
|
|
*/
|
2014-03-09 14:58:06 -04:00
|
|
|
unsigned int actual_size();
|
|
|
|
|
2014-10-17 17:15:02 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns an iterator pointing to the first entry in the database.
|
|
|
|
*/
|
2014-03-09 14:58:06 -04:00
|
|
|
iterator begin();
|
2014-10-17 17:15:02 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns an iterator pointing past the end of the database.
|
|
|
|
*/
|
2014-03-09 14:58:06 -04:00
|
|
|
iterator end();
|
2014-10-17 17:15:02 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns an iterator pointing to the next valid item in the database.
|
|
|
|
*/
|
2014-03-09 14:58:06 -04:00
|
|
|
iterator next(iterator &);
|
|
|
|
|
2014-10-17 17:15:02 -04:00
|
|
|
/**
|
|
|
|
* Returns the database item at index n.
|
|
|
|
* @param n The database index to access.
|
|
|
|
* @return A valid DatabaseItem or NULL.
|
|
|
|
*/
|
2014-03-24 19:51:13 -04:00
|
|
|
T *at(unsigned int);
|
2014-10-17 17:15:02 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Find a DatabaseItem with a specific primary key.
|
|
|
|
* @param key The key to search for.
|
|
|
|
* @return A valid DatabaseItem or NULL.
|
|
|
|
*/
|
2014-03-24 19:51:13 -04:00
|
|
|
T *find(const std::string &);
|
2013-08-10 23:38:57 -04:00
|
|
|
};
|
|
|
|
|
2013-08-11 10:41:22 -04:00
|
|
|
#include "database.hpp"
|
|
|
|
|
2014-06-21 09:37:47 -04:00
|
|
|
#endif /* OCARINA_CORE_DATABASE_H */
|