home *** CD-ROM | disk | FTP | other *** search
/ Geek Gadgets 1 / ADE-1.bin / ade-dist / libg++-2.7.1-bin.lha / lib / g++-include / std / typeinfo.h < prev   
C/C++ Source or Header  |  1996-10-12  |  8KB  |  246 lines

  1. // RTTI support for -*- C++ -*-
  2. // Copyright (C) 1994, 1995 Free Software Foundation
  3.  
  4. // This file is part of the GNU ANSI C++ Library.  This library is free
  5. // software; you can redistribute it and/or modify it under the
  6. // terms of the GNU General Public License as published by the
  7. // Free Software Foundation; either version 2, or (at your option)
  8. // any later version.
  9.  
  10. // This library is distributed in the hope that it will be useful,
  11. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13. // GNU General Public License for more details.
  14.  
  15. // You should have received a copy of the GNU General Public License
  16. // along with this library; see the file COPYING.  If not, write to the Free
  17. // Software Foundation, 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
  18.  
  19. // As a special exception, if you link this library with files
  20. // compiled with a GNU compiler to produce an executable, this does not cause
  21. // the resulting executable to be covered by the GNU General Public License.
  22. // This exception does not however invalidate any other reasons why
  23. // the executable file might be covered by the GNU General Public License.
  24.  
  25. // Written by Kung Hsu based upon the specification in the 20 September 1994
  26. // C++ working paper, ANSI document X3J16/94-0158.
  27.  
  28. #ifndef __TYPEINFO__
  29. #define __TYPEINFO__
  30.  
  31. #ifdef __GNUG__
  32. #pragma interface "std/typeinfo.h"
  33. #endif
  34.  
  35. extern "C" void* __throw_type_match_rtti (void *, void *, void *);
  36.  
  37. extern "C++" {
  38. class type_info {
  39. private:
  40.   // assigning type_info is not supported.  made private.
  41.   type_info& operator=(const type_info&);
  42.   type_info(const type_info&);
  43.  
  44. public:
  45.   enum node_type {
  46.     _RTTI_BUILTIN_TYPE,               // builtin type
  47.     _RTTI_USER_TYPE,                  // user defined type
  48.     _RTTI_CLASS_TYPE,                 // class type
  49.     _RTTI_POINTER_TYPE,               // pointer type
  50.     _RTTI_ATTR_TYPE,                  // attribute type for const and volatile
  51.     _RTTI_FUNC_TYPE,                  // function type
  52.     _RTTI_PTMF_TYPE,                  // pointer to member function type
  53.     _RTTI_PTMD_TYPE                   // pointer to member data type
  54.     };
  55.  
  56.   // return node type of the object
  57.   virtual node_type __rtti_get_node_type() const { return _RTTI_BUILTIN_TYPE; }
  58.  
  59.   // get_name will return the name of the type, NULL if no name (like builtin)
  60.   virtual const char * __rtti_get_name() const { return 0; }
  61.  
  62.   // compare if type represented by the type_info are the same type
  63.   virtual int __rtti_compare(const type_info&) const { return 0; }
  64.  
  65.   // argument passed is the desired type, 
  66.   // for class type, if the type can be converted to the desired type, 
  67.   // it will be, and returned, else 0 is returned.  If the match
  68.   // succeeds, the return value will be adjusted to point to the sub-object.
  69.   virtual void* __rtti_match(const type_info&, int, void *) const {
  70.     // This should never be called.
  71.     return 0;
  72.   };
  73.  
  74.   // destructor
  75.   virtual ~type_info() {}
  76.   type_info() {}
  77.     
  78.   bool before(const type_info& arg);
  79.   const char* name() const
  80.     { return __rtti_get_name(); }
  81.   bool operator==(const type_info& arg) const 
  82.     { return __rtti_compare(arg) == 0; }
  83.   bool operator!=(const type_info& arg) const 
  84.     { return __rtti_compare(arg) != 0; }
  85. };
  86.  
  87. // type_info for builtin type
  88.  
  89. class __builtin_type_info : public type_info {
  90. public:
  91.   enum builtin_type_val {
  92.     _RTTI_BI_BOOL = 1, _RTTI_BI_CHAR, _RTTI_BI_SHORT, _RTTI_BI_INT, 
  93.     _RTTI_BI_LONG, _RTTI_BI_LONGLONG, _RTTI_BI_FLOAT,
  94.     _RTTI_BI_DOUBLE, _RTTI_BI_LDOUBLE, _RTTI_BI_UCHAR, 
  95.     _RTTI_BI_USHORT, _RTTI_BI_UINT, _RTTI_BI_ULONG,
  96.     _RTTI_BI_ULONGLONG, _RTTI_BI_SCHAR, _RTTI_BI_WCHAR, _RTTI_BI_VOID
  97.   };
  98.  
  99.   builtin_type_val b_type;
  100.  
  101.   __builtin_type_info (builtin_type_val bt) : b_type (bt) {}
  102.   node_type __rtti_get_node_type () const
  103.     { return _RTTI_BUILTIN_TYPE; }
  104.   const char *__rtti_get_name () const
  105.     { return (const char *)0; }
  106.   int __rtti_compare (const type_info& arg) const
  107.     { return (arg.__rtti_get_node_type () == _RTTI_BUILTIN_TYPE && 
  108.           ((__builtin_type_info&)arg).b_type == b_type) ? 0 : -1; }
  109. };
  110.  
  111. // serice function for comparing types by name.
  112.  
  113. inline int __fast_compare (const char *n1, const char *n2) {
  114.   int c;
  115.   if (n1 == n2) return 0;
  116.   if (n1 == 0) return *n2;
  117.   else if (n2 == 0) return *n1;
  118.  
  119.   c = (int)*n1++ - (int)*n2++;
  120.   return c == 0 ? strcmp (n1, n2) : c;
  121. };
  122.  
  123. // type_info for user type.
  124.  
  125. class __user_type_info : public type_info {
  126.  private:
  127.   const char *_name;
  128.  
  129. public:
  130.   __user_type_info (const char *nm) : _name (nm) {}
  131.   node_type __rtti_get_node_type () const
  132.     { return _RTTI_USER_TYPE; }
  133.   const char *__rtti_get_name () const
  134.     { return _name; }
  135.   int __rtti_compare (const type_info& arg) const
  136.     { return (arg.__rtti_get_node_type () == __rtti_get_node_type() &&
  137.     __fast_compare (_name, arg.__rtti_get_name ()) == 0) ? 0 : -1; }
  138. };
  139.  
  140. // type_info for a class.
  141.  
  142. class __class_type_info : public __user_type_info {
  143. private:
  144.   enum access_mode {
  145.     _RTTI_ACCESS_PUBLIC, _RTTI_ACCESS_PROTECTED, _RTTI_ACCESS_PRIVATE
  146.     };
  147.   type_info **base_list;
  148.   int *offset_list;
  149.   int *is_virtual_list;
  150.   access_mode *access_list;
  151.   int n_bases;
  152.  
  153. public:
  154.   __class_type_info (const char *name, type_info **bl, int *off, 
  155.              int *is_vir, access_mode *acc, int bn)
  156.     : __user_type_info (name), base_list (bl), offset_list(off), 
  157.       is_virtual_list(is_vir), access_list(acc), n_bases (bn) {}
  158.   node_type __rtti_get_node_type () const
  159.     { return _RTTI_CLASS_TYPE; }
  160.  
  161.   // inherit __rtti_compare from __user_type_info
  162.  
  163.   // This is a little complex defined in typeinfo.cc
  164.   void* __rtti_match(const type_info&, int, void *) const;
  165. };
  166.  
  167. // type info for pointer type.
  168.  
  169. class __pointer_type_info : public type_info {
  170. private:
  171.   type_info& type;
  172.  
  173. public:
  174.   __pointer_type_info (type_info& ti) : type (ti) {}
  175.   node_type __rtti_get_node_type () const
  176.     { return _RTTI_POINTER_TYPE; }
  177.   const char *__rtti_get_name () const
  178.     { return (const char *)0; }
  179.   int __rtti_compare (const type_info& arg) const
  180.     { return (arg.__rtti_get_node_type () == __rtti_get_node_type() &&
  181.     type.__rtti_compare ( ((__pointer_type_info&)arg).type) == 0) ? 0 : -1; }
  182.   void* __rtti_match(const type_info& catch_type, int, void *objptr) const;
  183. };
  184.  
  185. // type info for attributes
  186.  
  187. class __attr_type_info : public type_info {
  188. public:
  189.   enum attr_val {
  190.     _RTTI_ATTR_CONST = 1, _RTTI_ATTR_VOLATILE, _RTTI_ATTR_CONSTVOL
  191.     };
  192.  
  193.   attr_val attr;
  194.   type_info& type;
  195.  
  196.   __attr_type_info (attr_val a, type_info& t) : attr (a), type(t) {}
  197.   node_type __rtti_get_node_type () const
  198.     { return _RTTI_ATTR_TYPE; }
  199.   const char *__rtti_get_name () const
  200.     { return (const char *)0; }
  201.   int __rtti_compare (const type_info& arg)  const
  202.     { return (arg.__rtti_get_node_type () == _RTTI_ATTR_TYPE && 
  203.           attr == ((__attr_type_info&)arg).attr &&
  204.           type.__rtti_compare ( ((__attr_type_info&)arg).type ) == 0) 
  205.           ? 0 : -1; }
  206. };
  207.  
  208. // type info for function.
  209.  
  210. class __func_type_info : public __user_type_info {
  211. public:
  212.   __func_type_info (const char *name) : __user_type_info (name) {}
  213.   node_type __rtti_get_node_type () const
  214.     { return _RTTI_FUNC_TYPE; }
  215. };
  216.  
  217. // type info for pointer to member function.
  218.  
  219. class __ptmf_type_info : public __user_type_info {
  220. public:
  221.   __ptmf_type_info (const char *name) : __user_type_info (name) {}
  222.   node_type __rtti_get_node_type () const
  223.     { return _RTTI_PTMF_TYPE; }
  224. };
  225.  
  226. // type info for pointer to data member.
  227.  
  228. class __ptmd_type_info : public type_info {
  229.   type_info& classtype;
  230.   type_info& type;
  231. public:
  232.   __ptmd_type_info (type_info& tc, type_info& t) : classtype (tc), type (t) {}
  233.   node_type __rtti_get_node_type () const
  234.     { return _RTTI_PTMD_TYPE; }
  235.   int __rtti_compare (const type_info& arg)  const
  236.     { return (arg.__rtti_get_node_type () == _RTTI_PTMD_TYPE && 
  237.     classtype.__rtti_compare ( ((__ptmd_type_info&)arg).classtype ) == 0 &&
  238.     type.__rtti_compare ( ((__ptmd_type_info&)arg).type ) == 0) 
  239.           ? 0 : -1; }
  240. };
  241. } // extern "C++"
  242.  
  243. #include <stdexcept>
  244.  
  245. #endif
  246.