-
Notifications
You must be signed in to change notification settings - Fork 286
Add make_bstr*(std::wstring_view) #634
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -132,6 +132,28 @@ inline PCWSTR str_raw_ptr(const std::wstring& str) | |||||
| return str.c_str(); | ||||||
| } | ||||||
|
|
||||||
| #if defined(__WIL_OLEAUTO_H_) | ||||||
| #if defined(_STRING_VIEW_) || defined(__cpp_lib_string_view) | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In the general case, this is a problematic check because
Suggested change
... which means you can just move this definition a few lines lower since this check is already present. |
||||||
| // Create wil::unique_bstr from std::wstring_view (regardless if not null terminated, or if it contains embedded nulls) | ||||||
| inline wil::unique_bstr make_bstr_nothrow(std::wstring_view source) noexcept | ||||||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For bonus points - there is also the
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It might. I didn't actually check and just assumed a specialization would be needed. |
||||||
| { | ||||||
| return wil::unique_bstr(::SysAllocStringLen(source.data(), static_cast<UINT>(source.size()))); | ||||||
| } | ||||||
| inline wil::unique_bstr make_bstr_failfast(std::wstring_view source) noexcept | ||||||
| { | ||||||
| return wil::unique_bstr(FAIL_FAST_IF_NULL_ALLOC(::SysAllocStringLen(source.data(), static_cast<UINT>(source.size())))); | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Consider implementing this in terms of the nothrow variant, rather than repeating it here. |
||||||
| } | ||||||
| #ifdef WIL_ENABLE_EXCEPTIONS | ||||||
| inline wil::unique_bstr make_bstr(std::wstring_view source) | ||||||
| { | ||||||
| wil::unique_bstr result(make_bstr_nothrow(source)); | ||||||
| THROW_IF_NULL_ALLOC(result); | ||||||
| return result; | ||||||
| } | ||||||
| #endif // WIL_ENABLE_EXCEPTIONS | ||||||
| #endif // defined(_STRING_VIEW_) || defined(__cpp_lib_string_view) | ||||||
| #endif // defined(__WIL_OLEAUTO_H_) | ||||||
|
|
||||||
| #if __cpp_lib_string_view >= 201606L | ||||||
|
|
||||||
| /** | ||||||
|
|
||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
stl.his not likeresource.hand cannot be included multiple times to pick up new definitions, so this will be fragile and subject to include order dependencies. That said, trying to put this inresource.hmay not be the best option either. In theory, there could be a_STRING_VIEW_check, however I'm not a fan of these since (1) this is an implementation detail and might change, (2) might break if someone uses something other than the MSVC STL, and (3) isn't compatible with modules. We could also add a conditional include such as:and then later
which eliminates the "is
string_viewincluded already" check (the purpose ofWIL_USE_STL), however given how widely usedresource.his used, the probability of unintentional side effects is high.Another option is to add a function to
resource.hthat accepts a template argument that is constrained onis_string_view_like, so you don't have to bother with proper guards.