cloudy  trunk
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
count_ptr.h
Go to the documentation of this file.
1 /* This file is part of Cloudy and is copyright (C)1978-2017 by Gary J. Ferland and
2  * others. For conditions of distribution and use see copyright notice in license.txt */
3 #ifndef COUNT_PTR_H_
4 #define COUNT_PTR_H_
5 
6 #include <algorithm>
7 
8 // Temporary stand-in for shared_ptr<> until we can rely on it being
9 // available as standard
10 template <class T>
11 class count_ptr {
12 private:
13  T* m_ptr;
14  long *m_count;
15 public:
16  // Constructors
17  explicit count_ptr(T* ptr = 0)
18  : m_ptr(ptr), m_count(new long(1))
19  { }
21  : m_ptr(p.m_ptr), m_count(p.m_count)
22  {
23  ++*m_count;
24  }
25  ~count_ptr() throw ()
26  {
27  cancel();
28  }
29  // Modifiers
31  {
32  swap(temp);
33  return *this;
34  }
35  // Don't implement arithmetic -- would corrupt the owned pointer
36  // Accessors
37  T& operator*() const
38  {
39  return *m_ptr;
40  }
41  T* operator->() const throw()
42  {
43  return m_ptr;
44  }
45  int equiv(const count_ptr<T>& p) const throw()
46  {
47  return m_ptr == p.m_ptr;
48  }
49  T* get_ptr() const throw()
50  {
51  return m_ptr;
52  }
53  long count() const
54  {
55  return *m_count;
56  }
57  void swap (count_ptr<T> &a) throw()
58  {
59  std::swap(m_ptr,a.m_ptr);
60  std::swap(m_count,a.m_count);
61  }
62  int compare (const count_ptr<T> &a) const
63  {
64  if (m_ptr < a.m_ptr)
65  return -1;
66  else if (m_ptr > a.m_ptr)
67  return 1;
68  return 0;
69  }
70 private:
71  void cancel ()
72  {
73  if (0 == --*m_count)
74  {
75  delete m_count;
76  delete m_ptr;
77  }
78  }
79 };
80 
81 template <class T>
82 inline void swap (count_ptr<T> &a, count_ptr<T> &b)
83 {
84  a.swap(b);
85 }
86 
87 template <class T>
88 inline bool operator< (const count_ptr<T> &a, const count_ptr<T> &b)
89 {
90  return a.compare(b) < 0;
91 }
92 template <class T>
93 inline bool operator> (const count_ptr<T> &a, const count_ptr<T> &b)
94 {
95  return a.compare(b) > 0;
96 }
97 template <class T>
98 inline bool operator<= (const count_ptr<T> &a, const count_ptr<T> &b)
99 {
100  return a.compare(b) <= 0;
101 }
102 template <class T>
103 inline bool operator>= (const count_ptr<T> &a, const count_ptr<T> &b)
104 {
105  return a.compare(b) >= 0;
106 }
107 template <class T>
108 inline bool operator== (const count_ptr<T> &a, const count_ptr<T> &b)
109 {
110  return a.compare(b) == 0;
111 }
112 template <class T>
113 inline bool operator!= (const count_ptr<T> &a, const count_ptr<T> &b)
114 {
115  return !(a == b);
116 }
117 
118 #endif /* COUNT_PTR_H_ */
count_ptr< T > & operator=(count_ptr< T > temp)
Definition: count_ptr.h:30
int equiv(const count_ptr< T > &p) const
Definition: count_ptr.h:45
bool operator!=(const count_ptr< T > &a, const count_ptr< T > &b)
Definition: count_ptr.h:113
void swap(count_ptr< T > &a)
Definition: count_ptr.h:57
void swap(count_ptr< T > &a, count_ptr< T > &b)
Definition: count_ptr.h:82
void cancel()
Definition: count_ptr.h:71
~count_ptr()
Definition: count_ptr.h:25
long * m_count
Definition: count_ptr.h:14
bool operator>=(const count_ptr< T > &a, const count_ptr< T > &b)
Definition: count_ptr.h:103
long count() const
Definition: count_ptr.h:53
T * get_ptr() const
Definition: count_ptr.h:49
bool operator==(const count_ptr< T > &a, const count_ptr< T > &b)
Definition: count_ptr.h:108
T & operator*() const
Definition: count_ptr.h:37
bool operator>(const count_ptr< T > &a, const count_ptr< T > &b)
Definition: count_ptr.h:93
T * operator->() const
Definition: count_ptr.h:41
int compare(const count_ptr< T > &a) const
Definition: count_ptr.h:62
T * m_ptr
Definition: count_ptr.h:13
count_ptr(T *ptr=0)
Definition: count_ptr.h:17
count_ptr(const count_ptr< T > &p)
Definition: count_ptr.h:20