VTK
|
00001 /*========================================================================= 00002 00003 Program: Visualization Toolkit 00004 Module: vtkTypeTemplate.h 00005 00006 ------------------------------------------------------------------------- 00007 Copyright 2008 Sandia Corporation. 00008 Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation, 00009 the U.S. Government retains certain rights in this software. 00010 ------------------------------------------------------------------------- 00011 00012 Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen 00013 All rights reserved. 00014 See Copyright.txt or http://www.kitware.com/Copyright.htm for details. 00015 00016 This software is distributed WITHOUT ANY WARRANTY; without even 00017 the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR 00018 PURPOSE. See the above copyright notice for more information. 00019 00020 =========================================================================*/ 00021 00030 #ifndef __vtkTypeTemplate_h 00031 #define __vtkTypeTemplate_h 00032 00033 #include "vtkObjectBase.h" 00034 #include <typeinfo> 00035 00036 template<class ThisT, class BaseT> 00037 class vtkTypeTemplate : 00038 public BaseT 00039 { 00040 public: 00041 typedef BaseT Superclass; 00042 00043 ThisT* NewInstance() const 00044 { 00045 return ThisT::SafeDownCast(this->NewInstanceInternal()); 00046 } 00047 00048 static ThisT* SafeDownCast(vtkObjectBase* o) 00049 { 00050 if(o && o->IsA(typeid(ThisT).name())) 00051 { 00052 return static_cast<ThisT*>(o); 00053 } 00054 00055 return 0; 00056 } 00057 00058 protected: 00059 virtual vtkObjectBase* NewInstanceInternal() const 00060 { 00061 return ThisT::New(); 00062 } 00063 00064 // We don't expose this publicly, because the typename we generate 00065 // for our template instantiations isn't human-readable, unlike 00066 // "normal" VTK classes. 00067 static int IsTypeOf(const char* type) 00068 { 00069 if(!strcmp(typeid(ThisT).name(), type)) 00070 { 00071 return 1; 00072 } 00073 return BaseT::IsTypeOf(type); 00074 } 00075 00076 // We don't expose this publicly, because the typename we generate 00077 // for our template instantiations isn't human-readable, unlike 00078 // "normal" VTK classes. 00079 virtual int IsA(const char *type) 00080 { 00081 return this->IsTypeOf(type); 00082 } 00083 00084 private: 00085 virtual const char* GetClassNameInternal() const 00086 { 00087 return typeid(ThisT).name(); 00088 } 00089 }; 00090 00091 #endif 00092 00093 // VTK-HeaderTest-Exclude: vtkTypeTemplate.h