home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / misc / volume6 / lookup.c++ / README < prev    next >
Encoding:
Text File  |  1989-03-04  |  2.8 KB  |  91 lines

  1. DESCRIPTION
  2.  
  3. Using the files lookup.c [or lookup2.c] and lookup.h, you can easily maintain
  4. a command lookup table.  The table must be of type "entry *", and you can see
  5. in the example code [test.c, testcmd.c, testhelp.c] that I used an aggreagate
  6. array of the form:
  7.  
  8. entry list[] = { ... };
  9.  
  10. The first "entry" in the list must have a count and a pointer to an error
  11. function.  If you have no error function, create a null function, which
  12. does nothing, and use a pointer to it.  Since the number occupies the
  13. position of a char*, you have to cast it to such.  i.e., if you have
  14. #define'd NUM to the number of elements, you would say
  15.  
  16. entry list[] = {
  17.   (char *)NUM, &err,
  18.   ...
  19.   };
  20.  
  21. where err is the error-handling function.  err would be called when a string
  22. not in the list is referenced.
  23.  
  24. Now, the list is not the object that does the work.  Another object, of class
  25. "lookup" is initialized with a pointer to an "entry *".  e.g., given the
  26. above example, you could say:
  27.  
  28. lookup cmd(list);
  29.  
  30. Thereafter, to retrieve the function pointer desired, you simply reference
  31. "cmd" with a string:
  32.  
  33. int (*proc)(const char *);
  34. proc=cmd["query"];
  35.  
  36. This would cause "proc" to point to the function associated with the
  37. word "query", if any; otherwise, it would point to the error function,
  38. which we designated above to be "err".  Now you can happily dereference the
  39. pointer and pass it a "const char *":
  40.  
  41. proc("testing");
  42.  
  43. Now note that you can do both of these at once:
  44.  
  45. cmd["query"]("Testing");
  46.  
  47. Isn't that special?
  48.  
  49. The only difference between lookup.c and lookup2.c is the implementation.
  50. Each gives you an advantage.  If your list of names is sorted, use
  51. lookup.c since it uses a binary search.  Obviously, if they are not
  52. in any order, a binary search will not work, so lookup2.c just does
  53. a linear search.  This is the only difference between the two.
  54.  
  55.  
  56. ARGUMENT TYPE
  57.  
  58. All of the function pointers in the "entry" list must take the same
  59. argument[s], if any, and return the same value type.  I haven't built
  60. any elegant macros, but there are #defines in "lookup.h" to handle both
  61. of these problems in a less than elegant manner.
  62.  
  63. The two macros ARG_TYPE and RET_TYPE define the argument type and value
  64. value returned by the function pointers.  Just change these to handle whatever
  65. types you like.
  66.  
  67.  
  68. CAVEATS
  69.  
  70. This is one of my first attempts at C++;  I hope you find it useful.  If
  71. you make any major modifications, or even more importantly, if you find any
  72. bugs, please let me know so I can fix my source and post the patches for
  73. one and all.
  74.  
  75.  
  76. COPYRIGHT
  77.  
  78. I the author hereby place this code in the public domain.  Make enough
  79. money to buy Canada and hire me as Prime Minister!
  80.  
  81.  
  82. AUTHOR
  83.  
  84. Reid Ellis
  85. 176 Brookbanks Drive
  86. Don Mills, Ontario
  87. M3A 2T5
  88. CANADA
  89. +1 416 446 1644
  90. rae@geaclib.uucp if you're lucky, or else geaclib!rae@geac.uucp
  91.