author | zecke <zecke> | 2004-11-18 21:36:33 (UTC) |
---|---|---|
committer | zecke <zecke> | 2004-11-18 21:36:33 (UTC) |
commit | 3302eb30390e6053637929316670da3e8fbe279e (patch) (unidiff) | |
tree | 5d8eb530e081a95b2ee5152ef63575b6c966154c /libopie2 | |
parent | 7ac32658ba09d8456cc75e5cf80707caa616848b (diff) | |
download | opie-3302eb30390e6053637929316670da3e8fbe279e.zip opie-3302eb30390e6053637929316670da3e8fbe279e.tar.gz opie-3302eb30390e6053637929316670da3e8fbe279e.tar.bz2 |
-Move ORefCount to a non inline implementation in osmartpointer.cpp
-Add OSharedPointer as a smart pointer adapter to adapt any pointer
This is used by Opie PIM
Change pro file
-rw-r--r-- | libopie2/opiecore/opiecore.pro | 2 | ||||
-rw-r--r-- | libopie2/opiecore/osharedpointer.h | 136 | ||||
-rw-r--r-- | libopie2/opiecore/osmartpointer.cpp | 57 | ||||
-rw-r--r-- | libopie2/opiecore/osmartpointer.h | 33 |
4 files changed, 209 insertions, 19 deletions
diff --git a/libopie2/opiecore/opiecore.pro b/libopie2/opiecore/opiecore.pro index 8189a7c..e72576c 100644 --- a/libopie2/opiecore/opiecore.pro +++ b/libopie2/opiecore/opiecore.pro | |||
@@ -1,43 +1,45 @@ | |||
1 | TEMPLATE = lib | 1 | TEMPLATE = lib |
2 | CONFIG += qt warn_on | 2 | CONFIG += qt warn_on |
3 | DESTDIR = $(OPIEDIR)/lib | 3 | DESTDIR = $(OPIEDIR)/lib |
4 | HEADERS = oapplication.h \ | 4 | HEADERS = oapplication.h \ |
5 | oconfig.h \ | 5 | oconfig.h \ |
6 | odebug.h \ | 6 | odebug.h \ |
7 | oglobal.h \ | 7 | oglobal.h \ |
8 | oglobalsettings.h \ | 8 | oglobalsettings.h \ |
9 | okeyconfigmanager.h \ | 9 | okeyconfigmanager.h \ |
10 | okeyfilter.h \ | 10 | okeyfilter.h \ |
11 | opluginloader.h \ | 11 | opluginloader.h \ |
12 | oprocess.h \ | 12 | oprocess.h \ |
13 | oprocctrl.h \ | 13 | oprocctrl.h \ |
14 | osharedpointer.h \ | ||
14 | osmartpointer.h \ | 15 | osmartpointer.h \ |
15 | ostorageinfo.h \ | 16 | ostorageinfo.h \ |
16 | xmltree.h | 17 | xmltree.h |
17 | 18 | ||
18 | SOURCES = oapplication.cpp \ | 19 | SOURCES = oapplication.cpp \ |
19 | oconfig.cpp \ | 20 | oconfig.cpp \ |
20 | odebug.cpp \ | 21 | odebug.cpp \ |
21 | oglobal.cpp \ | 22 | oglobal.cpp \ |
22 | oglobalsettings.cpp \ | 23 | oglobalsettings.cpp \ |
23 | okeyconfigmanager.cpp \ | 24 | okeyconfigmanager.cpp \ |
24 | okeyfilter.cpp \ | 25 | okeyfilter.cpp \ |
25 | opluginloader.cpp \ | 26 | opluginloader.cpp \ |
26 | oprocess.cpp \ | 27 | oprocess.cpp \ |
27 | oprocctrl.cpp \ | 28 | oprocctrl.cpp \ |
29 | osmartpointer.cpp \ | ||
28 | ostorageinfo.cpp \ | 30 | ostorageinfo.cpp \ |
29 | xmltree.cpp | 31 | xmltree.cpp |
30 | 32 | ||
31 | 33 | ||
32 | # The following files are currently not compileable on mac ! | 34 | # The following files are currently not compileable on mac ! |
33 | # Therfore I removed them from the build .. (eilers) | 35 | # Therfore I removed them from the build .. (eilers) |
34 | 36 | ||
35 | CONFTEST = $$system( echo $CONFIG_TARGET_MACOSX ) | 37 | CONFTEST = $$system( echo $CONFIG_TARGET_MACOSX ) |
36 | !contains( CONFTEST, y ) { | 38 | !contains( CONFTEST, y ) { |
37 | HEADERS += ofilenotify.h | 39 | HEADERS += ofilenotify.h |
38 | SOURCES += ofilenotify.cpp | 40 | SOURCES += ofilenotify.cpp |
39 | } else { | 41 | } else { |
40 | message( "ofilenotify is not available in a mac build !" ) | 42 | message( "ofilenotify is not available in a mac build !" ) |
41 | } | 43 | } |
42 | 44 | ||
43 | include ( device/device.pro ) | 45 | include ( device/device.pro ) |
diff --git a/libopie2/opiecore/osharedpointer.h b/libopie2/opiecore/osharedpointer.h new file mode 100644 index 0000000..f19d836 --- a/dev/null +++ b/libopie2/opiecore/osharedpointer.h | |||
@@ -0,0 +1,136 @@ | |||
1 | // -*- Mode: C++; -*- | ||
2 | /* | ||
3 | This file is part of the Opie Project | ||
4 | Copyright (C) 2004 Rajko Albrecht <alwin@handhelds.org> | ||
5 | Copyright (C) 2004 Holger Hans Peter Freyther <freyther@handhelds.org> | ||
6 | Copyright (C) The Opie Team <opie-devel@handhelds.org> | ||
7 | =. | ||
8 | .=l. | ||
9 | .>+-= | ||
10 | _;:, .> :=|. This program is free software; you can | ||
11 | .> <`_, > . <= redistribute it and/or modify it under | ||
12 | :`=1 )Y*s>-.-- : the terms of the GNU Library General Public | ||
13 | .="- .-=="i, .._ License as published by the Free Software | ||
14 | - . .-<_> .<> Foundation; either version 2 of the License, | ||
15 | ._= =} : or (at your option) any later version. | ||
16 | .%`+i> _;_. | ||
17 | .i_,=:_. -<s. This program is distributed in the hope that | ||
18 | + . -:. = it will be useful, but WITHOUT ANY WARRANTY; | ||
19 | : .. .:, . . . without even the implied warranty of | ||
20 | =_ + =;=|` MERCHANTABILITY or FITNESS FOR A | ||
21 | _.=:. : :=>`: PARTICULAR PURPOSE. See the GNU | ||
22 | ..}^=.= = ; Library General Public License for more | ||
23 | ++= -. .` .: details. | ||
24 | : = ...= . :.=- | ||
25 | -. .:....=;==+<; You should have received a copy of the GNU | ||
26 | -_. . . )=. = Library General Public License along with | ||
27 | -- :-=` this library; see the file COPYING.LIB. | ||
28 | If not, write to the Free Software Foundation, | ||
29 | Inc., 59 Temple Place - Suite 330, | ||
30 | Boston, MA 02111-1307, USA. | ||
31 | */ | ||
32 | |||
33 | |||
34 | #ifndef O_SHARED_POINTER_H | ||
35 | #define O_SHARED_POINTER_H | ||
36 | |||
37 | #include <opie2/osmartpointer.h> | ||
38 | |||
39 | /*! | ||
40 | * \file osharedpointer.h | ||
41 | * \brief Adapter with Refcounting around any data type | ||
42 | * \author Rajko Albrecht and Holger Hans Peter Freyther | ||
43 | */ | ||
44 | |||
45 | namespace Opie { | ||
46 | namespace Core { | ||
47 | template<class T> | ||
48 | class OSharedPointerData : public ORefCount { | ||
49 | public: | ||
50 | OSharedPointerData( T* dt ) { | ||
51 | data = dt; | ||
52 | } | ||
53 | |||
54 | ~OSharedPointerData() { | ||
55 | delete data; | ||
56 | } | ||
57 | |||
58 | T *data; | ||
59 | }; | ||
60 | |||
61 | |||
62 | /** | ||
63 | * \brief A small Adapter around any Pointer | ||
64 | * | ||
65 | * | ||
66 | * | ||
67 | */ | ||
68 | template<class T> | ||
69 | class OSharedPointer { | ||
70 | typedef OSharedPointerData<T> Data; | ||
71 | Data* data; | ||
72 | |||
73 | void unref() { | ||
74 | if (data) { data->Decr(); if (!data->Shared()) delete data; } | ||
75 | data = 0; | ||
76 | } | ||
77 | |||
78 | public: | ||
79 | OSharedPointer () { data = 0;} | ||
80 | |||
81 | OSharedPointer( T* t ) { | ||
82 | data = new Data( t ); | ||
83 | data->Incr(); | ||
84 | } | ||
85 | |||
86 | OSharedPointer( const OSharedPointer<T>& p ) { | ||
87 | if ( data = p.data ) data->Incr(); | ||
88 | } | ||
89 | |||
90 | ~OSharedPointer() { | ||
91 | unref(); | ||
92 | } | ||
93 | |||
94 | OSharedPointer<T> &operator=( const OSharedPointer<T>& p ) { | ||
95 | // already same: nothing to do | ||
96 | if (data == p.data) return *this; | ||
97 | // decouple reference | ||
98 | unref(); | ||
99 | // establish new reference | ||
100 | if (data = p.data) data->Incr(); | ||
101 | return *this; | ||
102 | } | ||
103 | |||
104 | OSharedPointer<T> &operator=( T *p ) { | ||
105 | unref(); | ||
106 | data = new Data( p ); | ||
107 | data->Incr(); | ||
108 | |||
109 | return *this; | ||
110 | } | ||
111 | |||
112 | operator T* () const { return data->data; } | ||
113 | T& operator*() { return *data->data; } | ||
114 | const T& operator*()const { return data->data; } | ||
115 | |||
116 | //! deref with method call | ||
117 | T* operator-> () {return data->data; } | ||
118 | //! deref with const method call | ||
119 | const T* operator-> ()const {return data->data; } | ||
120 | |||
121 | |||
122 | //! supports "if (pointer)" | ||
123 | operator bool () const { return (data != 0 && data->data != 0); } | ||
124 | //! "if (pointer)" as non const | ||
125 | operator bool () { return ( data != 0 && data->data != NULL );} | ||
126 | |||
127 | //! support if (!pointer)" | ||
128 | bool operator! () const { return (data == 0 || data->data == 0); } | ||
129 | //! support if (!pointer)" as non const | ||
130 | bool operator! () { return (data == 0 || data->data == 0); } | ||
131 | }; | ||
132 | |||
133 | } | ||
134 | } | ||
135 | |||
136 | #endif | ||
diff --git a/libopie2/opiecore/osmartpointer.cpp b/libopie2/opiecore/osmartpointer.cpp new file mode 100644 index 0000000..480e03a --- a/dev/null +++ b/libopie2/opiecore/osmartpointer.cpp | |||
@@ -0,0 +1,57 @@ | |||
1 | // -*- Mode: C++; -*- | ||
2 | /* | ||
3 | This file is part of the Opie Project | ||
4 | Copyright (C) 2004 Rajko Albrecht <alwin@handhelds.org> | ||
5 | Copyright (C) The Opie Team <opie-devel@handhelds.org> | ||
6 | =. | ||
7 | .=l. | ||
8 | .>+-= | ||
9 | _;:, .> :=|. This program is free software; you can | ||
10 | .> <`_, > . <= redistribute it and/or modify it under | ||
11 | :`=1 )Y*s>-.-- : the terms of the GNU Library General Public | ||
12 | .="- .-=="i, .._ License as published by the Free Software | ||
13 | - . .-<_> .<> Foundation; either version 2 of the License, | ||
14 | ._= =} : or (at your option) any later version. | ||
15 | .%`+i> _;_. | ||
16 | .i_,=:_. -<s. This program is distributed in the hope that | ||
17 | + . -:. = it will be useful, but WITHOUT ANY WARRANTY; | ||
18 | : .. .:, . . . without even the implied warranty of | ||
19 | =_ + =;=|` MERCHANTABILITY or FITNESS FOR A | ||
20 | _.=:. : :=>`: PARTICULAR PURPOSE. See the GNU | ||
21 | ..}^=.= = ; Library General Public License for more | ||
22 | ++= -. .` .: details. | ||
23 | : = ...= . :.=- | ||
24 | -. .:....=;==+<; You should have received a copy of the GNU | ||
25 | -_. . . )=. = Library General Public License along with | ||
26 | -- :-=` this library; see the file COPYING.LIB. | ||
27 | If not, write to the Free Software Foundation, | ||
28 | Inc., 59 Temple Place - Suite 330, | ||
29 | Boston, MA 02111-1307, USA. | ||
30 | */ | ||
31 | |||
32 | #include "osmartpointer.h" | ||
33 | |||
34 | namespace Opie { | ||
35 | namespace Core { | ||
36 | |||
37 | ORefCount::ORefCount() | ||
38 | : m_RefCount(0) | ||
39 | {} | ||
40 | |||
41 | ORefCount::~ORefCount() | ||
42 | {} | ||
43 | |||
44 | void ORefCount::Incr() { | ||
45 | ++m_RefCount; | ||
46 | } | ||
47 | |||
48 | void ORefCount::Decr() { | ||
49 | --m_RefCount; | ||
50 | } | ||
51 | |||
52 | bool ORefCount::Shared() { | ||
53 | return (m_RefCount > 0); | ||
54 | } | ||
55 | |||
56 | } | ||
57 | } | ||
diff --git a/libopie2/opiecore/osmartpointer.h b/libopie2/opiecore/osmartpointer.h index e9cee0c..f2f6464 100644 --- a/libopie2/opiecore/osmartpointer.h +++ b/libopie2/opiecore/osmartpointer.h | |||
@@ -1,22 +1,22 @@ | |||
1 | // -*- Mode: C++; -*- | 1 | // -*- Mode: C++; -*- |
2 | /* | 2 | /* |
3 | This file is part of the Opie Project | 3 | This file is part of the Opie Project |
4 | Copyright (C) 2004 Rajko Albrecht <alwin@handhelds.org> | 4 | Copyright (C) 2004 Rajko Albrecht <alwin@handhelds.org> |
5 | Copyright (C) The Opie Team <opie-devel@handhelds.org> | 5 | Copyright (C) The Opie Team <opie-devel@handhelds.org> |
6 | =. | 6 | =. |
7 | .=l. | 7 | .=l. |
8 | .>+-= | 8 | .>+-= |
9 | _;:, .> :=|. This program is free software; you can | 9 | _;:, .> :=|. This program is free software; you can |
10 | .> <`_, > . <= redistribute it and/or modify it under | 10 | .> <`_, > . <= redistribute it and/or modify it under |
11 | :`=1 )Y*s>-.-- : the terms of the GNU Library General Public | 11 | :`=1 )Y*s>-.-- : the terms of the GNU Library General Public |
12 | .="- .-=="i, .._ License as published by the Free Software | 12 | .="- .-=="i, .._ License as published by the Free Software |
13 | - . .-<_> .<> Foundation; either version 2 of the License, | 13 | - . .-<_> .<> Foundation; either version 2 of the License, |
14 | ._= =} : or (at your option) any later version. | 14 | ._= =} : or (at your option) any later version. |
15 | .%`+i> _;_. | 15 | .%`+i> _;_. |
16 | .i_,=:_. -<s. This program is distributed in the hope that | 16 | .i_,=:_. -<s. This program is distributed in the hope that |
17 | + . -:. = it will be useful, but WITHOUT ANY WARRANTY; | 17 | + . -:. = it will be useful, but WITHOUT ANY WARRANTY; |
18 | : .. .:, . . . without even the implied warranty of | 18 | : .. .:, . . . without even the implied warranty of |
19 | =_ + =;=|` MERCHANTABILITY or FITNESS FOR A | 19 | =_ + =;=|` MERCHANTABILITY or FITNESS FOR A |
20 | _.=:. : :=>`: PARTICULAR PURPOSE. See the GNU | 20 | _.=:. : :=>`: PARTICULAR PURPOSE. See the GNU |
21 | ..}^=.= = ; Library General Public License for more | 21 | ..}^=.= = ; Library General Public License for more |
22 | ++= -. .` .: details. | 22 | ++= -. .` .: details. |
@@ -35,115 +35,110 @@ _;:, .> :=|. This program is free software; you can | |||
35 | #include <stddef.h> | 35 | #include <stddef.h> |
36 | 36 | ||
37 | /*! | 37 | /*! |
38 | * \file osmartpointer.h | 38 | * \file osmartpointer.h |
39 | * \brief smart pointer and reference counter | 39 | * \brief smart pointer and reference counter |
40 | * \author Rajko Albrecht | 40 | * \author Rajko Albrecht |
41 | * | 41 | * |
42 | */ | 42 | */ |
43 | 43 | ||
44 | namespace Opie { | 44 | namespace Opie { |
45 | namespace Core { | 45 | namespace Core { |
46 | 46 | ||
47 | //! simple reference counter class | 47 | //! simple reference counter class |
48 | class ORefCount { | 48 | class ORefCount { |
49 | protected: | 49 | protected: |
50 | //! reference count member | 50 | //! reference count member |
51 | long m_RefCount; | 51 | unsigned long m_RefCount; |
52 | public: | 52 | public: |
53 | //! first reference must be added after "new" via Pointer() | 53 | //! first reference must be added after "new" via Pointer() |
54 | ORefCount() : m_RefCount(0) | 54 | ORefCount(); |
55 | {} | 55 | virtual ~ORefCount(); |
56 | virtual ~ORefCount() {} | ||
57 | //! add a reference | 56 | //! add a reference |
58 | void Incr() { | 57 | void Incr(); |
59 | ++m_RefCount; | ||
60 | } | ||
61 | //! delete a reference | 58 | //! delete a reference |
62 | void Decr() { | 59 | void Decr(); |
63 | --m_RefCount; | ||
64 | } | ||
65 | //! is it referenced | 60 | //! is it referenced |
66 | bool Shared() { return (m_RefCount > 0); } | 61 | bool Shared(); |
67 | }; | 62 | }; |
68 | 63 | ||
69 | //! reference counting wrapper class | 64 | //! reference counting wrapper class |
70 | template<class T> class OSmartPointer { | 65 | template<class T> class OSmartPointer { |
71 | //! pointer to object | 66 | //! pointer to object |
72 | /*! | 67 | /*! |
73 | * this object must contain Incr(), Decr() and Shared() | 68 | * this object must contain Incr(), Decr() and Shared() |
74 | * methode as public members. The best way is, that it will be a child | 69 | * methode as public members. The best way is, that it will be a child |
75 | * class of RefCount | 70 | * class of RefCount |
76 | */ | 71 | */ |
77 | T *ptr; | 72 | T *ptr; |
78 | public: | 73 | public: |
79 | //! standart constructor | 74 | //! standart constructor |
80 | OSmartPointer() { ptr = NULL; } | 75 | OSmartPointer() { ptr = NULL; } |
81 | //! standart destructor | 76 | //! standart destructor |
82 | /*! | 77 | /*! |
83 | * release the reference, if it were the last reference, destroys | 78 | * release the reference, if it were the last reference, destroys |
84 | * ptr | 79 | * ptr |
85 | */ | 80 | */ |
86 | ~OSmartPointer() | 81 | ~OSmartPointer() |
87 | { | 82 | { |
88 | if (ptr){ | 83 | if (ptr){ |
89 | ptr->Decr(); | 84 | ptr->Decr(); |
90 | if (!ptr->Shared()) | 85 | if (!ptr->Shared()) |
91 | delete ptr; | 86 | delete ptr; |
92 | } | 87 | } |
93 | } | 88 | } |
94 | //! construction | 89 | //! construction |
95 | OSmartPointer(T* t) { if (ptr = t) ptr->Incr(); } | 90 | OSmartPointer(T* t) { if (ptr = t) ptr->Incr(); } |
96 | //! Pointer copy | 91 | //! Pointer copy |
97 | OSmartPointer(const OSmartPointer<T>& p) | 92 | OSmartPointer(const OSmartPointer<T>& p) |
98 | { if (ptr = p.ptr) ptr->Incr(); } | 93 | { if (ptr = p.ptr) ptr->Incr(); } |
99 | //! pointer copy by assignment | 94 | //! pointer copy by assignment |
100 | OSmartPointer<T>& operator= (const OSmartPointer<T>& p) | 95 | OSmartPointer<T>& operator= (const OSmartPointer<T>& p) |
101 | { | 96 | { |
102 | // already same: nothing to do | 97 | // already same: nothing to do |
103 | if (ptr == p.ptr) return *this; | 98 | if (ptr == p.ptr) return *this; |
104 | // decouple reference | 99 | // decouple reference |
105 | if (ptr) { ptr->Decr(); if (!ptr->Shared()) delete ptr; } | 100 | if (ptr) { ptr->Decr(); if (!ptr->Shared()) delete ptr; } |
106 | // establish new reference | 101 | // establish new reference |
107 | if (ptr = p.ptr) ptr->Incr(); | 102 | if (ptr = p.ptr) ptr->Incr(); |
108 | return *this; | 103 | return *this; |
109 | } | 104 | } |
110 | OSmartPointer<T>& operator= (T*p) | 105 | OSmartPointer<T>& operator= (T*p) |
111 | { | 106 | { |
112 | if (ptr==p)return *this; | 107 | if (ptr==p)return *this; |
113 | if (ptr) { | 108 | if (ptr) { |
114 | ptr->Decr(); | 109 | ptr->Decr(); |
115 | if (!ptr->Shared()) delete ptr; | 110 | if (!ptr->Shared()) delete ptr; |
116 | } | 111 | } |
117 | if (ptr=p) ptr->Incr(); | 112 | if (ptr=p) ptr->Incr(); |
118 | return *this; | 113 | return *this; |
119 | } | 114 | } |
120 | 115 | ||
121 | //! cast to conventional pointer | 116 | //! cast to conventional pointer |
122 | operator T* () const { return ptr; } | 117 | operator T* () const { return ptr; } |
123 | 118 | ||
124 | //! deref: fails for NULL pointer | 119 | //! deref: fails for NULL pointer |
125 | T& operator* () {return *ptr; } | 120 | T& operator* () {return *ptr; } |
126 | //! deref: fails for NULL pointer | 121 | //! deref: fails for NULL pointer |
127 | const T& operator* ()const {return *ptr; } | 122 | const T& operator* ()const {return *ptr; } |
128 | 123 | ||
129 | //! deref with method call | 124 | //! deref with method call |
130 | T* operator-> () {return ptr; } | 125 | T* operator-> () {return ptr; } |
131 | //! deref with const method call | 126 | //! deref with const method call |
132 | const T* operator-> ()const {return ptr; } | 127 | const T* operator-> ()const {return ptr; } |
133 | 128 | ||
134 | //! supports "if (pointer)" | 129 | //! supports "if (pointer)" |
135 | operator bool () const { return (ptr != NULL); } | 130 | operator bool () const { return (ptr != NULL); } |
136 | //! "if (pointer)" as non const | 131 | //! "if (pointer)" as non const |
137 | operator bool () { return ptr != NULL;} | 132 | operator bool () { return ptr != NULL;} |
138 | 133 | ||
139 | //! support if (!pointer)" | 134 | //! support if (!pointer)" |
140 | bool operator! () const { return (ptr == NULL); } | 135 | bool operator! () const { return (ptr == NULL); } |
141 | //! support if (!pointer)" as non const | 136 | //! support if (!pointer)" as non const |
142 | bool operator! () { return (ptr == NULL); } | 137 | bool operator! () { return (ptr == NULL); } |
143 | }; | 138 | }; |
144 | 139 | ||
145 | } | 140 | } |
146 | } | 141 | } |
147 | 142 | ||
148 | #endif | 143 | #endif |
149 | 144 | ||