cpp-mirai-client  v2.6.1
cpp client for mirai-api-http
IMessage.hpp
浏览该文件的文档.
1// Copyright (C) 2022 Numendacil and contributors
2//
3// This program is free software: you can redistribute it and/or modify
4// it under the terms of the GNU Affero General Public License as
5// published by the Free Software Foundation, either version 3 of the
6// License, or (at your option) any later version.
7//
8// This program is distributed in the hope that it will be useful,
9// but WITHOUT ANY WARRANTY; without even the implied warranty of
10// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11// GNU Affero General Public License for more details.
12//
13// You should have received a copy of the GNU Affero General Public License
14// along with this program. If not, see <http://www.gnu.org/licenses/>.
15
16#ifndef MIRAI_MESSAGE_INTERFACE_HPP_
17#define MIRAI_MESSAGE_INTERFACE_HPP_
18
19
20#include <memory>
21#include <string>
22#include <type_traits>
23
25
26namespace Mirai
27{
28
29/**
30 * @brief Common interface for all message types
31 *
32 */
34{
35protected:
36 IMessage() = default;
37
38 virtual MessageTypes GetType_() const = 0;
39 virtual bool isSendSupported_() const = 0;
40 virtual bool isValid_() const = 0;
41
42public:
43 virtual ~IMessage() = default;
44
45 virtual std::unique_ptr<IMessage> clone() const = 0;
46
47 /**
48 * @brief Return the type of the class
49 *
50 * @return `MessageTypes`
51 */
52 MessageTypes type() const { return this->GetType_(); }
53
54 /**
55 * @brief 检查消息是否可以用于发送
56 *
57 * 部分消息仅支持接收
58 * @return `bool`
59 */
60 bool allowSend() const { return this->isSendSupported_(); }
61
62 /**
63 * @brief 检查消息是否有效
64 *
65 * 发送无效消息( `valid() = false` )会导致mirai-api-http返回400或500错误,并抛出异常。
66 * 检测的内容靠测试经验和mirai-api-http源码确定, `valid() = true` 不保证一定能发送成功。
67 * @return `bool`
68 */
69 bool valid() const { return this->isValid_(); }
70};
71
72/**
73 * @brief CRTP helper layer
74 *
75 */
76template <class Message>
77class IMessageImpl : public IMessage
78{
79protected:
80 // Better not to set default value to avoid errors
81 // static constexpr bool SUPPORT_SEND_ = true;
82 // static constexpr MessageTypes TYPE_ = MessageTypes::ENUM_END;
83
84 IMessageImpl() = default;
85
86 MessageTypes GetType_() const override { return Message::TYPE_; }
87
88 bool isSendSupported_() const override { return Message::SUPPORT_SEND_; }
89
90private:
91 Message& top_() { return *static_cast<Message*>(this); }
92 const Message& top_() const { return *static_cast<const Message*>(this); }
93
94public:
95 static constexpr MessageTypes GetType() { return Message::TYPE_; }
96
97 static constexpr bool isSendSupported() { return Message::SUPPORT_SEND_; }
98
99 std::unique_ptr<IMessage> clone() const override
100 {
101 return std::make_unique<Message>(top_());
102 }
103};
104
105} // namespace Mirai
106
107
108#endif
CRTP helper layer
Definition: IMessage.hpp:78
std::unique_ptr< IMessage > clone() const override
Definition: IMessage.hpp:99
static constexpr MessageTypes GetType()
Definition: IMessage.hpp:95
IMessageImpl()=default
static constexpr bool isSendSupported()
Definition: IMessage.hpp:97
bool isSendSupported_() const override
Definition: IMessage.hpp:88
MessageTypes GetType_() const override
Definition: IMessage.hpp:86
Common interface for all message types
Definition: IMessage.hpp:34
virtual ~IMessage()=default
IMessage()=default
virtual bool isSendSupported_() const =0
virtual bool isValid_() const =0
virtual std::unique_ptr< IMessage > clone() const =0
bool allowSend() const
检查消息是否可以用于发送
Definition: IMessage.hpp:60
MessageTypes type() const
Return the type of the class
Definition: IMessage.hpp:52
bool valid() const
检查消息是否有效
Definition: IMessage.hpp:69
virtual MessageTypes GetType_() const =0
所有mirai相关的对象的命名空间