Dripline-Cpp  v2.4.2
Dripline Implementation in C++
dripline_constants.cc
Go to the documentation of this file.
1 /*
2  * dripline_constants.cc
3  *
4  * Created on: Jan 5, 2016
5  * Author: N.S. Oblath
6  */
7 
8 #define DRIPLINE_API_EXPORTS
9 
10 #include "dripline_constants.hh"
11 #include "dripline_exceptions.hh"
12 
13 namespace dripline
14 {
15  // op_t utility functions
16  // Conversion functions for use when a numeric value is needed
17  DRIPLINE_API uint32_t to_uint( op_t an_op )
18  {
19  return static_cast< uint32_t >( an_op );
20  }
21  DRIPLINE_API op_t to_op_t( uint32_t an_op_uint )
22  {
23  return static_cast< op_t >( an_op_uint );
24  }
25  DRIPLINE_API std::ostream& operator<<( std::ostream& a_os, op_t an_op )
26  {
27  return a_os << to_uint( an_op );
28  }
29  // Conversion functions for use when string values are required
30  DRIPLINE_API std::string to_string( op_t an_op )
31  {
32  switch (an_op) {
33  case op_t::set: return "set";
34  case op_t::get: return "get";
35  case op_t::cmd: return "cmd";
36  case op_t::unknown: return "unknown";
37  default: throw dripline_error() << "op_t value <" << an_op << "> not recognized";
38  }
39  }
40  DRIPLINE_API op_t to_op_t( std::string an_op_str )
41  {
42  if ( an_op_str == to_string( op_t::set ) ) return op_t::set;
43  if ( an_op_str == to_string( op_t::get ) ) return op_t::get;
44  if ( an_op_str == to_string( op_t::cmd ) ) return op_t::cmd;
45  if ( an_op_str == to_string( op_t::unknown ) ) return op_t::unknown;
46  throw dripline_error() << "unable to map <" << an_op_str << "> to an op_t value";
47  }
48 
49  // msg_t utility functions
50  // Conversion functions for use when a numeric value is needed
51  DRIPLINE_API uint32_t to_uint( msg_t a_msg )
52  {
53  return static_cast< uint32_t >( a_msg );
54  }
55  DRIPLINE_API msg_t to_msg_t( uint32_t a_msg_uint )
56  {
57  return static_cast< msg_t >( a_msg_uint );
58  }
59  DRIPLINE_API std::ostream& operator<<( std::ostream& a_os, msg_t a_msg )
60  {
61  return a_os << to_uint( a_msg );
62  }
63  // Conversion functions for use when string values are required
64  DRIPLINE_API std::string to_string( msg_t a_msg )
65  {
66  switch (a_msg) {
67  case msg_t::reply: return "reply";
68  case msg_t::request: return "request";
69  case msg_t::alert: return "alert";
70  case msg_t::unknown: return "unknown";
71  default: throw dripline_error() << "msg_t value <" << a_msg << "> not recognized";
72  }
73  }
74  DRIPLINE_API msg_t to_msg_t( std::string a_msg_str )
75  {
76  if ( a_msg_str == to_string( msg_t::reply ) ) return msg_t::reply;
77  if ( a_msg_str == to_string( msg_t::request ) ) return msg_t::request;
78  if ( a_msg_str == to_string( msg_t::alert ) ) return msg_t::alert;
79  if ( a_msg_str == to_string( msg_t::unknown ) ) return msg_t::unknown;
80  throw dripline_error() << "unable to map <" << a_msg_str << "> to a msg_t value";
81  }
82 
83 } /* namespace dripline */
op_t to_op_t(uint32_t an_op_uint)
Dripline-specific errors.
std::ostream & operator<<(std::ostream &a_os, op_t an_op)
Pass the integer-equivalent of a message-operation enum to an ostream.
#define DRIPLINE_API
Definition: dripline_api.hh:34
msg_t to_msg_t(uint32_t a_msg_uint)
std::string to_string(op_t an_op)
Gives the human-readable version of a message operation.
uint32_t to_uint(op_t an_op)
Convert a message-operation enum to an integer.