tgbot-cpp
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 
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
std::function< void(const CallbackQuery::Ptr)> CallbackQueryListener
std::shared_ptr< Message > Ptr
Definition: Message.h:52
std::shared_ptr< ChosenInlineResult > Ptr
void onInlineQuery(const InlineQueryListener &listener)
void onCallbackQuery(const CallbackQueryListener &listener)
std::function< void(const ChosenInlineResult::Ptr)> ChosenInlineResultListener
void onUnknownCommand(const MessageListener &listener)
void onCommand(const std::string &commandName, const MessageListener &listener)
std::function< void(const InlineQuery::Ptr)> InlineQueryListener
void onCommand(const std::initializer_list< std::string > &commandsList, const MessageListener &listener)
void onNonCommandMessage(const MessageListener &listener)
void onChosenInlineResult(const ChosenInlineResultListener &listener)
std::shared_ptr< InlineQuery > Ptr
Definition: InlineQuery.h:22
std::function< void(const Message::Ptr)> MessageListener