WvStreams
ptr.h
Go to the documentation of this file.
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2  *
3  * XPLC - Cross-Platform Lightweight Components
4  * Copyright (C) 2002-2004, Pierre Phaneuf
5  * Copyright (C) 2002-2004, Net Integration Technologies, Inc.
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public License
9  * as published by the Free Software Foundation; either version 2.1 of
10  * the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
20  * USA
21  *
22  * As a special exception, you may use this file as part of a free
23  * software library without restriction. Specifically, if other files
24  * instantiate templates or use macros or inline functions from this
25  * file, or you compile this file and link it with other files to
26  * produce an executable, this file does not by itself cause the
27  * resulting executable to be covered by the GNU Lesser General Public
28  * License. This exception does not however invalidate any other
29  * reasons why the executable file might be covered by the GNU Lesser
30  * General Public License.
31  */
32 
33 #ifndef __XPLC_PTR_H__
34 #define __XPLC_PTR_H__
35 
36 #if defined(__GNUC__) && __GNUC__ > 3
37 # pragma GCC system_header
38 #endif
39 
45 #include <xplc/IObject.h>
46 
47 #ifndef UNSTABLE
48 #error "xplc_ptr is experimental!"
49 #endif
50 
51 
55 template<class T>
56 class xplc_ptr {
57 private:
58  T* ptr;
59 
60  class ProtectedPtr: public T {
61  private:
62  virtual unsigned int addRef() = 0;
63  virtual unsigned int release() = 0;
64 #ifndef __XPLC_DELETE_H__
65  void operator delete(void*);
66 #endif
67  };
68 
69  xplc_ptr& operator=(const xplc_ptr&);
70 
71 public:
72  xplc_ptr():
73  ptr(0) {
74  }
80  explicit xplc_ptr(T* aObj):
81  ptr(aObj) {
82  }
87  template<class P>
88  explicit xplc_ptr(const xplc_ptr<P>& aObj):
89  ptr(aObj) {
90  if(ptr)
91  ptr->addRef();
92  }
93  ~xplc_ptr() {
94  if(ptr)
95  ptr->release();
96  }
104  ProtectedPtr* operator->() const {
105  return static_cast<ProtectedPtr*>(ptr);
106  }
112  operator ProtectedPtr*() const {
113  return static_cast<ProtectedPtr*>(ptr);
114  }
120  xplc_ptr& operator=(T* _ptr) {
121  if(_ptr)
122  _ptr->addRef();
123 
124  if(ptr)
125  ptr->release();
126 
127  ptr = _ptr;
128 
129  return *this;
130  }
131 };
132 
133 
138 template<class T>
139 T* do_addRef(T* obj) {
140  if (obj)
141  obj->addRef();
142 
143  return obj;
144 }
145 
146 
147 #endif /* __XPLC_PTR_H__ */
xplc_ptr::operator->
ProtectedPtr * operator->() const
Provide an operator->.
Definition: ptr.h:104
xplc_ptr::operator=
xplc_ptr & operator=(T *_ptr)
Assign a raw pointer to an xplc_ptr.
Definition: ptr.h:120
IObject.h
do_addRef
T * do_addRef(T *obj)
Used to addRef an object before passing it to something that would otherwise "steal" the reference.
Definition: ptr.h:139
xplc_ptr::xplc_ptr
xplc_ptr(const xplc_ptr< P > &aObj)
Construct an xplc_ptr from another xplc_ptr.
Definition: ptr.h:88
xplc_ptr
Smart pointer class for XPLC interfaces.
Definition: ptr.h:56
xplc_ptr::xplc_ptr
xplc_ptr(T *aObj)
Construct an xplc_ptr from a raw pointer.
Definition: ptr.h:80