ocarina/include/database.h
Anna Schumaker 81f3ef458f database: Each DatabaseEntry should know its id
This will let me set up a Track class that has pointers to the
corresponding artist, album and genre information without needing to
know their IDs directly.  Having this information available means I
won't need to keep a "join struct" when doing lookups - instead I can
return a pointer to a Track class that already knows everything.

Signed-off-by: Anna Schumaker <schumaker.anna@gmail.com>
2014-04-06 19:57:06 -04:00

62 lines
1.0 KiB
C++

/*
* Copyright 2013 (c) Anna Schumaker.
*/
#ifndef OCARINA_DATABASE_H
#define OCARINA_DATABASE_H
#include <file.h>
#include <print.h>
#include <map>
#include <vector>
class DatabaseEntry {
public:
bool valid;
unsigned int id;
DatabaseEntry();
virtual const std::string primary_key() = 0;
virtual void write(File &) = 0;
virtual void read(File &) = 0;
};
template <class T>
class Database {
private:
std::vector<T> _db;
std::map<const std::string, unsigned int> _keys;
unsigned int _size;
bool _autosave;
File _file;
public:
typedef typename std::vector<T>::iterator iterator;
typedef typename std::vector<T>::const_iterator const_iterator;
Database(std::string, bool);
~Database();
void save();
void autosave();
void load();
unsigned int insert(T);
void remove(unsigned int);
unsigned int size();
unsigned int actual_size();
iterator begin();
iterator end();
iterator next(iterator &);
iterator at(unsigned int);
iterator find(const std::string &);
};
#include "database.hpp"
#endif /* OCARINA_DATABASE_H */