~ubuntu-branches/ubuntu/precise/koffice/precise

« back to all changes in this revision

Viewing changes to filters/kword/msword-odf/wv2/src/sharedptr.h

  • Committer: Bazaar Package Importer
  • Author(s): Jonathan Riddell
  • Date: 2010-09-21 15:36:35 UTC
  • mfrom: (1.4.1 upstream) (60.2.11 maverick)
  • Revision ID: james.westby@ubuntu.com-20100921153635-6tejqkiro2u21ydi
Tags: 1:2.2.2-0ubuntu3
Add kubuntu_03_fix-crash-on-closing-sqlite-connection-2.2.2.diff and
kubuntu_04_support-large-memo-values-for-msaccess-2.2.2.diff as
recommended by upstream http://kexi-
project.org/wiki/wikiview/index.php@Kexi2.2_Patches.html#sqlite_stab
ility

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* This file is part of the KDE libraries
 
2
   Copyright (c) 1999 Waldo Bastian <bastian@kde.org>
 
3
   Copyright (c) 2001-2003 Werner Trobin <trobin@kde.org>
 
4
 
 
5
   This library is free software; you can redistribute it and/or
 
6
   modify it under the terms of the GNU Library General Public
 
7
   License version 2 as published by the Free Software Foundation.
 
8
 
 
9
   This library is distributed in the hope that it will be useful,
 
10
   but WITHOUT ANY WARRANTY; without even the implied warranty of
 
11
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
12
   Library General Public License for more details.
 
13
 
 
14
   You should have received a copy of the GNU Library General Public License
 
15
   along with this library; see the file COPYING.LIB.  If not, write to
 
16
   the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 
17
   Boston, MA 02111-1307, USA.
 
18
*/
 
19
#ifndef SharedPTR_H
 
20
#define SharedPTR_H
 
21
 
 
22
#include "wv2_export.h"
 
23
 
 
24
namespace wvWare {
 
25
 
 
26
/**
 
27
 * Reference counting for shared objects.  If you derive your object
 
28
 * from this class, then you may use it in conjunction with
 
29
 * @ref SharedPtr to control the lifetime of your object.
 
30
 *
 
31
 * Specifically, all classes that derive from Shared have an internal
 
32
 * counter keeping track of how many other objects have a reference to
 
33
 * their object.  If used with @ref SharedPtr, then your object will
 
34
 * not be deleted until all references to the object have been
 
35
 * released.
 
36
 *
 
37
 * You should probably not ever use any of the methods in this class
 
38
 * directly -- let the @ref SharedPtr take care of that.  Just derive
 
39
 * your class from Shared and forget about it.
 
40
 *
 
41
 * @author Waldo Bastian <bastian@kde.org>
 
42
 */
 
43
class WV2_EXPORT Shared {
 
44
public:
 
45
   /**
 
46
    * Standard constructor.  This will initialize the reference count
 
47
    * on this object to 0
 
48
    */
 
49
   Shared() : count( 0 ) { }
 
50
 
 
51
   /**
 
52
    * Copy constructor.  This will @em not actually copy the objects
 
53
    * but it will initialize the reference count on this object to 0
 
54
    */
 
55
   Shared( const Shared & ) : count( 0 ) { }
 
56
 
 
57
   /**
 
58
    * Overloaded assignment operator
 
59
    */
 
60
   Shared &operator=( const Shared & ) { return *this; }
 
61
 
 
62
   /**
 
63
    * Increases the reference count by one
 
64
    */
 
65
   void _Shared_ref() const { count++; }
 
66
 
 
67
   /**
 
68
    * Releases a reference (decreases the reference count by one).  If
 
69
    * the count goes to 0, this object will delete itself
 
70
    */
 
71
   void _Shared_deref() const { if (!--count) delete this; }
 
72
 
 
73
   /**
 
74
    * Return the current number of references held
 
75
    *
 
76
    * @return Number of references
 
77
    */
 
78
   int _Shared_count() const { return count; }
 
79
 
 
80
protected:
 
81
   virtual ~Shared() { }
 
82
private:
 
83
   mutable int count;
 
84
};
 
85
 
 
86
/**
 
87
 * Can be used to control the lifetime of an object that has derived
 
88
 * @ref Shared. As long a someone holds a SharedPtr on some Shared
 
89
 * object it won't become deleted but is deleted once its reference
 
90
 * count is 0.  This struct emulates C++ pointers perfectly. So just
 
91
 * use it like a simple C++ pointer.
 
92
 *
 
93
 * @author Waldo Bastian <bastian@kde.org>
 
94
 */
 
95
template< class T >
 
96
struct SharedPtr
 
97
{
 
98
public:
 
99
  SharedPtr()
 
100
    : ptr(0) { }
 
101
  SharedPtr( T* t )
 
102
    : ptr(t) { if ( ptr ) ptr->_Shared_ref(); }
 
103
  SharedPtr( const SharedPtr& p )
 
104
    : ptr(p.ptr) { if ( ptr ) ptr->_Shared_ref(); }
 
105
 
 
106
  ~SharedPtr() { if ( ptr ) ptr->_Shared_deref(); }
 
107
 
 
108
  SharedPtr<T>& operator= ( const SharedPtr<T>& p ) {
 
109
    if ( ptr == p.ptr ) return *this;
 
110
    if ( ptr ) ptr->_Shared_deref();
 
111
    ptr = p.ptr;
 
112
    if ( ptr ) ptr->_Shared_ref();
 
113
    return *this;
 
114
  }
 
115
  SharedPtr<T>& operator= ( T* p ) {
 
116
    if ( ptr == p ) return *this;
 
117
    if ( ptr ) ptr->_Shared_deref();
 
118
    ptr = p;
 
119
    if ( ptr ) ptr->_Shared_ref();
 
120
    return *this;
 
121
  }
 
122
  bool operator== ( const SharedPtr<T>& p ) const { return ( ptr == p.ptr ); }
 
123
  bool operator!= ( const SharedPtr<T>& p ) const { return ( ptr != p.ptr ); }
 
124
  bool operator== ( const T* p ) const { return ( ptr == p ); }
 
125
  bool operator!= ( const T* p ) const { return ( ptr != p ); }
 
126
  bool operator!() const { return ( ptr == 0 ); }
 
127
  operator T*() const { return ptr; }
 
128
 
 
129
  T* data() { return ptr; }
 
130
  const T* data() const { return ptr; }
 
131
 
 
132
  const T& operator*() const { return *ptr; }
 
133
  T& operator*() { return *ptr; }
 
134
  const T* operator->() const { return ptr; }
 
135
  T* operator->() { return ptr; }
 
136
 
 
137
  int count() const { return ptr->_Shared_count(); } // for debugging purposes
 
138
private:
 
139
  T* ptr;
 
140
};
 
141
 
 
142
} // namespace wvWare
 
143
 
 
144
#endif