cheshirekow  v0.1.0
RefPtr.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2012 Josh Bialkowski (jbialk@mit.edu)
3  *
4  * This file is part of cppfreetype.
5  *
6  * cppfreetype is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * cppfreetype is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with cppfreetype. If not, see <http://www.gnu.org/licenses/>.
18  */
27 #ifndef CPPFREETYPE_REFPTR_H_
28 #define CPPFREETYPE_REFPTR_H_
29 
31 #include <cpp_freetype/CPtr.h>
32 
33 
34 namespace freetype {
35 
38 template< class Traits >
39 class RefPtr
40 {
41  public:
42  typedef typename Traits::cobjptr cobjptr;
43  typedef typename Traits::Delegate Delegate;
44  typedef typename Traits::Storage Storage;
45 
46  private:
48 
50  void reference(){}
51 
53  void dereference(){}
54 
55 
56  public:
58 
66  explicit RefPtr( cobjptr ptr=0, bool doRef=false ):
67  m_ptr(ptr)
68  {
69  if(doRef)
70  reference();
71  }
72 
75  m_ptr(other.m_ptr)
76  {
77  reference();
78  }
79 
83  {
84  dereference();
85  }
86 
88  void unlink()
89  {
90  dereference();
91  m_ptr = 0;
92  }
93 
97  {
98  return m_ptr;
99  }
100 
101  const cobjptr subvert() const
102  {
103  return m_ptr;
104  }
105 
109  {
110  dereference();
111  m_ptr = other.m_ptr;
112  reference();
113  return *this;
114  }
115 
118  {
119  return Delegate(m_ptr);
120  }
121 
122  const Delegate operator->() const
123  {
124  return Delegate(m_ptr);
125  }
126 
128  {
129  return CPtr<Traits>(m_ptr);
130  }
131 
133  {
134  return ConstCPtr<Traits>(m_ptr);
135  }
136 
137  operator bool() const
138  {
139  return m_ptr;
140  }
141 
142  template <typename T2>
144  {
145  return LValuePair< RefPtr<Traits>,T2 >(*this,other);
146  }
147 
148 };
149 
150 
151 } // namespace cppfreetype
152 
153 
154 
155 
156 #endif // REFPTR_H_
Storage m_ptr
Definition: RefPtr.h:47
pointer to a reference counted object, auto destruct when reference count is zero ...
Definition: freetype.h:35
ConstCPtr< Traits > operator*() const
Definition: RefPtr.h:132
Traits::Delegate Delegate
Definition: RefPtr.h:43
RefPtr< Traits > & operator=(const RefPtr< Traits > &other)
assignment operator, decreases reference count of current object, increases reference count of copied...
Definition: RefPtr.h:108
const Delegate operator->() const
Definition: RefPtr.h:122
void reference()
increase reference count by one, see specializations
Definition: RefPtr.h:50
cobjptr subvert()
return the stored pointer, subverting reference safety, see specializations if Storage is not the sam...
Definition: RefPtr.h:96
RefPtr(cobjptr ptr=0, bool doRef=false)
create a RefPtr from the specified cobj
Definition: RefPtr.h:66
Traits::Storage Storage
Definition: RefPtr.h:44
LValuePair< RefPtr< Traits >, T2 > operator,(T2 &other)
Definition: RefPtr.h:143
CPtr< Traits > operator*()
Definition: RefPtr.h:127
void dereference()
decrease reference count by one, see specializations
Definition: RefPtr.h:53
allows an error to be returned with a result in a single expression
acts like a c-pointer by overloading the ->() operator, but is not copyable and doesn't allow the unde...
Definition: CPtr.h:38
acts like a const c-pointer by overloading the ->() operator, but is not copyable and doesn't allow th...
Definition: CPtr.h:75
void unlink()
dereference the stored object and turn this into a null pointer
Definition: RefPtr.h:88
RefPtr(const RefPtr< Traits > &other)
copy construct a pointer, increasing the reference count
Definition: RefPtr.h:74
BinaryKey other(const BinaryKey &key)
Definition: BinaryKey.h:44
Traits::cobjptr cobjptr
Definition: RefPtr.h:42
~RefPtr()
when the RefPtr is destroyed the reference count of the pointed-to object is decreased ...
Definition: RefPtr.h:82
const cobjptr subvert() const
Definition: RefPtr.h:101
Delegate operator->()
the member operator, exposes the underlying cobj pointer
Definition: RefPtr.h:117