Lodestar
An integrated real-time control package in C++
Status.hpp
1 //
2 // Created by Hamza El-Kebir on 6/17/21.
3 //
4 
5 #ifndef LODESTAR_STATUS_HPP
6 #define LODESTAR_STATUS_HPP
7 
8 #include <string>
9 
10 namespace ls {
11  namespace core {
12  enum class StatusCode : int {
13  Ok = 0,
14  UnknownError,
15  InternalError,
16  NetworkError,
17  InvalidAddress,
18  NotInitializedError,
19  ResourceBusyError,
20  SafetyViolationError,
21  NotArmedError,
22  CommandExecutionException,
23  ConcurrentProcessError,
24  HardwareNotPresentError,
25  ControllerError,
26  RealtimeError,
27  InvalidInputError
28  };
29 
30  // TODO: Add efficient way of encoding compile-time string messages with minimal overhead.
31  class Status {
32  public:
33  Status() : errorCode_(StatusCode::Ok)
34  {};
35 
36  Status(StatusCode errorCode) : errorCode_(errorCode)
37  {};
38 
39  Status(const Status &other) : errorCode_(other.errorCode_)
40  {};
41 
42  Status &operator=(const Status &s) = default;
43 
44  ~Status() = default;
45 
46  inline bool ok() const
47  {
48  return errorCode_ == StatusCode::Ok;
49  }
50 
51  inline StatusCode code() const
52  {
53  return errorCode_;
54  }
55 
56  bool operator==(const Status &s) const;
57 
58  bool operator!=(const Status &s) const
59  {
60  return !operator==(s);
61  }
62 
63  std::string toString() const;
64 
65  protected:
66  StatusCode errorCode_;
67  };
68 
69  inline
70  std::string statusCodeToString(StatusCode code)
71  {
72  switch (code) {
73  case StatusCode::Ok:
74  return "OK";
75  case StatusCode::InternalError:
76  return "InternalError";
77  case StatusCode::UnknownError:
78  return "UnknownError";
79  case StatusCode::NetworkError:
80  return "NetworkError";
81  case StatusCode::InvalidAddress:
82  return "InvalidAddress";
83  case StatusCode::NotInitializedError:
84  return "NotInitializedError";
85  case StatusCode::ResourceBusyError:
86  return "ResourceBusyError";
87  case StatusCode::SafetyViolationError:
88  return "SafetyViolationError";
89  case StatusCode::NotArmedError:
90  return "NotArmedError";
91  case StatusCode::CommandExecutionException:
92  return "CommandExecutionException";
93  case StatusCode::ConcurrentProcessError:
94  return "ConcurrentProcessError";
95  case StatusCode::HardwareNotPresentError:
96  return "HardwareNotPresentError";
97  case StatusCode::ControllerError:
98  return "ControllerError";
99  case StatusCode::RealtimeError:
100  return "RealtimeError";
101  case StatusCode::InvalidInputError:
102  return "InvalidInputError";
103  }
104 
105  return "UNKNOWN";
106  }
107 
108  namespace util {
109  Status OkStatus();
110 
111  Status UnknownError();
112 
113  Status InternalError();
114  }
115  }
116 }
117 
118 #endif //LODESTAR_STATUS_HPP
ls
Main Lodestar code.
Definition: BilinearTransformation.hpp:12
ls::core::Status
Definition: Status.hpp:31