cloudy trunk
Loading...
Searching...
No Matches
count_ptr.h
Go to the documentation of this file.
1/* This file is part of Cloudy and is copyright (C)1978-2013 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
10template <class T>
11class count_ptr {
12private:
14 long *m_count;
15public:
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 }
70private:
71 void cancel ()
72 {
73 if (0 == --*m_count)
74 {
75 delete m_count;
76 delete m_ptr;
77 }
78 }
79};
80
81template <class T>
82inline void swap (count_ptr<T> &a, count_ptr<T> &b)
83{
84 a.swap(b);
85}
86
87template <class T>
88inline bool operator< (const count_ptr<T> &a, const count_ptr<T> &b)
89{
90 return a.compare(b) < 0;
91}
92template <class T>
93inline bool operator> (const count_ptr<T> &a, const count_ptr<T> &b)
94{
95 return a.compare(b) > 0;
96}
97template <class T>
98inline bool operator<= (const count_ptr<T> &a, const count_ptr<T> &b)
99{
100 return a.compare(b) <= 0;
101}
102template <class T>
103inline bool operator>= (const count_ptr<T> &a, const count_ptr<T> &b)
104{
105 return a.compare(b) >= 0;
106}
107template <class T>
108inline bool operator== (const count_ptr<T> &a, const count_ptr<T> &b)
109{
110 return a.compare(b) == 0;
111}
112template <class T>
113inline bool operator!= (const count_ptr<T> &a, const count_ptr<T> &b)
114{
115 return !(a == b);
116}
117
118#endif /* COUNT_PTR_H_ */
int equiv(const count_ptr< T > &p) const
Definition count_ptr.h:45
T * m_ptr
Definition count_ptr.h:13
T & operator*() const
Definition count_ptr.h:37
count_ptr< T > & operator=(count_ptr< T > temp)
Definition count_ptr.h:30
void swap(count_ptr< T > &a)
Definition count_ptr.h:57
count_ptr(T *ptr=0)
Definition count_ptr.h:17
long * m_count
Definition count_ptr.h:14
T * get_ptr() const
Definition count_ptr.h:49
long count() const
Definition count_ptr.h:53
void cancel()
Definition count_ptr.h:71
int compare(const count_ptr< T > &a) const
Definition count_ptr.h:62
T * operator->() const
Definition count_ptr.h:41
count_ptr(const count_ptr< T > &p)
Definition count_ptr.h:20
bool operator<(const count_ptr< T > &a, const count_ptr< T > &b)
Definition count_ptr.h:88
bool operator>=(const count_ptr< T > &a, const count_ptr< T > &b)
Definition count_ptr.h:103
bool operator==(const count_ptr< T > &a, const count_ptr< T > &b)
Definition count_ptr.h:108
void swap(count_ptr< T > &a, count_ptr< T > &b)
Definition count_ptr.h:82
bool operator!=(const count_ptr< T > &a, const count_ptr< T > &b)
Definition count_ptr.h:113
bool operator>(const count_ptr< T > &a, const count_ptr< T > &b)
Definition count_ptr.h:93
bool operator<=(const count_ptr< T > &a, const count_ptr< T > &b)
Definition count_ptr.h:98