Alchemy  1.0
A framework to robustly process network messages and structured data
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
unpack_message.h
1 /// @file detail/unpack_message.h
2 ///
3 /// Unpacks the bytes from a raw memory buffer into Hg (Mercury).
4 ///
5 /// The MIT License(MIT)
6 /// @copyright 2014 Paul M Watt
7 // ****************************************************************************
8 #ifndef UNPACK_MESSAGE_H_INCLUDED
9 #define UNPACK_MESSAGE_H_INCLUDED
10 // Includes *******************************************************************
11 #include <Hg/msg_fwd.h>
12 #include <Hg/detail/unpack_message_detail.h>
13 #include <Hg/detail/unpack_array.h>
17 
18 namespace Hg
19 {
20 
21 // ****************************************************************************
22 /// Reads the values of a message structure from a packed memory buffer.
23 ///
24 /// @param msg_values The message structure that contains the values
25 /// to be read.
26 /// @param buffer The buffer this data should be read from.
27 ///
28 /// @return The message object populated by the input buffer.
29 /// This is simply the same message object reference
30 /// passed as input for expressive syntax.
31 ///
32 template< typename MsgT,
33  typename BufferT,
34  typename SizeTraitT
35  >
36 MsgT& unpack_message( MsgT &msg_values,
37  const BufferT &buffer)
38 {
39  return detail::unpack_message ( msg_values,
40  buffer,
41  SizeTraitT());
42 }
43 
44 
45 // ****************************************************************************
46 /// Reads the values of a message structure from a packed memory buffer.
47 ///
48 /// @param msg_values The message structure that contains the values
49 /// to be read.
50 /// @param buffer The buffer this data should be read from.
51 /// @param offset The offset the reading should occur.
52 ///
53 /// @return The number of bytes that were read in from the buffer.
54 ///
55 template< typename MsgT,
56  typename BufferT,
57  typename SizeTraitT
58  >
59 size_t unpack_message( MsgT &msg_values,
60  const BufferT &buffer,
61  size_t offset)
62 {
63  return detail::unpack_message ( msg_values,
64  buffer,
65  offset,
66  SizeTraitT());
67 }
68 
69 
70 // ****************************************************************************
71 /// Reads the values of a message from an istream object.
72 ///
73 /// @param is The input stream that will provide the data
74 /// to populate the message.
75 /// @param msg The message to be serialized.
76 ///
77 /// @return A reference to is will be returned to allow these
78 /// calls to be chained.
79 ///
80 template< typename T >
81 std::istream& operator>>(std::istream& is, T& msg)
82 {
84  {
85  detail::unpack_stream_dynamic(is, msg);
86  }
87  else
88  {
89  detail::unpack_stream_static(is, msg);
90  }
91 
92  return is;
93 }
94 
95 } // namespace Hg
96 
97 #endif