Reinforcement Learning  1.1
api_status.h
Go to the documentation of this file.
1 
8 #pragma once
9 
10 #include "err_constants.h"
11 
12 #include <sstream>
13 #include <string>
14 
15 namespace reinforcement_learning
16 {
17 class i_trace;
22 {
23 public:
24  api_status();
25 
32  int get_error_code() const;
33 
40  const char* get_error_msg() const;
41 
49  static void try_update(api_status* status, int new_code, const char* new_msg);
50 
56  static void try_clear(api_status* status);
57 
58 private:
59  int _error_code;
60  std::string _error_msg;
61 };
62 
67 {
75  status_builder(i_trace* trace, api_status* status, int code);
76  ~status_builder();
77 
79  operator int() const;
80 
82  int _code;
86  std::ostringstream _os;
87 
88  status_builder(const status_builder&&) = delete;
89  status_builder(const status_builder&) = delete;
90  status_builder& operator=(const status_builder&) = delete;
91  status_builder& operator=(status_builder&&) = delete;
92 
93 private:
95  i_trace* _trace;
97  bool enable_logging() const;
98 };
99 
107 template <typename Last>
108 void report_error(std::ostringstream& oss, const Last& last)
109 {
110  oss << last;
111 }
112 
122 template <typename First, typename... Rest>
123 void report_error(std::ostringstream& oss, const First& first, const Rest&... rest)
124 {
125  oss << first;
126  report_error(oss, rest...);
127 }
128 
138 template <typename... All>
139 int report_error(api_status* status, int scode, const All&... all)
140 {
141  if (status != nullptr)
142  {
143  std::ostringstream oss;
144  report_error(oss, all...);
145  api_status::try_update(status, scode, oss.str().c_str());
146  }
147  return scode;
148 }
149 
159 template <typename T>
161 {
162  if (sbuilder._status != nullptr) { sbuilder._os << ", " << val; }
163  return sbuilder;
164 }
165 
172 inline int report_error(status_builder& sbuilder) { return sbuilder; }
173 
182 template <typename Last>
183 int report_error(status_builder& sbuilder, const Last& last)
184 {
185  return sbuilder << last;
186 }
187 
198 template <typename First, typename... Rest>
199 int report_error(status_builder& sbuilder, const First& first, const Rest&... rest)
200 {
201  sbuilder << first;
202  return report_error(sbuilder, rest...);
203 }
204 } // namespace reinforcement_learning
205 
209 #define RETURN_ERROR(trace, status, code, ...) \
210  do { \
211  if (status != nullptr) \
212  { \
213  reinforcement_learning::status_builder sbuilder(trace, status, reinforcement_learning::error_code::code); \
214  sbuilder << reinforcement_learning::error_code::code##_s; \
215  return report_error(sbuilder); \
216  } \
217  return reinforcement_learning::error_code::code; \
218  } while (0);
219 
223 #define RETURN_ERROR_ARG(trace, status, code, ...) \
224  do { \
225  if (status != nullptr) \
226  { \
227  reinforcement_learning::status_builder sbuilder(trace, status, reinforcement_learning::error_code::code); \
228  sbuilder << reinforcement_learning::error_code::code##_s; \
229  return report_error(sbuilder, __VA_ARGS__); \
230  } \
231  return reinforcement_learning::error_code::code; \
232  } while (0);
233 
237 #define RETURN_ERROR_LS(trace, status, code) \
238  reinforcement_learning::status_builder sbuilder(trace, status, reinforcement_learning::error_code::code); \
239  return sbuilder << reinforcement_learning::error_code::code##_s
240 
244 #define RETURN_IF_FAIL(x) \
245  do { \
246  int retval__LINE__ = (x); \
247  if (retval__LINE__ != 0) { return retval__LINE__; } \
248  } while (0)
Report status of all API calls.
Definition: api_status.h:22
static void try_update(api_status *status, int new_code, const char *new_msg)
Helper method for returning error object Checks to see if api_status is not null before setting the e...
const char * get_error_msg() const
(API Error Codes) Get the error msg string All API calls will return a status code....
int get_error_code() const
(API Error Codes) Get the error code All API calls will return a status code. If the optional api_sta...
static void try_clear(api_status *status)
Helper method to clear the error object Checks to see if api_status is not null before clearing curre...
Definition of all API error return codes and descriptions.
[Error Generator]
Definition: live_model.h:25
reinforcement_learning::status_builder & operator<<(reinforcement_learning::status_builder &sbuilder, const T &val)
left shift operator to serialize types into stringstream held in status_builder
Definition: api_status.h:160
void report_error(std::ostringstream &oss, const Last &last)
Ends recursion of report_error variadic template with the Last argument.
Definition: api_status.h:108
Helper class used in report_error template funcstions to return status from API calls.
Definition: api_status.h:67
status_builder(i_trace *trace, api_status *status, int code)
Construct a new status builder object.
std::ostringstream _os
String stream used to serialize detailed error message.
Definition: api_status.h:86
int _code
Error code.
Definition: api_status.h:82
api_status * _status
Api status object which can be null.
Definition: api_status.h:84