casa
$Rev:20696$
|
00001 //# Register.h: Templated function to provide simple type identification 00002 //# Copyright (C) 1993,1994,1995,1999,2001 00003 //# Associated Universities, Inc. Washington DC, USA. 00004 //# 00005 //# This library is free software; you can redistribute it and/or modify it 00006 //# under the terms of the GNU Library General Public License as published by 00007 //# the Free Software Foundation; either version 2 of the License, or (at your 00008 //# option) any later version. 00009 //# 00010 //# This library is distributed in the hope that it will be useful, but WITHOUT 00011 //# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 00012 //# FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public 00013 //# License for more details. 00014 //# 00015 //# You should have received a copy of the GNU Library General Public License 00016 //# along with this library; if not, write to the Free Software Foundation, 00017 //# Inc., 675 Massachusetts Ave, Cambridge, MA 02139, USA. 00018 //# 00019 //# Correspondence concerning AIPS++ should be addressed as follows: 00020 //# Internet email: aips2-request@nrao.edu. 00021 //# Postal address: AIPS++ Project Office 00022 //# National Radio Astronomy Observatory 00023 //# 520 Edgemont Road 00024 //# Charlottesville, VA 22903-2475 USA 00025 //# 00026 //# $Id: Register.h 20551 2009-03-25 00:11:33Z Malte.Marquarding $ 00027 00028 #ifndef CASA_REGISTER_H 00029 #define CASA_REGISTER_H 00030 00031 #include <casa/Utilities/RegSequence.h> 00032 00033 namespace casa { //# NAMESPACE CASA - BEGIN 00034 00035 // <summary> 00036 // Primitive Run Time Type Information (<em>RTTI</em>) 00037 // </summary> 00038 // 00039 // <use visibility=export> 00040 // <reviewed reviewer="UNKNOWN" date="before2004/08/25" tests="" demos=""></reviewed> 00041 // 00042 // <prerequisite> 00043 // <li> <em>none</em> 00044 // </prerequisite> 00045 // 00046 // <etymology> 00047 // This function is called Register because the user is <em>registering</em> 00048 // a type with the run-time type mechanism. 00049 // </etymology> 00050 // 00051 // <synopsis> 00052 // This function returns a unique unsigned integer (<b>uInt</b>) for each 00053 // type of pointer the user passes in as a parameter. It will always return 00054 // the same number for a given type during a particular execution. The 00055 // unsigned integer which is returned can be use to identify the particular 00056 // type. 00057 // 00058 // <note role=warning> This function does not work correctly for 00059 // <em>multiple inheritance</em>, but <b>ONLY</b> for 00060 // <em>single inheritance</em>. In addition, the type <em>number</em> 00061 // which is returned is <b>NOT</b> unique across program executions. 00062 // </note> 00063 // 00064 // This RTTI mechanism is simple, it does not require extra functions to be 00065 // added to the classes which are to be <em>identified</em>, and it is 00066 // similar to the RTTI mechanism which will be a part of the C++ language 00067 // in the future. 00068 // 00069 // To be useful, however, this mechanism must be used as part of the 00070 // implementation of a <em>virtual</em> member function. For example: 00071 // <srcblock> 00072 // #include <casa/Utilities/Register.h> 00073 // #include <iostream> 00074 // 00075 // class foo { public: virtual uInt type() { return Register(this);}}; 00076 // class bar : public foo { public: uInt type() { return Register(this);}}; 00077 // main() { 00078 // foo *b = new bar(); 00079 // foo *f = new foo(); 00080 // 00081 // cout << "f: type()=" << f->type() << " Register()=" << Register(f) << endl; 00082 // cout << "b: type()=" << b->type() << " Register()=" << Register(b) << endl; 00083 // } 00084 // </srcblock> 00085 // The output of this code would look something like: 00086 // <pre> 00087 // f: type()=1 Register()=1 00088 // b: type()=2 Register()=1 00089 // </pre> 00090 // Without the virtual function, <src>type()</src>, the output of 00091 // <src>Register()</src> is deceiving and of little use. 00092 // </synopsis> 00093 // 00094 // <motivation> 00095 // Needed a simple type identification mechanism for the 00096 // <linkto class=Notice>Notice</linkto> class. This was necessary so that 00097 // multiple notices could be distinguished. 00098 // It can be replaced by the future standard RTTI. 00099 // </motivation> 00100 // 00101 // <templating arg=t> 00102 // <li> <em>none</em> 00103 // </templating> 00104 // 00105 // <group name=register> 00106 00107 // This is a templated function which takes a pointer to any class as a parameter. 00108 // The parameter's type is then used to generate a unique id. The parameter is 00109 // a pointer rather than a <em>value</em> for efficiency reasons. With a 00110 // <em>value</em> parameter, it would be difficult to do things like: 00111 // <srcblock> 00112 // Register(static_cast<MyClass*>(0)); 00113 // </srcblock> 00114 // to find the <src>Register</src> type id for a random class. 00115 template<class t> uInt Register(const t *); 00116 00117 // </group> 00118 00119 // Bother!! 00120 // template<class t> inline uInt Register(const t *) { 00121 // static uInt type = 0; 00122 // if (!type) type = RegSequence::SgetNext(); 00123 // return type; 00124 // } 00125 00126 // BOTHER!! BOTHER!! BOTHER!! 00127 // template<class t> inline uInt Register(const t &v) { 00128 // return Register(&v); 00129 // } 00130 00131 00132 } //# NAMESPACE CASA - END 00133 00134 #ifndef CASACORE_NO_AUTO_TEMPLATES 00135 #include <casa/Utilities/Register.tcc> 00136 #endif //# CASACORE_NO_AUTO_TEMPLATES 00137 #endif