Skip to content
This repository was archived by the owner on Aug 16, 2024. It is now read-only.

Commit 286a55e

Browse files
committed
新增: MessageChain 支持 Insert 方法.
1 parent 3c6f77f commit 286a55e

File tree

2 files changed

+19
-0
lines changed

2 files changed

+19
-0
lines changed

UnitTest/main.cpp

+10
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,16 @@ TEST(MessageChain_Test, Add_and_Get3) {
6262
ASSERT_STREQ("Hello", m.Text().c_str());
6363
}
6464

65+
TEST(MessageChain_Test, Insert_and_Get) {
66+
using namespace Cyan;
67+
MessageChain mc;
68+
mc.Add<PlainMessage>("OOOOOO");
69+
mc.Insert(mc.begin(), PlainMessage("Hello"));
70+
ASSERT_TRUE(mc[0]->GetType() == "Plain");
71+
auto mptr = std::dynamic_pointer_cast<PlainMessage>(mc[0]);
72+
ASSERT_STREQ("Hello", mptr->Text().c_str());
73+
}
74+
6575
TEST(MessageChain_Test, GetAll) {
6676
using namespace Cyan;
6777
MessageChain mc;

include/mirai/defs/message_chain.hpp

+9
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,15 @@ namespace Cyan
9999
return *this;
100100
}
101101

102+
template<class Iterator, class T>
103+
void Insert(const Iterator& iterator, T&& m)
104+
{
105+
using real_type = typename std::remove_const<typename std::remove_reference<T>::type >::type;
106+
static_assert(std::is_base_of<IMessage, real_type>::value, "只能接受 IMessage 的派生类");
107+
std::shared_ptr<IMessage> m_ptr(new real_type(std::forward<T>(m)));
108+
messages_.insert(iterator, std::move(m_ptr));
109+
}
110+
102111
template<class T>
103112
void Remove(const T& m)
104113
{

0 commit comments

Comments
 (0)