2013-07-06 11:42:13 -04:00
|
|
|
== Files ==
|
|
|
|
ocarina/include/
|
|
|
|
database.h
|
|
|
|
database.hpp
|
|
|
|
ocarina/lib/
|
|
|
|
database.cpp
|
|
|
|
|
|
|
|
== Depends ==
|
|
|
|
idle file
|
|
|
|
|
|
|
|
Database: (lib/database.cpp)
|
|
|
|
Ocarina 5.x created a different save file format for each type of
|
|
|
|
data that needed to be stored (preferences, library paths, playlists).
|
|
|
|
I intend to unify everything into a generic file format that can be
|
|
|
|
accessed through a generic database interface. The database code will
|
|
|
|
be in charge of printing the "valid" bit for each DatabaseEntry so that
|
|
|
|
child classes do not need to call into the parent class. If valid ==
|
|
|
|
true, the DatabaseEntry will be streamed out followed by a newline. If
|
|
|
|
valid == false the database will print the next entry in the vector.
|
|
|
|
|
|
|
|
Modules should inherit from the DatabasEntry class and implement their
|
|
|
|
own read() and write() functions. The "valid" field will be stored
|
|
|
|
before these functions are called, and the entry will be skipped if
|
|
|
|
valid is set to false.
|
|
|
|
|
|
|
|
The Database class is a templated class, so code could potentially
|
|
|
|
get messy. Normal class declarations can still exist in the file
|
|
|
|
include/database.h and member functions can be written in the file
|
|
|
|
include/database.hpp, which will be included by database.h. Any
|
|
|
|
function not relying on a template can be written in lib/database.cpp.
|
|
|
|
|
|
|
|
- DatabaseEntry:
|
|
|
|
class DatabaseEntry { /* let database modify valid flag */
|
|
|
|
private:
|
|
|
|
bool valid;
|
|
|
|
public:
|
|
|
|
virtual File &operator>>(File &) = 0;
|
|
|
|
virtual File &operator<<(File &) = 0;
|
|
|
|
friend class Database;
|
|
|
|
};
|
|
|
|
|
|
|
|
File << <CHILD_CLASS_DATA>
|
|
|
|
|
|
|
|
- Database:
|
|
|
|
template <class T>
|
|
|
|
class Database {
|
|
|
|
private:
|
|
|
|
unsigned int _size; /* Number of valid rows */
|
|
|
|
File filename;
|
|
|
|
vector<T> db;
|
|
|
|
public:
|
|
|
|
Database::Database(filename);
|
|
|
|
void load();
|
|
|
|
void save();
|
|
|
|
unsigned int insert(T);
|
|
|
|
void delete(unsigned int);
|
|
|
|
const unsigned int &size();
|
2013-08-07 22:00:14 -04:00
|
|
|
T &operator[](unsigned int);
|
2013-07-06 11:42:13 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
File << db.size() << endl
|
|
|
|
File << INDEX_0 << db[INDEX_0].valid << db[INDEX_0] << endl;
|
|
|
|
File << Index_1 << db[INDEX_1].valid << db[INDEX_1] << endl;
|
|
|
|
...
|
|
|
|
|
|
|
|
- API:
|
|
|
|
Database.Database(filename);
|
|
|
|
Initializes database to use ~/.ocarina{-debug}/filename. Pass
|
|
|
|
an empty string if you do not want this database to be saved.
|
2013-08-07 22:00:14 -04:00
|
|
|
|
|
|
|
void Database.load();
|
2013-07-06 11:42:13 -04:00
|
|
|
Reads data from file. Call after static initialization of
|
|
|
|
Ocarina to ensure idle tasks are configured so loading can
|
|
|
|
happen at a later time.
|
2013-08-07 22:00:14 -04:00
|
|
|
|
|
|
|
void Database.save();
|
2013-07-06 11:42:13 -04:00
|
|
|
Saves data to file.
|
2013-08-07 22:00:14 -04:00
|
|
|
|
|
|
|
template <class T>
|
|
|
|
unsigned int Database.insert(T &);
|
2013-07-06 11:42:13 -04:00
|
|
|
Adds a new item to the db, returns the id of the item
|
2013-08-07 22:00:14 -04:00
|
|
|
|
|
|
|
void Database.delete(unsigned int index);
|
2013-07-06 11:42:13 -04:00
|
|
|
Mark db[index] as invalid (quick deletion)
|
2013-08-07 22:00:14 -04:00
|
|
|
|
|
|
|
unsigned Database.size();
|
2013-07-06 11:42:13 -04:00
|
|
|
Returns number of valid rows in the database
|
2013-08-07 22:00:14 -04:00
|
|
|
|
|
|
|
template <class T>
|
|
|
|
T &Database.operator[unsigned int index]
|
2013-07-06 11:42:13 -04:00
|
|
|
Return a reference to db[index]
|