netwerk/base/src/AutoClose.h

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

michael@0 1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
michael@0 2 /* vim: set ts=2 et sw=2 tw=80: */
michael@0 3 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 4 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
michael@0 5 * You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 6
michael@0 7 #ifndef mozilla_net_AutoClose_h
michael@0 8 #define mozilla_net_AutoClose_h
michael@0 9
michael@0 10 #include "nsCOMPtr.h"
michael@0 11
michael@0 12 namespace mozilla { namespace net {
michael@0 13
michael@0 14 // Like an nsAutoPtr for XPCOM streams (e.g. nsIAsyncInputStream) and other
michael@0 15 // refcounted classes that need to have the Close() method called explicitly
michael@0 16 // before they are destroyed.
michael@0 17 template <typename T>
michael@0 18 class AutoClose
michael@0 19 {
michael@0 20 public:
michael@0 21 AutoClose() { }
michael@0 22 ~AutoClose(){
michael@0 23 Close();
michael@0 24 }
michael@0 25
michael@0 26 operator bool() const
michael@0 27 {
michael@0 28 return mPtr;
michael@0 29 }
michael@0 30
michael@0 31 already_AddRefed<T> forget()
michael@0 32 {
michael@0 33 return mPtr.forget();
michael@0 34 }
michael@0 35
michael@0 36 void takeOver(nsCOMPtr<T> & rhs)
michael@0 37 {
michael@0 38 Close();
michael@0 39 mPtr = rhs.forget();
michael@0 40 }
michael@0 41
michael@0 42 void takeOver(AutoClose<T> & rhs)
michael@0 43 {
michael@0 44 Close();
michael@0 45 mPtr = rhs.mPtr.forget();
michael@0 46 }
michael@0 47
michael@0 48 void CloseAndRelease()
michael@0 49 {
michael@0 50 Close();
michael@0 51 mPtr = nullptr;
michael@0 52 }
michael@0 53
michael@0 54 T* operator->() const
michael@0 55 {
michael@0 56 return mPtr.operator->();
michael@0 57 }
michael@0 58
michael@0 59 private:
michael@0 60 void Close()
michael@0 61 {
michael@0 62 if (mPtr) {
michael@0 63 mPtr->Close();
michael@0 64 }
michael@0 65 }
michael@0 66
michael@0 67 void operator=(const AutoClose<T> &) MOZ_DELETE;
michael@0 68 AutoClose(const AutoClose<T> &) MOZ_DELETE;
michael@0 69
michael@0 70 nsCOMPtr<T> mPtr;
michael@0 71 };
michael@0 72
michael@0 73 } } // namespace mozilla::net
michael@0 74
michael@0 75 #endif // mozilla_net_AutoClose_h

mercurial