VTK
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 
76 #ifndef vtkDispatcher_h
77 #define vtkDispatcher_h
78 
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
86  <
87  class BaseLhs,
88  typename ReturnType = void,
89  template <class, class> class CastingPolicy = vtkDispatcherCommon::vtkCaster
90  >
92 {
93 public:
106  template <class SomeLhs, class Functor>
107  void Add(Functor fun) { this->AddInternal<SomeLhs>(fun, 1); }
108 
113  template <class SomeLhs>
114  bool Remove() { return DoRemove(typeid(SomeLhs)); }
115 
134  ReturnType Go(BaseLhs* lhs);
135 
136 protected:
139 
140  void DoAddFunctor(TypeInfo lhs, MappedType fun);
141  bool DoRemove(TypeInfo lhs);
142  typedef std::map<TypeInfo, MappedType > MapType;
143  MapType FunctorMap;
144 private:
145  template <class SomeLhs, class Functor>
146  void AddInternal(Functor const& fun, long);
147  template <class SomeLhs, class Functor>
148  void AddInternal(Functor* fun, int);
149 };
150 
151 //We are making all these method non-inline to reduce compile time overhead
152 //----------------------------------------------------------------------------
153 template<class BaseLhs,typename ReturnType,
154  template <class, class> class CastingPolicy>
155 template <class SomeLhs, class Functor>
157 {
159  BaseLhs,
160  SomeLhs,
161  ReturnType,
162  CastingPolicy<SomeLhs, BaseLhs>,
163  Functor> Adapter;
164  Adapter ada(fun);
165  MappedType mt(ada);
166  DoAddFunctor(typeid(SomeLhs),mt);
167 }
168 
169 
170 //----------------------------------------------------------------------------
171 template<class BaseLhs,typename ReturnType,
172  template <class, class> class CastingPolicy>
173 template <class SomeLhs, class Functor>
175 {
177  BaseLhs,
178  SomeLhs,
179  ReturnType,
180  CastingPolicy<SomeLhs, BaseLhs>,
181  Functor> Adapter;
182  Adapter ada(*fun);
183  MappedType mt(ada);
184  DoAddFunctor(typeid(SomeLhs),mt);
185 }
186 
187 //----------------------------------------------------------------------------
188 template<class BaseLhs,typename ReturnType,
189  template <class, class> class CastingPolicy>
192 {
193  FunctorMap[TypeInfo(lhs)] = fun;
194 }
195 
196 //----------------------------------------------------------------------------
197 template <class BaseLhs, typename ReturnType,
198  template <class, class> class CastingPolicy>
201 {
202  return FunctorMap.erase(TypeInfo(lhs)) == 1;
203 }
204 
205 //----------------------------------------------------------------------------
206 template <class BaseLhs,typename ReturnType,
207  template <class, class> class CastingPolicy>
209 ::Go(BaseLhs* lhs)
210 {
211  typename MapType::key_type k(typeid(*lhs));
212  typename MapType::iterator i = FunctorMap.find(k);
213  if (i == FunctorMap.end())
214  {
215  //we return a default type, currently i don't want exceptions thrown
216  return ReturnType();
217  }
218  return (i->second)(*lhs);
219 }
220 
221 #endif // vtkDispatcher_h
222 // VTK-HeaderTest-Exclude: vtkDispatcher.h
void Add(Functor fun)
Add in a functor that is mapped to the template SomeLhs parameter.
bool DoRemove(TypeInfo lhs)
vtkDispatcherPrivate::Functor< ReturnType, BaseLhs > MappedType
vtkDispatcherCommon::TypeInfo TypeInfo
void DoAddFunctor(TypeInfo lhs, MappedType fun)
Dispatch to functor based on a pointer type.
Definition: vtkDispatcher.h:91
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.
std::map< TypeInfo, MappedType > MapType
MapType FunctorMap