e281286291
I transferred information from my DESIGN file and worked it into Doxygen comments. Signed-off-by: Anna Schumaker <Anna@OcarinaProject.net>
170 lines
2.8 KiB
C++
170 lines
2.8 KiB
C++
/**
|
|
* @file
|
|
* 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, bool autosave)
|
|
: _size(0), _autosave(autosave), _file(filepath, 0)
|
|
{
|
|
}
|
|
|
|
template <class T>
|
|
Database<T> :: ~Database()
|
|
{
|
|
iterator it;
|
|
for (it = begin(); it != end(); it = next(it))
|
|
delete (*it);
|
|
}
|
|
|
|
template <class T>
|
|
void Database<T> :: save()
|
|
{
|
|
if (_file.open(OPEN_WRITE) == false)
|
|
return;
|
|
|
|
_file << actual_size() << std::endl;
|
|
for (unsigned int i = 0; i < _db.size(); i++) {
|
|
if (_db[i] == NULL)
|
|
_file << false;
|
|
else {
|
|
_file << true << " ";
|
|
_db[i]->write(_file);
|
|
}
|
|
_file << std::endl;
|
|
}
|
|
|
|
_file.close();
|
|
}
|
|
|
|
template <class T>
|
|
void Database<T> :: autosave()
|
|
{
|
|
if (_autosave == true)
|
|
save();
|
|
}
|
|
|
|
template <class T>
|
|
void Database<T> :: load()
|
|
{
|
|
unsigned int db_size;
|
|
bool valid;
|
|
|
|
if (_file.exists() == false)
|
|
return;
|
|
else if (_file.open(OPEN_READ) == false)
|
|
return;
|
|
|
|
_file >> db_size;
|
|
_db.resize(db_size);
|
|
for (unsigned int i = 0; i < db_size; i++) {
|
|
_file >> valid;
|
|
if (valid == false)
|
|
_db[i] = NULL;
|
|
else {
|
|
_db[i] = new T;
|
|
_db[i]->_index = i;
|
|
_db[i]->read(_file);
|
|
_keys[_db[i]->primary_key()] = i;
|
|
_size++;
|
|
}
|
|
}
|
|
|
|
_file.close();
|
|
}
|
|
|
|
template <class T>
|
|
T *Database<T> :: insert(const T &item)
|
|
{
|
|
T *t = find(item.primary_key());
|
|
|
|
if (t != NULL)
|
|
return NULL;
|
|
|
|
t = new T(item);
|
|
t->_index = actual_size();
|
|
_db.push_back(t);
|
|
|
|
_keys[t->primary_key()] = t->index();
|
|
_size++;
|
|
autosave();
|
|
return t;
|
|
}
|
|
|
|
template <class T>
|
|
void Database<T> :: remove(unsigned int index)
|
|
{
|
|
if (index >= actual_size())
|
|
return;
|
|
if (_db[index] == NULL)
|
|
return;
|
|
_keys.erase(_db[index]->primary_key());
|
|
delete _db[index];
|
|
_db[index] = NULL;
|
|
_size--;
|
|
autosave();
|
|
}
|
|
|
|
template <class T>
|
|
unsigned int Database<T> :: size()
|
|
{
|
|
return _size;
|
|
}
|
|
|
|
template <class T>
|
|
unsigned int Database<T> :: actual_size()
|
|
{
|
|
return _db.size();
|
|
}
|
|
|
|
template <class T>
|
|
typename Database<T>::iterator Database<T> :: begin()
|
|
{
|
|
if (size() == 0)
|
|
return end();
|
|
|
|
iterator it = _db.begin();
|
|
if ( (*it) != NULL )
|
|
return it;
|
|
return next(it);
|
|
}
|
|
|
|
template <class T>
|
|
typename Database<T>::iterator Database<T> :: end()
|
|
{
|
|
return _db.end();
|
|
}
|
|
|
|
template <class T>
|
|
typename Database<T>::iterator Database<T> :: next(iterator &it)
|
|
{
|
|
do {
|
|
it++;
|
|
} while ((it != end()) && (*it) == NULL);
|
|
return it;
|
|
}
|
|
|
|
template <class T>
|
|
T *Database<T> :: at(unsigned int index)
|
|
{
|
|
if (index >= actual_size())
|
|
return NULL;
|
|
return _db[index];
|
|
}
|
|
|
|
template <class T>
|
|
T *Database<T> :: find(const std::string &key)
|
|
{
|
|
std::map<const std::string, unsigned int>::iterator it;
|
|
it = _keys.find(key);
|
|
if (it == _keys.end())
|
|
return NULL;
|
|
return _db[it->second];
|
|
}
|
|
#endif /* OCARINA_DATABASE_HPP */
|