ncmpc  0.31
Queue.hxx
Go to the documentation of this file.
1 /* ncmpc (Ncurses MPD Client)
2  * (c) 2004-2018 The Music Player Daemon Project
3  * Project homepage: http://musicpd.org
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License along
16  * with this program; if not, write to the Free Software Foundation, Inc.,
17  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18  */
19 
20 #ifndef QUEUE_HXX
21 #define QUEUE_HXX
22 
23 #include "util/Compiler.h"
24 
25 #include <mpd/client.h>
26 
27 #include <vector>
28 #include <memory>
29 
30 #include <assert.h>
31 
32 struct SongDeleter {
33  void operator()(struct mpd_song *song) const {
34  mpd_song_free(song);
35  }
36 };
37 
38 struct MpdQueue {
39  /* queue version number (obtained from mpd_status) */
40  unsigned version = 0;
41 
42  using Vector = std::vector<std::unique_ptr<struct mpd_song, SongDeleter>>;
43 
44  /* the list */
46 
47  using size_type = Vector::size_type;
48 
49  size_type size() const {
50  return items.size();
51  }
52 
53  bool empty() const {
54  return items.empty();
55  }
56 
58  void clear();
59 
60  const struct mpd_song &operator[](size_type i) const {
61  assert(i < size());
62 
63  return *items[i];
64  }
65 
66  struct mpd_song &operator[](size_type i) {
67  assert(i < size());
68 
69  return *items[i];
70  }
71 
72  gcc_pure
73  const struct mpd_song *GetChecked(int i) const;
74 
75  void push_back(const struct mpd_song &song) {
76  items.emplace_back(mpd_song_dup(&song));
77  }
78 
79  void Replace(size_type i, const struct mpd_song &song) {
80  items[i].reset(mpd_song_dup(&song));
81  }
82 
84  items.erase(std::next(items.begin(), i));
85  }
86 
87  void Move(unsigned dest, unsigned src);
88 
96  gcc_pure
97  size_type FindByReference(const struct mpd_song &song) const;
98 
104  gcc_pure
105  int FindById(unsigned id) const;
106 
112  gcc_pure
113  int FindByUri(const char *uri) const;
114 
120  gcc_pure
121  int FindIdByUri(const char *uri) const {
122  int i = FindByUri(uri);
123  if (i >= 0)
124  i = mpd_song_get_id(items[i].get());
125  return i;
126  }
127 
128  gcc_pure
129  bool ContainsUri(const char *uri) const {
130  return FindByUri(uri) >= 0;
131  }
132 };
133 
134 #endif
std::vector< std::unique_ptr< struct mpd_song, SongDeleter > > Vector
Definition: Queue.hxx:42
size_type size() const
Definition: Queue.hxx:49
Vector items
Definition: Queue.hxx:45
void Replace(size_type i, const struct mpd_song &song)
Definition: Queue.hxx:79
struct mpd_song & operator[](size_type i)
Definition: Queue.hxx:66
gcc_pure int FindIdByUri(const char *uri) const
Definition: Queue.hxx:121
Definition: Queue.hxx:32
void operator()(struct mpd_song *song) const
Definition: Queue.hxx:33
const struct mpd_song & operator[](size_type i) const
Definition: Queue.hxx:60
Vector::size_type size_type
Definition: Queue.hxx:47
gcc_pure bool ContainsUri(const char *uri) const
Definition: Queue.hxx:129
void RemoveIndex(size_type i)
Definition: Queue.hxx:83
bool empty() const
Definition: Queue.hxx:53
void push_back(const struct mpd_song &song)
Definition: Queue.hxx:75
Definition: Queue.hxx:38