Music Hub ..
A session-wide music playback service
hashed_keyed_player_store.cpp
Go to the documentation of this file.
1/*
2 * Copyright © 2014 Canonical Ltd.
3 *
4 * This program is free software: you can redistribute it and/or modify it
5 * under the terms of the GNU Lesser General Public License version 3,
6 * as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU Lesser General Public License for more details.
12 *
13 * You should have received a copy of the GNU Lesser General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 *
16 * Authored by: Thomas Voß <thomas.voss@canonical.com>
17 */
18
20
21namespace media = core::ubuntu::media;
22
24{
25}
26
27const core::Property<std::shared_ptr<media::Player>>& media::HashedKeyedPlayerStore::current_player() const
28{
29 return prop_current_player;
30}
31
33{
34 std::lock_guard<std::recursive_mutex> lg{guard};
35 return map.count(key) > 0;
36}
37
38std::shared_ptr<media::Player> media::HashedKeyedPlayerStore::player_for_key(const media::Player::PlayerKey& key) const
39{
40 std::lock_guard<std::recursive_mutex> lg{guard};
41 auto it = map.find(key);
42
43 if (it == map.end()) throw std::out_of_range
44 {
45 "HashedKeyedPlayerStore::player_for_key: No player known for " + std::to_string(key)
46 };
47
48 return it->second;
49}
50
52{
53 std::lock_guard<std::recursive_mutex> lg{guard};
54 for (const auto& pair : map)
55 enumerator(pair.first, pair.second);
56}
57
58void media::HashedKeyedPlayerStore::add_player_for_key(const media::Player::PlayerKey& key, const std::shared_ptr<media::Player>& player)
59{
60 std::lock_guard<std::recursive_mutex> lg{guard};
61 map[key] = player;
62}
63
65{
66 std::lock_guard<std::recursive_mutex> lg{guard};
67
68 auto it = map.find(key);
69 if (it != map.end())
70 {
71 if (prop_current_player == it->second)
72 prop_current_player = nullptr;
73
74 map.erase(it);
75 }
76}
77
79{
80 std::lock_guard<std::recursive_mutex> lg{guard};
81 return map.size();
82}
83
85{
86 prop_current_player = player_for_key(key);
87}
void enumerate_players(const PlayerEnumerator &enumerator) const override
void remove_player_for_key(const Player::PlayerKey &key) override
std::shared_ptr< Player > player_for_key(const Player::PlayerKey &key) const override
bool has_player_for_key(const Player::PlayerKey &key) const override
void add_player_for_key(const Player::PlayerKey &key, const std::shared_ptr< Player > &player) override
const core::Property< std::shared_ptr< media::Player > > & current_player() const override
void set_current_player_for_key(const Player::PlayerKey &key) override
std::function< void(const Player::PlayerKey &, const std::shared_ptr< Player > &) > PlayerEnumerator