This is the mail archive of the
gcc@gcc.gnu.org
mailing list for the GCC project.
compile-time type introspection
- From: Lally Singh <death_to_nt at mac dot com>
- To: gcc at gcc dot gnu dot org
- Date: Fri, 16 Jan 2004 12:20:24 -0500
- Subject: compile-time type introspection
- Reply-to: lallysingh at mac dot com
Hey everyone,
I put together a compile-time introspection system, implemented as a
patch to G++. I think
it's fun to play with. There's a README with some instructions on
installation and the full API
presented. Would it be worth trying to submit it as a g++ extension?
Here's the lowdown:
CHANGES TO SOURCE:
- a single function decl in cp-tree.h
- one new function call in cp/pt.c
- one new file: ctti.c
- one new system header <ctti>
SIZE:
about 1300 lines of C code.
DOWNLOAD:
http://homepage.mac.com/lallysingh/ctti-jan16-04.tar.gz
It's like 10k.
EXAMPLE USE:
#include <ctti>
#include <stdio.h>
class foo {
void priv_method( int, char, float );
public:
foo (const foo& other) throw();
~foo();
virtual int pub_method (int) const;
};
template<class T, int N>
class report_worker {
public:
static void do_report() {
enum { n = N-1 };
printf ("[%d]: name{%s}\n",
n,
std::type_description<T>::template method<n>::name );
report_worker<T,N-1>::do_report();
}
};
template<class T>
class report_worker<T,0> {
public:
static void do_report() {
printf("end of report\n");
}
};
template<class T>
class reporter {
public:
static void report() {
enum { NR= std::type_description<T>::num_methods };
report_worker<T, NR>::do_report();
}
};
int main() {
reporter<foo>::report();
return 0;
}
OUTPUT:
[3]: name{operator=}
[2]: name{pub_method}
[1]: name{~foo}
[0]: name{foo}
end of report
--
Government is not reason, it is not eloquence, it is force; like fire,
a troublesome servant and a fearful master. Never for a moment should
it be left to irresponsible action.
- George Washington