scoped_bstr.h 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. // Copyright (c) 2011 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_BSTR_H_
  5. #define BASE_WIN_SCOPED_BSTR_H_
  6. #include <windows.h>
  7. #include <oleauto.h>
  8. #include <stddef.h>
  9. #include "base/base_export.h"
  10. #include "base/check.h"
  11. #include "base/macros.h"
  12. #include "base/strings/string_piece.h"
  13. namespace base {
  14. namespace win {
  15. // Manages a BSTR string pointer.
  16. // The class interface is based on unique_ptr.
  17. class BASE_EXPORT ScopedBstr {
  18. public:
  19. ScopedBstr() = default;
  20. // Constructor to create a new BSTR.
  21. //
  22. // NOTE: Do not pass a BSTR to this constructor expecting ownership to
  23. // be transferred - even though it compiles! ;-)
  24. explicit ScopedBstr(WStringPiece non_bstr);
  25. ~ScopedBstr();
  26. BSTR Get() const { return bstr_; }
  27. // Give ScopedBstr ownership over an already allocated BSTR or null.
  28. // If you need to allocate a new BSTR instance, use |allocate| instead.
  29. void Reset(BSTR bstr = nullptr);
  30. // Releases ownership of the BSTR to the caller.
  31. BSTR Release();
  32. // Creates a new BSTR from a 16-bit C-style string.
  33. //
  34. // If you already have a BSTR and want to transfer ownership to the
  35. // ScopedBstr instance, call |reset| instead.
  36. //
  37. // Returns a pointer to the new BSTR.
  38. BSTR Allocate(WStringPiece str);
  39. // Allocates a new BSTR with the specified number of bytes.
  40. // Returns a pointer to the new BSTR.
  41. BSTR AllocateBytes(size_t bytes);
  42. // Sets the allocated length field of the already-allocated BSTR to be
  43. // |bytes|. This is useful when the BSTR was preallocated with e.g.
  44. // SysAllocStringLen or SysAllocStringByteLen (call |AllocateBytes|) and then
  45. // not all the bytes are being used.
  46. //
  47. // Note that if you want to set the length to a specific number of
  48. // characters, you need to multiply by sizeof(wchar_t). Oddly, there's no
  49. // public API to set the length, so we do this ourselves by hand.
  50. //
  51. // NOTE: The actual allocated size of the BSTR MUST be >= bytes. That
  52. // responsibility is with the caller.
  53. void SetByteLen(size_t bytes);
  54. // Swap values of two ScopedBstr's.
  55. void Swap(ScopedBstr& bstr2);
  56. // Retrieves the pointer address.
  57. // Used to receive BSTRs as out arguments (and take ownership).
  58. // The function DCHECKs on the current value being null.
  59. // Usage: GetBstr(bstr.Receive());
  60. BSTR* Receive();
  61. // Returns number of chars in the BSTR.
  62. size_t Length() const;
  63. // Returns the number of bytes allocated for the BSTR.
  64. size_t ByteLength() const;
  65. // Forbid comparison of ScopedBstr types. You should never have the same
  66. // BSTR owned by two different scoped_ptrs.
  67. bool operator==(const ScopedBstr& bstr2) const = delete;
  68. bool operator!=(const ScopedBstr& bstr2) const = delete;
  69. protected:
  70. BSTR bstr_ = nullptr;
  71. private:
  72. DISALLOW_COPY_AND_ASSIGN(ScopedBstr);
  73. };
  74. } // namespace win
  75. } // namespace base
  76. #endif // BASE_WIN_SCOPED_BSTR_H_