|
From: Markus R. <god...@us...> - 2009-11-12 12:49:59
|
Module: performous
Branch: master
Commit: c17639f9cbadd3c1503dcbdbe0d9784f243d2544
Author: Markus Raab <un...@ma...>
Date: Thu Nov 12 09:29:49 2009 +0100
a pointer to song is stored
(for more complete information, e.g. artist)
---
game/songitems.cc | 34 ++++++++++++++++++++++++++--------
game/songitems.hh | 26 +++++++++++++++++++++-----
2 files changed, 47 insertions(+), 13 deletions(-)
diff --git a/game/songitems.cc b/game/songitems.cc
index 99b4eba..d4d44a1 100644
--- a/game/songitems.cc
+++ b/game/songitems.cc
@@ -38,7 +38,7 @@ void SongItems::save(xmlpp::Element *songs) {
}
}
-void SongItems::addSongItem(std::string const& artist, std::string const& title, int id) {
+int SongItems::addSongItem(std::string const& artist, std::string const& title, int id) {
SongItem si;
if (id==-1) id = assign_id_internal();
si.id = id;
@@ -51,22 +51,40 @@ void SongItems::addSongItem(std::string const& artist, std::string const& title,
si.id = assign_id_internal();
m_songs.insert(si); // now do the insert with the fresh id
}
+ return si.id;
}
void SongItems::addSong(boost::shared_ptr<Song> song) {
- if (lookup(song) == -1) addSongItem(song->artist, song->title);
+ int id = lookup(song);
+ if (id == -1)
+ {
+ id = addSongItem(song->artist, song->title);
+ }
+
+ SongItem si;
+ si.id = id;
+ songs_t::iterator it = m_songs.find(si);
+ if (it == m_songs.end()) throw SongItemsException("Cant find song which was added just before");
+ // it->song.reset(song); // does not work, it is a read only structure...
+
+ // fill up the rest of the information
+ si.artist = it->artist;
+ si.title = it->title;
+ si.song = song;
+
+ m_songs.erase(it);
+ m_songs.insert(si);
}
-int SongItems::lookup(boost::shared_ptr<Song> song) {
- for (songs_t::iterator it = m_songs.begin(); it != m_songs.end(); ++it)
- {
+int SongItems::lookup(boost::shared_ptr<Song> song) const {
+ for (songs_t::const_iterator it = m_songs.begin(); it != m_songs.end(); ++it) {
if (song->collateByArtistOnly == it->artist && song->collateByTitleOnly == it->title) return it->id;
}
return -1;
}
-int SongItems::assign_id_internal() {
- songs_t::const_reverse_iterator it = m_songs.rbegin();
- if (it != m_songs.rend()) return it->id+1;
+int SongItems::assign_id_internal() const {
+ songs_t::const_iterator it = m_songs.begin();
+ if (it != m_songs.end()) return it->id+1;
else return 1; // empty set
}
diff --git a/game/songitems.hh b/game/songitems.hh
index 9b1f2c5..a71f100 100644
--- a/game/songitems.hh
+++ b/game/songitems.hh
@@ -21,11 +21,22 @@ struct SongItemsException: public std::runtime_error {
struct SongItem
{
- int id; // TODO use a PUID instead (LibOFA)
+ int id; ///< The unique id for every song
+ /** This data is stored separate because it is read in before
+ the song is added.
+ A short, but relatively non-ambiguous collate form is used.
+ */
std::string artist;
std::string title;
+ /** This shared pointer is stored to access all song
+ information available.
+ E.g. the full artist information can be accessed using
+ this pointer.
+ */
+ boost::shared_ptr<Song> song;
+
bool operator< (SongItem const& other) const
{
return id < other.id;
@@ -54,10 +65,15 @@ struct SongItems
There will be no check if artist and title already exist - if you
need that you want addSong().
*/
- void addSongItem(std::string const& artist, std::string const& title, int id = -1);
+ int addSongItem(std::string const& artist, std::string const& title, int id = -1);
/**Adds or Links an already existing song with an songitem.
+
The id will be assigned and artist and title will be filled in.
- If there is already a song with the same artist and title nothing will be done.
+ If there is already a song with the same artist and title the existing
+ will be used.
+
+ Afterwards the pointer to the song will be stored for entire available
+ song information.
lookup is used internally to achieve that.
*/
@@ -65,10 +81,10 @@ struct SongItems
/**Lookup a songid for a specific song.
@return -1 if no song found.*/
- int lookup(boost::shared_ptr<Song> song);
+ int lookup(boost::shared_ptr<Song> song) const;
private:
- int assign_id_internal();
+ int assign_id_internal() const;
typedef std::set<SongItem> songs_t;
songs_t m_songs;
|