VTK
vtkTypeTemplate.h
Go to the documentation of this file.
1 /*=========================================================================
2 
3  Program: Visualization Toolkit
4  Module: vtkTypeTemplate.h
5 
6 -------------------------------------------------------------------------
7  Copyright 2008 Sandia Corporation.
8  Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
9  the U.S. Government retains certain rights in this software.
10 -------------------------------------------------------------------------
11 
12  Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
13  All rights reserved.
14  See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
15 
16  This software is distributed WITHOUT ANY WARRANTY; without even
17  the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
18  PURPOSE. See the above copyright notice for more information.
19 
20 =========================================================================*/
21 
30 #ifndef __vtkTypeTemplate_h
31 #define __vtkTypeTemplate_h
32 
33 #include "vtkObjectBase.h"
34 #include <typeinfo>
35 
36 template<class ThisT, class BaseT>
38  public BaseT
39 {
40 public:
41  typedef BaseT Superclass;
42 
43  ThisT* NewInstance() const
44  {
45  return ThisT::SafeDownCast(this->NewInstanceInternal());
46  }
47 
48  static ThisT* SafeDownCast(vtkObjectBase* o)
49  {
50  if(o && o->IsA(typeid(ThisT).name()))
51  {
52  return static_cast<ThisT*>(o);
53  }
54 
55  return 0;
56  }
57 
58 protected:
60  {
61  return ThisT::New();
62  }
63 
64  // We don't expose this publicly, because the typename we generate
65  // for our template instantiations isn't human-readable, unlike
66  // "normal" VTK classes.
67  static int IsTypeOf(const char* type)
68  {
69  if(!strcmp(typeid(ThisT).name(), type))
70  {
71  return 1;
72  }
73  return BaseT::IsTypeOf(type);
74  }
75 
76  // We don't expose this publicly, because the typename we generate
77  // for our template instantiations isn't human-readable, unlike
78  // "normal" VTK classes.
79  virtual int IsA(const char *type)
80  {
81  return this->IsTypeOf(type);
82  }
83 
84 private:
85  virtual const char* GetClassNameInternal() const
86  {
87  return typeid(ThisT).name();
88  }
89 };
90 
91 #endif
92 
Provides the equivalent of vtkTypeMacro for use with template classes.
ThisT * NewInstance() const
static int IsTypeOf(const char *type)
static ThisT * SafeDownCast(vtkObjectBase *o)
virtual vtkObjectBase * NewInstanceInternal() const
virtual int IsA(const char *type)
abstract base class for most VTK objects
Definition: vtkObjectBase.h:59
virtual int IsA(const char *name)