Commit ecced0ce authored by Max Kellermann's avatar Max Kellermann

apple/StringRef: new library wrapping CFStringRef

parent 6e1a21a4
/*
* Copyright 2020 Max Kellermann <max.kellermann@gmail.com>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* - Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* - Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the
* distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef APPLE_STRING_REF_HXX
#define APPLE_STRING_REF_HXX
#include <CoreFoundation/CFString.h>
#include <utility>
namespace Apple {
class StringRef {
CFStringRef ref = nullptr;
public:
explicit StringRef(CFStringRef _ref) noexcept
:ref(_ref) {}
StringRef(StringRef &&src) noexcept
:ref(std::exchange(src.ref, nullptr)) {}
~StringRef() noexcept {
if (ref)
CFRelease(ref);
}
StringRef &operator=(StringRef &&src) noexcept {
using std::swap;
swap(ref, src.ref);
return *this;
}
operator bool() const noexcept {
return ref != nullptr;
}
bool GetCString(char *buffer, std::size_t size,
CFStringEncoding encoding=kCFStringEncodingUTF8) const noexcept
{
return CFStringGetCString(ref, buffer, size, encoding);
}
};
} // namespace Apple
#endif
......@@ -17,11 +17,11 @@
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include "apple/StringRef.hxx"
#include "config.h"
#include "OSXOutputPlugin.hxx"
#include "../OutputAPI.hxx"
#include "mixer/MixerList.hxx"
#include "util/ScopeExit.hxx"
#include "util/RuntimeError.hxx"
#include "util/Domain.hxx"
#include "util/Manual.hxx"
......@@ -112,15 +112,13 @@ static void
osx_os_status_to_cstring(OSStatus status, char *str, size_t size)
{
CFErrorRef cferr = CFErrorCreate(nullptr, kCFErrorDomainOSStatus, status, nullptr);
CFStringRef cfstr = CFErrorCopyDescription(cferr);
if (!CFStringGetCString(cfstr, str, size, kCFStringEncodingUTF8)) {
const Apple::StringRef cfstr(CFErrorCopyDescription(cferr));
if (!cfstr.GetCString(str, size)) {
/* conversion failed, return empty string */
*str = '\0';
}
if (cferr)
CFRelease(cferr);
if (cfstr)
CFRelease(cfstr);
}
static bool
......@@ -629,12 +627,6 @@ osx_output_set_device(OSXOutput *oo)
{
OSStatus status;
UInt32 size, numdevices;
CFStringRef cfname = nullptr;
AtScopeExit(&cfname) {
if (cfname)
CFRelease(cfname);
};
if (oo->component_subtype != kAudioUnitSubType_HALOutput)
return;
......@@ -678,6 +670,7 @@ osx_output_set_device(OSXOutput *oo)
unsigned i;
size = sizeof(CFStringRef);
for (i = 0; i < numdevices; i++) {
CFStringRef cfname = nullptr;
status = AudioObjectGetPropertyData(deviceids[i], &aopa_name,
0, nullptr,
&size, &cfname);
......@@ -690,9 +683,10 @@ osx_output_set_device(OSXOutput *oo)
errormsg);
}
const Apple::StringRef cfname_(cfname);
char name[256];
if (!CFStringGetCString(cfname, name, sizeof(name),
kCFStringEncodingUTF8))
if (!cfname_.GetCString(name, sizeof(name)))
throw std::runtime_error("Unable to convert device name from CFStringRef to char*");
if (StringIsEqual(oo->device_name, name)) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment