• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2010 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #ifdef UNSAFE_BUFFERS_BUILD
6 // TODO(crbug.com/40284755): Remove this and spanify to fix the errors.
7 #pragma allow_unsafe_buffers
8 #endif
9 
10 #ifndef BASE_WIN_SCOPED_HGLOBAL_H_
11 #define BASE_WIN_SCOPED_HGLOBAL_H_
12 
13 #include <windows.h>
14 
15 #include <stddef.h>
16 
17 #include <utility>
18 
19 namespace base {
20 namespace win {
21 
22 // Like ScopedHandle except for HGLOBAL.
23 template <class Ptr>
24 class ScopedHGlobal {
25  public:
ScopedHGlobal(HGLOBAL glob)26   explicit ScopedHGlobal(HGLOBAL glob)
27       : glob_(glob), data_(static_cast<Ptr>(GlobalLock(glob_))) {}
28 
29   ScopedHGlobal(const ScopedHGlobal&) = delete;
30   ScopedHGlobal& operator=(const ScopedHGlobal&) = delete;
31 
~ScopedHGlobal()32   ~ScopedHGlobal() { GlobalUnlock(glob_); }
33 
data()34   Ptr data() { return data_; }
size()35   size_t size() const { return GlobalSize(glob_); }
36 
37   Ptr operator->() const {
38     assert(data_ != 0);
39     return data_;
40   }
41 
release()42   Ptr release() { return std::exchange(data_, nullptr); }
43 
begin()44   Ptr begin() { return data(); }
end()45   Ptr end() { return data() + size(); }
46 
47  private:
48   HGLOBAL glob_;
49 
50   Ptr data_;
51 };
52 
53 }  // namespace win
54 }  // namespace base
55 
56 #endif  // BASE_WIN_SCOPED_HGLOBAL_H_
57