Version: SMASH-1.5
actions.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2015-2018
3  * SMASH Team
4  *
5  * GNU General Public License (GPLv3 or later)
6  */
7 #ifndef SRC_INCLUDE_ACTIONS_H_
8 #define SRC_INCLUDE_ACTIONS_H_
9 
10 #include <algorithm>
11 #include <stdexcept>
12 #include <utility>
13 #include <vector>
14 
15 #include "action.h"
16 #include "forwarddeclarations.h"
17 
18 namespace smash {
19 
29 class Actions {
30  public:
32  Actions() {}
42  explicit Actions(ActionList&& action_list) : data_(std::move(action_list)) {
43  std::make_heap(data_.begin(), data_.end(), cmp);
44  }
45 
47  Actions(const Actions&) = delete;
49  Actions& operator=(const Actions&) = delete;
50 
52  bool is_empty() const { return data_.empty(); }
53 
59  ActionPtr pop() {
60  if (data_.empty()) {
61  throw std::runtime_error("Empty actions list!");
62  }
63  std::pop_heap(data_.begin(), data_.end(), cmp);
64  ActionPtr act = std::move(data_.back());
65  data_.pop_back();
66  return act;
67  }
68 
76  void insert(ActionList&& new_acts) {
77  for (auto& a : new_acts) {
78  insert(std::move(a));
79  }
80  }
81 
89  void insert(ActionPtr&& action) {
90  data_.push_back(std::move(action));
91  std::push_heap(data_.begin(), data_.end(), cmp);
92  }
93 
95  ActionList::size_type size() const { return data_.size(); }
96 
98  void clear() { data_.clear(); }
99 
101  std::vector<ActionPtr>::const_reverse_iterator begin() const {
102  return data_.crbegin();
103  }
104 
106  std::vector<ActionPtr>::const_reverse_iterator end() const {
107  return data_.crend();
108  }
109 
110  private:
119  static bool cmp(const ActionPtr& a, const ActionPtr& b) {
120  return a->time_of_execution() > b->time_of_execution();
121  }
122 
130  std::vector<ActionPtr> data_;
131 };
132 
133 } // namespace smash
134 
135 #endif // SRC_INCLUDE_ACTIONS_H_
Actions & operator=(const Actions &)=delete
Cannot be copied.
void insert(ActionPtr &&action)
Insert an action into this container.
Definition: actions.h:89
The Actions class abstracts the storage and manipulation of actions.
Definition: actions.h:29
void insert(ActionList &&new_acts)
Insert a list of actions into this object.
Definition: actions.h:76
STL namespace.
std::vector< ActionPtr >::const_reverse_iterator end() const
Definition: actions.h:106
Actions(ActionList &&action_list)
Creates a new Actions object from an ActionList.
Definition: actions.h:42
Actions()
Default constructor, creating an empty Actions object.
Definition: actions.h:32
std::vector< ActionPtr >::const_reverse_iterator begin() const
Definition: actions.h:101
ActionPtr pop()
Return the first action in the list and removes it from the list.
Definition: actions.h:59
void clear()
Delete all actions.
Definition: actions.h:98
bool is_empty() const
Definition: actions.h:52
static bool cmp(const ActionPtr &a, const ActionPtr &b)
Compare two action pointer such that the maximum is the most recent action.
Definition: actions.h:119
ActionList::size_type size() const
Definition: actions.h:95
std::vector< ActionPtr > data_
Dynamic data.
Definition: actions.h:130
Definition: action.h:24