implement ProxySongList for handling general operations on lists
[ncmpcpp.git] / src / proxy_song_list.h
blob336442707df3a9ee7fb206affb75e5bd056e6652
1 /***************************************************************************
2 * Copyright (C) 2008-2012 by Andrzej Rybczak *
3 * electricityispower@gmail.com *
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 *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. *
19 ***************************************************************************/
21 #ifndef _PROXY_SONG_LIST
22 #define _PROXY_SONG_LIST
24 #include "menu.h"
25 #include "song.h"
27 class ProxySongList
29 struct Interface
31 virtual ~Interface() { }
33 virtual bool empty() = 0;
34 virtual size_t size() = 0;
35 virtual size_t choice() = 0;
36 virtual void highlight(size_t pos) = 0;
38 virtual bool isSelected(size_t pos) = 0;
39 virtual void setSelected(size_t pos, bool selected) = 0;
41 virtual MPD::Song *getSong(size_t pos) = 0;
42 virtual MPD::Song *currentSong() = 0;
45 template <typename T, typename F> struct Impl : Interface
47 typedef typename NC::Menu<T> Menu;
49 Impl(Menu &menu, F f) : m_menu(menu), m_song_getter(f) { }
50 virtual ~Impl() { }
52 virtual bool empty() { return m_menu.Empty(); }
53 virtual size_t size() { return m_menu.Size(); }
54 virtual size_t choice() { return m_menu.Choice(); }
55 virtual void highlight(size_t pos) { m_menu.Highlight(pos); }
57 virtual bool isSelected(size_t pos) {
58 return m_menu[pos].isSelected();
61 virtual void setSelected(size_t pos, bool selected) {
62 m_menu[pos].setSelected(selected);
65 virtual MPD::Song *getSong(size_t pos) {
66 return m_song_getter(m_menu[pos]);
69 virtual MPD::Song *currentSong() {
70 if (!m_menu.Empty())
71 return getSong(m_menu.Choice());
72 else
73 return 0;
76 private:
77 Menu &m_menu;
78 F m_song_getter;
81 std::shared_ptr<Interface> m_impl;
83 public:
84 template <typename T, typename F>
85 ProxySongList(typename NC::Menu<T> &menu, F f) : m_impl(new Impl<T, F>(menu, f)) { }
87 bool empty() { return m_impl->empty(); }
88 size_t size() { return m_impl->size(); }
89 size_t choice() { return m_impl->choice(); }
90 void highlight(size_t pos) { m_impl->highlight(pos); }
92 bool isSelected(size_t pos) { return m_impl->isSelected(pos); }
93 void setSelected(size_t pos, bool selected) { m_impl->setSelected(pos, selected); }
95 MPD::Song *getSong(size_t pos) { return m_impl->getSong(pos); }
96 MPD::Song *currentSong() { return m_impl->currentSong(); }
99 template <typename T, typename F>
100 std::shared_ptr<ProxySongList> mkProxySongList(typename NC::Menu<T> &menu, F f)
102 return std::make_shared<ProxySongList>(ProxySongList(menu, f));
105 inline std::shared_ptr<ProxySongList> nullProxySongList()
107 return std::shared_ptr<ProxySongList>();
110 #endif