xdrpp
RFC4506 XDR compiler and message library
message.h
Go to the documentation of this file.
1 // -*- C++ -*-
2 
3 #ifndef _XDRPP_MESSAGE_H_HEADER_INCLUDED_
4 #define _XDRPP_MESSAGE_H_HEADER_INCLUDED_ 1
5 
6 #include <cassert>
7 #include <cstddef>
8 #include <cstdint>
9 #include <memory>
10 #include <xdrpp/endian.h>
11 
12 //! \file message.h Message buffer with space for marshaled length.
13 
14 namespace xdr {
15 
16 class message_t;
17 using msg_ptr = std::unique_ptr<message_t>;
18 
19 //! Message buffer, with room at beginning for 4-byte length. Note
20 //! the constructor is private, so you must create one with \c
21 //! message_t::alloc, which allocates more space than the size of the
22 //! \c message_t structure. Hence \c message_t is just a data
23 //! structure at the beginning of the buffer.
24 class message_t {
25  const std::size_t size_;
26  alignas(std::uint32_t) char buf_[4];
27  message_t(std::size_t size) : size_(size) {}
28 public:
29  std::size_t size() const { return size_; }
30  char *data() { return buf_ + 4; }
31  const char *data() const { return buf_ + 4; }
32  const uint32_t word(std::ptrdiff_t i) const {
33  return reinterpret_cast<const uint32_t *>(data())[i];
34  }
35  //const void *offset(std::ptrdiff_t i) const { return buf_ + i; }
36  //! End of the buffer (one past last byte).
37  char *end() { return buf_ + 4 + size_; }
38  const char *end() const { return buf_ + 4 + size_; }
39 
40  //! 4-byte buffer to store size in network byte order, followed by data.
41  char *raw_data() { return buf_; }
42  const char *raw_data() const { return buf_; }
43  //! Size of 4-byte length plus data.
44  std::size_t raw_size() const { return size_ + 4; }
45 
46  //! Allocate a new buffer.
47  static msg_ptr alloc(std::size_t size);
48 };
49 
50 }
51 
52 #endif // !_XDRPP_MESSAGE_H_HEADER_INCLUDED_
static msg_ptr alloc(std::size_t size)
Allocate a new buffer.
Definition: marshal.cc:7
char * end()
End of the buffer (one past last byte).
Definition: message.h:37
Most of the xdrpp library is encapsulated in the xdr namespace.
Definition: arpc.cc:4
Message buffer, with room at beginning for 4-byte length.
Definition: message.h:24
Low-level byteswap and miscellaneous OS compatibility routines.
char * raw_data()
4-byte buffer to store size in network byte order, followed by data.
Definition: message.h:41
std::size_t raw_size() const
Size of 4-byte length plus data.
Definition: message.h:44