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 
21 namespace media = core::ubuntu::media;
22 
24 {
25 }
26 
27 const 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 
38 std::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 
58 void 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  auto it = map.find(key);
68  if (it != map.end())
69  {
70  if (prop_current_player == it->second)
71  prop_current_player = nullptr;
72 
73  map.erase(it);
74  }
75 }
76 
78 {
79  std::lock_guard<std::recursive_mutex> lg{guard};
80  return map.size();
81 }
82 
84 {
85  prop_current_player = player_for_key(key);
86 }
void set_current_player_for_key(const Player::PlayerKey &key) override
void enumerate_players(const PlayerEnumerator &enumerator) const override
std::function< void(const Player::PlayerKey &, const std::shared_ptr< Player > &) > PlayerEnumerator
bool has_player_for_key(const Player::PlayerKey &key) 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
const core::Property< std::shared_ptr< media::Player > > & current_player() const override
void add_player_for_key(const Player::PlayerKey &key, const std::shared_ptr< Player > &player) override