GNU   davin.50webs.com/research
Bringing to you notes for the ages

       Main Menu          Research Projects         Photo Album            Curriculum Vitae      The Greatest Artists
    Email Address       Computer Games          Web Design          Java Training Wheels      The Fly (A Story)   
  Political Activism   Bob Dylan Quotes+       My Life Story          Smoking Cessation          Other Links      
Debugging Macros     String Class I     Linked List System I Java for C Programmers Naming Convention
    String Class II         How I use m4              Strings III                 Symmetrical I/O             Linked Lists II     
Run-Time Type Info   Virtual Methods      An Array System        Science & Religion            Submodes       
  Nested Packages      Memory Leaks    Garbage Collection      Internet & Poverty      What is Knowledge?
Limits of Evolution   Emacs Additions      Function Plotter           Romantic Love        The Next Big Thing
    Science Fiction     Faster Compilation Theory of Morality         Elisp Scoping               Elisp Advice      
  S.O.G.M. Pattern       Safe Properties         School Bullying          Charisma Control          Life and Death    
     Splitting Java          Multiple Ctors       Religious Beliefs         Conversation 1           Conversation 2    
   J.T.W. Language    Emacs Additions II      Build Counter             Relation Plotter          Lisp++ Language  
  Memory Leaks II   Super Constructors CRUD Implementation Order a Website Form There Is An Afterlife
More Occam's Razor C to Java Translator Theory of Morality II


A system for run-time type information Free Stuff

Abstract

This article presents a pair of macros for implementing R.T.T.I. (Run-Time Type Information) in the C++ language. The naming of these macros is inspired by Java. It should be noted that there is another R.T.T.I. system in C++ namely <typeinfo.h>. This system is an alternative to that system but my system doesn't work properly with multiple inheritance.

1.0 R.T.T.I. Macros

The following macros add R.T.T.I. functionality to C++ classes:

#define RTTI_ROOT(T) \
virtual bool     instanceof_##T() const { return false; } \
virtual const T* castto_##T() const     { return null; } \
virtual T*       castto_##T()           { return null; } \

#define RTTI_SAME(T) \
virtual bool     instanceof_##T() const { return true; } \
virtual const T* castto_##T() const     { return this; } \
virtual T*       castto_##T()           { return this; } \

2.0 Adding R.T.T.I. to Classes

With the above macro definitions in place, classes can have R.T.T.I. functionality by calling these macros in their class definitions. Example:

class A {
public:
   RTTI_SAME(A);
   RTTI_ROOT(B1);
   RTTI_ROOT(B2);
   RTTI_ROOT(C);

public:
   // rest of class definition
};

class B1 : public A {
public:
   RTTI_SAME(B1);
   void b1_method() {
      cout << "(inside b1_method)\n";
   }
public:
   // rest of class definition
};

class B2 : public A {
public:
   RTTI_SAME(B2);
   void b2_method() {
      cout << "(inside b2_method)\n";
   }
public:
   // rest of class definition
};

class C : public B1 {
public:
   RTTI_SAME(C);
   void c_method() {
      cout << "(inside c_method)\n";
   }
public:
   // rest of class definition
};

3.0 Using R.T.T.I.

With the above class definitions in place, R.T.T.I. can be achieved by the following statements.'

void foo(A* a) 
{
   if (a->instanceof_A()) { 
      cout << "(a is instance of A)\n";
   }
   if (a->instanceof_B1()) {
      cout << "(a is instance of B1)\n";
      B1* b = a->castto_B1();
      b->b1_method();
   }
   if (a->instanceof_B2()) {
      cout << "(a is instance of B2)\n";
      B2* b = a->castto_B2();
      b->b2_method();
   }
   if (a->instanceof_C()) {
      cout << "(a is instance of C)\n";
      C* c = a->castto_C();
      c->c_method();
   }
}

int main()
{
   A* a = new A();
   A* b1 = new B1();
   A* b2 = new B2();
   A* c  = new C();

   foo(a);       // outputs:
                 // (a is instance of A)
   cout << endl;
   foo(b1);      // outputs:
                 // (a is instance of A)
                 // (a is instance of B1)
                 // (inside b1_method)
   cout << endl;
   foo(b2);      // outputs:
                 // (a is instance of A)
                 // (a is instance of B2)
                 // (inside b2_method)
   cout << endl;
   foo(c);       // outputs:
                 // (a is instance of A)
                 // (a is instance of B1)
                 // (inside b1_method)
                 // (a is instance of C)
                 // (inside c_method)
}

4.0 Emacs Syntax Highlighting

Here is some Emacs Lisp code that highlights the R.T.T.I. methods in the keyword face. Place the following code in your .emacs file:

;; The following code highlights the R.T.T.I. methods:
(add-hook 'font-lock-mode-hook 'my-cc--rtti)
(defun my-cc--rtti ()
  (if (eq major-mode 'c++-mode)
      (font-lock-add-keywords nil
                              '(("\\<\\(instanceof_\\|castto_\\)\\(\\sw*\\)"
                                 (1 font-lock-keyword-face t)
                                 (2 font-lock-type-face t)))
                               'APPEND)))
Back to Research Projects
This page has the following hit count:
| Main Menu | Research Projects | Photo Album | Curriculum Vitae | The Greatest Artists |
| Email Address | Computer Games | Web Design | Java Training Wheels | The Fly (A Story) |
| Political Activism | Bob Dylan Quotes+ | My Life Story | Smoking Cessation | Other Links |
| Debugging Macros | String Class I | Linked List System I | Java for C Programmers | Naming Convention |
| String Class II | How I use m4 | Strings III | Symmetrical I/O | Linked Lists II |
| Run-Time Type Info | Virtual Methods | An Array System | Science & Religion | Submodes |
| Nested Packages | Memory Leaks | Garbage Collection | Internet & Poverty | What is Knowledge? |
| Limits of Evolution | Emacs Additions | Function Plotter | Romantic Love | The Next Big Thing |
| Science Fiction | Faster Compilation | Theory of Morality | Elisp Scoping | Elisp Advice |
| S.O.G.M. Pattern | Safe Properties | School Bullying | Charisma Control | Life and Death |
| Splitting Java | Multiple Ctors | Religious Beliefs | Conversation 1 | Conversation 2 |
| J.T.W. Language | Emacs Additions II | Build Counter | Relation Plotter | Lisp++ Language |
| Memory Leaks II | Super Constructors | CRUD Implementation | Order a Website Form | There Is An Afterlife |
| More Occam's Razor | C to Java Translator | Theory of Morality II
Last modified: Sun Sep 25 16:11:37 NZDT 2016
Best viewed at 800x600 or above resolution.
© Copyright 1999-2016 Davin Pearson.
Please report any broken links to