home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / IDIOMS.ZIP / EK.H < prev    next >
C/C++ Source or Header  |  1991-12-04  |  3KB  |  85 lines

  1. /* Copyright (c) 1992 by AT&T Bell Laboratories. */
  2. /* Advanced C++ Programming Styles and Idioms */
  3. /* James O. Coplien */
  4. /* All rights reserved. */
  5.  
  6. //************************************************************//
  7. //                                                            //
  8. //      F I L E :   E K . H                                   //
  9. //                                                            //
  10. //         Declarations for classes Thing and Top             //
  11. //                                                            //
  12. //************************************************************//
  13.  
  14. #ifndef _MPTR_H
  15. #include "emptr.h"
  16. #endif
  17.  
  18. #define _K_H
  19. #include <String.h>
  20.  
  21. //  MACHINE AND COMPILER DEPENDENT
  22. const unsigned int WRDSIZE = sizeof(void*);
  23.  
  24. inline size_t
  25. Round(size_t s) {
  26.     return (size_t)((s + WRDSIZE - 1)/WRDSIZE)*WRDSIZE;
  27. }
  28.  
  29. // commonly used character pointer type
  30. typedef char *Char_p;
  31.  
  32. // dummy type, simply used to disambiguate between
  33. // constructors of ShapeRep derived classes
  34. enum Exemplar { };
  35.  
  36. //  MACHINE AND COMPILER DEPENDENT
  37.  
  38. class Top {
  39. public:
  40.     // This class has no data, except __vtbl which is
  41.     // provided by the compiler.  Deriving all classes
  42.     // from this class assures that the __vtbl will
  43.     // always be the first element in any object.
  44.     // If that is not true for the compiler being
  45.     // used, then other mechanisms will be necessary
  46.     // to find the __vtbl, and the implementation
  47.     // here may need to change (viz., findVtblEntry).
  48.  
  49.     // This also reserves a __vtbl slot for system
  50.     // internal use
  51.  
  52.     virtual ~Top() { }
  53.     mptr* findVtblEntry(vptp);
  54.     void update(String, String, const char *const = "");
  55.     static void operator delete(void *p) {
  56.         ::operator delete(p);
  57.     }
  58.     // doit is a general-purpose function to help users
  59.     // to orchestrate update.
  60.     virtual Top *doit();
  61. protected:
  62.     Top()  { }
  63.     static void *operator new(size_t l) {
  64.         return ::operator new(l);
  65.     }
  66. private:
  67.     // compare two function pointers for equality
  68.     int compareFuncs(int, vptp, vptp);
  69. };
  70.  
  71. typedef Top *Topp;
  72.  
  73. class Thing: public Top {
  74.     // All  "rep" fields are derived from Thing;  it defines
  75.     // the canonical form for all Letter classes
  76. public:
  77.     virtual Thing *type() { return this; }
  78.     Thing() { }
  79.     virtual Thing *cutover();       // field update function
  80.     virtual ~Thing() { }            // destructor
  81.     int docutover();
  82. };
  83.  
  84. typedef Thing *Thingp;
  85.