Alchemy  1.0
A framework to robustly process network messages and structured data
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
msg_view.h
Go to the documentation of this file.
1 /// @file Hg/msg_view.h
2 ///
3 /// The msg_view type for working with opaque buffers.
4 ///
5 /// The MIT License(MIT)
6 /// @copyright 2014 Paul M Watt
7 // ****************************************************************************
8 #ifndef MSG_VIEW_H_INCLUDED
9 #define MSG_VIEW_H_INCLUDED
10 // Includes ******************************************************************
11 #include <Hg/msg_view_iterator.h>
12 
13 namespace Hg
14 {
15 
16 template< typename MsgT,
17  typename StorageT
18  >
19 class Message;
20 
21 // **************************************************************************
22 /// The MsgView object to manage structured access over an opaque buffer.
23 ///
24 template< typename MsgT,
25  typename ByteOrderT = Hg::HostByteOrder
26  >
27 class msg_view
28 {
29 public:
30  // Typedefs *****************************************************************
31  typedef MsgT message_type;
32 
33  typedef typename
34  MsgT::format_type format_type;
36 
37  typedef ByteOrderT byte_order_type;
38 
39  typedef Message < MsgT,
40  byte_order_type
41  > value_type;
42 
43  typedef byte_t* raw_pointer;
44 
45  typedef value_type* pointer;
46  typedef const value_type* const_pointer;
47  typedef value_type& reference;
48  typedef const value_type& const_reference;
49 
50  // Constants ****************************************************************
51  enum { k_size = SizeOf<format_type>::value };
52  ///< Indicates the size in bytes of the
53  /// data buffer managed by this message.
54 
55  // TODO: For this type, this must be false. Add static assertion.
56  static const
58  ///< Indicates if the format of this
59  /// message contains fields that are
60  /// potentially dynamically allocated.
61 
64 
65 
66  // **************************************************************************
67  /// Default constructor.
68  ///
70  { }
71 
72  // **************************************************************************
73  /// Value constructor
74  ///
75  /// Create a view from an array of bytes.
76  ///
77  /// @param view A reference to an array of bytes, to which this
78  /// view will be mapped.
79  ///
80  template <size_t SizeT>
81  msg_view(std::array<byte_t,SizeT>& data)
82  {
83  m_pFirst = &data[0];
84 
85  // Configure the last pointer to be the byte
86  // passed the last valid message that fits
87  // into the buffer specified by the caller.
88  size_t count = SizeT / k_size;
89  m_pLast = m_pFirst + (count * k_size);
90  }
91 
92  // **************************************************************************
93  /// Value constructor
94  ///
95  /// Create a view from an vector of bytes.
96  ///
97  /// @param view A reference to a vector of bytes, to which this
98  /// view will be mapped.
99  ///
100  msg_view(std::vector<byte_t>& data)
101  {
102  m_pFirst = &data[0];
103 
104  // Configure the last pointer to be the byte
105  // passed the last valid message that fits
106  // into the buffer specified by the caller.
107  size_t count = data.size() / k_size;
108  m_pLast = m_pFirst + (count * k_size);
109  }
110 
111  // **************************************************************************
112  /// Value constructor
113  ///
114  /// Create a view from an existing array of bytes.
115  ///
116  /// @param pData A pointer to an array of bytes, to which this
117  /// view will be mapped.
118  /// @param length The number of bytes in the buffer.
119  ///
120  msg_view(const byte_t* pData, size_t length)
121  {
122  m_pFirst = pData;
123 
124  // Configure the last pointer to be the byte
125  // passed the last valid message that fits
126  // into the buffer specified by the caller.
127  size_t count = length / k_size;
128  m_pLast = m_pFirst + (count * k_size);
129  }
130 
131  // **************************************************************************
132  /// Returns the number of valid objects managed by this array structure.
133  ///
134  size_t size() const
135  {
136  return (m_pLast - m_pFirst)
137  / k_size;
138  }
139 
140  // Iterator Functions *******************************************************
141  // **************************************************************************
142  /// Returns an iterator to the first item in the array.
143  ///
145  const_iterator begin() const { return msg_view_const_iterator<MsgT>(m_pFirst); }
146  const_iterator cbegin() const { return msg_view_const_iterator<MsgT>(m_pFirst); }
147 
148  // **************************************************************************
149  /// Returns an iterator to the item one passed the end of the array.
150  ///
152  const_iterator end() const { return msg_view_const_iterator<MsgT>(m_pLast); }
153  const_iterator cend() const { return msg_view_const_iterator<MsgT>(m_pLast); }
154 
155  // TODO: Revisit and complete.
156  //// **************************************************************************
157  ///// Returns an iterator to the last item of the array moving in reverse.
158  /////
159  //reverse_iterator rbegin() { return this->get().rbegin(); }
160  //const_reverse_iterator rbegin() const { return this->get().rbegin(); }
161  //const_reverse_iterator crbegin() const { return this->get().crbegin();}
162 
163  //// **************************************************************************
164  ///// Returns an iterator to the item one passed the beginning of the array,
165  ///// moving in reverse.
166  /////
167  //reverse_iterator rend() { return this->get().rend(); }
168  //const_reverse_iterator rend() const { return this->get().rend(); }
169  //const_reverse_iterator crend() const { return this->get().crend(); }
170 protected:
171  // Member Variables *********************************************************
172  raw_pointer m_pFirst; ///< A pointer to the first element.
173  raw_pointer m_pLast; ///< A pointer to the last element + 1.
174 
175 };
176 
177 
178 // **************************************************************************
179 // Opaque-type functions ****************************************************
180 // These functions are only present for buffers to bytes.
181 
182 // **************************************************************************
183 template< typename MsgT,
184  typename T
185  >
186 typename
187  std::enable_if< is_opaque<T>::value,
188  msg_view<MsgT> >::type
189 make_view(T& buffer)
190 {
191  return msg_view<MsgT>(buffer);
192 }
193 
194 
195 } // namespace Hg
196 
197 #endif