hstring_reference.h 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. // Copyright 2019 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_HSTRING_REFERENCE_H_
  5. #define BASE_WIN_HSTRING_REFERENCE_H_
  6. #include <hstring.h>
  7. #include "base/base_export.h"
  8. namespace base {
  9. namespace win {
  10. // HStringReference is an HSTRING representation of a null terminated
  11. // string backed by memory that outlives the HStringReference instance.
  12. //
  13. // If you need an HSTRING class that manages its own memory, you should
  14. // use ScopedHString instead.
  15. //
  16. // Note that HStringReference requires certain functions that are only
  17. // available on Windows 8 and later, and that these functions need to be
  18. // delayloaded to avoid breaking Chrome on Windows 7.
  19. //
  20. // Callers MUST check the return value of ResolveCoreWinRTStringDelayLoad()
  21. // *before* using HStringReference.
  22. //
  23. // One-time Initialization for HStringReference:
  24. //
  25. // const bool success = HStringReference::ResolveCoreWinRTStringDelayload();
  26. // if (success) {
  27. // // HStringReference can be used.
  28. // } else {
  29. // // Handle error.
  30. // }
  31. //
  32. // Example use:
  33. //
  34. // HStringReference string(L"abc");
  35. //
  36. class BASE_EXPORT HStringReference {
  37. public:
  38. // Loads all required HSTRING functions, available from Win8 and onwards.
  39. static bool ResolveCoreWinRTStringDelayload();
  40. HStringReference(const wchar_t* str, size_t len);
  41. explicit HStringReference(const wchar_t* str);
  42. HSTRING Get() const { return hstring_; }
  43. // HSTRING_HEADER is a structure that contains a pointer to the string
  44. // passed into the constructor, along with its length.
  45. // Since HSTRING is a pointer to HSTRING_HEADER, HStringReference
  46. // cannot be copyable, moveable or assignable, as that would invalidate
  47. // the HSTRING we're passing out to clients.
  48. // In the future, we can consider implementing these methods by storing
  49. // the string passed in the constructor and re-creating the HSTRING and
  50. // HSTRING_HEADER datastructures. For now, we'll keep things simple and
  51. // forbid these operations.
  52. HStringReference(const HStringReference&) = delete;
  53. HStringReference& operator=(const HStringReference&) = delete;
  54. private:
  55. HSTRING hstring_ = nullptr;
  56. HSTRING_HEADER hstring_header_;
  57. };
  58. } // namespace win
  59. } // namespace base
  60. #endif // BASE_WIN_HSTRING_REFERENCE_H_