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$ 00027 00028 #ifndef CASA_REGISTER_H 00029 #define CASA_REGISTER_H 00030 00031 #include <casacore/casa/aips.h> 00032 #include <casacore/casa/Utilities/RegSequence.h> 00033 00034 namespace casacore { //# NAMESPACE CASACORE - BEGIN 00035 00036 // <summary> 00037 // Primitive Run Time Type Information (<em>RTTI</em>) 00038 // </summary> 00039 // 00040 // <use visibility=export> 00041 // <reviewed reviewer="UNKNOWN" date="before2004/08/25" tests="" demos=""></reviewed> 00042 // 00043 // <prerequisite> 00044 // <li> <em>none</em> 00045 // </prerequisite> 00046 // 00047 // <etymology> 00048 // This function is called Register because the user is <em>registering</em> 00049 // a type with the run-time type mechanism. 00050 // </etymology> 00051 // 00052 // <synopsis> 00053 // This function returns a unique unsigned integer (<b>uInt</b>) for each 00054 // type of pointer the user passes in as a parameter. It will always return 00055 // the same number for a given type during a particular execution. The 00056 // unsigned integer which is returned can be use to identify the particular 00057 // type. 00058 // 00059 // <note role=warning> This function does not work correctly for 00060 // <em>multiple inheritance</em>, but <b>ONLY</b> for 00061 // <em>single inheritance</em>. In addition, the type <em>number</em> 00062 // which is returned is <b>NOT</b> unique across program executions. 00063 // </note> 00064 // 00065 // This RTTI mechanism is simple, it does not require extra functions to be 00066 // added to the classes which are to be <em>identified</em>, and it is 00067 // similar to the RTTI mechanism which will be a part of the C++ language 00068 // in the future. 00069 // 00070 // To be useful, however, this mechanism must be used as part of the 00071 // implementation of a <em>virtual</em> member function. For example: 00072 // <srcblock> 00073 // #include <casacore/casa/Utilities/Register.h> 00074 // #include <iostream> 00075 // 00076 // class foo { public: virtual uInt type() { return Register(this);}}; 00077 // class bar : public foo { public: uInt type() { return Register(this);}}; 00078 // main() { 00079 // foo *b = new bar(); 00080 // foo *f = new foo(); 00081 // 00082 // cout << "f: type()=" << f->type() << " Register()=" << Register(f) << endl; 00083 // cout << "b: type()=" << b->type() << " Register()=" << Register(b) << endl; 00084 // } 00085 // </srcblock> 00086 // The output of this code would look something like: 00087 // <pre> 00088 // f: type()=1 Register()=1 00089 // b: type()=2 Register()=1 00090 // </pre> 00091 // Without the virtual function, <src>type()</src>, the output of 00092 // <src>Register()</src> is deceiving and of little use. 00093 // </synopsis> 00094 // 00095 // <motivation> 00096 // Needed a simple type identification mechanism for the 00097 // <linkto class=Notice>Notice</linkto> class. This was necessary so that 00098 // multiple notices could be distinguished. 00099 // It can be replaced by the future standard RTTI. 00100 // </motivation> 00101 // 00102 // <templating arg=t> 00103 // <li> <em>none</em> 00104 // </templating> 00105 // 00106 // <group name=register> 00107 00108 // This is a templated function which takes a pointer to any class as a parameter. 00109 // The parameter's type is then used to generate a unique id. The parameter is 00110 // a pointer rather than a <em>value</em> for efficiency reasons. With a 00111 // <em>value</em> parameter, it would be difficult to do things like: 00112 // <srcblock> 00113 // Register(static_cast<MyClass*>(0)); 00114 // </srcblock> 00115 // to find the <src>Register</src> type id for a random class. 00116 template<class t> uInt Register(const t *); 00117 00118 // </group> 00119 00120 // Bother!! 00121 // template<class t> inline uInt Register(const t *) { 00122 // static uInt type = 0; 00123 // if (!type) type = RegSequence::SgetNext(); 00124 // return type; 00125 // } 00126 00127 // BOTHER!! BOTHER!! BOTHER!! 00128 // template<class t> inline uInt Register(const t &v) { 00129 // return Register(&v); 00130 // } 00131 00132 00133 } //# NAMESPACE CASACORE - END 00134 00135 #ifndef CASACORE_NO_AUTO_TEMPLATES 00136 #include <casacore/casa/Utilities/Register.tcc> 00137 #endif //# CASACORE_NO_AUTO_TEMPLATES 00138 #endif