CppServer  1.0.0.0
C++ Server Library
message.h
Go to the documentation of this file.
1 
9 #ifndef CPPSERVER_NANOMSG_MESSAGE_H
10 #define CPPSERVER_NANOMSG_MESSAGE_H
11 
12 #include "nanomsg.h"
13 
14 #include "errors/exceptions.h"
15 
16 #include <iostream>
17 #include <string>
18 
19 namespace CppServer {
20 namespace Nanomsg {
21 
23 
29 class Message
30 {
31  friend class Socket;
32 
33 public:
35  Message();
37 
48  explicit Message(size_t size, int type = 0);
50 
55  explicit Message(const void* data, size_t size, int type = 0);
56  Message(const Message& message);
57  Message(Message&&) noexcept = default;
58  ~Message();
59 
60  Message& operator=(const Message& message);
61  Message& operator=(Message&&) noexcept = default;
62 
64  explicit operator bool() const { return (_buffer == nullptr); }
65 
67  uint8_t* buffer() noexcept { return _buffer; }
69  const uint8_t* buffer() const noexcept { return _buffer; }
71  size_t size() const noexcept { return _size; }
73  int type() const noexcept { return _type; }
74 
76  void Clear();
77 
79 
82  void Reallocate(size_t size);
83 
85  std::string string() const { return std::string((const char*)_buffer, _size); }
86 
88  friend std::ostream& operator<<(std::ostream& os, const Message& instance)
89  { os << instance.string(); return os; }
90 
92  void swap(Message& message) noexcept;
93  friend void swap(Message& message1, Message& message2) noexcept;
94 
95 private:
96  uint8_t* _buffer;
97  size_t _size;
98  int _type;
99 };
100 
101 } // namespace Nanomsg
102 } // namespace CppServer
103 
104 #include "message.inl"
105 
106 #endif // CPPSERVER_NANOMSG_MESSAGE_H
void Reallocate(size_t size)
Reallocate the message size.
Definition: message.cpp:71
Nanomsg message.
Definition: message.h:29
int type() const noexcept
Get the message type.
Definition: message.h:73
size_t size() const noexcept
Get the message size.
Definition: message.h:71
C++ Server project definitions.
Definition: asio.h:24
std::string string() const
Convert the current message to a string.
Definition: message.h:85
Nanomsg socket.
Definition: socket.h:29
Nanomsg C++ Library definition.
uint8_t * buffer() noexcept
Get the message buffer.
Definition: message.h:67
friend std::ostream & operator<<(std::ostream &os, const Message &instance)
Output instance into the given output stream.
Definition: message.h:88
Nanomsg message inline implementation.
Message()
Create an empty message.
Definition: message.cpp:19
void Clear()
Clear the message buffer.
Definition: message.cpp:60
const uint8_t * buffer() const noexcept
Get the constant message buffer.
Definition: message.h:69
void swap(Message &message) noexcept
Swap two instances.
Definition: message.inl:12