VTK
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 "vtkDispatcher_Private.h" //needed for Functor,CastingPolicy,TypeInfo
74 #include <map> //Required for the storage of template params to runtime params
75 
76 template
77 <
78  class BaseLhs,
79  class BaseRhs = BaseLhs,
80  typename ReturnType = void,
81  template <class, class> class CastingPolicy = vtkDispatcherCommon::vtkCaster
82  >
84 {
85 public:
99  template <class SomeLhs, class SomeRhs, class Functor>
100  void Add(Functor fun) { this->AddInternal<SomeLhs,SomeRhs>(fun, 1); }
101 
106  template <class SomeLhs, class SomeRhs>
107  bool Remove() { return DoRemove(typeid(SomeLhs), typeid(SomeRhs)); }
108 
127  ReturnType Go(BaseLhs* lhs, BaseRhs* rhs);
128 
129 protected:
133 
134  void DoAddFunctor(TypeInfo lhs,TypeInfo rhs, MappedType fun);
135  bool DoRemove(TypeInfo lhs, TypeInfo rhs);
136 
137  typedef std::pair<TypeInfo,TypeInfo> KeyType;
138  typedef std::map<KeyType, MappedType > MapType;
140 private:
141  template <class SomeLhs, class SomeRhs, class Functor>
142  void AddInternal(const Functor& fun, long);
143  template <class SomeLhs, class SomeRhs, class Functor>
144  void AddInternal(Functor* fun, int);
145 };
146 
147 //We are making all these method non-inline to reduce compile time overhead
148 //----------------------------------------------------------------------------
149 template<class BaseLhs, class BaseRhs, typename ReturnType,
150  template <class, class> class CastingPolicy>
151 template <class SomeLhs, class SomeRhs, class Functor>
153 ::AddInternal(const Functor& fun, long)
154 {
156  BaseLhs, BaseRhs,
157  SomeLhs, SomeRhs,
158  ReturnType,
159  CastingPolicy<SomeLhs, BaseLhs>,
160  CastingPolicy<SomeRhs, BaseRhs>,
161  Functor> Adapter;
162  Adapter ada(fun);
163  MappedType mt(ada);
164  DoAddFunctor(typeid(SomeLhs), typeid(SomeRhs),mt);
165 }
166 
167 //----------------------------------------------------------------------------
168 template<class BaseLhs, class BaseRhs, typename ReturnType,
169  template <class, class> class CastingPolicy>
170 template <class SomeLhs, class SomeRhs, class Functor>
172 ::AddInternal(Functor* fun, int)
173 {
175  BaseLhs, BaseRhs,
176  SomeLhs, SomeRhs,
177  ReturnType,
178  CastingPolicy<SomeLhs, BaseLhs>,
179  CastingPolicy<SomeRhs, BaseRhs>,
180  Functor> 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>
191 {
192  FunctorMap[KeyType(lhs, rhs)] = fun;
193 }
194 
195 //----------------------------------------------------------------------------
196 template <class BaseLhs, class BaseRhs, typename ReturnType,
197  template <class, class> class CastingPolicy>
200 {
201  return FunctorMap.erase(KeyType(lhs, rhs)) == 1;
202 }
203 
204 //----------------------------------------------------------------------------
205 template <class BaseLhs, class BaseRhs, typename ReturnType,
206  template <class, class> class CastingPolicy>
208 ::Go(BaseLhs* lhs, BaseRhs* rhs)
209 {
210  typename MapType::key_type k(typeid(*lhs),typeid(*rhs));
211  typename MapType::iterator i = FunctorMap.find(k);
212  if (i == FunctorMap.end())
213  {
214  //we don't want to throw exceptions so we have two options.
215  //we can return the default, or make a lightweight struct for return value
216  return ReturnType();
217  }
218  return (i->second)(*lhs,*rhs);
219 }
220 
221 #endif // vtkDoubleDispatcher_h
222 // VTK-HeaderTest-Exclude: vtkDoubleDispatcher.h
vtkDoubleDispatcher::DoAddFunctor
void DoAddFunctor(TypeInfo lhs, TypeInfo rhs, MappedType fun)
Definition: vtkDoubleDispatcher.h:190
vtkDispatcherCommon::TypeInfo
Definition: vtkDispatcher_Private.h:378
vtkDoubleDispatcher::TypeInfo
vtkDispatcherCommon::TypeInfo TypeInfo
Definition: vtkDoubleDispatcher.h:130
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:100
vtkDispatcher_Private.h
vtkDoubleDispatcher::FunctorMap
MapType FunctorMap
Definition: vtkDoubleDispatcher.h:139
vtkDoubleDispatcher::MapType
std::map< KeyType, MappedType > MapType
Definition: vtkDoubleDispatcher.h:138
vtkDoubleDispatcherPrivate::Functor
Definition: vtkDispatcher_Private.h:302
vtkDoubleDispatcher::MappedType
vtkDoubleDispatcherPrivate::Functor< ReturnType, BaseLhs, BaseRhs > MappedType
Definition: vtkDoubleDispatcher.h:132
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:208
vtkDoubleDispatcher::KeyType
std::pair< TypeInfo, TypeInfo > KeyType
Definition: vtkDoubleDispatcher.h:137
vtkDoubleDispatcher::Remove
bool Remove()
Remove a functor that is bound to the given parameter types.
Definition: vtkDoubleDispatcher.h:107
vtkDoubleDispatcher
Dispatch to functor based on two pointer types.
Definition: vtkDoubleDispatcher.h:83
vtkDispatcherCommon::vtkCaster
Definition: vtkDispatcher_Private.h:365
vtkDoubleDispatcherPrivate::FunctorDoubleDispatcherHelper
Definition: vtkDispatcher_Private.h:228
vtkDoubleDispatcher::DoRemove
bool DoRemove(TypeInfo lhs, TypeInfo rhs)
Definition: vtkDoubleDispatcher.h:199
vtkDoubleDispatcherPrivate::FunctorRefDispatcherHelper
Definition: vtkDispatcher_Private.h:207