home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1993 #3 / NN_1993_3.iso / spool / alt / hackers / 1986 < prev    next >
Encoding:
Text File  |  1993-01-26  |  4.1 KB  |  158 lines

  1. Path: sparky!uunet!haven.umd.edu!darwin.sura.net!mlb.semi.harris.com!uflorida!cybernet!news
  2. From: spc@pineal.math.fau.edu
  3. Newsgroups: alt.hackers
  4. Subject: Re: Ugly macros (or whatever that thread is called)
  5. Message-ID: <1993Jan26.075330.13531@cybernet.cse.fau.edu>
  6. Date: 26 Jan 93 07:53:30 GMT
  7. Sender: news@cybernet.cse.fau.edu
  8. Organization: are you crazy?
  9. Lines: 146
  10. Approved: Yea, right.
  11.  
  12.  
  13. ObHack: I guess posting here ... 8-)
  14.  
  15. ObUglyMacroHack:
  16.  
  17.   When trying to do object oriented programming under ANSI-C (no weenie
  18. using C++ here 8-), I needed to manipulate several stacks (each a different
  19. type, one integer, one float, etc ...).  So, I wrote the following C code
  20. (at the end of this message).
  21.  
  22.   -spc (love playing around with that C preprocessor)
  23.  
  24. ===============[ header file ]=========================================
  25.  
  26. /***************************************************************************
  27. *
  28. * Because the ## in a macro definition does not expand any argument macros,
  29. * we simply can't do a _MKPROTO(x,y)    x ## y, because then
  30. * _MKPROTO(T_PREFIX,_push) would improperly evaluate to T_PREFIX_push, which
  31. * is what we DON'T want, therefore, we have to do a bit of nesting to get
  32. * what we want.
  33. *
  34. * That's why this looks so strange ...
  35. *
  36. *****************************************************************************/
  37.  
  38. #define _MKKPROTO(x,y)  x ## y
  39. #define _MKPROTO(x,y)   _MKKPROTO(x,y)
  40.  
  41. void    _MKPROTO(T_PREFIX,_push)        (T_TYPE);
  42. T_TYPE  _MKPROTO(T_PREFIX,_pop)         (void);
  43. void    _MKPROTO(T_PREFIX,_drop)        (void);
  44. void    _MKPROTO(T_PREFIX,_swap)        (void);
  45. void    _MKPROTO(T_PREFIX,_dup)         (void);
  46. void    _MKPROTO(T_PREFIX,_over)        (void);
  47.  
  48. /*************************************************************/
  49.  
  50. ==============[ C source file ]=================================
  51.  
  52. #include "that.header.file.h"
  53.  
  54. #define DX(x)           x
  55.  
  56. /*********************************************************************/
  57.  
  58. void _MKPROTO(T_PREFIX,_push)(T_TYPE v)
  59. {
  60.   struct dictionary *pdc = _MKPROTO(T_PREFIX,_parent);
  61.   T_TYPE            *pv;
  62.  
  63.   if (pdc->dc_Stackptr == 0)
  64.   {
  65.     _Error(__FILE__,__LINE__,"%s stack overflow",pdc->dc_Node.ln_Name);
  66.     _Stackerr = TRUE;
  67.     return;
  68.   }
  69.   pdc->dc_Stackptr--;
  70.   pv = (T_TYPE *)(pdc->dc_Stack);
  71.   pv[pdc->dc_Stackptr] = v;
  72. }
  73.  
  74. /********************************************************************/
  75.  
  76. T_TYPE _MKPROTO(T_PREFIX,_pop)(void)
  77. {
  78.   struct dictionary *pdc = _MKPROTO(T_PREFIX,_parent);
  79.   T_TYPE            *pv;
  80.   T_TYPE             v;
  81.  
  82.   if (pdc->dc_Stackptr == pdc->dc_Stacktop)
  83.   {
  84.     _Error(__FILE__,__LINE__,"%s stack underflow",pdc->dc_Node.ln_Name);
  85.     _Stackerr = TRUE;
  86.     return(T_RETV);
  87.   }
  88.   pv = (T_TYPE *)(pdc->dc_Stack);
  89.   v  = pv[pdc->dc_Stackptr];
  90.  
  91.   pdc->dc_Stackptr++;
  92.   return(v);
  93. }
  94.  
  95. /*******************************************************************/
  96. void _MKPROTO(T_PREFIX,_drop)(void)
  97. {
  98.   struct dictionary *pdc = _MKPROTO(T_PREFIX,_parent);
  99.  
  100.   if (pdc->dc_Stackptr == pdc->dc_Stacktop)
  101.   {
  102.     _Error(__FILE__,__LINE__,"%s stack underflow",pdc->dc_Node.ln_Name);
  103.     return;
  104.   }
  105.   pdc->dc_Stackptr++;
  106. }
  107.  
  108. /*****************************************************************/
  109.  
  110. void _MKPROTO(T_PREFIX,_swap)(void)
  111. {
  112.   T_TYPE v1;
  113.   T_TYPE v2;
  114.  
  115.   _Stackerr = FALSE;
  116.   v1 = _MKPROTO(T_PREFIX,_pop)();
  117.   if (_Stackerr) return;
  118.   v2 = _MKPROTO(T_PREFIX,_pop)();
  119.   if (_Stackerr) return;
  120.   _MKPROTO(T_PREFIX,_push)(v1);
  121.   if (_Stackerr) return;
  122.   _MKPROTO(T_PREFIX,_push)(v2);
  123. }
  124.  
  125. /******************************************************************/
  126.  
  127. void _MKPROTO(T_PREFIX,_dup)(void)
  128. {
  129.   T_TYPE v;
  130.  
  131.   _Stackerr = FALSE;
  132.   v = _MKPROTO(T_PREFIX,_pop)();
  133.   if (_Stackerr) return;
  134.   _MKPROTO(T_PREFIX,_push)(v);
  135.   _MKPROTO(T_PREFIX,_push)(v);
  136. }
  137.  
  138. /****************************************************************/
  139.  
  140. void _MKPROTO(T_PREFIX,_over)(void)
  141. {
  142.   T_TYPE v1;
  143.   T_TYPE v2;
  144.  
  145.   _Stackerr = FALSE;
  146.  
  147.   v1 = _MKPROTO(T_PREFIX,_pop)();
  148.   if (_Stackerr) return;
  149.   _MKPROTO(T_PREFIX,_dup)();
  150.   if (_Stackerr) return;
  151.   v2 = _MKPROTO(T_PREFIX,_pop)();
  152.   if (_Stackerr) return;
  153.  
  154.   _MKPROTO(T_PREFIX,_push)(v1);
  155.   if (_Stackerr) return;
  156.   _MKPROTO(T_PREFIX,_push)(v2);
  157. }
  158.