1 // Copyright 2019 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 #ifndef BASE_WIN_HSTRING_REFERENCE_H_ 6 #define BASE_WIN_HSTRING_REFERENCE_H_ 7 8 #include <hstring.h> 9 10 #include "base/base_export.h" 11 12 namespace base::win { 13 14 // HStringReference is an HSTRING representation of a null terminated 15 // string backed by memory that outlives the HStringReference instance. 16 // 17 // If you need an HSTRING class that manages its own memory, you should 18 // use ScopedHString instead. 19 // 20 // Example use: 21 // 22 // HStringReference string(L"abc"); 23 // 24 class BASE_EXPORT HStringReference { 25 public: 26 // Creates an HStringReference from `str`, which must be null terminated. 27 explicit HStringReference(const wchar_t* str); 28 Get()29 HSTRING Get() const { return hstring_; } 30 31 // HSTRING_HEADER is a structure that contains a pointer to the string 32 // passed into the constructor, along with its length. 33 34 // Since HSTRING is a pointer to HSTRING_HEADER, HStringReference 35 // cannot be copyable, moveable or assignable, as that would invalidate 36 // the HSTRING we're passing out to clients. 37 38 // In the future, we can consider implementing these methods by storing 39 // the string passed in the constructor and re-creating the HSTRING and 40 // HSTRING_HEADER datastructures. For now, we'll keep things simple and 41 // forbid these operations. 42 HStringReference(const HStringReference&) = delete; 43 HStringReference& operator=(const HStringReference&) = delete; 44 45 private: 46 HSTRING hstring_ = nullptr; 47 HSTRING_HEADER hstring_header_; 48 }; 49 50 } // namespace base::win 51 52 #endif // BASE_WIN_HSTRING_REFERENCE_H_ 53