xref: /aosp_15_r20/external/webrtc/modules/portal/scoped_glib.h (revision d9f758449e529ab9291ac668be2861e7a55c2422)
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 
18 namespace webrtc {
19 
20 template <class T>
21 class Scoped {
22  public:
Scoped()23   Scoped() {}
Scoped(T * val)24   explicit Scoped(T* val) { ptr_ = val; }
~Scoped()25   ~Scoped() { RTC_DCHECK_NOTREACHED(); }
26 
27   T* operator->() const { return ptr_; }
28 
29   explicit operator bool() const { return ptr_ != nullptr; }
30 
31   bool operator!() const { return ptr_ == nullptr; }
32 
get()33   T* get() const { return ptr_; }
34 
receive()35   T** receive() {
36     RTC_CHECK(!ptr_);
37     return &ptr_;
38   }
39 
40   Scoped& operator=(T* val) {
41     RTC_DCHECK(val);
42     ptr_ = val;
43     return *this;
44   }
45 
46  protected:
47   T* ptr_ = nullptr;
48 };
49 
50 template <>
51 Scoped<GError>::~Scoped();
52 template <>
53 Scoped<char>::~Scoped();
54 template <>
55 Scoped<GVariant>::~Scoped();
56 template <>
57 Scoped<GVariantIter>::~Scoped();
58 template <>
59 Scoped<GDBusMessage>::~Scoped();
60 template <>
61 Scoped<GUnixFDList>::~Scoped();
62 
63 }  // namespace webrtc
64 
65 #endif  // MODULES_PORTAL_SCOPED_GLIB_H_
66