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