diff options
author | Shane Kearns <shane.kearns@accenture.com> | 2010-10-01 13:14:01 (GMT) |
---|---|---|
committer | Shane Kearns <shane.kearns@accenture.com> | 2011-06-27 14:44:20 (GMT) |
commit | 4846920c4c42615ecca40b5d2daed276e1de3805 (patch) | |
tree | 0d2e34456ebefc4388c9efad2404aad0a7124e56 /src/corelib | |
parent | b2e678bfd4f190ab27cb380201207d8f227f0739 (diff) | |
download | Qt-4846920c4c42615ecca40b5d2daed276e1de3805.zip Qt-4846920c4c42615ecca40b5d2daed276e1de3805.tar.gz Qt-4846920c4c42615ecca40b5d2daed276e1de3805.tar.bz2 |
Create QScopedResource class
This has similar purpose to QScopedPointer, and is implemented using it.
Symbian has many resource classes, e.g. RFile, RSocket, which behave as
value types, but require the Close() member function to be called to release
the server side resource owned by the handle.
This class can be used to give exception safety, for example when trying to
add a resource class to a container std::bad_alloc could be thrown.
By assigning the resource to QScopedResource when it is opened, and calling
take() after it is added to the container, it is ensured that the resource
will be closed if an exception is thrown.
Equivalent to the symbian pattern:
CleanupClosePushL(file);
container.AppendL(file); //transfers ownership, may leave
CleanupStack::Pop(file);
With STL exceptions we can do:
QScopedResource ptr(file);
container.append(file); //transfers ownership, may throw
ptr.take;
Reviewed-By: mread
Diffstat (limited to 'src/corelib')
-rw-r--r-- | src/corelib/kernel/qcore_symbian_p.h | 24 |
1 files changed, 24 insertions, 0 deletions
diff --git a/src/corelib/kernel/qcore_symbian_p.h b/src/corelib/kernel/qcore_symbian_p.h index 3019e05..a8f576d 100644 --- a/src/corelib/kernel/qcore_symbian_p.h +++ b/src/corelib/kernel/qcore_symbian_p.h @@ -59,6 +59,7 @@ #include <qstring.h> #include <qrect.h> #include <qhash.h> +#include <qscopedpointer.h> #include <f32file.h> #include <es_sock.h> @@ -262,6 +263,29 @@ private: RConnection *iDefaultConnection; }; +template <typename T> class QScopedPointerResourceCloser +{ +public: + static inline void cleanup(T* pointer) + { + if (pointer) + pointer->Close(); + } +}; + +/*typical use: + RFile file; + file.Open(...); + QScopedResource<RFile> ptr(file); + container.append(file); //this may throw std::bad_alloc, in which case file.Close() is called by destructor + ptr.take(); //if we reach this line, ownership is transferred to the container + */ +template <typename T> class QScopedResource : public QScopedPointer<T, QScopedPointerResourceCloser<T> > +{ +public: + inline QScopedResource(T& resource) : QScopedPointer<T, QScopedPointerResourceCloser<T> >(&resource) {} +}; + QT_END_NAMESPACE QT_END_HEADER |