mavtables  0.2.1
MAVLink router and firewall.
Action.hpp
Go to the documentation of this file.
1 // MAVLink router and firewall.
2 // Copyright (C) 2018 Michael R. Shannon <mrshannon.aerospace@gmail.com>
3 //
4 // This program is free software; you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation; either version 2 of the License, or
7 // (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with this program. If not, see <http://www.gnu.org/licenses/>.
16 
17 
18 #ifndef ACTION_HPP_
19 #define ACTION_HPP_
20 
21 
22 #include <optional>
23 #include <ostream>
24 
25 
26 /** An action that is to be taken with a packet.
27  *
28  * This is used as a return value to determine what to do with a packet.
29  */
30 class Action
31 {
32  public:
33  /** Possible actions.
34  */
35  enum Option
36  {
37  ACCEPT, //!< The packet has been accepted, possibly with priority.
38  REJECT, //!< The packet has been rejected.
39  CONTINUE, //!< Continue evaluating rules.
40  DEFAULT //!< Use the default rule.
41  };
42  // Methods
43  /** Copy constructor.
44  *
45  * \param other Action to copy from.
46  */
47  Action(const Action &other) = default;
48  /** Move constructor.
49  *
50  * \param other Action to move from.
51  */
52  Action(Action &&other) = default;
53  Action::Option action() const;
54  void priority(int priority);
55  int priority() const;
56  /** Assignment operator.
57  *
58  * \param other Action to copy from.
59  */
60  Action &operator=(const Action &other) = default;
61  /** Assignment operator (by move semantics).
62  *
63  * \param other Action to move from.
64  */
65  Action &operator=(Action &&other) = default;
66 
67  static Action make_accept(std::optional<int> priority = {});
68  static Action make_reject();
69  static Action make_continue();
70  static Action make_default();
71 
72  private:
73  Action::Option action_;
74  // Note: The reason this is optional is because there is a difference
75  // between {} and 0. This is because a priority of {} can still
76  // be set to something other than 0 by a rule higher up the chain
77  // (see \ref Call or \ref GoTo) while if the priority has been set
78  // to 0 it should not be set again.
79  std::optional<int> priority_;
80  Action(Action::Option action, std::optional<int> priority = {});
81 };
82 
83 
84 bool operator==(const Action &lhs, const Action &rhs);
85 bool operator!=(const Action &lhs, const Action &rhs);
86 std::ostream &operator<<(std::ostream &os, const Action &action);
87 
88 
89 #endif // ACTION_HPP_
static Action make_reject()
Definition: Action.cpp:113
The packet has been accepted, possibly with priority.
Definition: Action.hpp:37
int priority() const
Definition: Action.cpp:80
Continue evaluating rules.
Definition: Action.hpp:39
static Action make_accept(std::optional< int > priority={})
Definition: Action.cpp:100
static Action make_continue()
Definition: Action.cpp:126
std::ostream & operator<<(std::ostream &os, const Action &action)
Definition: Action.cpp:188
Action & operator=(const Action &other)=default
bool operator==(const Action &lhs, const Action &rhs)
Definition: Action.cpp:154
static Action make_default()
Definition: Action.cpp:140
Action::Option action() const
Definition: Action.cpp:45
bool operator!=(const Action &lhs, const Action &rhs)
Definition: Action.cpp:168
Action(const Action &other)=default
Use the default rule.
Definition: Action.hpp:40
The packet has been rejected.
Definition: Action.hpp:38
Option
Definition: Action.hpp:35