home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / misc / volume33 / problem / part01 / classes.h < prev    next >
Encoding:
C/C++ Source or Header  |  1992-10-19  |  8.6 KB  |  257 lines

  1. /*
  2. ** classes.h - the declarations of the classes used in problem
  3. **
  4. ** classes.h classes.h 1.7   Delta'd: 15:50:54 9/22/92   Mike Lijewski, CNSF
  5. **
  6. ** Copyright (c) 1991, 1992 Cornell University
  7. ** All rights reserved.
  8. **
  9. ** Redistribution and use in source and binary forms are permitted
  10. ** provided that: (1) source distributions retain this entire copyright
  11. ** notice and comment, and (2) distributions including binaries display
  12. ** the following acknowledgement:  ``This product includes software
  13. ** developed by Cornell University'' in the documentation or other
  14. ** materials provided with the distribution and in all advertising
  15. ** materials mentioning features or use of this software. Neither the
  16. ** name of the University nor the names of its contributors may be used
  17. ** to endorse or promote products derived from this software without
  18. ** specific prior written permission.
  19. **
  20. ** THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
  21. ** IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
  22. ** WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  23. */
  24.  
  25. #ifndef __CLASSES_H
  26. #define __CLASSES_H
  27.  
  28. #include <stddef.h>
  29. #include <string.h>
  30.  
  31. #include "problem.h"
  32.  
  33. /////////////////////////////////////////////////////////////////////////////
  34. // A Simple reference counted string class.  It is implemented as an
  35. // Envelope-Letter abstaction with String being the envelope and StringRep
  36. // being the letter.
  37. /////////////////////////////////////////////////////////////////////////////
  38.  
  39. class String;
  40. class SBHelper;
  41.  
  42. class StringRep {
  43.   public:
  44.     friend class String;
  45.     friend class SBHelper;
  46.  
  47.     StringRep();
  48.     StringRep(const char *s);
  49.     StringRep(char** r, size_t slen) { rep = *r; len = slen; count = 1; }
  50.     ~StringRep()                     { DELETE rep;                      }
  51.  
  52.     enum { chunksize = 50 };     // # of StringReps to allocate at a time
  53.     static StringRep *freeList;  // we manage our own storage
  54.     void *operator new(size_t size);
  55.     void operator delete(void *object);
  56.  
  57.     int operator!=(const char *rhs) const;
  58.     int operator==(const char *rhs) const;
  59.     int operator!=(const StringRep& rhs) const;
  60.     int operator==(const StringRep& rhs) const;
  61.  
  62.     String operator+(const String& s) const;
  63.  
  64.     size_t length() const { return len; }
  65.   private:
  66.     //
  67.     // Disable these two methods
  68.     //
  69.     StringRep(const StringRep&);
  70.     StringRep& operator=(const StringRep &);
  71.     union {
  72.         char *rep;
  73.         StringRep *next;
  74.     };
  75.     size_t len;
  76.     int count;
  77. };
  78.  
  79. class String {
  80.   public:
  81.     friend class StringRep;
  82.     friend class SBHelper;
  83.  
  84.     String()                       { p = new StringRep();           }
  85.     String(const String& s)        { p = s.p; p->count++;           }
  86.     String(const char *s)          { p = new StringRep(s);          }
  87.     String(char **s)               { p = new StringRep(s, ::strlen(*s)); }
  88.     String(char** s, size_t slen)  { p = new StringRep(s, slen);    }
  89.     ~String();
  90.  
  91.     String& operator=(const String& rhs);
  92.  
  93.     int operator==(const char *rhs)   const;
  94.     int operator==(const String& rhs) const;
  95.     int operator!=(const char *rhs)   const;
  96.     int operator!=(const String& rhs) const;
  97.  
  98.     String operator+(const String &rhs) const   { return *p + rhs;      }
  99.     friend String operator+(const char *lhs, const String& rhs)
  100.                                             { return rhs + String(lhs); }
  101.  
  102.     void operator+=(const String &rhs);
  103.     void operator+=(const char *rhs);
  104.  
  105.     operator const char *() const { return p->rep; }
  106.     SBHelper operator[](int index);
  107.     size_t length() const { return p->len; }
  108.     void range_error(int index);
  109.   private:
  110.     StringRep *p;
  111. };
  112.  
  113. /////////////////////////////////////////////////////////////////////////////
  114. // This class is a helper class used by String::operator[] to distinguish
  115. // between applications of operator[] on the lhs and rhs of "=" signs.
  116. /////////////////////////////////////////////////////////////////////////////
  117.  
  118. class SBHelper {
  119.   public:
  120.     SBHelper(String &s, int i);
  121.     char operator=(char c);
  122.     operator char() { return str.p->rep[index]; }
  123.   private:
  124.     SBHelper(const SBHelper&);        // disallow this method
  125.     void operator=(const SBHelper&);  // disallow this method
  126.     String &str;
  127.     int index;
  128. };
  129.  
  130. ///////////////////////////////////////////////////////////////////////////////
  131. // DLink - Class modeling a link in a doubly-linked list of strings.
  132. //         We also maintain the length of the string.
  133. ///////////////////////////////////////////////////////////////////////////////
  134.  
  135. class DLink {
  136.     friend class DList;
  137.   public:
  138.     DLink(char **);
  139.     ~DLink() { }
  140.  
  141.     static DLink *freeList;    // we manage our own storage for DLinks
  142.     enum { chunksize = 50 };   // size blocks of DLinks we allocate
  143.     void *operator new(size_t size);
  144.     void operator delete(void *object);
  145.  
  146.     const char *line()  const { return _line;          }
  147.     int length()        const { return _line.length(); }
  148.     DLink *next()       const { return _next;          }
  149.     DLink *prev()       const { return _prev;          }
  150.     void update(char **);
  151.   private:
  152.     String            _line;
  153.     DLink            *_next;
  154.     DLink            *_prev;
  155.     //
  156.     // Disallow these operations by not providing definitions.
  157.     // Also keep compiler from generating default versions of these.
  158.     //
  159.     DLink();
  160.     DLink(const DLink &);
  161.     DLink &operator=(const DLink &); 
  162. };
  163.  
  164. ///////////////////////////////////////////////////////////////////////////////
  165. // DList - Class modeling a doubly-linked list of DLinks.
  166. //         It also maintains our current notion of what
  167. //         is and isn't visible in the window.
  168. ///////////////////////////////////////////////////////////////////////////////
  169.  
  170. class DList {
  171.   public:
  172.     DList();
  173.     ~DList();
  174.  
  175.     DLink *head()              const { return _head;                   }
  176.     DLink *tail()              const { return _tail;                   }
  177.     DLink *firstLine()         const { return _firstLine;              }
  178.     DLink *lastLine()          const { return _lastLine;               }
  179.     DLink *currLine()          const { return _currLine;               }
  180.     DList *next()              const { return _next;                   }
  181.     DList *prev()              const { return _prev;                   }
  182.  
  183.     int savedXPos()            const { return _saved_x;                }
  184.     int savedYPos()            const { return _saved_y;                }
  185.  
  186.     void setFirst(DLink *e)          { _firstLine = e;                 }
  187.     void setLast (DLink *e)          { _lastLine  = e;                 }
  188.     void setCurrLine (DLink *ln)     { _currLine = ln;                 }
  189.  
  190.     void setNext (DList *l)          { _next = l;                      }
  191.     void setPrev (DList *l)          { _prev = l;                      }
  192.  
  193.     int nelems()                 const { return _nelems;               }
  194.     void saveYXPos(int y, int x)       { _saved_x = (short)x;
  195.                                          _saved_y = (short)y;          }
  196.  
  197.     int atBegOfList()            const { return _currLine == _head;    }
  198.     int atEndOfList()            const { return _currLine == _tail;    }
  199.  
  200.     int atWindowTop()            const { return _currLine == _firstLine; }
  201.     int atWindowBot()            const { return _currLine == _lastLine;  }
  202.  
  203.     void add(DLink *);
  204.     void deleteLine();
  205.   private:
  206.     DLink      *_head;
  207.     DLink      *_tail;
  208.     int         _nelems;
  209.     short       _saved_x;     // saved x cursor position
  210.     short       _saved_y;     // saved y cursor position
  211.     DLink      *_firstLine;   // first viewable DLink in window
  212.     DLink      *_lastLine;    // last  viewable DLink in window
  213.     DLink      *_currLine;    // line cursor is on in window
  214.     DList      *_next;
  215.     DList      *_prev;
  216.     //
  217.     // Disallow these operations by not providing definitions.
  218.     // Also keep compiler from generating default versions of these.
  219.     //
  220.     DList(const DList &);
  221.     DList &operator=(const DList &);
  222. };
  223.  
  224. inline int StringRep::operator!=(const char *rhs) const {
  225.     return strcmp(rep, rhs);
  226. }
  227.  
  228. inline int StringRep::operator==(const char *rhs) const {
  229.     return strcmp(rep, rhs) == 0;
  230. }
  231.  
  232. inline int StringRep::operator!=(const StringRep& rhs) const {
  233.     return strcmp(rep, rhs.rep);
  234. }
  235.  
  236. inline int StringRep::operator==(const StringRep& rhs) const {
  237.     return strcmp(rep, rhs.rep) == 0;
  238. }
  239.  
  240. inline int String::operator==(const char *rhs) const {
  241.     return *p == rhs;
  242. }
  243.  
  244. inline int String::operator==(const String& rhs) const {
  245.     return *p == *(rhs.p);
  246. }
  247.  
  248. inline int String::operator!=(const char *rhs) const {
  249.     return *p != rhs;
  250. }
  251.  
  252. inline int String::operator!=(const String& rhs) const {
  253.     return *p != *(rhs.p);
  254. }
  255.  
  256. #endif /* __CLASSES_H */
  257.