Pipeline模型在QT中的实现

文章展示了如何使用C++实现基于QT的GUI应用配置,以及一个基于模板的组件化系统,该系统支持数据生命周期(初始化、启动、关闭)和管道(pipeline)模式。通过抽象组件和模板类,实现了不同组件间的数据传递和处理,例如IntSource、intStringChannel和printSink之间的数据流动。
摘要由CSDN通过智能技术生成
  1. 工程文件

QT -= gui

CONFIG += c++17 console
CONFIG -= app_bundle

# The following define makes your compiler emit warnings if you use
# any Qt feature that has been marked deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS

# You can also make your code fail to compile if it uses deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0

SOURCES += \
        main.cpp

# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target
  1. 主程序

#include <iostream>
#include <unordered_set>
#include <type_traits>
#include <string>
#include <cassert>

using namespace std;

class LifeCycle
{
public:
    LifeCycle() {}
    virtual void init(std::string config) = 0;
    virtual void startUp() = 0;
    virtual void shutDown() = 0;
};

template <typename T>
class Component:public LifeCycle
{
public:
    virtual std::string getName() = 0;
    virtual void execute(T t) = 0;
};

template<typename T, typename R>
class AbstractComponent : public Component<T> {
 private:
    std::unordered_set<shared_ptr<Component<R> > > down_stream;
 protected:
    const std::unordered_set<shared_ptr<Component<R> > > &getDownStream() {
        return down_stream;
    }

 protected:
    virtual R doExecute(T t) = 0;

 public:
    void addDownStream(shared_ptr<Component<R>> component) {
        down_stream.insert(component);
    }

    void init(std::string config) override {
    }

    void startUp() override {
        for (auto &&obj : this->getDownStream()) {
            obj->startUp();
        }
        cout << "------------------ " + this->getName() + " is starting ----------------------" << endl;
    }

    void shutDown() override {
        auto downStreams = this->getDownStream();
        for (auto &&obj : downStreams) {
            obj->shutDown();
        }
        cout << "------------------ " + this->getName() + " is stopping ----------------------" << endl;
    }

    void execute(T t) override {
        R r = doExecute(t);
        cout << this->getName() + "\treceive\t" << typeid(t).name() << "\t" << t << "\treturn\t" << typeid(r).name()
             << "\t" << r << endl;

        for (auto &&obj : getDownStream()) {//读取数据
            obj->execute(r);//一层层查找
        }

        if constexpr (is_same_v<R, void>) {
            return;
        }

    }
};

/**从这向下才是pipeline的操作,上面的是数据的生命周期**/

template<typename T, typename R>
using Source = AbstractComponent<T, R>;//using来重命名,使用typedef定义的别名和使用using定义的别名在语义上是等效的。 唯一的区别是typedef在模板中有一定的局限性,而using没有。https://blog.csdn.net/qq_35789421/article/details/117591212

template<typename T, typename R>
using Channel = AbstractComponent<T, R>;

template<typename T, typename R>
using Sink = AbstractComponent<T, R>;

class printSink;// 申明class,以防找不到class申明,如果写在两个文件中,会采用前置声明的方式声明class,这里并没有什么用。https://www.shuzhiduo.com/A/obzb7pZVJE/

class intStringChannel;

class printSink : public Sink<string, int> {
 public:
    string getName() override {
        return "printSink";
    }

 protected:
    int doExecute(string t) override {
        return INT_MIN;
    }
};

class intStringChannel : public Channel<int, string> {
 public:
    string getName() override {
        return "intStringChannel";
    }

    void startUp() override {

    }

 protected:
    string doExecute(int t) override {
        return to_string(t + 100);
    }
};

class IntSource : public Source<int, int> {
 private:
    int val = 0;
 public:
    void init(std::string config) override {
        cout << "--------- " + getName() + " init --------- ";
        val = 1;
    }

    string getName() override {
        return "Int Source";
    }

    void startUp() override {
        this->execute(val);//处理数据
    }

 protected:
    int doExecute(int) override {
        return val + 1;
    }
};

template<typename R, typename T>
class pipeline : public LifeCycle {
 private:
    shared_ptr<Source<R, T>> source;

 public:
    void setSource(shared_ptr<Source<R, T>> component) {
        source = component;//传入类
    }

    void init(std::string config) override {
    }

    void startUp() override {
        assert(source.get() != nullptr);
        source->startUp();//完成类函数的处理
    }

    void shutDown() override {
        source->shutDown();
    }
};

int main() {
    pipeline<int, int> p;
    // source
    auto is = make_shared<IntSource>();

    // channel
    auto isc = make_shared<intStringChannel>();

    // sink
    auto ps = make_shared<printSink>();

    is->addDownStream(isc);
    isc->addDownStream(ps);

    // 设置 source
    p.setSource(is);

    // 启动
    p.startUp();

    //关闭
    p.shutDown();
}
  1. 参考链接

(80条消息) C++之管道(Pipeline)模式_c++ 管道_敢敢のwings的博客-CSDN博客

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值