Mir
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
prompt_session_container.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 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 General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program. If not, see <http://www.gnu.org/licenses/>.
15  *
16  * Authored By: Nick Dedekind <nick.dedekind@canonical.com>
17  */
18 
19 #ifndef MIR_SCENE_PROMPT_SESSION_CONTAINER_H_
20 #define MIR_SCENE_PROMPT_SESSION_CONTAINER_H_
21 
22 #include <sys/types.h>
23 #include <mutex>
24 #include <unordered_map>
25 
26 #include <boost/multi_index_container.hpp>
27 #include <boost/multi_index/member.hpp>
28 #include <boost/multi_index/ordered_index.hpp>
29 #include <boost/multi_index/composite_key.hpp>
30 #include <boost/multi_index/mem_fun.hpp>
31 
32 namespace mir
33 {
34 using boost::multi_index_container;
35 using namespace boost::multi_index;
36 
37 namespace scene
38 {
39 class Session;
40 class PromptSession;
41 
43 {
44 public:
46  virtual ~PromptSessionContainer() = default;
47 
48  enum class ParticipantType
49  {
50  helper,
51  application,
52  prompt_provider,
53  };
54 
55  void insert_prompt_session(std::shared_ptr<PromptSession> const& prompt_session);
56  void remove_prompt_session(std::shared_ptr<PromptSession> const& prompt_session);
57 
58  bool insert_participant(PromptSession* prompt_session, std::weak_ptr<Session> const& session, ParticipantType participant_type);
59  bool remove_participant(PromptSession* prompt_session, std::weak_ptr<Session> const& session, ParticipantType participant_type);
60 
61  void for_each_participant_in_prompt_session(PromptSession* prompt_session, std::function<void(std::weak_ptr<Session> const&, ParticipantType)> f) const;
62  void for_each_prompt_session_with_participant(std::weak_ptr<Session> const& participant, ParticipantType participant_type, std::function<void(std::shared_ptr<PromptSession> const&)> f) const;
63  void for_each_prompt_session_with_participant(std::weak_ptr<Session> const& participant, std::function<void(std::shared_ptr<PromptSession> const&, ParticipantType)> f) const;
64 
65 private:
66  std::mutex mutable mutex;
67 
68  std::unordered_map<PromptSession*, std::shared_ptr<PromptSession>> prompt_sessions;
69 
70  struct Participant
71  {
72  PromptSession* prompt_session;
73  std::weak_ptr<Session> session;
74  ParticipantType participant_type;
75  uint insert_order;
76  };
77 
85  typedef multi_index_container<
86  Participant,
87  indexed_by<
88  ordered_non_unique<
89  composite_key<
90  Participant,
91  member<Participant, PromptSession*, &Participant::prompt_session>,
92  member<Participant, uint, &Participant::insert_order>
93  >
94  >,
95  ordered_unique<
96  composite_key<
97  Participant,
98  member<Participant, std::weak_ptr<Session>, &Participant::session>,
99  member<Participant, ParticipantType, &Participant::participant_type>,
100  member<Participant, PromptSession*, &Participant::prompt_session>
101  >,
102  composite_key_compare<
103  std::owner_less<std::weak_ptr<Session>>,
104  std::less<ParticipantType>,
105  std::less<PromptSession*>>
106  >
107  >
108  > PromptSessionParticipants;
109 
110  typedef nth_index<PromptSessionParticipants,0>::type participant_by_prompt_session;
111  typedef nth_index<PromptSessionParticipants,1>::type participant_by_session;
112 
113  PromptSessionParticipants participant_map;
114  participant_by_prompt_session& prompt_session_index;
115  participant_by_session& participant_index;
116 };
117 
118 }
119 }
120 
121 #endif // MIR_SCENE_PROMPT_SESSION_CONTAINER_H_
All things Mir.
Definition: aging_buffer.h:24
Definition: prompt_session.h:30
void helper(const char *server)
Definition: prompt_session.c:140
ParticipantType
Definition: prompt_session_container.h:48
Definition: prompt_session_container.h:42

Copyright © 2012,2013 Canonical Ltd.
Generated on Fri Oct 10 14:07:14 UTC 2014