Cppgram  1.0.0
Easy and modern C++14 Telegram Bot API wrapper
command_handler_impl.hpp
1 #include "cppgram/command_handler.hpp"
2 #include "cppgram/commands/command.hpp"
3 #include "cppgram/types/update.hpp"
4 
7 
8 using cppgram::types::Update;
9 
11 
12 template <class T>
14  : owner( nullptr )
15 {
16 }
17 
18 template <class T>
20  : owner( bot )
21 {
22 }
23 
24 template <class T>
26  : owner( bot )
27  , commands( c.commands )
28 {
29 }
30 
31 template <class T>
33  : owner( c.owner )
34  , commands( c.commands )
35 {
36 }
37 
38 template <class T>
41 {
42  // There is no owner as two CommandHandler objects cannot share the same owner
43  owner = nullptr;
44  commands = c.commands;
45 
46  return *this;
47 }
48 
49 template <class T>
52 {
53  owner = std::move( c.owner );
54  commands = std::move( c.commands );
55 
56  return *this;
57 }
58 
59 template <class T>
60 void
62 {
63  commands[new_command->type].push_back( new_command );
64 }
65 
66 template <class T>
67 void
69 {
70  commands = c.commands;
71 }
72 
73 template <class T>
74 void
76 {
77  if ( !owner )
78  {
79  owner = bot;
80  }
81 }
82 
83 template <class T>
84 bool
85 CommandHandler<T>::processCommands( const Update &update )
86 {
87  // For each command (valid for the current update)
88  for ( Command<T> *c : commands[update.type] )
89  {
90  if ( c->isValid( update ) )
91  {
92  c->callClosure( *owner, update );
93  return true;
94  }
95  }
96 
97  return false;
98 }
const EUpdate type
type of the update for which the command will be triggered.
Definition: command.hpp:32
void addCommand(commands::Command< T > *new_command)
Add a bot command.
Definition: command_handler_impl.hpp:61
void init(T *bot)
Check if this object has an owner and set it to bot if not.
Definition: command_handler_impl.hpp:75
CommandHandler & operator=(const CommandHandler &c)
Assigment constructor.
Definition: command_handler_impl.hpp:40
Handle bot commands.
Definition: command_handler.hpp:26
void setCommands(const CommandHandler &c)
copy commands of another object of type CommandHandler
Definition: command_handler_impl.hpp:68
contains api methods, update handlers and listener.
Definition: basic_bot.hpp:199
Abstract class for bot commands.
Definition: command_handler.hpp:15
CommandHandler()
Empty object.
Definition: command_handler_impl.hpp:13