Alchemy  1.0
A framework to robustly process network messages and structured data
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
pack_message.h
Go to the documentation of this file.
1 /// @file Hg/pack_message.h
2 ///
3 /// The implementation to pack a Hg Message into a packed byte-buffer.
4 ///
5 /// The MIT License(MIT)
6 /// @copyright 2014 Paul M Watt
7 // ****************************************************************************
8 #ifndef PACK_MESSAGE_H_INCLUDED
9 #define PACK_MESSAGE_H_INCLUDED
10 // Includes *******************************************************************
11 #include <Hg/msg_fwd.h>
13 #include <Hg/detail/pack_array.h>
14 #include <Hg/detail/pack_nested.h>
16 #include <Hg/detail/pack_vector.h>
17 
18 namespace Hg
19 {
20 
21 // ****************************************************************************
22 /// Writes the values of a message into a packed memory buffer.
23 ///
24 /// @param msg_values The message structure that contains the values
25 /// to be written.
26 /// @param size The calculated size required for this message.
27 ///
28 /// @return The buffer that has been allocated to store the
29 /// message.
30 ///
31 template< typename MsgT,
32  typename BufferT,
33  typename SizeTraitT
34  >
35 BufferT&
36  pack_message( MsgT& msg_values,
37  size_t size,
38  BufferT & buffer
39  )
40 {
41  detail::pack_message < MsgT,
42  BufferT
43  >(msg_values,
44  size,
45  buffer,
46  SizeTraitT());
47  return buffer;
48 }
49 
50 // ****************************************************************************
51 /// Writes the values of a variable-sized message into a packed memory buffer.
52 /// A separate instance exists to eliminate dynamic size tests from messages
53 /// that are completely fixed in size.
54 ///
55 /// @param msg_values The message structure that contains the values
56 /// to be written.
57 /// @param buffer The buffer this data should be written into.
58 /// @param offset The offset the writing should be at.
59 ///
60 /// @return The buffer that has been allocated to store the
61 /// message.
62 ///
63 template< typename MsgT,
64  typename BufferT,
65  typename SizeTraitT
66  >
67 size_t pack_message(MsgT &msg_values,
68  BufferT &buffer,
69  size_t offset)
70 {
71  return detail::pack_message < MsgT,
72  BufferT
73  >(msg_values,
74  buffer,
75  offset,
76  SizeTraitT());
77 }
78 
79 
80 // ****************************************************************************
81 /// Writes the values of a message into a packed memory buffer.
82 ///
83 /// @param msg_values The message structure that contains the values
84 /// to be written.
85 /// @param size The calculated size required for this message.
86 ///
87 /// @return The buffer that has been allocated to store the
88 /// message.
89 ///
90 template< typename MsgT,
91  typename BufferT,
92  typename SizeTraitT
93  >
94 bool
95  pack_message( MsgT& msg_values,
96  BufferT & fixed_buffer)
97 {
98  return detail::pack_fixed_size_message( msg_values,
99  fixed_buffer,
100  SizeTraitT());
101 }
102 
103 
104 // ****************************************************************************
105 /// Writes the values of a message into an ostream object.
106 ///
107 /// @param os The output stream that will accept the serialized
108 /// form of the message.
109 /// @param msg The message to be serialized.
110 ///
111 /// @return A reference to os will be returned to allow these
112 /// calls to be chained.
113 ///
114 template< typename T >
115 std::ostream& operator<<(std::ostream& os, const T& msg)
116 {
117  Hg::Message
118  <
120  typename T::byte_order_type
121  > outMsg;
122 
123  outMsg = msg;
124 
125  os.write(reinterpret_cast<const char*>(outMsg.data()), outMsg.size());
126  return os;
127 }
128 
129 } // namespace Hg
130 
131 #endif