scoped_propvariant.h 1.5 KB

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