浏览代码

refactor & feat: 上下行消息

SongZihuan 3 年之前
父节点
当前提交
46d10ca6c3
共有 6 个文件被更改,包括 173 次插入4 次删除
  1. 3 3
      include/core/code.hpp
  2. 54 0
      include/core/msg.hpp
  3. 73 0
      src/core/msg.cpp
  4. 3 1
      test/src/CMakeLists.txt
  5. 11 0
      test/src/core-down-msg.cpp
  6. 29 0
      test/src/core-up-msg.cpp

+ 3 - 3
include/core/code.hpp

@@ -1,5 +1,5 @@
-#ifndef AFUN_CODE_H
-#define AFUN_CODE_H
+#ifndef AFUN_CODE_HPP
+#define AFUN_CODE_HPP
 #include "tool.hpp"
 
 namespace aFuncore {
@@ -71,4 +71,4 @@ namespace aFuncore {
     };
 }
 
-#endif //AFUN_CODE_H
+#endif //AFUN_CODE_HPP

+ 54 - 0
include/core/msg.hpp

@@ -0,0 +1,54 @@
+#ifndef AFUN_MSG_HPP
+#define AFUN_MSG_HPP
+#include "tool.hpp"
+
+namespace aFuncore {
+    class MessageStream;
+
+    class Message {
+        std::string type;  // 消息类型标注
+        Message *next;  // 下一条消息
+
+    friend class MessageStream;
+    friend class UpMessage;
+
+    public:
+        explicit Message(const std::string &type);
+        virtual ~Message() = default;
+        [[nodiscard]] const std::string &getType() const {return type;}
+    };
+
+    class MessageStream {
+    protected:
+        Message *stream;
+        [[nodiscard]] virtual Message *_getMessage(const std::string &type) const;
+
+    public:
+        MessageStream();
+        virtual ~MessageStream();
+
+        template<class T>
+        [[nodiscard]] T *getMessage(const std::string &type) const {
+            Message *msg = this->_getMessage(type);
+            T *ret = dynamic_cast<T*>(msg);
+            return ret;
+        }
+
+        virtual Message *popMessage(const std::string &type);
+        void pushMessage(Message *msg);
+    };
+
+    class UpMessage : public MessageStream {
+    protected:
+        Message *old;
+    public:
+        explicit UpMessage(const UpMessage *old);
+        ~UpMessage() override;
+        Message *popMessage(const std::string &type) override;
+    };
+
+    class DownMessage : public MessageStream {};
+}
+
+
+#endif //AFUN_MSG_HPP

+ 73 - 0
src/core/msg.cpp

@@ -0,0 +1,73 @@
+#include "msg.hpp"
+using namespace aFuncore;
+using namespace aFuntool;
+
+aFuncore::Message::Message(const std::string &type){
+    this->type = type;
+    next = nullptr;
+}
+
+MessageStream::MessageStream(){
+    stream = nullptr;
+}
+
+MessageStream::~MessageStream(){
+    for (Message *msg = stream; msg != nullptr; msg = msg->next)
+        delete msg;
+}
+
+void MessageStream::pushMessage(Message *msg){
+    msg->next = stream;
+    stream = msg;
+}
+
+Message *MessageStream::_getMessage(const std::string &type) const{
+    for (Message *msg = stream; msg != nullptr; msg = msg->next) {
+        if (msg->type == type)
+            return msg;
+    }
+    return nullptr;
+}
+
+Message *MessageStream::popMessage(const std::string &type) {
+    for (Message **msg = &stream; *msg != nullptr; msg = &((*msg)->next)) {
+        if ((*msg)->type == type) {
+            Message *ret = *msg;
+            *msg = ret->next;
+            return ret;
+        }
+    }
+    return nullptr;
+}
+
+UpMessage::UpMessage(const UpMessage *old) : MessageStream() {
+    if (old != nullptr)
+        this->old = old->stream;
+    else
+        this->old = nullptr;
+    this->stream = this->old;
+}
+
+UpMessage::~UpMessage(){
+    if (old != nullptr) {
+        for (Message **msg = &stream; *msg != nullptr; msg = &((*msg)->next)) {
+            if (*msg == old) {
+                *msg = nullptr;
+                break;
+            }
+        }
+    }
+}
+
+Message *UpMessage::popMessage(const std::string &type){
+    for (Message **msg = &stream; *msg != nullptr; msg = &((*msg)->next)) {
+        if ((*msg) == old)
+            break;
+        if ((*msg)->type == type) {
+            Message *ret = *msg;
+            *msg = ret->next;
+            return ret;
+        }
+    }
+    return nullptr;
+}

+ 3 - 1
test/src/CMakeLists.txt

@@ -28,4 +28,6 @@ set_test_label(tool tool-mem tool-byte tool-dlc tool-regex tool-md5 tool-utf too
 
 add_new_test(core-init COMMAND "$<TARGET_FILE:core-init>")
 add_new_test(core-code COMMAND "$<TARGET_FILE:core-code>")
-add_new_test(core-env-var COMMAND "$<TARGET_FILE:core-env-var>")
+add_new_test(core-env-var COMMAND "$<TARGET_FILE:core-env-var>")
+add_new_test(core-down-msg COMMAND "$<TARGET_FILE:core-down-msg>")
+add_new_test(core-up-msg COMMAND "$<TARGET_FILE:core-up-msg>")

+ 11 - 0
test/src/core-down-msg.cpp

@@ -0,0 +1,11 @@
+#include "msg.hpp"
+using namespace aFuncore;
+using namespace aFuntool;
+
+int main() {
+    auto *um = new DownMessage();
+    um->pushMessage(new Message("test-1"));
+    std::cout << um->getMessage<Message>("test-1") << std::endl;
+    delete um;
+    return 0;
+}

+ 29 - 0
test/src/core-up-msg.cpp

@@ -0,0 +1,29 @@
+#include "msg.hpp"
+using namespace aFuncore;
+using namespace aFuntool;
+
+int main() {
+    auto *um = new UpMessage(nullptr);
+    um->pushMessage(new Message("test-1"));
+    std::cout << um->getMessage<Message>("test-1") << std::endl;
+
+    auto *um2 = new UpMessage(um);
+    um2->pushMessage(new Message("test-2"));
+    std::cout << um2->getMessage<Message>("test-1") << std::endl;
+    std::cout << um2->getMessage<Message>("test-2") << std::endl;
+
+    auto msg1 = um2->popMessage("test-2");
+    auto msg2 = um2->popMessage("test-1");
+
+    std::cout << msg1 << ", " << msg2 << std::endl;
+
+    delete msg1;
+    delete msg2;
+    delete um2;
+
+    std::cout << um->getMessage<Message>("test-1") << std::endl;
+    std::cout << um->getMessage<Message>("test-2") << std::endl;
+
+    delete um;
+    return 0;
+}