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 
73 #ifndef vtkDispatcher_h
74 #define vtkDispatcher_h
75 
76 #include "vtkDispatcher_Private.h" //needed for Functor,CastingPolicy,TypeInfo
77 #include <map> //Required for the storage of template params to runtime params
78 
80 // class template FunctorDispatcher
82 template
83  <
84  class BaseLhs,
85  typename ReturnType = void,
86  template <class, class> class CastingPolicy = vtkDispatcherCommon::vtkCaster
87  >
89 {
90 public:
103  template <class SomeLhs, class Functor>
104  void Add(Functor fun) { this->AddInternal<SomeLhs>(fun, 1); }
105 
110  template <class SomeLhs>
111  bool Remove() { return DoRemove(typeid(SomeLhs)); }
112 
131  ReturnType Go(BaseLhs* lhs);
132 
133 protected:
136 
137  void DoAddFunctor(TypeInfo lhs, MappedType fun);
138  bool DoRemove(TypeInfo lhs);
139  typedef std::map<TypeInfo, MappedType > MapType;
140  MapType FunctorMap;
141 private:
142  template <class SomeLhs, class Functor>
143  void AddInternal(Functor const& fun, long);
144  template <class SomeLhs, class Functor>
145  void AddInternal(Functor* fun, int);
146 };
147 
148 //We are making all these method non-inline to reduce compile time overhead
149 //----------------------------------------------------------------------------
150 template<class BaseLhs,typename ReturnType,
151  template <class, class> class CastingPolicy>
152 template <class SomeLhs, class Functor>
154 {
156  BaseLhs,
157  SomeLhs,
158  ReturnType,
159  CastingPolicy<SomeLhs, BaseLhs>,
160  Functor> Adapter;
161  Adapter ada(fun);
162  MappedType mt(ada);
163  DoAddFunctor(typeid(SomeLhs),mt);
164 }
165 
166 
167 //----------------------------------------------------------------------------
168 template<class BaseLhs,typename ReturnType,
169  template <class, class> class CastingPolicy>
170 template <class SomeLhs, class Functor>
172 {
174  BaseLhs,
175  SomeLhs,
176  ReturnType,
177  CastingPolicy<SomeLhs, BaseLhs>,
178  Functor> Adapter;
179  Adapter ada(*fun);
180  MappedType mt(ada);
181  DoAddFunctor(typeid(SomeLhs),mt);
182 }
183 
184 //----------------------------------------------------------------------------
185 template<class BaseLhs,typename ReturnType,
186  template <class, class> class CastingPolicy>
189 {
190  FunctorMap[TypeInfo(lhs)] = fun;
191 }
192 
193 //----------------------------------------------------------------------------
194 template <class BaseLhs, typename ReturnType,
195  template <class, class> class CastingPolicy>
198 {
199  return FunctorMap.erase(TypeInfo(lhs)) == 1;
200 }
201 
202 //----------------------------------------------------------------------------
203 template <class BaseLhs,typename ReturnType,
204  template <class, class> class CastingPolicy>
206 ::Go(BaseLhs* lhs)
207 {
208  typename MapType::key_type k(typeid(*lhs));
209  typename MapType::iterator i = FunctorMap.find(k);
210  if (i == FunctorMap.end())
211  {
212  //we return a default type, currently i don't want exceptions thrown
213  return ReturnType();
214  }
215  return (i->second)(*lhs);
216 }
217 
218 #endif // vtkDispatcher_h
219 // 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:88
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