From 6919d3578769202957d1ba320ff458e959935e05 Mon Sep 17 00:00:00 2001 From: JP Cimalando Date: Fri, 10 Aug 2018 15:24:46 +0200 Subject: [PATCH] allow to obtain api names regardless of being compiled or not --- RtAudio.cpp | 14 ++++---------- RtAudio.h | 10 ++++------ rtaudio_c.cpp | 4 ++-- rtaudio_c.h | 4 ++-- tests/apinames.cpp | 48 +++++++++++++++++++++++----------------------- 5 files changed, 36 insertions(+), 44 deletions(-) diff --git a/RtAudio.cpp b/RtAudio.cpp index 4e75d68..1e480d4 100644 --- a/RtAudio.cpp +++ b/RtAudio.cpp @@ -166,22 +166,16 @@ void RtAudio :: getCompiledApi( std::vector &apis ) rtaudio_compiled_apis + rtaudio_num_compiled_apis); } -const std::string RtAudio :: getCompiledApiName( RtAudio::Api api ) +std::string RtAudio :: getApiName( RtAudio::Api api ) { - if (api < 0 || api > RtAudio::NUM_APIS - || (std::find(rtaudio_compiled_apis, - rtaudio_compiled_apis+rtaudio_num_compiled_apis, - api) == rtaudio_compiled_apis+rtaudio_num_compiled_apis)) + if (api < 0 || api >= RtAudio::NUM_APIS) return ""; return rtaudio_api_names[api][0]; } -const std::string RtAudio :: getCompiledApiDisplayName( RtAudio::Api api ) +std::string RtAudio :: getApiDisplayName( RtAudio::Api api ) { - if (api < 0 || api > RtAudio::NUM_APIS - || (std::find(rtaudio_compiled_apis, - rtaudio_compiled_apis+rtaudio_num_compiled_apis, - api) == rtaudio_compiled_apis+rtaudio_num_compiled_apis)) + if (api < 0 || api >= RtAudio::NUM_APIS) return "Unknown"; return rtaudio_api_names[api][1]; } diff --git a/RtAudio.h b/RtAudio.h index 9976541..7eb7ac8 100644 --- a/RtAudio.h +++ b/RtAudio.h @@ -406,18 +406,16 @@ class RTAUDIO_DLL_PUBLIC RtAudio /*! This obtains a short lower-case name used for identification purposes. This value is guaranteed to remain identical across library versions. - If the API is unknown or not compiled, this function will return - the empty string. + If the API is unknown, this function will return the empty string. */ - static const std::string getCompiledApiName( RtAudio::Api api ); + static std::string getApiName( RtAudio::Api api ); //! Return the display name of a specified compiled audio API. /*! This obtains a long name used for display purposes. - If the API is unknown or not compiled, this function will return - the empty string. + If the API is unknown, this function will return the empty string. */ - static const std::string getCompiledApiDisplayName( RtAudio::Api api ); + static std::string getApiDisplayName( RtAudio::Api api ); //! Return the compiled audio API having the given name. /*! diff --git a/rtaudio_c.cpp b/rtaudio_c.cpp index 59d6cd1..da3ab24 100644 --- a/rtaudio_c.cpp +++ b/rtaudio_c.cpp @@ -22,13 +22,13 @@ extern "C" const unsigned int rtaudio_num_compiled_apis; const rtaudio_api_t *rtaudio_compiled_api() { return rtaudio_compiled_apis; } extern "C" const char* rtaudio_api_names[][2]; -const char *rtaudio_compiled_api_name(rtaudio_api_t api) { +const char *rtaudio_api_name(rtaudio_api_t api) { if (api < 0 || api >= RTAUDIO_API_NUM) return NULL; return rtaudio_api_names[api][0]; } -const char *rtaudio_compiled_api_display_name(rtaudio_api_t api) +const char *rtaudio_api_display_name(rtaudio_api_t api) { if (api < 0 || api >= RTAUDIO_API_NUM) return "Unknown"; diff --git a/rtaudio_c.h b/rtaudio_c.h index e4cdf82..a366117 100644 --- a/rtaudio_c.h +++ b/rtaudio_c.h @@ -107,8 +107,8 @@ typedef struct rtaudio *rtaudio_t; RTAUDIOAPI const char *rtaudio_version(void); RTAUDIOAPI const rtaudio_api_t *rtaudio_compiled_api(void); -RTAUDIOAPI const char *rtaudio_compiled_api_name(rtaudio_api_t api); -RTAUDIOAPI const char *rtaudio_compiled_api_display_name(rtaudio_api_t api); +RTAUDIOAPI const char *rtaudio_api_name(rtaudio_api_t api); +RTAUDIOAPI const char *rtaudio_api_display_name(rtaudio_api_t api); RTAUDIOAPI rtaudio_api_t rtaudio_compiled_api_by_name(const char *name); RTAUDIOAPI const char *rtaudio_error(rtaudio_t audio); diff --git a/tests/apinames.cpp b/tests/apinames.cpp index 9dc634c..c270764 100644 --- a/tests/apinames.cpp +++ b/tests/apinames.cpp @@ -21,14 +21,14 @@ int test_cpp() { // ensure the known APIs return valid names std::cout << "API names by identifier (C++):\n"; for ( size_t i = 0; i < apis.size() ; ++i ) { - const std::string name = RtAudio::getCompiledApiName(apis[i]); + const std::string name = RtAudio::getApiName(apis[i]); if (name.empty()) { - std::cerr << "Invalid name for API " << (int)apis[i] << "\n"; + std::cout << "Invalid name for API " << (int)apis[i] << "\n"; exit(1); } - const std::string displayName = RtAudio::getCompiledApiDisplayName(apis[i]); + const std::string displayName = RtAudio::getApiDisplayName(apis[i]); if (displayName.empty()) { - std::cerr << "Invalid display name for API " << (int)apis[i] << "\n"; + std::cout << "Invalid display name for API " << (int)apis[i] << "\n"; exit(1); } std::cout << "* " << (int)apis[i] << " '" << name << "': '" << displayName << "'\n"; @@ -36,14 +36,14 @@ int test_cpp() { // ensure unknown APIs return the empty string { - const std::string name = RtAudio::getCompiledApiName((RtAudio::Api)-1); + const std::string name = RtAudio::getApiName((RtAudio::Api)-1); if (!name.empty()) { - std::cerr << "Bad string for invalid API '" << name << "'\n"; + std::cout << "Bad string for invalid API '" << name << "'\n"; exit(1); } - const std::string displayName = RtAudio::getCompiledApiDisplayName((RtAudio::Api)-1); + const std::string displayName = RtAudio::getApiDisplayName((RtAudio::Api)-1); if (displayName!="Unknown") { - std::cerr << "Bad display string for invalid API '" << displayName << "'\n"; + std::cout << "Bad display string for invalid API '" << displayName << "'\n"; exit(1); } } @@ -51,9 +51,9 @@ int test_cpp() { // try getting API identifier by name std::cout << "API identifiers by name (C++):\n"; for ( size_t i = 0; i < apis.size() ; ++i ) { - std::string name = RtAudio::getCompiledApiName(apis[i]); + std::string name = RtAudio::getApiName(apis[i]); if ( RtAudio::getCompiledApiByName(name) != apis[i] ) { - std::cerr << "Bad identifier for API '" << name << "'\n"; + std::cout << "Bad identifier for API '" << name << "'\n"; exit( 1 ); } std::cout << "* '" << name << "': " << (int)apis[i] << "\n"; @@ -62,7 +62,7 @@ int test_cpp() { name[j] = (j & 1) ? toupper(name[j]) : tolower(name[j]); RtAudio::Api api = RtAudio::getCompiledApiByName(name); if ( api != RtAudio::UNSPECIFIED ) { - std::cerr << "Identifier " << (int)api << " for invalid API '" << name << "'\n"; + std::cout << "Identifier " << (int)api << " for invalid API '" << name << "'\n"; exit( 1 ); } } @@ -72,7 +72,7 @@ int test_cpp() { RtAudio::Api api; api = RtAudio::getCompiledApiByName(""); if ( api != RtAudio::UNSPECIFIED ) { - std::cerr << "Bad identifier for unknown API name\n"; + std::cout << "Bad identifier for unknown API name\n"; exit( 1 ); } } @@ -88,14 +88,14 @@ int test_c() { // ensure the known APIs return valid names std::cout << "API names by identifier (C):\n"; for ( size_t i = 0; apis[i] != RTAUDIO_API_UNSPECIFIED; ++i) { - const std::string name = rtaudio_compiled_api_name(apis[i]); + const std::string name = rtaudio_api_name(apis[i]); if (name.empty()) { - std::cerr << "Invalid name for API " << (int)apis[i] << "\n"; + std::cout << "Invalid name for API " << (int)apis[i] << "\n"; exit(1); } - const std::string displayName = rtaudio_compiled_api_display_name(apis[i]); + const std::string displayName = rtaudio_api_display_name(apis[i]); if (displayName.empty()) { - std::cerr << "Invalid display name for API " << (int)apis[i] << "\n"; + std::cout << "Invalid display name for API " << (int)apis[i] << "\n"; exit(1); } std::cout << "* " << (int)apis[i] << " '" << name << "': '" << displayName << "'\n"; @@ -103,16 +103,16 @@ int test_c() { // ensure unknown APIs return the empty string { - const char *s = rtaudio_compiled_api_name((rtaudio_api_t)-1); + const char *s = rtaudio_api_name((rtaudio_api_t)-1); const std::string name(s?s:""); if (!name.empty()) { - std::cerr << "Bad string for invalid API '" << name << "'\n"; + std::cout << "Bad string for invalid API '" << name << "'\n"; exit(1); } - s = rtaudio_compiled_api_display_name((rtaudio_api_t)-1); + s = rtaudio_api_display_name((rtaudio_api_t)-1); const std::string displayName(s?s:""); if (displayName!="Unknown") { - std::cerr << "Bad display string for invalid API '" << displayName << "'\n"; + std::cout << "Bad display string for invalid API '" << displayName << "'\n"; exit(1); } } @@ -120,10 +120,10 @@ int test_c() { // try getting API identifier by name std::cout << "API identifiers by name (C):\n"; for ( size_t i = 0; apis[i] != RTAUDIO_API_UNSPECIFIED ; ++i ) { - const char *s = rtaudio_compiled_api_name(apis[i]); + const char *s = rtaudio_api_name(apis[i]); std::string name(s?s:""); if ( rtaudio_compiled_api_by_name(name.c_str()) != apis[i] ) { - std::cerr << "Bad identifier for API '" << name << "'\n"; + std::cout << "Bad identifier for API '" << name << "'\n"; exit( 1 ); } std::cout << "* '" << name << "': " << (int)apis[i] << "\n"; @@ -132,7 +132,7 @@ int test_c() { name[j] = (j & 1) ? toupper(name[j]) : tolower(name[j]); rtaudio_api_t api = rtaudio_compiled_api_by_name(name.c_str()); if ( api != RTAUDIO_API_UNSPECIFIED ) { - std::cerr << "Identifier " << (int)api << " for invalid API '" << name << "'\n"; + std::cout << "Identifier " << (int)api << " for invalid API '" << name << "'\n"; exit( 1 ); } } @@ -142,7 +142,7 @@ int test_c() { rtaudio_api_t api; api = rtaudio_compiled_api_by_name(""); if ( api != RTAUDIO_API_UNSPECIFIED ) { - std::cerr << "Bad identifier for unknown API name\n"; + std::cout << "Bad identifier for unknown API name\n"; exit( 1 ); } } -- 2.30.2