From 2fca50e53286d31d28679565336139b337bc608c Mon Sep 17 00:00:00 2001 From: Oleg Morozenkov Date: Tue, 21 Mar 2017 19:08:13 +0300 Subject: Docs --- docs/_event_broadcaster_8h_source.html | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) (limited to 'docs/_event_broadcaster_8h_source.html') diff --git a/docs/_event_broadcaster_8h_source.html b/docs/_event_broadcaster_8h_source.html index 599cba2..dd16e50 100644 --- a/docs/_event_broadcaster_8h_source.html +++ b/docs/_event_broadcaster_8h_source.html @@ -83,7 +83,7 @@ $(document).ready(function(){initNavTree('_event_broadcaster_8h_source.html','')
EventBroadcaster.h
-Go to the documentation of this file.
1 /*
2  * Copyright (c) 2015 Oleg Morozenkov
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a copy
5  * of this software and associated documentation files (the "Software"), to deal
6  * in the Software without restriction, including without limitation the rights
7  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8  * copies of the Software, and to permit persons to whom the Software is
9  * furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in all
12  * copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20  * SOFTWARE.
21  */
22 
23 #ifndef TGBOT_EVENTBROADCASTER_H
24 #define TGBOT_EVENTBROADCASTER_H
25 
26 #include <string>
27 #include <functional>
28 #include <vector>
29 #include <map>
30 
31 #include "tgbot/types/Message.h"
35 
36 namespace TgBot {
37 
38 class EventHandler;
39 
45 
46 friend EventHandler;
47 
48 public:
49  typedef std::function<void (const Message::Ptr)> MessageListener;
50  typedef std::function<void (const InlineQuery::Ptr)> InlineQueryListener;
51  typedef std::function<void (const ChosenInlineResult::Ptr)> ChosenInlineResultListener;
52  typedef std::function<void (const CallbackQuery::Ptr)> CallbackQueryListener;
53 
58  inline void onAnyMessage(const MessageListener& listener) {
59  _onAnyMessageListeners.push_back(listener);
60  }
61 
67  inline void onCommand(const std::string& commandName, const MessageListener& listener) {
68  _onCommandListeners[commandName] = listener;
69  }
70 
75  inline void onUnknownCommand(const MessageListener& listener) {
76  _onUnknownCommandListeners.push_back(listener);
77  }
78 
83  inline void onNonCommandMessage(const MessageListener& listener) {
84  _onNonCommandMessageListeners.push_back(listener);
85  }
86 
91  inline void onInlineQuery(const InlineQueryListener& listener) {
92  _onInlineQueryListeners.push_back(listener);
93  }
94 
99  inline void onChosenInlineResult(const ChosenInlineResultListener& listener){
100  _onChosenInlineResultListeners.push_back(listener);
101  }
102 
103  inline void onCallbackQuery(const CallbackQueryListener& listener){
104  _onCallbackQueryListeners.push_back(listener);
105  }
106 
107 private:
108  template<typename ListenerType, typename ObjectType>
109  inline void broadcast(const std::vector<ListenerType>& listeners, const ObjectType object) const {
110  if (!object)
111  return;
112 
113  for (const ListenerType& item : listeners) {
114  item(object);
115  }
116  }
117 
118  inline void broadcastAnyMessage(const Message::Ptr message) const {
119  broadcast<MessageListener, Message::Ptr>(_onAnyMessageListeners, message);
120  }
121 
122  inline bool broadcastCommand(const std::string command, const Message::Ptr message) const {
123  std::map<std::string, MessageListener>::const_iterator iter = _onCommandListeners.find(command);
124  if (iter == _onCommandListeners.end()) {
125  return false;
126  }
127  iter->second(message);
128  return true;
129  }
130 
131  inline void broadcastUnknownCommand(const Message::Ptr message) const {
132  broadcast<MessageListener, Message::Ptr>(_onUnknownCommandListeners, message);
133  }
134 
135  inline void broadcastNonCommandMessage(const Message::Ptr message) const {
136  broadcast<MessageListener, Message::Ptr>(_onNonCommandMessageListeners, message);
137  }
138 
139  inline void broadcastInlineQuery(const InlineQuery::Ptr query) const {
140  broadcast<InlineQueryListener, InlineQuery::Ptr>(_onInlineQueryListeners, query);
141  }
142 
143  inline void broadcastChosenInlineResult(const ChosenInlineResult::Ptr result) const {
144  broadcast<ChosenInlineResultListener, ChosenInlineResult::Ptr>(_onChosenInlineResultListeners, result);
145  }
146 
147  inline void broadcastCallbackQuery(const CallbackQuery::Ptr result) const {
148  broadcast<CallbackQueryListener, CallbackQuery::Ptr>(_onCallbackQueryListeners, result);
149  }
150 
151  std::vector<MessageListener> _onAnyMessageListeners;
152  std::map<std::string, MessageListener> _onCommandListeners;
153  std::vector<MessageListener> _onUnknownCommandListeners;
154  std::vector<MessageListener> _onNonCommandMessageListeners;
155  std::vector<InlineQueryListener> _onInlineQueryListeners;
156  std::vector<ChosenInlineResultListener> _onChosenInlineResultListeners;
157  std::vector<CallbackQueryListener> _onCallbackQueryListeners;
158 };
159 
160 }
161 
162 #endif //TGBOT_EVENTBROADCASTER_H
void onAnyMessage(const MessageListener &listener)
+Go to the documentation of this file.
1 /*
2  * Copyright (c) 2015 Oleg Morozenkov
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a copy
5  * of this software and associated documentation files (the "Software"), to deal
6  * in the Software without restriction, including without limitation the rights
7  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8  * copies of the Software, and to permit persons to whom the Software is
9  * furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in all
12  * copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20  * SOFTWARE.
21  */
22 
23 #ifndef TGBOT_EVENTBROADCASTER_H
24 #define TGBOT_EVENTBROADCASTER_H
25 
26 #include <string>
27 #include <functional>
28 #include <vector>
29 #include <map>
30 
31 #include "tgbot/types/Message.h"
35 
36 namespace TgBot {
37 
38 class EventHandler;
39 
45 
46 friend EventHandler;
47 
48 public:
49  typedef std::function<void (const Message::Ptr)> MessageListener;
50  typedef std::function<void (const InlineQuery::Ptr)> InlineQueryListener;
51  typedef std::function<void (const ChosenInlineResult::Ptr)> ChosenInlineResultListener;
52  typedef std::function<void (const CallbackQuery::Ptr)> CallbackQueryListener;
53 
58  inline void onAnyMessage(const MessageListener& listener) {
59  _onAnyMessageListeners.push_back(listener);
60  }
61 
67  inline void onCommand(const std::string& commandName, const MessageListener& listener) {
68  _onCommandListeners[commandName] = listener;
69  }
70 
76  inline void onCommand(const std::initializer_list<std::string>& commandsList, const MessageListener& listener) {
77  for (const auto& command : commandsList)
78  {
79  _onCommandListeners[command] = listener;
80  }
81  }
82 
87  inline void onUnknownCommand(const MessageListener& listener) {
88  _onUnknownCommandListeners.push_back(listener);
89  }
90 
95  inline void onNonCommandMessage(const MessageListener& listener) {
96  _onNonCommandMessageListeners.push_back(listener);
97  }
98 
103  inline void onInlineQuery(const InlineQueryListener& listener) {
104  _onInlineQueryListeners.push_back(listener);
105  }
106 
111  inline void onChosenInlineResult(const ChosenInlineResultListener& listener){
112  _onChosenInlineResultListeners.push_back(listener);
113  }
114 
115  inline void onCallbackQuery(const CallbackQueryListener& listener){
116  _onCallbackQueryListeners.push_back(listener);
117  }
118 
119 private:
120  template<typename ListenerType, typename ObjectType>
121  inline void broadcast(const std::vector<ListenerType>& listeners, const ObjectType object) const {
122  if (!object)
123  return;
124 
125  for (const ListenerType& item : listeners) {
126  item(object);
127  }
128  }
129 
130  inline void broadcastAnyMessage(const Message::Ptr message) const {
131  broadcast<MessageListener, Message::Ptr>(_onAnyMessageListeners, message);
132  }
133 
134  inline bool broadcastCommand(const std::string command, const Message::Ptr message) const {
135  std::map<std::string, MessageListener>::const_iterator iter = _onCommandListeners.find(command);
136  if (iter == _onCommandListeners.end()) {
137  return false;
138  }
139  iter->second(message);
140  return true;
141  }
142 
143  inline void broadcastUnknownCommand(const Message::Ptr message) const {
144  broadcast<MessageListener, Message::Ptr>(_onUnknownCommandListeners, message);
145  }
146 
147  inline void broadcastNonCommandMessage(const Message::Ptr message) const {
148  broadcast<MessageListener, Message::Ptr>(_onNonCommandMessageListeners, message);
149  }
150 
151  inline void broadcastInlineQuery(const InlineQuery::Ptr query) const {
152  broadcast<InlineQueryListener, InlineQuery::Ptr>(_onInlineQueryListeners, query);
153  }
154 
155  inline void broadcastChosenInlineResult(const ChosenInlineResult::Ptr result) const {
156  broadcast<ChosenInlineResultListener, ChosenInlineResult::Ptr>(_onChosenInlineResultListeners, result);
157  }
158 
159  inline void broadcastCallbackQuery(const CallbackQuery::Ptr result) const {
160  broadcast<CallbackQueryListener, CallbackQuery::Ptr>(_onCallbackQueryListeners, result);
161  }
162 
163  std::vector<MessageListener> _onAnyMessageListeners;
164  std::map<std::string, MessageListener> _onCommandListeners;
165  std::vector<MessageListener> _onUnknownCommandListeners;
166  std::vector<MessageListener> _onNonCommandMessageListeners;
167  std::vector<InlineQueryListener> _onInlineQueryListeners;
168  std::vector<ChosenInlineResultListener> _onChosenInlineResultListeners;
169  std::vector<CallbackQueryListener> _onCallbackQueryListeners;
170 };
171 
172 }
173 
174 #endif //TGBOT_EVENTBROADCASTER_H
void onAnyMessage(const MessageListener &listener)
std::shared_ptr< CallbackQuery > Ptr
Definition: CallbackQuery.h:22
Definition: Api.h:44
@@ -93,15 +93,16 @@ $(document).ready(function(){initNavTree('_event_broadcaster_8h_source.html','')
std::shared_ptr< Message > Ptr
Definition: Message.h:52
std::shared_ptr< ChosenInlineResult > Ptr
-
void onInlineQuery(const InlineQueryListener &listener)
-
void onCallbackQuery(const CallbackQueryListener &listener)
+
void onInlineQuery(const InlineQueryListener &listener)
+
void onCallbackQuery(const CallbackQueryListener &listener)
std::function< void(const ChosenInlineResult::Ptr)> ChosenInlineResultListener
-
void onUnknownCommand(const MessageListener &listener)
+
void onUnknownCommand(const MessageListener &listener)
void onCommand(const std::string &commandName, const MessageListener &listener)
std::function< void(const InlineQuery::Ptr)> InlineQueryListener
-
void onNonCommandMessage(const MessageListener &listener)
+
void onCommand(const std::initializer_list< std::string > &commandsList, const MessageListener &listener)
+
void onNonCommandMessage(const MessageListener &listener)
-
void onChosenInlineResult(const ChosenInlineResultListener &listener)
+
void onChosenInlineResult(const ChosenInlineResultListener &listener)
std::shared_ptr< InlineQuery > Ptr
Definition: InlineQuery.h:22
std::function< void(const Message::Ptr)> MessageListener
@@ -111,7 +112,7 @@ $(document).ready(function(){initNavTree('_event_broadcaster_8h_source.html','')