VTK  9.0.2
vtkDispatcher_Private.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 #ifndef vtkDispatcher_Private_h
31 #define vtkDispatcher_Private_h
32 
33 #include "vtkConfigure.h"
34 
35 #ifndef VTK_LEGACY_REMOVE
36 #ifndef __VTK_WRAP__
37 
38 #include <cassert>
39 #include <memory>
40 #include <typeinfo>
41 
43 {
45 // Dispatch helper for reference functors
47 template <class BaseLhs, class SomeLhs, typename RT, class CastLhs, class Fun>
49 {
50  Fun& fun_;
51 
52 public:
53  typedef RT ResultType;
54 
56  : fun_(rhs.fun_)
57  {
58  }
60  : fun_(f)
61  {
62  }
63 
64  ResultType operator()(BaseLhs& lhs) { return fun_(CastLhs::Cast(lhs)); }
65 
66 private:
68 };
69 
71 // Dispatch helper
73 template <class BaseLhs, class SomeLhs, typename RT, class CastLhs, class Fun>
75 {
76  Fun fun_;
77 
78 public:
79  typedef RT ResultType;
80 
82  : fun_(rhs.fun_)
83  {
84  }
86  : fun_(fun)
87  {
88  }
89 
90  ResultType operator()(BaseLhs& lhs) { return fun_(CastLhs::Cast(lhs)); }
91 };
92 
94 // Parent class for all FunctorImpl, helps hide functor template args
96 template <typename R, typename P1>
98 {
99 public:
100  typedef R ResultType;
101  typedef P1 Parm1;
102 
103  virtual ~FunctorImpl() {}
104  virtual R operator()(P1&) = 0;
105  virtual FunctorImpl* DoClone() const = 0;
106 
107  template <class U>
108  static U* Clone(U* pObj)
109  {
110  if (!pObj)
111  return nullptr;
112  U* pClone = static_cast<U*>(pObj->DoClone());
113  assert(typeid(*pClone) == typeid(*pObj));
114  return pClone;
115  }
116 
117 protected:
120 
121 private:
122  FunctorImpl& operator=(const FunctorImpl&) = delete;
123 };
124 
126 // Impl functor that calls a user functor
128 template <class ParentFunctor, typename Fun>
129 class FunctorHandler : public ParentFunctor::Impl
130 {
131  typedef typename ParentFunctor::Impl Base;
132 
133 public:
134  typedef typename Base::ResultType ResultType;
135  typedef typename Base::Parm1 Parm1;
136 
137  FunctorHandler(Fun& fun)
138  : f_(fun)
139  {
140  }
141  ~FunctorHandler() override {}
142 
143  ResultType operator()(Parm1& p1) override { return f_(p1); }
144  FunctorHandler* DoClone() const override { return new FunctorHandler(*this); }
145 
146 private:
147  Fun f_;
149  : ParentFunctor::Impl(b)
150  , f_(b.f_)
151  {
152  }
153  FunctorHandler& operator=(const FunctorHandler& b) = delete;
154 };
155 
157 // Functor wrapper class
159 template <typename R, typename Parm1>
160 class Functor
161 {
162 public:
164  typedef R ResultType;
165 
167  : spImpl_()
168  {
169  }
170 
171  Functor(const Functor& rhs)
172  : spImpl_(Impl::Clone(rhs.spImpl_.get()))
173  {
174  }
175 
176  template <typename Fun>
177  Functor(Fun fun)
178  : spImpl_(new FunctorHandler<Functor, Fun>(fun))
179  {
180  }
181 
182  Functor& operator=(const Functor& rhs)
183  {
184  Functor copy(rhs);
185  spImpl_.swap(copy.spImpl_);
186  return *this;
187  }
188 
189  ResultType operator()(Parm1& p1) { return (*spImpl_)(p1); }
190 
191 private:
192  std::unique_ptr<Impl> spImpl_;
193 };
194 
195 }
196 
198 {
199 
201 // Dispatch helper
203 template <class BaseLhs, class BaseRhs, class SomeLhs, class SomeRhs, typename RT, class CastLhs,
204  class CastRhs, class Fun>
206 {
207  Fun& fun_;
208 
209 public:
210  typedef RT ResultType;
212  : fun_(rhs.fun_)
213  {
214  }
216  : fun_(fun)
217  {
218  }
219 
220  ResultType operator()(BaseLhs& lhs, BaseRhs& rhs)
221  {
222  return fun_(CastLhs::Cast(lhs), CastRhs::Cast(rhs));
223  }
224 
225 private:
227 };
228 
229 template <class BaseLhs, class BaseRhs, class SomeLhs, class SomeRhs, typename RT, class CastLhs,
230  class CastRhs, class Fun>
232 {
233  Fun fun_;
234 
235 public:
236  typedef RT ResultType;
238  : fun_(rhs.fun_)
239  {
240  }
242  : fun_(fun)
243  {
244  }
245 
246  ResultType operator()(BaseLhs& lhs, BaseRhs& rhs)
247  {
248  return fun_(CastLhs::Cast(lhs), CastRhs::Cast(rhs));
249  }
250 };
251 
253 // Parent class for all FunctorImpl, helps hide functor template args
255 template <typename R, typename P1, typename P2>
257 {
258 public:
259  typedef R ResultType;
260  typedef P1 Parm1;
261  typedef P2 Parm2;
262 
263  virtual ~FunctorImpl() {}
264  virtual R operator()(P1&, P2&) = 0;
265  virtual FunctorImpl* DoClone() const = 0;
266 
267  template <class U>
268  static U* Clone(U* pObj)
269  {
270  if (!pObj)
271  return nullptr;
272  U* pClone = static_cast<U*>(pObj->DoClone());
273  assert(typeid(*pClone) == typeid(*pObj));
274  return pClone;
275  }
276 
277 protected:
280 
281 private:
282  FunctorImpl& operator=(const FunctorImpl&) = delete;
283 };
284 
286 // Impl functor that calls a user functor
288 template <class ParentFunctor, typename Fun>
289 class FunctorHandler : public ParentFunctor::Impl
290 {
291  typedef typename ParentFunctor::Impl Base;
292 
293 public:
294  typedef typename Base::ResultType ResultType;
295  typedef typename Base::Parm1 Parm1;
296  typedef typename Base::Parm2 Parm2;
297 
298  FunctorHandler(const Fun& fun)
299  : f_(fun)
300  {
301  }
302  ~FunctorHandler() override {}
303 
304  ResultType operator()(Parm1& p1, Parm2& p2) override { return f_(p1, p2); }
305 
306  FunctorHandler* DoClone() const override { return new FunctorHandler(*this); }
307 
308 private:
309  Fun f_;
311  : ParentFunctor::Impl(b)
312  , f_(b.f_)
313  {
314  }
315  FunctorHandler& operator=(const FunctorHandler& b) = delete;
316 };
317 
319 // Functor wrapper class
321 template <typename R, typename Parm1, typename Parm2>
322 class Functor
323 {
324 public:
326  typedef R ResultType;
327 
329  : spImpl_()
330  {
331  }
332 
333  Functor(const Functor& rhs)
334  : spImpl_(Impl::Clone(rhs.spImpl_.get()))
335  {
336  }
337 
338  template <typename Fun>
339  Functor(const Fun& fun)
340  : spImpl_(new FunctorHandler<Functor, Fun>(fun))
341  {
342  }
343 
344  Functor& operator=(const Functor& rhs)
345  {
346  Functor copy(rhs);
347  spImpl_.swap(copy.spImpl_);
348  return *this;
349  }
350 
351  ResultType operator()(Parm1& p1, Parm2& p2) { return (*spImpl_)(p1, p2); }
352 
353 private:
354  std::unique_ptr<Impl> spImpl_;
355 };
356 }
357 
359 {
360 
361 template <class To, class From>
363 {
364  static To& Cast(From& obj) { return dynamic_cast<To&>(obj); }
365 
366  static To* Cast(From* obj) { return dynamic_cast<To*>(obj); }
367 };
368 
369 template <class To, class From>
370 struct vtkCaster
371 {
372  static To& Cast(From& obj) { return *(To::SafeDownCast(&obj)); }
373 
374  static To* Cast(From* obj) { return To::SafeDownCast(obj); }
375 };
376 
377 class TypeInfo
378 {
379 public:
380  // Constructors
381  TypeInfo(); // needed for containers
382  TypeInfo(const std::type_info&); // non-explicit
383 
384  // Access for the wrapped std::type_info
385  const std::type_info& Get() const;
386  // Compatibility functions
387  bool before(const TypeInfo& rhs) const;
388  const char* name() const;
389 
390 private:
391  const std::type_info* pInfo_;
392 };
393 
394 // Implementation
395 
397 {
398  class Nil
399  {
400  };
401  pInfo_ = &typeid(Nil);
402  assert(pInfo_);
403 }
404 
405 inline TypeInfo::TypeInfo(const std::type_info& ti)
406  : pInfo_(&ti)
407 {
408  assert(pInfo_);
409 }
410 
411 inline bool TypeInfo::before(const TypeInfo& rhs) const
412 {
413  assert(pInfo_);
414  // type_info::before return type is int in some VC libraries
415  return pInfo_->before(*rhs.pInfo_) != 0;
416 }
417 
418 inline const std::type_info& TypeInfo::Get() const
419 {
420  assert(pInfo_);
421  return *pInfo_;
422 }
423 
424 inline const char* TypeInfo::name() const
425 {
426  assert(pInfo_);
427  return pInfo_->name();
428 }
429 
430 // Comparison operators
431 
432 inline bool operator==(const TypeInfo& lhs, const TypeInfo& rhs)
433 // type_info::operator== return type is int in some VC libraries
434 {
435  return (lhs.Get() == rhs.Get()) != 0;
436 }
437 
438 inline bool operator<(const TypeInfo& lhs, const TypeInfo& rhs)
439 {
440  return lhs.before(rhs);
441 }
442 
443 inline bool operator!=(const TypeInfo& lhs, const TypeInfo& rhs)
444 {
445  return !(lhs == rhs);
446 }
447 
448 inline bool operator>(const TypeInfo& lhs, const TypeInfo& rhs)
449 {
450  return rhs < lhs;
451 }
452 
453 inline bool operator<=(const TypeInfo& lhs, const TypeInfo& rhs)
454 {
455  return !(lhs > rhs);
456 }
457 
458 inline bool operator>=(const TypeInfo& lhs, const TypeInfo& rhs)
459 {
460  return !(lhs < rhs);
461 }
462 
463 }
464 
465 #endif // wrapping
466 #endif // legacy remove
467 #endif // vtkDispatcherPrivate_h
468 // VTK-HeaderTest-Exclude: vtkDispatcher_Private.h
bool before(const TypeInfo &rhs) const
const std::type_info & Get() const
FunctorDispatcherHelper(const FunctorDispatcherHelper &rhs)
ResultType operator()(Parm1 &p1) override
FunctorHandler * DoClone() const override
virtual FunctorImpl * DoClone() const =0
FunctorRefDispatcherHelper(const FunctorRefDispatcherHelper &rhs)
Functor & operator=(const Functor &rhs)
FunctorDoubleDispatcherHelper(const FunctorDoubleDispatcherHelper &rhs)
FunctorHandler * DoClone() const override
ResultType operator()(Parm1 &p1, Parm2 &p2) override
virtual R operator()(P1 &, P2 &)=0
virtual FunctorImpl * DoClone() const =0
FunctorRefDispatcherHelper(const FunctorRefDispatcherHelper &rhs)
ResultType operator()(Parm1 &p1, Parm2 &p2)
Functor & operator=(const Functor &rhs)
FunctorImpl< R, Parm1, Parm2 > Impl
double get(vtkDataArray *const &arr, vtkIdType key)
bool operator<=(const TypeInfo &lhs, const TypeInfo &rhs)
bool operator!=(const TypeInfo &lhs, const TypeInfo &rhs)
bool operator>=(const TypeInfo &lhs, const TypeInfo &rhs)
bool operator==(const TypeInfo &lhs, const TypeInfo &rhs)
bool operator<(const TypeInfo &lhs, const TypeInfo &rhs)
bool operator>(const TypeInfo &lhs, const TypeInfo &rhs)