Bug 785860 - fix sts preload list tests to skip private mode tests if private browsin...
[gecko.git] / netwerk / base / src / AutoClose.h
blob28d6fe11d1a4bc51b6778e92126f7075055ac54e
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
5 * You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #ifndef mozilla_net_AutoClose_h
8 #define mozilla_net_AutoClose_h
10 #include "nsCOMPtr.h"
12 namespace mozilla { namespace net {
14 // Like an nsAutoPtr for XPCOM streams (e.g. nsIAsyncInputStream) and other
15 // refcounted classes that need to have the Close() method called explicitly
16 // before they are destroyed.
17 template <typename T>
18 class AutoClose
20 public:
21 AutoClose() { }
22 ~AutoClose(){
23 Close();
26 operator bool() const
28 return mPtr;
31 already_AddRefed<T> forget()
33 return mPtr.forget();
36 void takeOver(nsCOMPtr<T> & rhs)
38 Close();
39 mPtr = rhs.forget();
42 void takeOver(AutoClose<T> & rhs)
44 Close();
45 mPtr = rhs.mPtr.forget();
48 void CloseAndRelease()
50 Close();
51 mPtr = nullptr;
54 T* operator->() const
56 return mPtr.operator->();
59 private:
60 void Close()
62 if (mPtr) {
63 mPtr->Close();
67 void operator=(const AutoClose<T> &) MOZ_DELETE;
68 AutoClose(const AutoClose<T> &) MOZ_DELETE;
70 nsCOMPtr<T> mPtr;
73 } } // namespace mozilla::net
75 #endif // mozilla_net_AutoClose_h