Dripline-Cpp  v2.4.2
Dripline Implementation in C++
return_codes.hh
Go to the documentation of this file.
1 /*
2  * @file return_codes.hh
3  *
4  * Created on: Aug 25, 2018
5  * Author: N.S. Oblath
6  *
7  * This file contains the basis for the extensible return code system.
8  * Return codes have a name and a value (unsigned integer).
9  * The class name corresponding to your return code will be dl_[name].
10  * The integer values must be unique, and that uniqueness is enforced in dripline-cpp at run time.
11  * If a non-unique return code is added, a scarab::error will be thrown.
12  *
13  * New return codes are created using the macro DEFINE_DL_RET_CODE or DEFINE_DL_RET_CODE_NOAPI in a header file,
14  * and the macro IMPLEMENT_DL_RET_CODE in a source file.
15  *
16  * This file includes the definitions for all Dripline-standard return codes. They are implemented in return_codes.cc.
17  */
18 
19 #ifndef DRIPLINE_RETURN_CODES_HH_
20 #define DRIPLINE_RETURN_CODES_HH_
21 
22 #include "dripline_api.hh"
23 
24 #include "indexed_factory.hh"
25 #include "macros.hh"
26 
27 #include <memory>
28 #include <set>
29 #include <string>
30 #include <vector>
31 
32 namespace dripline
33 {
40  {
41  virtual ~return_code() {};
42  virtual unsigned rc_value() const = 0;
43  virtual std::string rc_name() const = 0;
44  virtual std::string rc_description() const = 0;
45  };
46 
53  {
54  copy_code( unsigned a_value, const std::string& a_name, const std::string& a_description );
55  copy_code( const return_code& a_code );
56  virtual ~copy_code() {};
57  virtual unsigned rc_value() const { return f_value; }
58  virtual std::string rc_name() const { return f_name; }
59  virtual std::string rc_description() const { return f_description; }
60  unsigned f_value;
61  std::string f_name;
62  std::string f_description;
63  };
64 
65  // Macros for defining and implementing new return codes
66 
72 #define DEFINE_DL_RET_CODE( name, api_macro ) \
73  struct api_macro dl_##name : public ::dripline::return_code \
74  { \
75  static unsigned s_value; \
76  static std::string s_name; \
77  static std::string s_description; \
78  virtual ~dl_##name() {} \
79  virtual unsigned rc_value() const { return dl_##name::s_value; } \
80  virtual std::string rc_name() const {return dl_##name::s_name; } \
81  virtual std::string rc_description() const {return dl_##name::s_description; } \
82  };
83 
89 #define DEFINE_DL_RET_CODE_NOAPI( name ) \
90  struct dl_##name : public ::dripline::return_code \
91  { \
92  static unsigned s_value; \
93  static std::string s_name; \
94  static std::string s_description; \
95  virtual ~dl_##name() {} \
96  virtual unsigned rc_value() const { return dl_##name::s_value; } \
97  virtual std::string rc_name() const {return dl_##name::s_name; } \
98  virtual std::string rc_description() const {return dl_##name::s_description; } \
99  };
100 
107 #define IMPLEMENT_DL_RET_CODE( name, the_value, description ) \
108  unsigned dl_##name::s_value = the_value; \
109  std::string dl_##name::s_name( TOSTRING(name) ); \
110  std::string dl_##name::s_description( description );\
111  static scarab::indexed_registrar< unsigned, ::dripline::return_code, dl_##name > t_dl_##name##_rc_reg( the_value );
112 
113 // std::string ::dripline::dl_##name::s_name = TOSTRING(name);
114 
115  DRIPLINE_API bool operator==( const return_code& a_lhs, const return_code& a_rhs );
116 
117  DRIPLINE_API std::ostream& operator<<( std::ostream& a_os, const return_code& a_rc );
118 
119  //****************
120  // Return code definitions
121  //****************
122 
124 
125  DEFINE_DL_RET_CODE( warning_no_action_taken, DRIPLINE_API );
126  DEFINE_DL_RET_CODE( warning_deprecated_feature, DRIPLINE_API );
127  DEFINE_DL_RET_CODE( warning_dry_run, DRIPLINE_API );
128  DEFINE_DL_RET_CODE( warning_offline, DRIPLINE_API );
129  DEFINE_DL_RET_CODE( warning_sub_service, DRIPLINE_API );
130 
132  DEFINE_DL_RET_CODE( amqp_error_broker_connection, DRIPLINE_API );
133  DEFINE_DL_RET_CODE( amqp_error_routingkey_notfound, DRIPLINE_API );
134 
135  DEFINE_DL_RET_CODE( resource_error, DRIPLINE_API );
136  DEFINE_DL_RET_CODE( resource_error_connection, DRIPLINE_API );
137  DEFINE_DL_RET_CODE( resource_error_no_response, DRIPLINE_API );
138  DEFINE_DL_RET_CODE( resource_error_sub_service, DRIPLINE_API );
139 
140  DEFINE_DL_RET_CODE( service_error, DRIPLINE_API );
141  DEFINE_DL_RET_CODE( service_error_no_encoding, DRIPLINE_API );
142  DEFINE_DL_RET_CODE( service_error_decoding_fail, DRIPLINE_API );
143  DEFINE_DL_RET_CODE( service_error_bad_payload, DRIPLINE_API );
144  DEFINE_DL_RET_CODE( service_error_invalid_value, DRIPLINE_API );
145  DEFINE_DL_RET_CODE( service_error_timeout, DRIPLINE_API );
146  DEFINE_DL_RET_CODE( service_error_invalid_method, DRIPLINE_API );
147  DEFINE_DL_RET_CODE( service_error_access_denied, DRIPLINE_API );
148  DEFINE_DL_RET_CODE( service_error_invalid_key, DRIPLINE_API );
149  DEFINE_DL_RET_CODE( service_error_invalid_specifier, DRIPLINE_API );
150 
152  DEFINE_DL_RET_CODE( client_error_invalid_request, DRIPLINE_API );
153  DEFINE_DL_RET_CODE( client_error_handling_reply, DRIPLINE_API );
154  DEFINE_DL_RET_CODE( client_error_unable_to_send, DRIPLINE_API );
155  DEFINE_DL_RET_CODE( client_error_timeout, DRIPLINE_API );
156 
157  DEFINE_DL_RET_CODE( unhandled_exception, DRIPLINE_API );
158 
159  //****************
160  // Custom return codes
161  //****************
162 
164  void add_return_code( unsigned a_value, const std::string& a_name, const std::string& a_description );
168  bool check_and_add_return_code( unsigned a_value, const std::string& a_name, const std::string& a_description );
169  std::vector< unsigned > get_return_code_values();
170  std::map< unsigned, std::unique_ptr<return_code> > get_return_codes_map();
171 
172  class custom_return_code_registrar : public scarab::base_registrar< return_code >
173  {
174  public:
175  custom_return_code_registrar( const unsigned& a_value, const std::string& a_name, const std::string& a_description );
176  virtual ~custom_return_code_registrar();
177 
178  void register_class() const;
179 
180  return_code* create() const;
181 
182  protected:
183  unsigned f_value;
184  std::string f_name;
185  std::string f_description;
186  };
187 
188 
189 } /* namespace dripline */
190 
191 #endif /* DRIPLINE_RETURN_CODES_HH_ */
Base class for return codes.
Definition: return_codes.hh:39
std::map< unsigned, std::unique_ptr< return_code > > get_return_codes_map()
virtual unsigned rc_value() const
Definition: return_codes.hh:57
Stores a copy of the return-code value, name, and description, either as a custom return-code or copy...
Definition: return_codes.hh:52
std::ostream & operator<<(std::ostream &a_os, op_t an_op)
Pass the integer-equivalent of a message-operation enum to an ostream.
std::string f_name
Definition: return_codes.hh:61
#define DRIPLINE_API
Definition: dripline_api.hh:34
bool check_and_add_return_code(unsigned a_value, const std::string &a_name, const std::string &a_description)
Definition: return_codes.cc:97
virtual std::string rc_description() const
Definition: return_codes.hh:59
#define DEFINE_DL_RET_CODE(name, api_macro)
Definition: return_codes.hh:72
void add_return_code(unsigned a_value, const std::string &a_name, const std::string &a_description)
Helper function to add a return code (primarily for python binding); scarab::error will be thrown if ...
Definition: return_codes.cc:90
virtual std::string rc_name() const
Definition: return_codes.hh:58
bool operator==(const message &a_lhs, const message &a_rhs)
Definition: message.cc:586
std::vector< unsigned > get_return_code_values()
std::string f_description
Definition: return_codes.hh:62