handle_types.h
Upload User: gzelex
Upload Date: 2007-01-07
Package Size: 707k
Code Size: 3k
Development Platform:

MultiPlatform

  1. /*******************************************************************************
  2. +
  3. +  LEDA-R  3.2.3
  4. +
  5. +  handle_types.h
  6. +
  7. +  Copyright (c) 1995  by  Max-Planck-Institut fuer Informatik
  8. +  Im Stadtwald, 66123 Saarbruecken, Germany     
  9. +  All rights reserved.
  10. *******************************************************************************/
  11. #ifndef LEDA_HANDLE_H
  12. #define LEDA_HANDLE_H
  13. //------------------------------------------------------------------------------
  14. // handle_base/rep: base classes for handle types string, point, segment,...
  15. //------------------------------------------------------------------------------
  16. class handle_rep  {
  17. friend class handle_base;
  18. protected:
  19.  int  count;
  20.  handle_rep()  { count = 1; }
  21.  virtual ~handle_rep()  {}
  22. public:
  23.  LEDA_MEMORY(handle_rep)
  24. };
  25. class handle_base {
  26. protected:
  27. handle_rep* PTR;
  28. public:
  29.  handle_base() {}
  30.  handle_base(const handle_base& x) { PTR = x.PTR;  PTR->count++; }
  31. ~handle_base() { if (PTR && --PTR->count == 0)  delete PTR;}
  32. handle_base& operator=(const handle_base& x)
  33. { x.PTR->count++;
  34.   if (PTR && --PTR->count == 0)  delete PTR; // left side may be 0
  35.   PTR = x.PTR;
  36.   return *this;
  37.  }
  38. int    refs()  const { return PTR->count; }
  39. friend unsigned long ID_Number(const handle_base& x)
  40. { return (unsigned long)x.PTR; }
  41. friend bool identical(const handle_base& x, const handle_base& y)
  42. { return x.PTR == y.PTR; }
  43. #if !defined(__EXPLICIT_DESTRUCTION__)
  44. void  clear() { if (PTR && --PTR->count == 0) delete PTR; }
  45. friend void explicit_destruction(handle_base* p) {  p->clear(); }
  46. #endif
  47. #if !defined(__TEMPLATE_FUNCTIONS__)
  48.  
  49. // without function templates we define  Copy,Convert,Clear, ..
  50. // functions valid for all handle types derived from handle_base
  51.  
  52. GenPtr copy() const { PTR->count++; return PTR; }
  53. GenPtr conv() const { return PTR; }
  54. void   clear() { if (PTR && --PTR->count == 0) delete PTR; }
  55.  
  56. friend inline GenPtr Copy(const handle_base&);
  57. friend inline GenPtr Convert(const handle_base&);
  58. friend inline void   Clear(handle_base&);
  59. friend inline int    compare(const handle_base&, const handle_base&);
  60. friend inline int    Hash(const handle_base&);
  61. friend inline void   Print(const handle_base&, ostream&);
  62. friend inline void   Read(handle_base&, istream&);
  63.  
  64. #endif
  65.  
  66. };
  67.  
  68. #if !defined(__TEMPLATE_FUNCTIONS__)
  69.  
  70. inline GenPtr Copy(const handle_base& x)    { return x.copy(); }
  71. inline GenPtr Convert(const handle_base& x) { return x.conv(); }
  72. inline void   Clear(handle_base& x)         { x.clear(); }
  73.  
  74. inline int compare(const handle_base& x, const handle_base& y)
  75. { return compare(x.PTR, y.PTR); }
  76.  
  77. inline int  Hash(const handle_base& x)               { return *(int*)x.PTR; }
  78. inline void Print(const handle_base& x, ostream& os) { Print(x,os); }
  79. inline void Read(handle_base& x, istream& is)        { Read(x,is); }
  80.  
  81. #endif
  82. #define LEDA_HANDLE_TYPE(T)  /* historical, not used  anymore */
  83.  
  84.  
  85. #endif
  86.