Dripline-Cpp  v2.10.11
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 unsigned to_uint( op_t an_op )
18  {
19  return static_cast< unsigned >( an_op );
20  }
21  DRIPLINE_API op_t to_op_t( unsigned 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  default: return "unknown";
37  }
38  }
39  DRIPLINE_API op_t to_op_t( std::string an_op_str )
40  {
41  if ( an_op_str == to_string( op_t::set ) ) return op_t::set;
42  if ( an_op_str == to_string( op_t::get ) ) return op_t::get;
43  if ( an_op_str == to_string( op_t::cmd ) ) return op_t::cmd;
44  if ( an_op_str == to_string( op_t::unknown ) ) return op_t::unknown;
45  throw dripline_error() << "unable to map <" << an_op_str << "> to an op_t value";
46  }
47 
48  // msg_t utility functions
49  // Conversion functions for use when a numeric value is needed
50  DRIPLINE_API unsigned to_uint( msg_t a_msg )
51  {
52  return static_cast< unsigned >( a_msg );
53  }
54  DRIPLINE_API msg_t to_msg_t( unsigned a_msg_uint )
55  {
56  return static_cast< msg_t >( a_msg_uint );
57  }
58  DRIPLINE_API std::ostream& operator<<( std::ostream& a_os, msg_t a_msg )
59  {
60  return a_os << to_uint( a_msg );
61  }
62  // Conversion functions for use when string values are required
63  DRIPLINE_API std::string to_string( msg_t a_msg )
64  {
65  switch (a_msg) {
66  case msg_t::reply: return "reply";
67  case msg_t::request: return "request";
68  case msg_t::alert: return "alert";
69  default: return "unknown";
70  }
71  }
72  DRIPLINE_API msg_t to_msg_t( std::string a_msg_str )
73  {
74  if ( a_msg_str == to_string( msg_t::reply ) ) return msg_t::reply;
75  if ( a_msg_str == to_string( msg_t::request ) ) return msg_t::request;
76  if ( a_msg_str == to_string( msg_t::alert ) ) return msg_t::alert;
77  if ( a_msg_str == to_string( msg_t::unknown ) ) return msg_t::unknown;
78  throw dripline_error() << "unable to map <" << a_msg_str << "> to a msg_t value";
79  }
80 
81 } /* namespace dripline */
Dripline-specific errors.
#define DRIPLINE_API
Definition: dripline_api.hh:34
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 to_string(op_t an_op)
Gives the human-readable version of a message operation.
unsigned to_uint(op_t an_op)
Convert a message-operation enum to an integer.
op_t to_op_t(unsigned an_op_uint)
msg_t to_msg_t(unsigned a_msg_uint)