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 cppfontconfig.
5  *
6  * cppfontconfig 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  * cppfontconfig 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 cppfontconfig. If not, see <http://www.gnu.org/licenses/>.
18  */
27 #ifndef CPPFONTCONFIG_REFPTR_H_
28 #define CPPFONTCONFIG_REFPTR_H_
29 
31 #include <cpp_fontconfig/CPtr.h>
32 #include <iostream>
33 
34 namespace fontconfig {
35 
36 
39 
45 template< class Traits >
46 class RefPtr
47 {
48  public:
49  typedef typename Traits::cobjptr cobjptr;
50  typedef typename Traits::Delegate Delegate;
51  typedef typename Traits::Storage Storage;
52 
53  private:
55 
57  void reference(){}
58 
60  void dereference(){}
61 
62 
63  public:
65 
72  RefPtr( cobjptr ptr=0, bool doRef=false ):
73  m_ptr(ptr)
74  {
75  if(doRef)
76  reference();
77  }
78 
81  m_ptr(other.m_ptr)
82  {
83  reference();
84  }
85 
89  {
90  dereference();
91  }
92 
94  void unlink()
95  {
96  dereference();
97  m_ptr = 0;
98  }
99 
103  {
104  return m_ptr;
105  }
106 
109  const cobjptr subvert() const
110  {
111  return m_ptr;
112  }
113 
117  {
118  dereference();
119  m_ptr = other.m_ptr;
120  reference();
121  return *this;
122  }
123 
127  {
128  dereference();
129  m_ptr = ptr;
130  reference();
131  return *this;
132  }
133 
137  {
138  return Delegate(m_ptr);
139  }
140 
143  const Delegate operator->() const
144  {
145  return Delegate(m_ptr);
146  }
147 
151  {
152  return CPtr<Traits>(m_ptr);
153  }
154 
158  {
159  return ConstCPtr<Traits>(m_ptr);
160  }
161 
163  operator bool() const
164  {
165  return m_ptr;
166  }
167 
169  template <typename T2>
171  {
172  return LValuePair< RefPtr<Traits>,T2 >(*this,other);
173  }
174 
175 };
176 
177 
178 } // namespace cppfontconfig
179 
180 
181 
182 
183 #endif // REFPTR_H_
RefPtr< Traits > & operator=(const RefPtr< Traits > &other)
assignment operator, decreases reference count of current object, increases reference count of copied...
Definition: RefPtr.h:116
Traits::Delegate Delegate
Definition: RefPtr.h:50
LValuePair< RefPtr< Traits >, T2 > operator,(T2 &other)
can be paired with other objects for multiple (tuple) returns
Definition: RefPtr.h:170
RefPtr(const RefPtr< Traits > &other)
copy construct a pointer, increasing the reference count
Definition: RefPtr.h:80
Traits::Storage Storage
Definition: RefPtr.h:51
void dereference()
decrease reference count by one, see specializations
Definition: RefPtr.h:60
acts like a c-pointer by overloading the ->() operator, but is not copyable and doesn't allow the unde...
Definition: CPtr.h:38
Storage m_ptr
Definition: RefPtr.h:54
const Delegate operator->() const
returns a delegate object which exposes member functions of the underlying object ...
Definition: RefPtr.h:143
Delegate operator->()
returns a delegate object which exposes member functions of the underlying object ...
Definition: RefPtr.h:136
void reference()
increase reference count by one, see specializations
Definition: RefPtr.h:57
CPtr< Traits > operator*()
returns a delegate object which acts exactly like a c-pointer but cannot be copied and so reference c...
Definition: RefPtr.h:150
const cobjptr subvert() const
return the stored pointer, subverting reference safety, see specializations if Storage is not the sam...
Definition: RefPtr.h:109
~RefPtr()
when the RefPtr is destroyed the reference count of the pointed-to object is decreased ...
Definition: RefPtr.h:88
cobjptr subvert()
return the stored pointer, subverting reference safety, see specializations if Storage is not the sam...
Definition: RefPtr.h:102
allows an error to be returned with a result in a single expression
RefPtr(cobjptr ptr=0, bool doRef=false)
create a RefPtr from the specified cobj
Definition: RefPtr.h:72
BinaryKey other(const BinaryKey &key)
Definition: BinaryKey.h:44
Traits::cobjptr cobjptr
Definition: RefPtr.h:49
ConstCPtr< Traits > operator*() const
returns a delegate object which acts exactly like a c-pointer but cannot be copied and so reference c...
Definition: RefPtr.h:157
RefPtr< Traits > & operator=(cobjptr ptr)
assignment operator, decreases reference count of current object, increases reference count of copied...
Definition: RefPtr.h:126
holds a set of names with associated value lists;
Definition: Pattern.h:131
acts like a const c-pointer by overloading the ->() operator, but is not copyable and doesn't allow th...
Definition: CPtr.h:75
object which acts like a c-pointer, but when dereferenced returns a delegate object which adds method...
Definition: CPtr.h:33
void unlink()
dereference the stored object and turn this into a null pointer
Definition: RefPtr.h:94