Cpp-Taskflow  2.1.0
error.hpp
1 #pragma once
2 
3 #include <iostream>
4 #include <sstream>
5 #include <exception>
6 #include <system_error>
7 
8 namespace tf {
9 
15 struct Error : public std::error_category {
16 
21  enum Code : int {
22  SUCCESS = 0,
23  FLOW_BUILDER,
24  EXECUTOR
25  };
26 
30  inline const char* name() const noexcept override final;
31 
35  inline static const std::error_category& get();
36 
40  inline std::string message(int) const override final;
41 };
42 
43 // Function: name
44 inline const char* Error::name() const noexcept {
45  return "Taskflow error";
46 }
47 
48 // Function: get
50  static Error instance;
51  return instance;
52 }
53 
54 // Function: message
55 inline std::string Error::message(int code) const {
56  switch(auto ec = static_cast<Error::Code>(code); ec) {
57  case SUCCESS:
58  return "success";
59  break;
60 
61  case FLOW_BUILDER:
62  return "flow builder error";
63  break;
64 
65  case EXECUTOR:
66  return "executor error";
67  break;
68 
69  default:
70  return "unknown";
71  break;
72  };
73 }
74 
75 // Function: make_error_code
76 // Argument dependent lookup.
77 inline std::error_code make_error_code(Error::Code e) {
78  return std::error_code(static_cast<int>(e), Error::get());
79 }
80 
81 } // end of namespace tf ----------------------------------------------------
82 
83 // Register for implicit conversion
84 namespace std {
85  template <>
86  struct is_error_code_enum<tf::Error::Code> : true_type {};
87 }
88 
89 // ----------------------------------------------------------------------------
90 
91 namespace tf {
92 
93 // Procedure: throw_se
94 // Throws the system error under a given error code.
95 template <typename... ArgsT>
96 void throw_se(const char* fname, const size_t line, Error::Code c, ArgsT&&... args) {
98  oss << "[" << fname << ":" << line << "] ";
99  (oss << ... << args);
100  throw std::system_error(c, oss.str());
101 }
102 
103 } // ------------------------------------------------------------------------
104 
105 #define TF_THROW(...) tf::throw_se(__FILE__, __LINE__, __VA_ARGS__);
106 
Code
Error code definition.
Definition: error.hpp:21
Definition: taskflow.hpp:6
static const std::error_category & get()
acquires the singleton instance of the taskflow error category
Definition: error.hpp:49
const char * name() const noexcept override final
returns the name of the taskflow error category
Definition: error.hpp:44
std::string message(int) const override final
query the human-readable string of each error code
Definition: error.hpp:55
The error category of taskflow.
Definition: error.hpp:15