VTK  9.1.0
vtkDoubleDispatcher.h
Go to the documentation of this file.
1 /*=========================================================================
2 
3  Program: Visualization Toolkit
4  Module: vtkDoubleDispatcher.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 
70 #ifndef vtkDoubleDispatcher_h
71 #define vtkDoubleDispatcher_h
72 
73 #ifndef __VTK_WRAP__
74 
75 #include "vtkDeprecation.h" // for VTK_DEPRECATED_IN_9_0_0
76 #include "vtkDispatcher_Private.h" //needed for Functor,CastingPolicy,TypeInfo
77 #include <map> //Required for the storage of template params to runtime params
78 
79 template <class BaseLhs, class BaseRhs = BaseLhs, typename ReturnType = void,
80  template <class, class> class CastingPolicy = vtkDispatcherCommon::vtkCaster>
81 VTK_DEPRECATED_IN_9_0_0("Use vtkArrayDispatch")
83 {
84 public:
98  template <class SomeLhs, class SomeRhs, class Functor>
99  void Add(Functor fun)
100  {
101  VTK_LEGACY_BODY(vtkDoubleDispatcher, "VTK 9.0");
102  this->AddInternal<SomeLhs, SomeRhs>(fun, 1);
103  }
104 
109  template <class SomeLhs, class SomeRhs>
110  bool Remove()
111  {
112  return DoRemove(typeid(SomeLhs), typeid(SomeRhs));
113  }
114 
133  ReturnType Go(BaseLhs* lhs, BaseRhs* rhs);
134 
135 protected:
138 
139  void DoAddFunctor(TypeInfo lhs, TypeInfo rhs, MappedType fun);
140  bool DoRemove(TypeInfo lhs, TypeInfo rhs);
141 
142  typedef std::pair<TypeInfo, TypeInfo> KeyType;
143  typedef std::map<KeyType, MappedType> MapType;
145 
146 private:
147  template <class SomeLhs, class SomeRhs, class Functor>
148  void AddInternal(const Functor& fun, long);
149  template <class SomeLhs, class SomeRhs, class Functor>
150  void AddInternal(Functor* fun, int);
151 };
152 
153 // We are making all these method non-inline to reduce compile time overhead
154 //----------------------------------------------------------------------------
155 template <class BaseLhs, class BaseRhs, typename ReturnType,
156  template <class, class> class CastingPolicy>
157 template <class SomeLhs, class SomeRhs, class Functor>
158 VTK_DEPRECATED_IN_9_0_0("Use vtkArrayDispatch")
159 void vtkDoubleDispatcher<BaseLhs, BaseRhs, ReturnType, CastingPolicy>::AddInternal(
160  const Functor& fun, long)
161 {
162  typedef vtkDoubleDispatcherPrivate::FunctorDoubleDispatcherHelper<BaseLhs, BaseRhs, SomeLhs,
163  SomeRhs, ReturnType, CastingPolicy<SomeLhs, BaseLhs>, CastingPolicy<SomeRhs, BaseRhs>, Functor>
164  Adapter;
165  Adapter ada(fun);
166  MappedType mt(ada);
167  DoAddFunctor(typeid(SomeLhs), typeid(SomeRhs), mt);
168 }
169 
170 //----------------------------------------------------------------------------
171 template <class BaseLhs, class BaseRhs, typename ReturnType,
172  template <class, class> class CastingPolicy>
173 template <class SomeLhs, class SomeRhs, class Functor>
174 VTK_DEPRECATED_IN_9_0_0("Use vtkArrayDispatch")
175 void vtkDoubleDispatcher<BaseLhs, BaseRhs, ReturnType, CastingPolicy>::AddInternal(
176  Functor* fun, int)
177 {
178  typedef vtkDoubleDispatcherPrivate::FunctorRefDispatcherHelper<BaseLhs, BaseRhs, SomeLhs, SomeRhs,
179  ReturnType, CastingPolicy<SomeLhs, BaseLhs>, CastingPolicy<SomeRhs, BaseRhs>, Functor>
180  Adapter;
181  Adapter ada(*fun);
182  MappedType mt(ada);
183  DoAddFunctor(typeid(SomeLhs), typeid(SomeRhs), mt);
184 }
185 
186 //----------------------------------------------------------------------------
187 template <class BaseLhs, class BaseRhs, typename ReturnType,
188  template <class, class> class CastingPolicy>
189 VTK_DEPRECATED_IN_9_0_0("Use vtkArrayDispatch")
190 void vtkDoubleDispatcher<BaseLhs, BaseRhs, ReturnType, CastingPolicy>::DoAddFunctor(
191  TypeInfo lhs, TypeInfo rhs, MappedType fun)
192 {
193  FunctorMap[KeyType(lhs, rhs)] = fun;
194 }
195 
196 //----------------------------------------------------------------------------
197 template <class BaseLhs, class BaseRhs, typename ReturnType,
198  template <class, class> class CastingPolicy>
199 VTK_DEPRECATED_IN_9_0_0("Use vtkArrayDispatch")
200 bool vtkDoubleDispatcher<BaseLhs, BaseRhs, ReturnType, CastingPolicy>::DoRemove(
201  TypeInfo lhs, TypeInfo rhs)
202 {
203  return FunctorMap.erase(KeyType(lhs, rhs)) == 1;
204 }
205 
206 //----------------------------------------------------------------------------
207 template <class BaseLhs, class BaseRhs, typename ReturnType,
208  template <class, class> class CastingPolicy>
209 VTK_DEPRECATED_IN_9_0_0("Use vtkArrayDispatch")
210 ReturnType vtkDoubleDispatcher<BaseLhs, BaseRhs, ReturnType, CastingPolicy>::Go(
211  BaseLhs* lhs, BaseRhs* rhs)
212 {
213  typename MapType::key_type k(typeid(*lhs), typeid(*rhs));
214  typename MapType::iterator i = FunctorMap.find(k);
215  if (i == FunctorMap.end())
216  {
217  // we don't want to throw exceptions so we have two options.
218  // we can return the default, or make a lightweight struct for return value
219  return ReturnType();
220  }
221  return (i->second)(*lhs, *rhs);
222 }
223 
224 #endif // __VTK_WRAP__
225 #endif // vtkDoubleDispatcher_h
226 // VTK-HeaderTest-Exclude: vtkDoubleDispatcher.h
Dispatch to functor based on two pointer types.
std::pair< TypeInfo, TypeInfo > KeyType
vtkDispatcherCommon::TypeInfo TypeInfo
bool Remove()
Remove a functor that is bound to the given parameter types.
void Add(Functor fun)
Add in a functor that is mapped to the combination of the two template parameters passed in.
std::map< KeyType, MappedType > MapType
vtkDoubleDispatcherPrivate::Functor< ReturnType, BaseLhs, BaseRhs > MappedType
#define VTK_DEPRECATED_IN_9_0_0(reason)