工程文件
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
主程序
#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();
}
参考链接