cpp-mirai-client  v2.6.1
cpp client for mirai-api-http
MessageChain.cpp
浏览该文件的文档.
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#include "MessageChain.hpp"
17
18#include <algorithm>
19#include <optional>
20#include <string>
21
22namespace Mirai
23{
24
25MessageChain::MessageChain(const MessageChain& rhs) = default;
26
28{
29 if (this != &rhs)
30 {
31 this->message_ = rhs.message_;
32 }
33 return *this;
34}
35
36MessageChain::MessageChain(MessageChain&& rhs) noexcept : message_(std::move(rhs.message_)) {}
37
39{
40 if (this != &rhs) this->message_ = std::move(rhs.message_);
41 return *this;
42}
43
44
46{
47 this->message_.reserve(this->message_.size() + rhs.message_.size());
48 this->message_.insert(this->message_.end(), rhs.message_.begin(), rhs.message_.end());
49 return *this;
50}
51
53{
54 this->message_.reserve(this->message_.size() + rhs.message_.size());
55 this->message_.insert(
56 this->message_.end(),
57 std::make_move_iterator(rhs.message_.begin()),
58 std::make_move_iterator(rhs.message_.end())
59 );
60 rhs.message_.clear();
61 return *this;
62}
63
64
66{
67 this->message_.push_back(m);
68 return *this;
69}
70
72{
73 this->message_.push_back(std::move(m));
74 return *this;
75}
76
77
79{
80 MessageChain msg = lhs;
81 msg += rhs;
82 return msg;
83}
84
86{
87 MessageChain msg = lhs;
88 msg += rhs;
89 return msg;
90}
91
92
93std::optional<MessageChain::SourceInfo> MessageChain::GetSourceInfo() const
94{
95 for (const auto& p : this->message_)
96 {
97 if (p.type() == SourceMessage::GetType())
98 {
99 const auto& s = p.as<SourceMessage>();
100 return SourceInfo{s.GetMessageId(), s.GetTimestamp()};
101 }
102 }
103 return std::nullopt;
104}
105
106
108{
109 this->message_.erase(std::remove_if(this->message_.begin(), this->message_.end(),
110 [](const MessageElement& p)
111 { return !p.valid(); }),
112 this->message_.end());
113}
114
116{
117 bool empty = true;
118 for (const auto& p : this->message_)
119 {
120 if (!p.valid()) return false;
121 if (p.allowSend()) empty = false;
122 }
123 return !empty;
124}
125
126} // namespace Mirai
static constexpr MessageTypes GetType()
Definition: IMessage.hpp:95
消息链对象,由一系列消息元素组成
MessageChain & operator+=(const MessageChain &rhs)
bool empty() const noexcept
STL-like interface
MessageChain()=default
MessageContainer message_
std::optional< SourceInfo > GetSourceInfo() const
获取消息链中的来源信息
bool valid() const
检查消息链是否有效
MessageChain & operator=(const MessageChain &rhs)
void RemoveInvalid()
去除消息链中的无效消息
所有mirai相关的对象的命名空间
MessageChain operator+(const MessageChain &lhs, const MessageChain &rhs)