VTK  9.1.0
vtkDispatcher.h
Go to the documentation of this file.
1 /*=========================================================================
2 
3  Program: Visualization Toolkit
4  Module: vtkDispatcher.h
5 
6  Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
7  All rights reserved.
8  See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
9 
10  This software is distributed WITHOUT ANY WARRANTY; without even
11  the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
12  PURPOSE. See the above copyright notice for more information.
13 
14 =========================================================================*/
15 
17 // The Loki Library
18 // Copyright (c) 2001 by Andrei Alexandrescu
19 // This code accompanies the book:
20 // Alexandrescu, Andrei. "Modern C++ Design: Generic Programming and Design
21 // Patterns Applied". Copyright (c) 2001. Addison-Wesley.
22 // Permission to use, copy, modify, distribute and sell this software for any
23 // purpose is hereby granted without fee, provided that the above copyright
24 // notice appear in all copies and that both that copyright notice and this
25 // permission notice appear in supporting documentation.
26 // The author or Addison-Wesley Longman make no representations about the
27 // suitability of this software for any purpose. It is provided "as is"
28 // without express or implied warranty.
30 
73 #ifndef vtkDispatcher_h
74 #define vtkDispatcher_h
75 
76 #ifndef __VTK_WRAP__
77 
78 #include "vtkDeprecation.h" // for VTK_DEPRECATED_IN_9_0_0
79 #include "vtkDispatcher_Private.h" //needed for Functor,CastingPolicy,TypeInfo
80 #include <map> //Required for the storage of template params to runtime params
81 
83 // class template FunctorDispatcher
85 template <class BaseLhs, typename ReturnType = void,
86  template <class, class> class CastingPolicy = vtkDispatcherCommon::vtkCaster>
87 VTK_DEPRECATED_IN_9_0_0("Use vtkArrayDispatch")
89 {
90 public:
103  template <class SomeLhs, class Functor>
104  void Add(Functor fun)
105  {
106  VTK_LEGACY_BODY(vtkDispatcher, "VTK 9.0");
107  this->AddInternal<SomeLhs>(fun, 1);
108  }
109 
114  template <class SomeLhs>
115  bool Remove()
116  {
117  return DoRemove(typeid(SomeLhs));
118  }
119 
138  ReturnType Go(BaseLhs* lhs);
139 
140 protected:
143 
144  void DoAddFunctor(TypeInfo lhs, MappedType fun);
145  bool DoRemove(TypeInfo lhs);
146  typedef std::map<TypeInfo, MappedType> MapType;
148 
149 private:
150  template <class SomeLhs, class Functor>
151  void AddInternal(Functor const& fun, long);
152  template <class SomeLhs, class Functor>
153  void AddInternal(Functor* fun, int);
154 };
155 
156 // We are making all these method non-inline to reduce compile time overhead
157 //----------------------------------------------------------------------------
158 template <class BaseLhs, typename ReturnType, template <class, class> class CastingPolicy>
159 template <class SomeLhs, class Functor>
160 VTK_DEPRECATED_IN_9_0_0("Use vtkArrayDispatch")
161 void vtkDispatcher<BaseLhs, ReturnType, CastingPolicy>::AddInternal(const Functor& fun, long)
162 {
163  typedef vtkDispatcherPrivate::FunctorDispatcherHelper<BaseLhs, SomeLhs, ReturnType,
164  CastingPolicy<SomeLhs, BaseLhs>, Functor>
165  Adapter;
166  Adapter ada(fun);
167  MappedType mt(ada);
168  DoAddFunctor(typeid(SomeLhs), mt);
169 }
170 
171 //----------------------------------------------------------------------------
172 template <class BaseLhs, typename ReturnType, template <class, class> class CastingPolicy>
173 template <class SomeLhs, class Functor>
174 VTK_DEPRECATED_IN_9_0_0("Use vtkArrayDispatch")
175 void vtkDispatcher<BaseLhs, ReturnType, CastingPolicy>::AddInternal(Functor* fun, int)
176 {
177  typedef vtkDispatcherPrivate::FunctorRefDispatcherHelper<BaseLhs, SomeLhs, ReturnType,
178  CastingPolicy<SomeLhs, BaseLhs>, Functor>
179  Adapter;
180  Adapter ada(*fun);
181  MappedType mt(ada);
182  DoAddFunctor(typeid(SomeLhs), mt);
183 }
184 
185 //----------------------------------------------------------------------------
186 template <class BaseLhs, typename ReturnType, template <class, class> class CastingPolicy>
187 VTK_DEPRECATED_IN_9_0_0("Use vtkArrayDispatch")
188 void vtkDispatcher<BaseLhs, ReturnType, CastingPolicy>::DoAddFunctor(TypeInfo lhs, MappedType fun)
189 {
190  FunctorMap[TypeInfo(lhs)] = fun;
191 }
192 
193 //----------------------------------------------------------------------------
194 template <class BaseLhs, typename ReturnType, template <class, class> class CastingPolicy>
195 VTK_DEPRECATED_IN_9_0_0("Use vtkArrayDispatch")
196 bool vtkDispatcher<BaseLhs, ReturnType, CastingPolicy>::DoRemove(TypeInfo lhs)
197 {
198  return FunctorMap.erase(TypeInfo(lhs)) == 1;
199 }
200 
201 //----------------------------------------------------------------------------
202 template <class BaseLhs, typename ReturnType, template <class, class> class CastingPolicy>
203 VTK_DEPRECATED_IN_9_0_0("Use vtkArrayDispatch")
204 ReturnType vtkDispatcher<BaseLhs, ReturnType, CastingPolicy>::Go(BaseLhs* lhs)
205 {
206  typename MapType::key_type k(typeid(*lhs));
207  typename MapType::iterator i = FunctorMap.find(k);
208  if (i == FunctorMap.end())
209  {
210  // we return a default type, currently i don't want exceptions thrown
211  return ReturnType();
212  }
213  return (i->second)(*lhs);
214 }
215 
216 #endif // __VTK_WRAP__
217 #endif // vtkDispatcher_h
218 // VTK-HeaderTest-Exclude: vtkDispatcher.h
Dispatch to functor based on a pointer type.
Definition: vtkDispatcher.h:89
bool Remove()
Remove a functor that is bound to the given parameter type.
MapType FunctorMap
std::map< TypeInfo, MappedType > MapType
void Add(Functor fun)
Add in a functor that is mapped to the template SomeLhs parameter.
vtkDispatcherCommon::TypeInfo TypeInfo
vtkDispatcherPrivate::Functor< ReturnType, BaseLhs > MappedType
#define VTK_DEPRECATED_IN_9_0_0(reason)