Music Hub  ..
A session-wide music playback service
hashed_keyed_player_store.h
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 
19 #ifndef CORE_UBUNTU_MEDIA_HASHED_KEYED_PLAYER_STORE_H_
20 #define CORE_UBUNTU_MEDIA_HASHED_KEYED_PLAYER_STORE_H_
21 
23 
24 #include <mutex>
25 #include <unordered_map>
26 
27 namespace core
28 {
29 namespace ubuntu
30 {
31 namespace media
32 {
33 // Implements KeyedPlayerStore using a std::unordered_map.
35 {
36 public:
38  // We keep track of the "current" player, that is, the one
39  // that has been created most recently, or has been explicitly foregrounded, or has been enabled for
40  // background playback. We provide a getable/observable access to that designated instance.
41  const core::Property<std::shared_ptr<media::Player>>& current_player() const override;
42 
43  // We keep track of all known player sessions here and render them accessible via
44  // the key. All of these functions are thread-safe but not reentrant.
45  // Returns true iff a player is known for the given key.
46  bool has_player_for_key(const Player::PlayerKey& key) const override;
47 
48  // Returns the player for the given key or throws std::out_of_range if no player is known
49  // for the given key.
50  // Throws std::out_of_range if no player is known for the key.
51  std::shared_ptr<Player> player_for_key(const Player::PlayerKey& key) const override;
52 
53  // Enumerates all known players and invokes the given enumerator for each
54  // (key, player) pair.
55  void enumerate_players(const PlayerEnumerator& enumerator) const override;
56 
57  // Adds the given player with the given key.
58  void add_player_for_key(const Player::PlayerKey& key, const std::shared_ptr<Player>& player) override;
59 
60  // Removes the player for the given key, and unsets it if it is the current one.
61  void remove_player_for_key(const Player::PlayerKey& key) override;
62 
63  // Makes the player known under the given key current.
64  // Throws std::out_of_range if no player is known for the key.
65  void set_current_player_for_key(const Player::PlayerKey& key) override;
66 
67 private:
68  core::Property<std::shared_ptr<Player>> prop_current_player;
69  mutable std::mutex guard;
70  std::unordered_map<Player::PlayerKey, std::shared_ptr<Player>> map;
71 };
72 }
73 }
74 }
75 
76 #endif // CORE_UBUNTU_MEDIA_KEYED_PLAYER_STORE_H_
Definition: player.h:32
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