casa  5.7.0-16
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Register.h
Go to the documentation of this file.
1 //# Register.h: Templated function to provide simple type identification
2 //# Copyright (C) 1993,1994,1995,1999,2001
3 //# Associated Universities, Inc. Washington DC, USA.
4 //#
5 //# This library is free software; you can redistribute it and/or modify it
6 //# under the terms of the GNU Library General Public License as published by
7 //# the Free Software Foundation; either version 2 of the License, or (at your
8 //# option) any later version.
9 //#
10 //# This library is distributed in the hope that it will be useful, but WITHOUT
11 //# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 //# FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
13 //# License for more details.
14 //#
15 //# You should have received a copy of the GNU Library General Public License
16 //# along with this library; if not, write to the Free Software Foundation,
17 //# Inc., 675 Massachusetts Ave, Cambridge, MA 02139, USA.
18 //#
19 //# Correspondence concerning AIPS++ should be addressed as follows:
20 //# Internet email: aips2-request@nrao.edu.
21 //# Postal address: AIPS++ Project Office
22 //# National Radio Astronomy Observatory
23 //# 520 Edgemont Road
24 //# Charlottesville, VA 22903-2475 USA
25 //#
26 //# $Id$
27 
28 #ifndef CASA_REGISTER_H
29 #define CASA_REGISTER_H
30 
31 #include <casacore/casa/aips.h>
33 
34 namespace casacore { //# NAMESPACE CASACORE - BEGIN
35 
36 // <summary>
37 // Primitive Run Time Type Information (<em>RTTI</em>)
38 // </summary>
39 //
40 // <use visibility=export>
41 // <reviewed reviewer="UNKNOWN" date="before2004/08/25" tests="" demos=""></reviewed>
42 //
43 // <prerequisite>
44 // <li> <em>none</em>
45 // </prerequisite>
46 //
47 // <etymology>
48 // This function is called Register because the user is <em>registering</em>
49 // a type with the run-time type mechanism.
50 // </etymology>
51 //
52 // <synopsis>
53 // This function returns a unique unsigned integer (<b>uInt</b>) for each
54 // type of pointer the user passes in as a parameter. It will always return
55 // the same number for a given type during a particular execution. The
56 // unsigned integer which is returned can be use to identify the particular
57 // type.
58 //
59 // <note role=warning> This function does not work correctly for
60 // <em>multiple inheritance</em>, but <b>ONLY</b> for
61 // <em>single inheritance</em>. In addition, the type <em>number</em>
62 // which is returned is <b>NOT</b> unique across program executions.
63 // </note>
64 //
65 // This RTTI mechanism is simple, it does not require extra functions to be
66 // added to the classes which are to be <em>identified</em>, and it is
67 // similar to the RTTI mechanism which will be a part of the C++ language
68 // in the future.
69 //
70 // To be useful, however, this mechanism must be used as part of the
71 // implementation of a <em>virtual</em> member function. For example:
72 // <srcblock>
73 // #include <casacore/casa/Utilities/Register.h>
74 // #include <iostream>
75 //
76 // class foo { public: virtual uInt type() { return Register(this);}};
77 // class bar : public foo { public: uInt type() { return Register(this);}};
78 // main() {
79 // foo *b = new bar();
80 // foo *f = new foo();
81 //
82 // cout << "f: type()=" << f->type() << " Register()=" << Register(f) << endl;
83 // cout << "b: type()=" << b->type() << " Register()=" << Register(b) << endl;
84 // }
85 // </srcblock>
86 // The output of this code would look something like:
87 // <pre>
88 // f: type()=1 Register()=1
89 // b: type()=2 Register()=1
90 // </pre>
91 // Without the virtual function, <src>type()</src>, the output of
92 // <src>Register()</src> is deceiving and of little use.
93 // </synopsis>
94 //
95 // <motivation>
96 // Needed a simple type identification mechanism for the
97 // <linkto class=Notice>Notice</linkto> class. This was necessary so that
98 // multiple notices could be distinguished.
99 // It can be replaced by the future standard RTTI.
100 // </motivation>
101 //
102 // <templating arg=t>
103 // <li> <em>none</em>
104 // </templating>
105 //
106 // <group name=register>
108 // This is a templated function which takes a pointer to any class as a parameter.
109 // The parameter's type is then used to generate a unique id. The parameter is
110 // a pointer rather than a <em>value</em> for efficiency reasons. With a
111 // <em>value</em> parameter, it would be difficult to do things like:
112 // <srcblock>
113 // Register(static_cast<MyClass*>(0));
114 // </srcblock>
115 // to find the <src>Register</src> type id for a random class.
116 template<class t> uInt Register(const t *);
117 
118 // </group>
119 
120 // Bother!!
121 // template<class t> inline uInt Register(const t *) {
122 // static uInt type = 0;
123 // if (!type) type = RegSequence::SgetNext();
124 // return type;
125 // }
126 
127 // BOTHER!! BOTHER!! BOTHER!!
128 // template<class t> inline uInt Register(const t &v) {
129 // return Register(&v);
130 // }
131 
132 
133 } //# NAMESPACE CASACORE - END
134 
135 #ifndef CASACORE_NO_AUTO_TEMPLATES
136 #include <casacore/casa/Utilities/Register.tcc>
137 #endif //# CASACORE_NO_AUTO_TEMPLATES
138 #endif
unsigned int uInt
Definition: aipstype.h:51
#define casacore
&lt;X11/Intrinsic.h&gt; #defines true, false, casacore::Bool, and String.
Definition: X11Intrinsic.h:42