apple/StringRef: new library wrapping CFStringRef
This commit is contained in:
		
							
								
								
									
										73
									
								
								src/apple/StringRef.hxx
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										73
									
								
								src/apple/StringRef.hxx
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,73 @@ | ||||
| /* | ||||
|  * 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" | ||||
| @@ -111,15 +111,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 (strcmp(oo->device_name, name) == 0) { | ||||
|   | ||||
		Reference in New Issue
	
	Block a user
	 Max Kellermann
					Max Kellermann