/* tpipeline and tfilter: a type safe wrapper template around TBB pipeline. Copyright (C) 2007 Carlos Merino Gracia This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License version 2 as published by the Free Software Foundation. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. As a special exception, you may use this file as part of a free softwar library without restriction. Specifically, if other files instantiat templates or use macros or inline functions from this file, or you compil this file and link it with other files to produce an executable, thi file does not by itself cause the resulting executable to be covered b the GNU General Public License. This exception does not howeve invalidate any other reasons why the executable file might be covered b the GNU General Public License. $Id: tpipeline.h 680 2007-11-22 09:49:45Z carlos $ */ /* Please check http://www.nf.ull.es/software/tpipeline for additional documentation. */ #ifndef TBB_TPIPELINE_H #define TBB_TPIPELINE_H #include "tbb/pipeline.h" namespace tbb { template class tfilter { void *operate(void *d) { // The casts are done inside the wrapper class, ensuring // that when we are here, the compiler has already checked the types. return static_cast(operator()(static_cast(d))); } // This class is a wrapper around tbb::filter class FilterWrapper : public filter { public: tfilter *father; FilterWrapper(bool serial) : tbb::filter(serial) { } virtual void *operator()(void *d) { return father->operate(d); } }; mutable FilterWrapper fi; public: typedef enum { serial, parallel } filterState; tfilter(filterState state = serial) : fi(state == serial) { fi.father = this; } virtual ~tfilter() { } virtual void insertIntoPipeline(tbb::pipeline &p) const { p.add_filter(fi); } virtual Output *operator()(Input *c) = 0; }; namespace internal { template class PipelineNode : public tfilter { const tfilter &f1; const tfilter &f2; public: virtual void insertIntoPipeline(tbb::pipeline &p) const { f1.insertIntoPipeline(p); f2.insertIntoPipeline(p); } PipelineNode(const tfilter &_f1, const tfilter &_f2) : f1(_f1), f2(_f2) { } // This operator will never be called. virtual Output *operator()(Input *c) { return 0; } }; } // operator>> overload that allows to construct a sequence of filters template inline internal::PipelineNode operator>>(const tfilter &f1, const tfilter &f2) { return internal::PipelineNode(f1, f2); } // inline void pipeline_run(int tokens, const tfilter &t) { pipeline pipe; t.insertIntoPipeline(pipe); pipe.run(tokens); pipe.clear(); } } #endif