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 
22 // .NAME vtkTypeTemplate - Provides the equivalent of vtkTypeMacro
23 // for use with template classes
24 //
25 // .SECTION Thanks
26 // Developed by Timothy M. Shead (tshead@sandia.gov) at Sandia National Laboratories.
27 
28 #ifndef vtkTypeTemplate_h
29 #define vtkTypeTemplate_h
30 
31 #include "vtkObjectBase.h"
32 #include <string>
33 #include <typeinfo>
34 
35 template<class ThisT, class BaseT>
36 class vtkTypeTemplate :
37   public BaseT
38 {
39 public:
40   typedef BaseT Superclass;
41 
NewInstance()42   ThisT* NewInstance() const
43   {
44     return ThisT::SafeDownCast(this->NewInstanceInternal());
45   }
46 
SafeDownCast(vtkObjectBase * o)47   static ThisT* SafeDownCast(vtkObjectBase* o)
48   {
49     if(o &&
50        o->IsA(vtkTypeTemplate<ThisT, BaseT>::GetClassNameInternalCachedName()))
51       {
52       return static_cast<ThisT*>(o);
53       }
54 
55     return 0;
56   }
57 
58 protected:
NewInstanceInternal()59   virtual vtkObjectBase* NewInstanceInternal() const
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.
IsTypeOf(const char * type)67   static int IsTypeOf(const char* type)
68   {
69     if (strcmp(vtkTypeTemplate<ThisT, BaseT>::GetClassNameInternalCachedName(),
70                type) == 0)
71       {
72       return 1;
73       }
74     return BaseT::IsTypeOf(type);
75   }
76 
77   // We don't expose this publicly, because the typename we generate
78   // for our template instantiations isn't human-readable, unlike
79   // "normal" VTK classes.
IsA(const char * type)80   virtual int IsA(const char *type)
81   {
82     return this->IsTypeOf(type);
83   }
84 
vtkTypeTemplate()85   vtkTypeTemplate() {}
86 
87 private:
88   // not implemented:
89   vtkTypeTemplate(const vtkTypeTemplate<ThisT, BaseT>&);
90   void operator=(const vtkTypeTemplate<ThisT, BaseT>&);
91 
GetClassNameInternalCachedName()92   static const char* GetClassNameInternalCachedName()
93   {
94     static std::string thisType(typeid(ThisT).name());
95     return thisType.c_str();
96   }
97 
GetClassNameInternal()98   virtual const char* GetClassNameInternal() const
99   {
100     return this->GetClassNameInternalCachedName();
101   }
102 };
103 
104 #endif
105 
106 // VTK-HeaderTest-Exclude: vtkTypeTemplate.h
107