Alchemy  1.0
A framework to robustly process network messages and structured data
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
message_byte_order.h
Go to the documentation of this file.
1 /// @file Hg/message_byte_order.h
2 ///
3 /// An extraction of the Network to Host conversion functions.
4 ///
5 /// The MIT License(MIT)
6 /// @copyright 2014 Paul M Watt
7 // ****************************************************************************
8 #ifndef MESSAGE_BYTE_ORDER_H_INCLUDED
9 #define MESSAGE_BYTE_ORDER_H_INCLUDED
10 // Includes *******************************************************************
12 
13 namespace Hg
14 {
15 
16 // ****************************************************************************
17 /// Parameterized function to convert a Hg::Message to network byte-order.
18 ///
19 /// @param T [typename] The Hg::basic_msg format definition of the
20 /// message to be converted.
21 /// @param from The message object to convert from.
22 /// Ownership of the memory that belongs to the input type will
23 /// be transferred to the returned instance, and the conversion
24 /// will occur in-place.
25 ///
26 /// @return A Hg::basic_msg object using the same format and data type as the
27 /// input buffer will be returned. The data in the buffer will
28 /// be in network byte-order.
29 ///
30 /// If the input format was already in network byte-order,
31 /// no conversion operations will be performed.
32 ///
33 template< typename T >
34 typename
35  T::base_type::net_t
36  to_network(T& from)
37 {
38  typename T::base_type::net_t to;
39 
40  return detail::convert_byte_order < T,
42  >(from, to);
43 }
44 
45 // ****************************************************************************
46 /// Parameterized function to convert a Hg::Message to host byte-order.
47 ///
48 /// @param T [typename] The Hg::basic_msg format definition of the
49 /// message to be converted.
50 /// @param from The message object to convert from.
51 /// Ownership of the memory that belongs to the input type will
52 /// be transferred to the returned instance, and the conversion
53 /// will occur in-place.
54 ///
55 /// @return A Hg::basic_msg object using the same format and data type as the
56 /// input buffer will be returned. The data in the buffer will
57 /// be in host byte-order.
58 ///
59 /// If the input format was already in host byte-order,
60 /// no conversion operations will be performed.
61 ///
62 template< typename T >
63 typename
64  T::base_type::host_t
65  to_host(T& from)
66 {
67  typename T::base_type::host_t to;
68 
69  return detail::convert_byte_order < T,
71  >(from, to);
72 }
73 
74 // ****************************************************************************
75 /// Parameterized function to convert a Hg::Message to bit endian byte-order.
76 ///
77 /// @param T [typename] The Hg::basic_msg format definition of the
78 /// message to be converted.
79 /// @param from The message object to convert from.
80 /// Ownership of the memory that belongs to the input type will
81 /// be transferred to the returned instance, and the conversion
82 /// will occur in-place.
83 ///
84 /// @return A Hg::basic_msg object using the same format and data type as the
85 /// input buffer will be returned. The data in the buffer will
86 /// be in big endian byte-order.
87 ///
88 /// If the input format was already in big endian byte-order,
89 /// no conversion operations will be performed.
90 ///
91 template< typename T >
92  typename T::base_type::big_t
93  to_big_endian(T& from)
94 {
95  typename T::base_type::big_t to;
96 
97  return detail::convert_byte_order < T,
98  BigEndian
99  >(from, to);
100 }
101 
102 // ****************************************************************************
103 /// Parameterized function to convert a Hg::Message to little endian byte-order.
104 ///
105 /// @param T [typename] The Hg::basic_msg format definition of the
106 /// message to be converted.
107 /// @param from The message object to convert from.
108 /// Ownership of the memory that belongs to the input type will
109 /// be transferred to the returned instance, and the conversion
110 /// will occur in-place.
111 ///
112 /// @return A Hg::basic_msg object using the same format and data type as the
113 /// input buffer will be returned. The data in the buffer will
114 /// be in little endian byte-order.
115 ///
116 /// If the input format was already in little endian byte-order,
117 /// no conversion operations will be performed.
118 ///
119 template< typename T >
120  typename T::base_type::little_t
122 {
123  typename T::base_type::little_t to;
124 
125  // convert the values of the message parameters.
126  return detail::convert_byte_order < T,
128  >(from, to);
129 }
130 
131 } // namespace Hg
132 
133 #endif
134