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 
5 #ifndef BASE_WIN_SCOPED_HGLOBAL_H_
6 #define BASE_WIN_SCOPED_HGLOBAL_H_
7 
8 #include <windows.h>
9 #include <stddef.h>
10 
11 #include "base/macros.h"
12 
13 namespace base {
14 namespace win {
15 
16 // Like ScopedHandle except for HGLOBAL.
17 template<class T>
18 class ScopedHGlobal {
19  public:
ScopedHGlobal(HGLOBAL glob)20   explicit ScopedHGlobal(HGLOBAL glob) : glob_(glob) {
21     data_ = static_cast<T>(GlobalLock(glob_));
22   }
~ScopedHGlobal()23   ~ScopedHGlobal() {
24     GlobalUnlock(glob_);
25   }
26 
get()27   T get() { return data_; }
28 
Size()29   size_t Size() const { return GlobalSize(glob_); }
30 
31   T operator->() const {
32     assert(data_ != 0);
33     return data_;
34   }
35 
release()36   T release() {
37     T data = data_;
38     data_ = NULL;
39     return data;
40   }
41 
42  private:
43   HGLOBAL glob_;
44 
45   T data_;
46 
47   DISALLOW_COPY_AND_ASSIGN(ScopedHGlobal);
48 };
49 
50 }  // namespace win
51 }  // namespace base
52 
53 #endif  // BASE_WIN_SCOPED_HGLOBAL_H_
54