VTK  9.0.2
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 #include "vtkConfigure.h"
77 
78 #ifndef VTK_LEGACY_REMOVE
79 
80 #include "vtkDispatcher_Private.h" //needed for Functor,CastingPolicy,TypeInfo
81 #include <map> //Required for the storage of template params to runtime params
82 
84 // class template FunctorDispatcher
86 template <class BaseLhs, typename ReturnType = void,
87  template <class, class> class CastingPolicy = vtkDispatcherCommon::vtkCaster>
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>
161 {
162  typedef vtkDispatcherPrivate::FunctorDispatcherHelper<BaseLhs, SomeLhs, ReturnType,
163  CastingPolicy<SomeLhs, BaseLhs>, Functor>
164  Adapter;
165  Adapter ada(fun);
166  MappedType mt(ada);
167  DoAddFunctor(typeid(SomeLhs), mt);
168 }
169 
170 //----------------------------------------------------------------------------
171 template <class BaseLhs, typename ReturnType, template <class, class> class CastingPolicy>
172 template <class SomeLhs, class Functor>
174 {
175  typedef vtkDispatcherPrivate::FunctorRefDispatcherHelper<BaseLhs, SomeLhs, ReturnType,
176  CastingPolicy<SomeLhs, BaseLhs>, Functor>
177  Adapter;
178  Adapter ada(*fun);
179  MappedType mt(ada);
180  DoAddFunctor(typeid(SomeLhs), mt);
181 }
182 
183 //----------------------------------------------------------------------------
184 template <class BaseLhs, typename ReturnType, template <class, class> class CastingPolicy>
186 {
187  FunctorMap[TypeInfo(lhs)] = fun;
188 }
189 
190 //----------------------------------------------------------------------------
191 template <class BaseLhs, typename ReturnType, template <class, class> class CastingPolicy>
193 {
194  return FunctorMap.erase(TypeInfo(lhs)) == 1;
195 }
196 
197 //----------------------------------------------------------------------------
198 template <class BaseLhs, typename ReturnType, template <class, class> class CastingPolicy>
200 {
201  typename MapType::key_type k(typeid(*lhs));
202  typename MapType::iterator i = FunctorMap.find(k);
203  if (i == FunctorMap.end())
204  {
205  // we return a default type, currently i don't want exceptions thrown
206  return ReturnType();
207  }
208  return (i->second)(*lhs);
209 }
210 
211 #endif // legacy
212 
213 #endif // vtkDispatcher_h
214 // VTK-HeaderTest-Exclude: vtkDispatcher.h
Dispatch to functor based on a pointer type.
Definition: vtkDispatcher.h:89
ReturnType Go(BaseLhs *lhs)
Given a pointer to an object that derives from the BaseLhs we find the matching functor that was adde...
bool Remove()
Remove a functor that is bound to the given parameter type.
MapType FunctorMap
bool DoRemove(TypeInfo lhs)
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
void DoAddFunctor(TypeInfo lhs, MappedType fun)