xref: /aosp_15_r20/external/cronet/base/win/scoped_propvariant.h (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1 // Copyright 2013 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #ifndef BASE_WIN_SCOPED_PROPVARIANT_H_
6 #define BASE_WIN_SCOPED_PROPVARIANT_H_
7 
8 #include <propidl.h>
9 
10 #include "base/check_op.h"
11 
12 namespace base {
13 namespace win {
14 
15 // A PROPVARIANT that is automatically initialized and cleared upon respective
16 // construction and destruction of this class.
17 class ScopedPropVariant {
18  public:
ScopedPropVariant()19   ScopedPropVariant() { PropVariantInit(&pv_); }
20 
21   ScopedPropVariant(const ScopedPropVariant&) = delete;
22   ScopedPropVariant& operator=(const ScopedPropVariant&) = delete;
23 
~ScopedPropVariant()24   ~ScopedPropVariant() { Reset(); }
25 
26   // Returns a pointer to the underlying PROPVARIANT for use as an out param in
27   // a function call.
Receive()28   PROPVARIANT* Receive() {
29     DCHECK_EQ(pv_.vt, VT_EMPTY);
30     return &pv_;
31   }
32 
33   // Clears the instance to prepare it for re-use (e.g., via Receive).
Reset()34   void Reset() {
35     if (pv_.vt != VT_EMPTY) {
36       HRESULT result = PropVariantClear(&pv_);
37       DCHECK_EQ(result, S_OK);
38     }
39   }
40 
get()41   const PROPVARIANT& get() const { return pv_; }
ptr()42   const PROPVARIANT* ptr() const { return &pv_; }
43 
44  private:
45   PROPVARIANT pv_;
46 
47   // Comparison operators for ScopedPropVariant are not supported at this point.
48   bool operator==(const ScopedPropVariant&) const;
49   bool operator!=(const ScopedPropVariant&) const;
50 };
51 
52 }  // namespace win
53 }  // namespace base
54 
55 #endif  // BASE_WIN_SCOPED_PROPVARIANT_H_
56