home *** CD-ROM | disk | FTP | other *** search
/ BCI NET 2 / BCI NET 2.iso / archives / programming / c / objam01.lha / objam / objc / selector.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-02-10  |  3.0 KB  |  119 lines

  1. /*
  2. ** ObjectiveAmiga: Selector related functions
  3. ** See GNU:lib/libobjam/ReadMe for details
  4. */
  5.  
  6.  
  7. #include "runtime.h"
  8.  
  9. /* Initial selector hash table size. Value doesnt matter much */
  10. #define SELECTOR_HASH_SIZE 128
  11.  
  12. /* Tables mapping selector names to uid and opposite */
  13. static struct sarray* __objc_selector_array = 0; /* uid -> name */
  14. static cache_ptr      __objc_selector_hash  = 0; /* name -> uid */
  15.  
  16. static void register_selectors_from_list(MethodList_t);
  17.  
  18. /* Number of selectors stored in each of the above tables */
  19. int __objc_selector_max_index = 0;
  20.  
  21. void __objc_init_selector_tables()
  22. {
  23.   __objc_selector_array = sarray_new (SELECTOR_HASH_SIZE, 0);
  24.   __objc_selector_hash
  25.     = hash_new (SELECTOR_HASH_SIZE,
  26.         (hash_func_type) hash_string,
  27.         (compare_func_type) compare_strings);
  28. }  
  29.  
  30. /* This routine is given a class and records all of the methods in its class
  31.    structure in the record table.  */
  32. void
  33. __objc_register_selectors_from_class (OCClass* class)
  34. {
  35.   MethodList_t method_list;
  36.  
  37.   method_list = class->methods;
  38.   while (method_list)
  39.     {
  40.       register_selectors_from_list (method_list);
  41.       method_list = method_list->method_next;
  42.     }
  43. }
  44.  
  45.  
  46. /* This routine is given a list of methods and records each of the methods in
  47.    the record table.  This is the routine that does the actual recording
  48.    work.
  49.  
  50.    This one is only called for Class objects.  For categories,
  51.    class_add_method_list is called.
  52.    */
  53. static void
  54. register_selectors_from_list (MethodList_t method_list)
  55. {
  56.   int i = 0;
  57.   while (i < method_list->method_count)
  58.     {
  59.       Method_t method = &method_list->method_list[i];
  60.       method->method_name = sel_register_name ((char*)method->method_name);
  61.       i += 1;
  62.     }
  63. }
  64.  
  65. /* return selector representing name */
  66. SEL
  67. sel_get_uid (const char *name)
  68. {
  69.   return (SEL) hash_value_for_key (__objc_selector_hash, name);
  70. }
  71.  
  72. /* Get name of selector.  If selector is unknown, the empty string "" 
  73.    is returned */ 
  74. const char*
  75. sel_get_name (SEL selector)
  76. {
  77.   if ((soffset_decode((sidx)selector) > 0)
  78.       && (soffset_decode((sidx)selector) <= __objc_selector_max_index))
  79.     return sarray_get (__objc_selector_array, (sidx) selector);
  80.   else
  81.     return NULL;
  82. }
  83.  
  84. BOOL
  85. sel_is_mapped (SEL selector)
  86. {
  87.   unsigned int idx = soffset_decode ((sidx)selector);
  88.   return ((idx > 0) && (idx <= __objc_selector_max_index));
  89. }
  90.  
  91. /* The uninstalled dispatch table */
  92. extern struct sarray* __objc_uninstalled_dtable;
  93.  
  94. /* Store the passed selector name in the selector record and return its
  95.    selector value (value returned by sel_get_uid). */
  96. SEL
  97. sel_register_name (const char *sel)
  98. {
  99.   SEL j;
  100.   sidx i;
  101.  
  102.   if ((j = sel_get_uid ((const char *) sel)))
  103.     return j;
  104.  
  105.   /* Save the selector name.  */
  106.   __objc_selector_max_index += 1;
  107.   i = soffset_encode(__objc_selector_max_index);
  108.  
  109.   DEBUG_PRINTF ("Record selector %s as: %#x\n", sel, i);
  110.  
  111.   sarray_at_put_safe (__objc_selector_array, i, (void *) sel);
  112.   hash_add (&__objc_selector_hash, (void *) sel, (void *) i);
  113.  
  114.   sarray_realloc(__objc_uninstalled_dtable, __objc_selector_max_index+1);
  115.  
  116.   return (SEL) i;
  117. }
  118.  
  119.