VTK  9.0.1
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 #include "vtkConfigure.h"
74 
75 #ifndef VTK_LEGACY_REMOVE
76 
77 #include "vtkDispatcher_Private.h" //needed for Functor,CastingPolicy,TypeInfo
78 #include <map> //Required for the storage of template params to runtime params
79 
80 template <class BaseLhs, class BaseRhs = BaseLhs, typename ReturnType = void,
81  template <class, class> class CastingPolicy = vtkDispatcherCommon::vtkCaster>
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>
159  const Functor& fun, long)
160 {
161  typedef vtkDoubleDispatcherPrivate::FunctorDoubleDispatcherHelper<BaseLhs, BaseRhs, SomeLhs,
162  SomeRhs, ReturnType, CastingPolicy<SomeLhs, BaseLhs>, CastingPolicy<SomeRhs, BaseRhs>, Functor>
163  Adapter;
164  Adapter ada(fun);
165  MappedType mt(ada);
166  DoAddFunctor(typeid(SomeLhs), typeid(SomeRhs), mt);
167 }
168 
169 //----------------------------------------------------------------------------
170 template <class BaseLhs, class BaseRhs, typename ReturnType,
171  template <class, class> class CastingPolicy>
172 template <class SomeLhs, class SomeRhs, class Functor>
174  Functor* fun, int)
175 {
176  typedef vtkDoubleDispatcherPrivate::FunctorRefDispatcherHelper<BaseLhs, BaseRhs, SomeLhs, SomeRhs,
177  ReturnType, CastingPolicy<SomeLhs, BaseLhs>, CastingPolicy<SomeRhs, BaseRhs>, Functor>
178  Adapter;
179  Adapter ada(*fun);
180  MappedType mt(ada);
181  DoAddFunctor(typeid(SomeLhs), typeid(SomeRhs), mt);
182 }
183 
184 //----------------------------------------------------------------------------
185 template <class BaseLhs, class BaseRhs, typename ReturnType,
186  template <class, class> class CastingPolicy>
188  TypeInfo lhs, TypeInfo rhs, MappedType fun)
189 {
190  FunctorMap[KeyType(lhs, rhs)] = fun;
191 }
192 
193 //----------------------------------------------------------------------------
194 template <class BaseLhs, class BaseRhs, typename ReturnType,
195  template <class, class> class CastingPolicy>
197  TypeInfo lhs, TypeInfo rhs)
198 {
199  return FunctorMap.erase(KeyType(lhs, rhs)) == 1;
200 }
201 
202 //----------------------------------------------------------------------------
203 template <class BaseLhs, class BaseRhs, typename ReturnType,
204  template <class, class> class CastingPolicy>
206  BaseLhs* lhs, BaseRhs* rhs)
207 {
208  typename MapType::key_type k(typeid(*lhs), typeid(*rhs));
209  typename MapType::iterator i = FunctorMap.find(k);
210  if (i == FunctorMap.end())
211  {
212  // we don't want to throw exceptions so we have two options.
213  // we can return the default, or make a lightweight struct for return value
214  return ReturnType();
215  }
216  return (i->second)(*lhs, *rhs);
217 }
218 
219 #endif // legacy
220 #endif // vtkDoubleDispatcher_h
221 // VTK-HeaderTest-Exclude: vtkDoubleDispatcher.h
vtkDoubleDispatcher::DoAddFunctor
void DoAddFunctor(TypeInfo lhs, TypeInfo rhs, MappedType fun)
Definition: vtkDoubleDispatcher.h:187
vtkDispatcherCommon::TypeInfo
Definition: vtkDispatcher_Private.h:377
vtkDoubleDispatcher::TypeInfo
vtkDispatcherCommon::TypeInfo TypeInfo
Definition: vtkDoubleDispatcher.h:136
vtkDoubleDispatcher::Add
void Add(Functor fun)
Add in a functor that is mapped to the combination of the two template parameters passed in.
Definition: vtkDoubleDispatcher.h:99
vtkDispatcher_Private.h
vtkDoubleDispatcher::FunctorMap
MapType FunctorMap
Definition: vtkDoubleDispatcher.h:144
vtkDoubleDispatcher::MapType
std::map< KeyType, MappedType > MapType
Definition: vtkDoubleDispatcher.h:143
vtkDoubleDispatcherPrivate::Functor
Definition: vtkDispatcher_Private.h:322
vtkDoubleDispatcher::KeyType
std::pair< TypeInfo, TypeInfo > KeyType
Definition: vtkDoubleDispatcher.h:142
vtkDoubleDispatcher::Go
ReturnType Go(BaseLhs *lhs, BaseRhs *rhs)
Given two pointers of objects that derive from the BaseLhs and BaseRhs we find the matching functor t...
Definition: vtkDoubleDispatcher.h:205
vtkDoubleDispatcher::MappedType
vtkDoubleDispatcherPrivate::Functor< ReturnType, BaseLhs, BaseRhs > MappedType
Definition: vtkDoubleDispatcher.h:137
vtkDoubleDispatcher::Remove
bool Remove()
Remove a functor that is bound to the given parameter types.
Definition: vtkDoubleDispatcher.h:110
vtkDoubleDispatcher
Dispatch to functor based on two pointer types.
Definition: vtkDoubleDispatcher.h:82
vtkDispatcherCommon::vtkCaster
Definition: vtkDispatcher_Private.h:370
vtkDoubleDispatcherPrivate::FunctorDoubleDispatcherHelper
Definition: vtkDispatcher_Private.h:231
vtkDoubleDispatcher::DoRemove
bool DoRemove(TypeInfo lhs, TypeInfo rhs)
Definition: vtkDoubleDispatcher.h:196
vtkDoubleDispatcherPrivate::FunctorRefDispatcherHelper
Definition: vtkDispatcher_Private.h:205