Taskolib  1.3.3
Message.h
Go to the documentation of this file.
1 
23 // SPDX-License-Identifier: LGPL-2.1-or-later
24 
25 #ifndef TASKOLIB_MESSAGE_H_
26 #define TASKOLIB_MESSAGE_H_
27 
28 #include <array>
29 #include <memory>
30 #include <ostream>
31 #include <string>
32 
33 #include <gul14/escape.h>
34 
35 #include "taskolib/StepIndex.h"
36 #include "taskolib/time_types.h"
37 
38 namespace task {
39 
44 class Message
45 {
46 public:
48  enum class Type
49  {
50  output,
54  step_started,
55  step_stopped,
57  undefined
58  };
59 
60 private:
61  static constexpr std::array<char const*, static_cast<int>(Type::undefined) + 1>
63  {
64  "output",
65  "sequence_started",
66  "sequence_stopped",
67  "sequence_stopped_with_error",
68  "step_started",
69  "step_stopped",
70  "step_stopped_with_error",
71  "undefined"
72  };
73 
74 
75 public:
77  Message() = default;
78 
80  Message(Type type, std::string text, TimePoint timestamp,
81  OptionalStepIndex index)
82  : text_{ std::move(text) }
83  , timestamp_{ timestamp }
84  , type_{ type }
85  , index_{ index }
86  {}
87 
89  OptionalStepIndex get_index() const { return index_; }
90 
97  const std::string& get_text() const { return text_; }
98 
100  Type get_type() const noexcept { return type_; }
101 
103  TimePoint get_timestamp() const { return timestamp_; };
104 
106  Message& set_index(OptionalStepIndex index) { index_ = index; return *this; }
107 
109  Message& set_text(const std::string& text) { text_ = text; return *this; }
110 
112  Message& set_timestamp(TimePoint timestamp) { timestamp_ = timestamp; return *this; };
113 
115  Message& set_type(Type type) noexcept { type_ = type; return *this; }
116 
117  friend std::ostream& operator<<(std::ostream& stream, Type const& t) {
118  stream << Message::type_description_[static_cast<int>(t)];
119  return stream;
120  }
121 
122  friend std::ostream& operator<<(std::ostream& stream, Message const& mess) {
123  stream << "Message{ ";
124 
125  if (mess.index_.has_value())
126  stream << *(mess.index_) << ": ";
127 
128  stream
129  << mess.type_
130  << " \""
131  << gul14::escape(mess.text_)
132  << "\" "
133  << to_string(mess.timestamp_)
134  << " }\n";
135 
136  return stream;
137  };
138 
139 private:
140  std::string text_;
144 };
145 
146 } // namespace task
147 
148 #endif
Declaration of the StepIndex type.
A message carrying some text, a timestamp, and a type, to be transported with a message queue between...
Definition: Message.h:45
friend std::ostream & operator<<(std::ostream &stream, Type const &t)
Definition: Message.h:117
TimePoint get_timestamp() const
Return the timestamp.
Definition: Message.h:103
Message()=default
Construct an empty message.
Message & set_text(const std::string &text)
Set the message text.
Definition: Message.h:109
Type
The type of this message.
Definition: Message.h:49
@ undefined
marker for last type
@ sequence_started
a sequence has been started
@ step_stopped
a step inside a sequence has stopped regularly
@ output
a message string that was output by a step via print()
@ step_stopped_with_error
a step inside a sequence has been stopped because of an error
@ step_started
a step inside a sequence has been started
@ sequence_stopped_with_error
a sequence has been stopped because of an error
@ sequence_stopped
a sequence has stopped regularly
OptionalStepIndex index_
Definition: Message.h:143
std::string text_
Definition: Message.h:137
const std::string & get_text() const
Return the message text.
Definition: Message.h:97
TimePoint timestamp_
Definition: Message.h:141
Message & set_type(Type type) noexcept
Set the message type.
Definition: Message.h:115
static constexpr std::array< char const *, static_cast< int >Type::undefined)+1 > type_description_
Definition: Message.h:62
Type type_
Definition: Message.h:142
Message & set_index(OptionalStepIndex index)
Set the associated index.
Definition: Message.h:106
OptionalStepIndex get_index() const
Return the associated optional step index.
Definition: Message.h:89
Message & set_timestamp(TimePoint timestamp)
Set the timestamp.
Definition: Message.h:112
Message(Type type, std::string text, TimePoint timestamp, OptionalStepIndex index)
Construct an initialized message from the given parameters.
Definition: Message.h:80
Type get_type() const noexcept
Return the message type.
Definition: Message.h:100
friend std::ostream & operator<<(std::ostream &stream, Message const &mess)
Definition: Message.h:122
Definition: VariableName.h:154
Namespace task contains all Taskolib functions and classes.
Definition: CommChannel.h:33
std::string to_string(Step::Type type)
Return a lower-case name for a step type ("action", "if", "end").
Definition: Step.cc:287
gul14::optional< StepIndex > OptionalStepIndex
An optional step index (gul14::optional<StepIndex>).
Definition: StepIndex.h:37
std::chrono::time_point< Clock > TimePoint
Definition: time_types.h:34
Declaration of time-related types.