Dripline-Cpp  v2.4.2
Dripline Implementation in C++
return_codes.cc
Go to the documentation of this file.
1 /*
2  * return_codes.cc
3  *
4  * Created on: Aug 25, 2018
5  * Author: N.S. Oblath
6  */
7 
8 #define DRIPLINE_API_EXPORTS
9 
10 #include "return_codes.hh"
11 
12 #include "logger.hh"
13 
14 #include <vector>
15 
16 LOGGER( rclog, "return_codes" );
17 
18 namespace dripline
19 {
20  copy_code::copy_code( unsigned a_value, const std::string& a_name, const std::string& a_description ) :
21  f_value( a_value ),
22  f_name( a_name ),
23  f_description( a_description )
24  {}
25 
26  copy_code::copy_code( const return_code& a_code ) :
27  f_value( a_code.rc_value() ),
28  f_name( a_code.rc_name() ),
29  f_description( a_code.rc_description() )
30  {}
31 
32  bool operator==( const return_code& a_lhs, const return_code& a_rhs )
33  {
34  return a_lhs.rc_name() == a_rhs.rc_name() &&
35  a_lhs.rc_value() == a_rhs.rc_value() &&
36  a_lhs.rc_description() == a_rhs.rc_description();
37  }
38 
39  DRIPLINE_API std::ostream& operator<<( std::ostream& a_os, const return_code& a_rc )
40  {
41  a_os << a_rc.rc_description() << "(" << a_rc.rc_value() << ")";
42  return a_os;
43  }
44 
45  //****************
46  // Return code implementations
47  //****************
48 
49  IMPLEMENT_DL_RET_CODE( success, 0, "Success" );
50 
51  IMPLEMENT_DL_RET_CODE( warning_no_action_taken, 1, "No Action Taken" );
52  IMPLEMENT_DL_RET_CODE( warning_deprecated_feature, 2, "Deprecated Feature" );
53  IMPLEMENT_DL_RET_CODE( warning_dry_run, 3, "Dry Run" );
54  IMPLEMENT_DL_RET_CODE( warning_offline, 4, "Offline" );
55  IMPLEMENT_DL_RET_CODE( warning_sub_service, 5, "Sub-Service Warning")
56 
57  IMPLEMENT_DL_RET_CODE( amqp_error, 100, "Generic AMQP Error" );
58  IMPLEMENT_DL_RET_CODE( amqp_error_broker_connection, 101, "AMQP Connection Error" );
59  IMPLEMENT_DL_RET_CODE( amqp_error_routingkey_notfound, 102, "Invalid AMQP Routing Key" );
60 
61  IMPLEMENT_DL_RET_CODE( resource_error, 200, "Generic Resource Error" );
62  IMPLEMENT_DL_RET_CODE( resource_error_connection, 201, "Resource Connection Error" );
63  IMPLEMENT_DL_RET_CODE( resource_error_no_response, 202, "No Response Error" );
64  IMPLEMENT_DL_RET_CODE( resource_error_sub_service, 203, "Sub-Service Error" );
65 
66  IMPLEMENT_DL_RET_CODE( service_error, 300, "Generic Service Error" );
67  IMPLEMENT_DL_RET_CODE( service_error_no_encoding, 301, "Invalid Message Encoding" );
68  IMPLEMENT_DL_RET_CODE( service_error_decoding_fail, 302, "Decoding Failed" );
69  IMPLEMENT_DL_RET_CODE( service_error_bad_payload, 303, "Invalid Payload" );
70  IMPLEMENT_DL_RET_CODE( service_error_invalid_value, 304, "Invalid Value" );
71  IMPLEMENT_DL_RET_CODE( service_error_timeout, 305, "Timeout Handling Message" );
72  IMPLEMENT_DL_RET_CODE( service_error_invalid_method, 306, "Invalid Command" );
73  IMPLEMENT_DL_RET_CODE( service_error_access_denied, 307, "Access Denied" );
74  IMPLEMENT_DL_RET_CODE( service_error_invalid_key, 308, "Invalid Lockout Key" );
75  // 309 was formerly "Deprecated Feature"
76  IMPLEMENT_DL_RET_CODE( service_error_invalid_specifier, 310, "Invalid Specifier" );
77 
78  IMPLEMENT_DL_RET_CODE( client_error, 400, "Generic Client Error" );
79  IMPLEMENT_DL_RET_CODE( client_error_invalid_request, 401, "Invalid Request" );
80  IMPLEMENT_DL_RET_CODE( client_error_handling_reply, 402, "Error Handling Reply" );
81  IMPLEMENT_DL_RET_CODE( client_error_unable_to_send, 403, "Unable to Send" );
82  IMPLEMENT_DL_RET_CODE( client_error_timeout, 404, "Client Timeout" );
83 
84  IMPLEMENT_DL_RET_CODE( unhandled_exception, 999, "Unhandled Exception" );
85 
86  //****************
87  // Custom return codes
88  //****************
89 
90  void add_return_code( unsigned a_value, const std::string& a_name, const std::string& a_description )
91  {
92  static std::vector< std::unique_ptr<custom_return_code_registrar> > f_rc_registrars;
93  f_rc_registrars.emplace_back( new custom_return_code_registrar(a_value, a_name, a_description) );
94  return;
95  }
96 
97  bool check_and_add_return_code( unsigned a_value, const std::string& a_name, const std::string& a_description )
98  {
99  try
100  {
101  add_return_code( a_value, a_name, a_description );
102  return true;
103  }
104  catch( const scarab::error& )
105  {
106  return false;
107  }
108  }
109 
110  std::vector< unsigned > get_return_code_values()
111  {
112  std::vector< unsigned > return_codes;
113  scarab::indexed_factory< unsigned, return_code >* the_factory = scarab::indexed_factory< unsigned, return_code >::get_instance();
114  LDEBUG( rclog, "factory is at: " << the_factory );
115  for( auto code_entry = the_factory->begin(); code_entry != the_factory->end(); ++code_entry )
116  {
117  return_codes.push_back( code_entry->first );
118  }
119  return return_codes;
120  }
121 
122  std::map< unsigned, std::unique_ptr<return_code> > get_return_codes_map()
123  {
124  std::map< unsigned, std::unique_ptr<return_code> > the_return_codes;
125  scarab::indexed_factory< unsigned, return_code >* the_factory = scarab::indexed_factory< unsigned, return_code >::get_instance();
126  for( auto code_entry = the_factory->begin(); code_entry != the_factory->end(); ++code_entry )
127  {
128  the_return_codes.emplace( std::make_pair(
129  code_entry->first,
130  std::unique_ptr<return_code>( code_entry->second->create() )
131  ) );
132  }
133  return the_return_codes;
134  }
135 
136  custom_return_code_registrar::custom_return_code_registrar( const unsigned& a_value, const std::string& a_name, const std::string& a_description ) :
137  scarab::base_registrar< return_code >(),
138  f_value( a_value ),
139  f_name( a_name ),
140  f_description( a_description)
141  {
142  register_class();
143  }
144 
146  {
147  scarab::indexed_factory< unsigned, return_code >::get_instance()->remove_class( f_value );
148  }
149 
151  {
152  scarab::indexed_factory< unsigned, return_code >::get_instance()->register_class( f_value, this );
153  return;
154  }
155 
157  {
158  return dynamic_cast< return_code* >( new copy_code( f_value, f_name, f_description ) );
159  }
160 
161 } /* namespace dripline */
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
virtual unsigned rc_value() const =0
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
Definition: core.hh:17
#define DRIPLINE_API
Definition: dripline_api.hh:34
virtual std::string rc_name() const =0
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
static scarab::logger rclog("return_codes")
custom_return_code_registrar(const unsigned &a_value, const std::string &a_name, const std::string &a_description)
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
copy_code(unsigned a_value, const std::string &a_name, const std::string &a_description)
Definition: return_codes.cc:20
virtual std::string rc_name() const
Definition: return_codes.hh:58
#define IMPLEMENT_DL_RET_CODE(name, the_value, description)
virtual std::string rc_description() const =0
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