Skip to content. Skip to navigation

nf.ull.es

Sections
Personal tools
You are here: Home Software tpipeline tpipeline.h
Document Actions

tpipeline.h

Click here to get the file

Size 3.6 kB - File type text/x-chdr

File contents

/*
   tpipeline and tfilter: a type safe wrapper template around TBB pipeline.

   Copyright (C) 2007 Carlos Merino Gracia <cmerino@ull.es>

   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 <typename Input, typename Output>
   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<void *>(operator()(static_cast<Input *>(d)));
      }

      // This class is a wrapper around tbb::filter
      class FilterWrapper : public filter {
      public:
         tfilter<Input, Output> *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 <typename Input, typename Middle, typename Output>
      class PipelineNode : public tfilter<Input, Output> {
         const tfilter<Input, Middle> &f1;
         const tfilter<Middle, Output> &f2;

      public:
         virtual void insertIntoPipeline(tbb::pipeline &p) const {
            f1.insertIntoPipeline(p);
            f2.insertIntoPipeline(p);
         }
         PipelineNode(const tfilter<Input, Middle> &_f1, const tfilter<Middle, Output> &_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 <typename T1, typename T2, typename T3>
   inline internal::PipelineNode<T1, T2, T3> operator>>(const tfilter<T1, T2> &f1, const tfilter<T2, T3> &f2) {
      return internal::PipelineNode<T1, T2, T3>(f1, f2);
   }

   // 
   inline void pipeline_run(int tokens, const tfilter<void, void> &t) {
      pipeline pipe;
      t.insertIntoPipeline(pipe);
      pipe.run(tokens);
      pipe.clear();
   }
}

#endif
Navigation
Log in