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