libsaria: Use dynamic memory for library tracks
This allows me to create only one Track() instance for each song in the library. Before this, a Track() was being created and then copied when adding new songs to the library. I thought this was wasteful, so now each song is only created once.
This commit is contained in:
parent
ff4fcbf809
commit
6b7c3f3d50
|
@ -9,7 +9,7 @@ namespace libsaria
|
|||
namespace index
|
||||
{
|
||||
|
||||
void add_track(libsaria::Track &);
|
||||
void add_track(libsaria::Track *);
|
||||
void filter(string &);
|
||||
void refresh();
|
||||
bool is_visible(sid_t &);
|
||||
|
|
|
@ -38,6 +38,7 @@ namespace libsaria
|
|||
Track *get_info(sid_t &);
|
||||
void for_each_path(void (*)(struct PathInfo &));
|
||||
|
||||
void list_track(libsaria::Track *);
|
||||
unsigned int size();
|
||||
void filter(string &text);
|
||||
bool is_visible(sid_t &);
|
||||
|
|
|
@ -84,12 +84,12 @@ static void index_tag(sid_t &songid, const string &tag,
|
|||
namespace libsaria
|
||||
{
|
||||
|
||||
void index::add_track(libsaria::Track &track)
|
||||
void index::add_track(libsaria::Track *track)
|
||||
{
|
||||
sid_t songid = track.get_songid();
|
||||
index_tag(songid, track.get_artist(), &artist_index);
|
||||
index_tag(songid, track.get_album(), &album_index);
|
||||
index_tag(songid, track.get_title(), &title_index);
|
||||
sid_t songid = track->get_songid();
|
||||
index_tag(songid, track->get_artist(), &artist_index);
|
||||
index_tag(songid, track->get_album(), &album_index);
|
||||
index_tag(songid, track->get_title(), &title_index);
|
||||
}
|
||||
|
||||
void index::print_stats()
|
||||
|
|
|
@ -19,9 +19,9 @@ static bool compare_tracks(libsaria::Track *one, libsaria::Track *two)
|
|||
namespace libsaria
|
||||
{
|
||||
|
||||
void library::list_track(libsaria::Track &track)
|
||||
void library::list_track(libsaria::Track *track)
|
||||
{
|
||||
track_list.push_back(&track);
|
||||
track_list.push_back(track);
|
||||
}
|
||||
|
||||
void library::sort_list()
|
||||
|
|
|
@ -11,7 +11,6 @@ namespace libsaria
|
|||
namespace library
|
||||
{
|
||||
|
||||
void list_track(libsaria::Track &);
|
||||
void sort_list();
|
||||
|
||||
}; /* Namespace: library */
|
||||
|
|
|
@ -7,6 +7,7 @@
|
|||
void LibraryPath::load_file(InFile &in, string dir)
|
||||
{
|
||||
unsigned int size;
|
||||
libsaria::Track *track;
|
||||
|
||||
path = dir;
|
||||
in >> size;
|
||||
|
@ -14,18 +15,19 @@ void LibraryPath::load_file(InFile &in, string dir)
|
|||
println("Library path size: %d", size);
|
||||
|
||||
for (unsigned int i = 0; i < size; i++) {
|
||||
file_list.push_back(libsaria::Track(in));
|
||||
libsaria::index::add_track(file_list.back());
|
||||
libsaria::library::list_track(file_list.back());
|
||||
track = new libsaria::Track(in);
|
||||
file_list.push_back(track);
|
||||
libsaria::index::add_track(track);
|
||||
libsaria::library::list_track(track);
|
||||
}
|
||||
}
|
||||
|
||||
void LibraryPath::save(OutFile &out)
|
||||
{
|
||||
list<libsaria::Track>::iterator it;
|
||||
list<libsaria::Track *>::iterator it;
|
||||
|
||||
out << path << file_list.size() << "\n";
|
||||
for (it = file_list.begin(); it != file_list.end(); it++)
|
||||
it->save(out);
|
||||
(*it)->save(out);
|
||||
}
|
||||
|
||||
|
|
|
@ -15,6 +15,9 @@ LibraryPath::LibraryPath(string dir)
|
|||
|
||||
LibraryPath::~LibraryPath()
|
||||
{
|
||||
list<libsaria::Track *>::iterator it;
|
||||
for (it = file_list.begin(); it != file_list.end(); it++)
|
||||
delete *it;
|
||||
}
|
||||
|
||||
string LibraryPath::get_path()
|
||||
|
@ -32,9 +35,9 @@ void LibraryPath::get_info(void (*info_func)(struct libsaria::library::PathInfo
|
|||
|
||||
bool LibraryPath::has_id(sid_t id)
|
||||
{
|
||||
list<libsaria::Track>::iterator it;
|
||||
list<libsaria::Track *>::iterator it;
|
||||
for (it = file_list.begin(); it != file_list.end(); it++) {
|
||||
if (it->get_songid() == id)
|
||||
if ((*it)->get_songid() == id)
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
|
|
|
@ -12,7 +12,7 @@ class LibraryPath
|
|||
{
|
||||
private:
|
||||
string path;
|
||||
list<libsaria::Track> file_list;
|
||||
list<libsaria::Track *> file_list;
|
||||
bool has_id(sid_t);
|
||||
|
||||
public:
|
||||
|
@ -23,7 +23,7 @@ class LibraryPath
|
|||
void load_file(InFile &, string);
|
||||
string get_path();
|
||||
void get_info(void (*)(struct libsaria::library::PathInfo &));
|
||||
void insert_track(libsaria::Track &);
|
||||
void add_track(string &, sid_t &);
|
||||
void save(OutFile &);
|
||||
void update();
|
||||
};
|
||||
|
|
|
@ -52,17 +52,12 @@ static ScanTask *scandir(string dir, LibraryPath *lib_path, ScanTask *scan);
|
|||
|
||||
void ScanTask::run_task()
|
||||
{
|
||||
string filepath;
|
||||
sid_t songid;
|
||||
list<pair<string, sid_t> >::iterator it;
|
||||
SaveTask *save;
|
||||
|
||||
for (it = files.begin(); it != files.end(); it++) {
|
||||
filepath = (*it).first;
|
||||
songid = (*it).second;
|
||||
try {
|
||||
libsaria::Track track(filepath, songid);
|
||||
lib_path->insert_track(track);
|
||||
lib_path->add_track(it->first, it->second);
|
||||
} catch (string message) {
|
||||
println(message);
|
||||
}
|
||||
|
@ -131,12 +126,14 @@ void ReaddirTask::run_task()
|
|||
scan->queue();
|
||||
}
|
||||
|
||||
void LibraryPath::insert_track(libsaria::Track &track)
|
||||
void LibraryPath::add_track(string &filepath, sid_t &songid)
|
||||
{
|
||||
if (has_id(track.get_songid()) == false) {
|
||||
libsaria::Track *track;
|
||||
if (has_id(songid) == false) {
|
||||
track = new libsaria::Track(filepath, songid);
|
||||
file_list.push_back(track);
|
||||
libsaria::index::add_track(file_list.back());
|
||||
libsaria::library::list_track(file_list.back());
|
||||
libsaria::index::add_track(track);
|
||||
libsaria::library::list_track(track);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue