scoped_propvariant.h 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  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. #include "base/macros.h"
  9. namespace base {
  10. namespace win {
  11. // A PROPVARIANT that is automatically initialized and cleared upon respective
  12. // construction and destruction of this class.
  13. class ScopedPropVariant {
  14. public:
  15. ScopedPropVariant() { PropVariantInit(&pv_); }
  16. ~ScopedPropVariant() { Reset(); }
  17. // Returns a pointer to the underlying PROPVARIANT for use as an out param in
  18. // a function call.
  19. PROPVARIANT* Receive() {
  20. DCHECK_EQ(pv_.vt, VT_EMPTY);
  21. return &pv_;
  22. }
  23. // Clears the instance to prepare it for re-use (e.g., via Receive).
  24. void Reset() {
  25. if (pv_.vt != VT_EMPTY) {
  26. HRESULT result = PropVariantClear(&pv_);
  27. DCHECK_EQ(result, S_OK);
  28. }
  29. }
  30. const PROPVARIANT& get() const { return pv_; }
  31. const PROPVARIANT* ptr() const { return &pv_; }
  32. private:
  33. PROPVARIANT pv_;
  34. // Comparison operators for ScopedPropVariant are not supported at this point.
  35. bool operator==(const ScopedPropVariant&) const;
  36. bool operator!=(const ScopedPropVariant&) const;
  37. DISALLOW_COPY_AND_ASSIGN(ScopedPropVariant);
  38. };
  39. } // namespace win
  40. } // namespace base
  41. #endif // BASE_WIN_SCOPED_PROPVARIANT_H_