scoped_glib.h (1990B)
1 /* 2 * Copyright 2022 The WebRTC project authors. All Rights Reserved. 3 * 4 * Use of this source code is governed by a BSD-style license 5 * that can be found in the LICENSE file in the root of the source 6 * tree. An additional intellectual property rights grant can be found 7 * in the file PATENTS. All contributing project authors may 8 * be found in the AUTHORS file in the root of the source tree. 9 */ 10 11 #ifndef MODULES_PORTAL_SCOPED_GLIB_H_ 12 #define MODULES_PORTAL_SCOPED_GLIB_H_ 13 14 #include <gio/gio.h> 15 16 #include "rtc_base/checks.h" 17 #include "rtc_base/system/rtc_export.h" 18 #include "rtc_base/system/rtc_export_template.h" 19 20 namespace webrtc { 21 22 template <class T> 23 class Scoped { 24 public: 25 Scoped() {} 26 explicit Scoped(T* val) { ptr_ = val; } 27 ~Scoped() { RTC_DCHECK_NOTREACHED(); } 28 29 T* operator->() const { return ptr_; } 30 31 explicit operator bool() const { return ptr_ != nullptr; } 32 33 bool operator!() const { return ptr_ == nullptr; } 34 35 T* get() const { return ptr_; } 36 37 T** receive() { 38 RTC_CHECK(!ptr_); 39 return &ptr_; 40 } 41 42 Scoped& operator=(T* val) { 43 RTC_DCHECK(val); 44 ptr_ = val; 45 return *this; 46 } 47 48 protected: 49 T* ptr_ = nullptr; 50 }; 51 52 template <> 53 Scoped<GError>::~Scoped(); 54 template <> 55 Scoped<char>::~Scoped(); 56 template <> 57 Scoped<GVariant>::~Scoped(); 58 template <> 59 Scoped<GVariantIter>::~Scoped(); 60 template <> 61 Scoped<GDBusMessage>::~Scoped(); 62 template <> 63 Scoped<GUnixFDList>::~Scoped(); 64 65 extern template class RTC_EXPORT_TEMPLATE_DECLARE(RTC_EXPORT) Scoped<GError>; 66 extern template class RTC_EXPORT_TEMPLATE_DECLARE(RTC_EXPORT) Scoped<char>; 67 extern template class RTC_EXPORT_TEMPLATE_DECLARE(RTC_EXPORT) Scoped<GVariant>; 68 extern template class RTC_EXPORT_TEMPLATE_DECLARE( 69 RTC_EXPORT) Scoped<GVariantIter>; 70 extern template class RTC_EXPORT_TEMPLATE_DECLARE( 71 RTC_EXPORT) Scoped<GDBusMessage>; 72 extern template class RTC_EXPORT_TEMPLATE_DECLARE( 73 RTC_EXPORT) Scoped<GUnixFDList>; 74 75 } // namespace webrtc 76 77 #endif // MODULES_PORTAL_SCOPED_GLIB_H_