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