Pipeline设计模式在算法工程中得实际应用

2022-12-04 16:28:28 浏览数 (3)

NodeBase

代码语言:javascript复制
//node.h
template<typename T>
class NodeBase
{
    public:
        virtual ~NodeBase() = default;
        virtual bool Init(std::string conf) = 0;
        virtual void execute(T t) =0;
        virtual void flush() = 0;
        virtual void startUp() = 0;
};

NodeComponent

代码语言:javascript复制
template<typename T,typename R>
class NodeComponent:public NodeBase<T>
{
    public:
        virtual ~NodeComponent() = default;

        virtual bool Init(std::string conf) = 0;

        virtual R process(T input) = 0;

        virtual void flush() =0;

    public:
        void addDownStream(std::shared_ptr<NodeBase<R>> component)
        {
            NextNode_ = component;
        }

        void execute(T input)override
        {
            R ret = process(input);
            auto nextNode = this->getNext();
            if(nullptr != nextNode)
            {
                nextNode->execute(ret);
            }
        }

        void startUp()override
        {
            auto nextNode = this->getNext();
            if(nullptr != nextNode)
            {
                nextNode->startUp();
            }
        }

    private:
        std::shared_ptr<NodeBase<R>> NextNode_;
        const std::shared_ptr<NodeBase<R>> &getNext()
        {
            return NextNode_;
        }
};

template<typename T, typename R>
using PipelineNode = NodeComponent<T,R>;

NodeA

代码语言:javascript复制
//NodeA
class NodeA:public PipelineNode<int,double>{
    public:
        bool Init(std::string conf)override
        {
            std::cout<<"Init NodeA"<<std::endl;

            return true;
        }

        double process(int input)override
        {
            std::cout<<"process NodeA"<<std::endl;

            return 1.0;
        }

        void flush()override{

        }
};

NodeB

代码语言:javascript复制
//NodeB
class NodeB:public PipelineNode<double,float>{
    public:
        bool Init(std::string conf)override
        {
            std::cout<<"Init NodeB"<<std::endl;

            return true;
        }

        float process(double input)override
        {
            std::cout<<"process NodeB"<<std::endl;

            return 1.0;
        }

        void flush()override{

        }
};

Pipeline

代码语言:javascript复制
//pipeline.h
template<typename T,typename R>
class Pipeline
{
    public:
        void setSource(std::shared_ptr<PipelineNode<T,R>> component)
        {
            source = component;
        }

        void init(std::string conf)
        {
            std::cout<<"init pipeline"<<std::endl;

            source->Init(conf);
        }
     
        void startUp(T input)
        {
            assert(source.get() != nullptr);
            source->execute(input);
        }

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

    private:
        std::shared_ptr<PipelineNode<T,R>> source;
};

PerceptionPipeline

代码语言:javascript复制
//PerceptionPipeline
class PerceptionPipeline
{
    public:
        void Init(std::string conf)
        {
            pipeline_.reset(new Pipeline<int,double>());

            //create node
            auto node_A = std::make_shared<NodeA>();
            auto node_B = std::make_shared<NodeB>();

            node_A->addDownStream(node_B);
            pipeline_->setSource(node_A);

            pipeline_->init(conf);
        }

        void ProcessRequest(int input)
        {
            pipeline_->startUp(input);
        }
    private:
        std::unique_ptr<Pipeline<int,double>> pipeline_;
};

main

代码语言:javascript复制
//main

int main()
{
    std::unique_ptr<PerceptionPipeline> pipeline_;
    pipeline_.reset(new PerceptionPipeline());
    pipeline_->Init("lxk");

    pipeline_->ProcessRequest(1);
}

参考:https://zhuanlan.zhihu.com/p/355034910

0 人点赞