ipc/chromium/src/base/scoped_bstr_win.h

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/ipc/chromium/src/base/scoped_bstr_win.h	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,143 @@
     1.4 +// Copyright (c) 2006-2008 The Chromium Authors. All rights reserved.
     1.5 +// Use of this source code is governed by a BSD-style license that can be
     1.6 +// found in the LICENSE file.
     1.7 +
     1.8 +#ifndef BASE_SCOPED_BSTR_WIN_H_
     1.9 +#define BASE_SCOPED_BSTR_WIN_H_
    1.10 +
    1.11 +#include "base/basictypes.h"  // needed to pick up OS_WIN
    1.12 +
    1.13 +#include "base/logging.h"
    1.14 +
    1.15 +#include <windows.h>
    1.16 +#include <oleauto.h>
    1.17 +
    1.18 +// Manages a BSTR string pointer.
    1.19 +// The class interface is based on scoped_ptr.
    1.20 +class ScopedBstr {
    1.21 + public:
    1.22 +  ScopedBstr() : bstr_(NULL) {
    1.23 +  }
    1.24 +
    1.25 +  // Constructor to create a new BSTR.
    1.26 +  // NOTE: Do not pass a BSTR to this constructor expecting ownership to
    1.27 +  // be transferred - even though it compiles! ;-)
    1.28 +  explicit ScopedBstr(const wchar_t* non_bstr);
    1.29 +  ~ScopedBstr();
    1.30 +
    1.31 +  // Give ScopedBstr ownership over an already allocated BSTR or NULL.
    1.32 +  // If you need to allocate a new BSTR instance, use |allocate| instead.
    1.33 +  void Reset(BSTR bstr = NULL);
    1.34 +
    1.35 +  // Releases ownership of the BSTR to the caller.
    1.36 +  BSTR Release();
    1.37 +
    1.38 +  // Creates a new BSTR from a wide string.
    1.39 +  // If you already have a BSTR and want to transfer ownership to the
    1.40 +  // ScopedBstr instance, call |reset| instead.
    1.41 +  // Returns a pointer to the new BSTR, or NULL if allocation failed.
    1.42 +  BSTR Allocate(const wchar_t* wide_str);
    1.43 +
    1.44 +  // Allocates a new BSTR with the specified number of bytes.
    1.45 +  // Returns a pointer to the new BSTR, or NULL if allocation failed.
    1.46 +  BSTR AllocateBytes(int bytes);
    1.47 +
    1.48 +  // Sets the allocated length field of the already-allocated BSTR to be
    1.49 +  // |bytes|.  This is useful when the BSTR was preallocated with e.g.
    1.50 +  // SysAllocStringLen or SysAllocStringByteLen (call |AllocateBytes|) and
    1.51 +  // then not all the bytes are being used.
    1.52 +  // Note that if you want to set the length to a specific number of characters,
    1.53 +  // you need to multiply by sizeof(wchar_t).  Oddly, there's no public API to
    1.54 +  // set the length, so we do this ourselves by hand.
    1.55 +  //
    1.56 +  // NOTE: The actual allocated size of the BSTR MUST be >= bytes.
    1.57 +  //  That responsibility is with the caller.
    1.58 +  void SetByteLen(uint32_t bytes);
    1.59 +
    1.60 +  // Swap values of two ScopedBstr's.
    1.61 +  void Swap(ScopedBstr& bstr2);
    1.62 +
    1.63 +  // Retrieves the pointer address.
    1.64 +  // Used to receive BSTRs as out arguments (and take ownership).
    1.65 +  // The function DCHECKs on the current value being NULL.
    1.66 +  // Usage: GetBstr(bstr.Receive());
    1.67 +  BSTR* Receive();
    1.68 +
    1.69 +  // Returns number of chars in the BSTR.
    1.70 +  uint32_t Length() const;
    1.71 +
    1.72 +  // Returns the number of bytes allocated for the BSTR.
    1.73 +  uint32_t ByteLength() const;
    1.74 +
    1.75 +  operator BSTR() const {
    1.76 +    return bstr_;
    1.77 +  }
    1.78 +
    1.79 + protected:
    1.80 +  BSTR bstr_;
    1.81 +
    1.82 + private:
    1.83 +  // Forbid comparison of ScopedBstr types.  You should never have the same
    1.84 +  // BSTR owned by two different scoped_ptrs.
    1.85 +  bool operator==(const ScopedBstr& bstr2) const;
    1.86 +  bool operator!=(const ScopedBstr& bstr2) const;
    1.87 +  DISALLOW_COPY_AND_ASSIGN(ScopedBstr);
    1.88 +};
    1.89 +
    1.90 +// Template class to generate a BSTR from a static wide string
    1.91 +// without touching the heap.  Use this class via the StackBstrVar and
    1.92 +// StackBstr macros.
    1.93 +template <uint32_t string_bytes>
    1.94 +class StackBstrT {
    1.95 + public:
    1.96 +  // Try to stay as const as we can in an attempt to avoid someone
    1.97 +  // using the class incorrectly (e.g. by supplying a variable instead
    1.98 +  // of a verbatim string.  We also have an assert in the constructor
    1.99 +  // as an extra runtime check since the const-ness only catches one case.
   1.100 +  explicit StackBstrT(const wchar_t* const str) {
   1.101 +    // The BSTR API uses UINT, but we prefer uint32_t.
   1.102 +    // Make sure we'll know about it if these types don't match.
   1.103 +    COMPILE_ASSERT(sizeof(uint32_t) == sizeof(UINT), UintToUint32);
   1.104 +    COMPILE_ASSERT(sizeof(wchar_t) == sizeof(OLECHAR), WcharToOlechar);
   1.105 +
   1.106 +    // You shouldn't pass string pointers to this constructor since
   1.107 +    // there's no way for the compiler to calculate the length of the
   1.108 +    // string (string_bytes will be equal to pointer size in those cases).
   1.109 +    DCHECK(lstrlenW(str) == (string_bytes / sizeof(bstr_.str_[0])) - 1) <<
   1.110 +        "not expecting a string pointer";
   1.111 +    memcpy(bstr_.str_, str, string_bytes);
   1.112 +    bstr_.len_ = string_bytes - sizeof(wchar_t);
   1.113 +  }
   1.114 +
   1.115 +  operator BSTR() {
   1.116 +    return bstr_.str_;
   1.117 +  }
   1.118 +
   1.119 + protected:
   1.120 +  struct BstrInternal {
   1.121 +    uint32_t len_;
   1.122 +    wchar_t str_[string_bytes / sizeof(wchar_t)];
   1.123 +  } bstr_;
   1.124 +};
   1.125 +
   1.126 +// Use this macro to generate an inline BSTR from a wide string.
   1.127 +// This is about 6 times faster than using the SysAllocXxx functions to
   1.128 +// allocate a BSTR and helps with keeping heap fragmentation down.
   1.129 +// Example:
   1.130 +//  DoBstrStuff(StackBstr(L"This is my BSTR"));
   1.131 +// Where DoBstrStuff is:
   1.132 +//  HRESULT DoBstrStuff(BSTR bstr) { ... }
   1.133 +#define StackBstr(str) \
   1.134 +  static_cast<BSTR>(StackBstrT<sizeof(str)>(str))
   1.135 +
   1.136 +// If you need a named BSTR variable that's based on a fixed string
   1.137 +// (e.g. if the BSTR is used inside a loop or more than one place),
   1.138 +// use StackBstrVar to declare a variable.
   1.139 +// Example:
   1.140 +//   StackBstrVar(L"my_property", myprop);
   1.141 +//   for (int i = 0; i < objects.length(); ++i)
   1.142 +//     ProcessValue(objects[i].GetProp(myprop));  // GetProp accepts BSTR
   1.143 +#define StackBstrVar(str, var) \
   1.144 +  StackBstrT<sizeof(str)> var(str)
   1.145 +
   1.146 +#endif  // BASE_SCOPED_BSTR_WIN_H_

mercurial