cpp11chat/chat_messages.hpp

43 lines
1.0 KiB
C++
Raw Normal View History

#pragma once
#include <string>
#include <type_traits>
#include <ostream>
namespace chat
{
enum class message : char
{
HELLO = 1, NEPTUN = 2, PASSW = 3,
SERVER_DIRECTION = 4, MESSAGE = 5,
PING = 6, PONG = 7, BYE = 8,
LOGIN = 9, LOGOUT = 10, TERM = 0x7f
};
constexpr char byte(message msg) { return static_cast<char>(msg); }
class chat_message
{
message _header;
std::string _content;
public:
chat_message(message header, std::string content = "")
: _header(header), _content(content)
{}
chat_message(std::underlying_type<message>::type header_integral, std::string content)
: _header(static_cast<message>(header_integral)), _content(content)
{}
std::string get() const
{ return byte(_header) + _content + byte(message::TERM); }
std::string get_content() const
{ return _content; }
};
std::ostream& operator<<(std::ostream& os, const chat_message& msg)
{ return os << msg.get(); }
}