ANNA Suite  2020b
Multipurpose development suite for Telco applications
Runnable.hpp
Go to the documentation of this file.
1 // ANNA - Anna is Not Nothingness Anymore //
2 // //
3 // (c) Copyright 2005-2015 Eduardo Ramos Testillano & Francisco Ruiz Rayo //
4 // //
5 // See project site at http://redmine.teslayout.com/projects/anna-suite //
6 // See accompanying file LICENSE or copy at http://www.teslayout.com/projects/public/anna.LICENSE //
7 
8 
9 #ifndef anna_core_mt_Runnable_hpp
10 #define anna_core_mt_Runnable_hpp
11 
12 #include <string>
13 
14 #include <anna/core/functions.hpp>
15 
17 #include <anna/core/mt/Mutex.hpp>
18 
19 namespace anna {
20 
21 class Thread;
22 
27 class Runnable : public Mutex {
28 public:
29  // Accesores
34  const std::string& getId() const { return a_id; }
35 
42  bool hasRequestedStop() const { return a_requestedStop == true; }
43 
48  bool isRunning() const { return a_isRunning; }
49 
53  void requestStop() noexcept(false);
54 
59  virtual std::string asString() const
60  {
61  std::string result("anna::Runnable { Id: ");
62  result += a_id;
63  result += functions::asText(" | Running: ", a_isRunning);
64  result += functions::asText(" | RequestedStop: ", a_requestedStop);
65  return result += " }";
66  }
67 
68 protected:
72  Runnable() : a_id("<none>"), a_requestedStop(false), a_isRunning(false) {;}
73 
78  Runnable(const std::string& id) : a_id(id), a_requestedStop(false), a_isRunning(false) {;}
79 
84  Runnable(const char* id) : a_id(id), a_requestedStop(false), a_isRunning(false) {;}
85 
90  void setId(const std::string& id) { a_id = id; }
91 
97  void setIsRunning(const bool isRunning) { a_isRunning = isRunning; }
98 
103  virtual void initialize() noexcept(false) {;}
104 
109  virtual void run() noexcept(false);
110 
116  virtual void terminate() { a_requestedStop = false; }
117 
121  virtual void do_action() noexcept(false) = 0;
122 
123 private:
124  std::string a_id;
125  bool a_requestedStop;
126  bool a_isRunning;
127 
128  friend class Thread;
129 };
130 
131 #define anna_complete_runnable(Class) void do_action () {;}
132 
133 } //namespace anna
134 
135 #endif
136 
Definition: Thread.hpp:31
virtual void do_action() noexcept(false)=0
virtual void initialize() noexcept(false)
Definition: Runnable.hpp:103
const std::string & getId() const
Definition: Runnable.hpp:34
static std::string asText(const char *comment, const int number)
Definition: functions.hpp:153
void setIsRunning(const bool isRunning)
Definition: Runnable.hpp:97
virtual void run() noexcept(false)
Runnable(const char *id)
Definition: Runnable.hpp:84
virtual void terminate()
Definition: Runnable.hpp:116
Definition: Runnable.hpp:27
bool hasRequestedStop() const
Definition: Runnable.hpp:42
void setId(const std::string &id)
Definition: Runnable.hpp:90
void requestStop() noexcept(false)
Runnable()
Definition: Runnable.hpp:72
Definition: app.hpp:12
virtual std::string asString() const
Definition: Runnable.hpp:59
bool isRunning() const
Definition: Runnable.hpp:48
Runnable(const std::string &id)
Definition: Runnable.hpp:78
Definition: Mutex.hpp:41