VTK
dox/Common/DataModel/vtkDataArrayDispatcher.h
Go to the documentation of this file.
00001 /*=========================================================================
00002 
00003   Program:   Visualization Toolkit
00004   Module:    vtkDataArrayDispatcher.h
00005 
00006   Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
00007   All rights reserved.
00008   See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
00009 
00010      This software is distributed WITHOUT ANY WARRANTY; without even
00011      the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
00012      PURPOSE.  See the above copyright notice for more information.
00013 
00014 =========================================================================*/
00015 
00054 #ifndef __vtkDataArrayDispatcher_h
00055 #define __vtkDataArrayDispatcher_h
00056 
00057 #include "vtkType.h" //Required for vtkIdType
00058 #include "vtkDataArray.h" //required for constructor of the vtkDataArrayFunctor
00059 #include <map> //Required for the storage of template params to runtime params
00060 
00062 // Object that is passed to all functor that are used with this class
00063 // This allows the user the ability to find info about the size
00065 template<typename T>
00066 struct vtkDataArrayDispatcherPointer
00067 {
00068   typedef T ValueType;
00069 
00070   vtkIdType NumberOfTuples;
00071   vtkIdType NumberOfComponents;
00072   ValueType* RawPointer;
00073 
00074   explicit vtkDataArrayDispatcherPointer(vtkDataArray* array):
00075     NumberOfTuples(array->GetNumberOfTuples()),
00076     NumberOfComponents(array->GetNumberOfComponents()),
00077     RawPointer(static_cast<ValueType*>(array->GetVoidPointer(0)))
00078     {}
00079 };
00080 
00082 // class template FunctorDispatcher
00084 template
00085   <
00086     class DefaultFunctorType,
00087     typename ReturnType = void
00088   >
00089 class vtkDataArrayDispatcher
00090 {
00091 public:
00092 
00101   vtkDataArrayDispatcher(DefaultFunctorType& f);
00102 
00105   vtkDataArrayDispatcher();
00106 
00107   virtual ~vtkDataArrayDispatcher();
00108 
00110   ReturnType Go(vtkDataArray* lhs);
00111 
00112 protected:
00113   DefaultFunctorType* DefaultFunctor;
00114   bool OwnsFunctor;
00115 };
00116 
00117 //We are making all these method non-inline to reduce compile time overhead
00118 
00119 //----------------------------------------------------------------------------
00120 template<class DefaultFunctorType,typename ReturnType>
00121 vtkDataArrayDispatcher<DefaultFunctorType,ReturnType>::vtkDataArrayDispatcher(DefaultFunctorType& fun):
00122   DefaultFunctor(&fun),
00123   OwnsFunctor(false)
00124   {
00125   }
00126 
00127 //----------------------------------------------------------------------------
00128 template<class DefaultFunctorType,typename ReturnType>
00129 vtkDataArrayDispatcher<DefaultFunctorType,ReturnType>::vtkDataArrayDispatcher():
00130   DefaultFunctor(new DefaultFunctorType()),
00131   OwnsFunctor(true)
00132   {
00133   }
00134 
00135 //----------------------------------------------------------------------------
00136 template<class DefaultFunctorType,typename ReturnType>
00137 vtkDataArrayDispatcher<DefaultFunctorType,ReturnType>::~vtkDataArrayDispatcher()
00138   {
00139   if(OwnsFunctor)
00140     {
00141     delete this->DefaultFunctor;
00142     }
00143   }
00144 
00145 //----------------------------------------------------------------------------
00146 template <class DefaultFunctorType,typename ReturnType>
00147 ReturnType vtkDataArrayDispatcher<DefaultFunctorType,ReturnType>
00148 ::Go(vtkDataArray* lhs)
00149   {
00150   switch(lhs->GetDataType())
00151       {
00152       vtkTemplateMacro(return (*this->DefaultFunctor) (
00153                       vtkDataArrayDispatcherPointer<VTK_TT>(lhs) ));
00154       }
00155   return ReturnType();
00156   }
00157 
00158 #endif // __vtkDataArrayDispatcher_h
00159 // VTK-HeaderTest-Exclude: vtkDataArrayDispatcher.h