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 
75 #ifndef vtkDispatcher_h
76 #define vtkDispatcher_h
77 
78 #include "vtkDispatcher_Private.h" //needed for Functor,CastingPolicy,TypeInfo
79 #include <map> //Required for the storage of template params to runtime params
80 
82 // class template FunctorDispatcher
84 template
85  <
86  class BaseLhs,
87  typename ReturnType = void,
88  template <class, class> class CastingPolicy = vtkDispatcherCommon::vtkCaster
89  >
91 {
92 public:
94 
101  template <class SomeLhs, class Functor>
102  void Add(Functor fun) { this->AddInternal<SomeLhs>(fun, 1); }
104 
106 
108  template <class SomeLhs>
109  bool Remove() { return DoRemove(typeid(SomeLhs)); }
111 
122  ReturnType Go(BaseLhs* lhs);
123 
124 protected:
127 
128  void DoAddFunctor(TypeInfo lhs, MappedType fun);
129  bool DoRemove(TypeInfo lhs);
130  typedef std::map<TypeInfo, MappedType > MapType;
131  MapType FunctorMap;
132 private:
133  template <class SomeLhs, class Functor>
134  void AddInternal(Functor const& fun, long);
135  template <class SomeLhs, class Functor>
136  void AddInternal(Functor* fun, int);
137 };
138 
139 //We are making all these method non-inline to reduce compile time overhead
140 //----------------------------------------------------------------------------
141 template<class BaseLhs,typename ReturnType,
142  template <class, class> class CastingPolicy>
143 template <class SomeLhs, class Functor>
145  {
147  BaseLhs,
148  SomeLhs,
149  ReturnType,
150  CastingPolicy<SomeLhs, BaseLhs>,
151  Functor> Adapter;
152  Adapter ada(fun);
153  MappedType mt(ada);
154  DoAddFunctor(typeid(SomeLhs),mt);
155  }
156 
157 
158 //----------------------------------------------------------------------------
159 template<class BaseLhs,typename ReturnType,
160  template <class, class> class CastingPolicy>
161 template <class SomeLhs, class Functor>
163  {
165  BaseLhs,
166  SomeLhs,
167  ReturnType,
168  CastingPolicy<SomeLhs, BaseLhs>,
169  Functor> Adapter;
170  Adapter ada(*fun);
171  MappedType mt(ada);
172  DoAddFunctor(typeid(SomeLhs),mt);
173  }
174 
175 //----------------------------------------------------------------------------
176 template<class BaseLhs,typename ReturnType,
177  template <class, class> class CastingPolicy>
180  {
181  FunctorMap[TypeInfo(lhs)] = fun;
182  }
183 
184 //----------------------------------------------------------------------------
185 template <class BaseLhs, typename ReturnType,
186  template <class, class> class CastingPolicy>
189  {
190  return FunctorMap.erase(TypeInfo(lhs)) == 1;
191  }
192 
193 //----------------------------------------------------------------------------
194 template <class BaseLhs,typename ReturnType,
195  template <class, class> class CastingPolicy>
197 ::Go(BaseLhs* lhs)
198  {
199  typename MapType::key_type k(typeid(*lhs));
200  typename MapType::iterator i = FunctorMap.find(k);
201  if (i == FunctorMap.end())
202  {
203  //we return a default type, currently i don't want exceptions thrown
204  return ReturnType();
205  }
206  return (i->second)(*lhs);
207  }
208 
209 #endif // vtkDispatcher_h
210 // VTK-HeaderTest-Exclude: vtkDispatcher.h
void Add(Functor fun)
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:90
ReturnType Go(BaseLhs *lhs)
std::map< TypeInfo, MappedType > MapType
MapType FunctorMap