scoped_hglobal.h 983 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. // Copyright (c) 2010 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_HGLOBAL_H_
  5. #define BASE_WIN_SCOPED_HGLOBAL_H_
  6. #include <windows.h>
  7. #include <stddef.h>
  8. #include "base/macros.h"
  9. namespace base {
  10. namespace win {
  11. // Like ScopedHandle except for HGLOBAL.
  12. template <class T>
  13. class ScopedHGlobal {
  14. public:
  15. explicit ScopedHGlobal(HGLOBAL glob) : glob_(glob) {
  16. data_ = static_cast<T>(GlobalLock(glob_));
  17. }
  18. ~ScopedHGlobal() { GlobalUnlock(glob_); }
  19. T get() { return data_; }
  20. size_t Size() const { return GlobalSize(glob_); }
  21. T operator->() const {
  22. assert(data_ != 0);
  23. return data_;
  24. }
  25. T release() {
  26. T data = data_;
  27. data_ = NULL;
  28. return data;
  29. }
  30. private:
  31. HGLOBAL glob_;
  32. T data_;
  33. DISALLOW_COPY_AND_ASSIGN(ScopedHGlobal);
  34. };
  35. } // namespace win
  36. } // namespace base
  37. #endif // BASE_WIN_SCOPED_HGLOBAL_H_