home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #18 / NN_1992_18.iso / spool / comp / lang / cplus / 12516 < prev    next >
Encoding:
Text File  |  1992-08-18  |  3.8 KB  |  130 lines

  1. Path: sparky!uunet!wupost!darwin.sura.net!zaphod.mps.ohio-state.edu!uwm.edu!lll-winken!taurus!taygeta.oc.nps.navy.mil!skip
  2. From: skip@taygeta.oc.nps.navy.mil (Skip Carter)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Reference counting for vectors, (Tony Hansen's book).
  5. Message-ID: <5936@taurus.cs.nps.navy.mil>
  6. Date: 18 Aug 92 19:18:18 GMT
  7. References: <1992Aug14.164719.9719@wuecl.wustl.edu> <BRISTER.92Aug14110706@tirade.decwrl.dec.com>
  8. Sender: news@taurus.cs.nps.navy.mil
  9. Reply-To: skip@taygeta.oc.nps.navy.mil (Skip Carter)
  10. Lines: 118
  11.  
  12. In article <BRISTER.92Aug14110706@tirade.decwrl.dec.com>, brister@decwrl.dec.com (James Brister) writes:
  13. ...
  14. |> 
  15. |> I do remember seeing an article in a magazine a while back about COW (copy
  16. |> on write), where the new array (i.e. 'B' here)
  17. |> 
  18. |> >     vector B = A; 
  19. |> 
  20. |> would release the referrence and make a duplicate of the real data if any
  21. |> data was going to be modified. Unfortuantely I can't remember the details
  22. |> right now of how it worked, but it didn't seem too tough to do.
  23. |> 
  24. |>
  25.  
  26.     The article was in the Aug 1991 issue of the C Users Journal,
  27.     (see that article for details)   here are the base classes that are
  28.      required (I have used them to implement a Vector class as was 
  29.     described in the original posting),
  30.  
  31. // cow.hpp    the definition of a Copy-On-Write object
  32. //        see C Users Journal, Aug 1991
  33.  
  34. //    modified so that ~Obj_COW() is virtual so that the actual objects
  35. //         destructor is called            EFC  15 May 1992
  36. //    make get_ptr() a const function            EFC  21 May 1992
  37.  
  38. // rcsid: $Id$ @(#)cow.hpp    1.2 23:05:28 5/21/92  EFC
  39.  
  40. #ifndef _COW_HPP
  41. #define _COW_HPP
  42.  
  43. #include <stdlib.h>
  44. #ifdef __ZTC__
  45. #include <stdio.h>
  46. #endif
  47.  
  48. class Obj_COW        // a real object, for use by virtual objects
  49. {
  50.     friend class Obj_Virt;    // allow virtual object to access real object
  51.     private:
  52.          short count;        // count of uses
  53.     protected:
  54.          Obj_COW(void) {count = 0;}
  55.          virtual ~Obj_COW() {}
  56.          virtual Obj_COW *dup(void) = 0;
  57. };
  58.  
  59. class Obj_Virt        // a virtual object, points to real object
  60. {
  61.    private:
  62.          Obj_COW *po;
  63.     protected:
  64.           Obj_Virt(void) { po = NULL; }
  65.           Obj_Virt(const Obj_Virt& vo) { set_ptr(vo.po); }
  66.         ~Obj_Virt(void) { release(); }
  67.           Obj_Virt& operator = (Obj_Virt& vo)
  68.             { set_ptr(vo.po); return (*this); }
  69.          Obj_COW    *get_ptr(void) const { return(po); }
  70.          void    print(void);
  71.          void    release(void);
  72.          void    set_ptr(Obj_COW *pobj);
  73.          void    split(void);
  74. };
  75.  
  76. #endif
  77.  
  78.  
  79. // cow.cpp    implementation of part of the Copy-On-Write
  80. //        class handler
  81.  
  82. static const char rcsid[] = "$Id$ @(#)cow.cpp    1.2 23:05:43 5/21/92  EFC";
  83.  
  84. #include <classlib/cow.hpp>
  85.  
  86. void Obj_Virt::print(void)    // print current values
  87. {
  88.     printf("po = %p, count = %d\n", po, po->count);
  89. }
  90.  
  91. // release the real object for this virtual object
  92. void Obj_Virt::release(void)
  93. {
  94.          if ( NULL != po )
  95.         {
  96.             // decrement count, delete object of zero
  97.                 if ( 0 == --po->count )
  98.                     delete po;
  99.                 po = NULL;
  100.        }
  101. }
  102.  
  103. // do a virtual copy of a real object
  104. void Obj_Virt::set_ptr(Obj_COW *pobj)
  105. {
  106.         release();    // Release current object if any
  107.         po = pobj;
  108.         po->count++;
  109. }
  110.  
  111. // create a new real object if needed
  112. void Obj_Virt::split(void)
  113. {
  114.                   if ( 1 < po->count )        // split only if count > 1
  115.                    {
  116.                po->count--;
  117.                 po = po->dup();
  118.                        po->count = 1;
  119.                   }
  120. }
  121.                                               
  122.  
  123.  
  124.  
  125.  
  126.  Everett (Skip) Carter             Phone:  408-646-3318 FAX: 408-646-2712
  127.  Naval Postgraduate School         INTERNET: skip@taygeta.oc.nps.navy.mil
  128.  Dept. of Oceanography, Code OC/CR  UUCP:     ...!uunet!taygeta!skip
  129.  Monterey, CA. 93943               TELEMAIL: s.carter/omnet
  130.