Browse Source

Platform: replace std::string with String/StringView in Sdl2Application

Signed-off-by: Squareys <squareys@googlemail.com>
pull/559/head
Squareys 4 years ago
parent
commit
953a684f89
  1. 20
      src/Magnum/Platform/Sdl2Application.cpp
  2. 33
      src/Magnum/Platform/Sdl2Application.h

20
src/Magnum/Platform/Sdl2Application.cpp

@ -159,7 +159,7 @@ Sdl2Application::Sdl2Application(const Arguments& arguments, NoCreateT):
/* Save command-line arguments */ /* Save command-line arguments */
if(args.value("log") == "verbose") _verboseLog = true; if(args.value("log") == "verbose") _verboseLog = true;
const std::string dpiScaling = args.value("dpi-scaling"); const Containers::String dpiScaling = args.value("dpi-scaling");
if(dpiScaling == "default") if(dpiScaling == "default")
_commandLineDpiScalingPolicy = Implementation::Sdl2DpiScalingPolicy::Default; _commandLineDpiScalingPolicy = Implementation::Sdl2DpiScalingPolicy::Default;
#ifdef CORRADE_TARGET_APPLE #ifdef CORRADE_TARGET_APPLE
@ -174,7 +174,7 @@ Sdl2Application::Sdl2Application(const Arguments& arguments, NoCreateT):
else if(dpiScaling == "physical") else if(dpiScaling == "physical")
_commandLineDpiScalingPolicy = Implementation::Sdl2DpiScalingPolicy::Physical; _commandLineDpiScalingPolicy = Implementation::Sdl2DpiScalingPolicy::Physical;
#endif #endif
else if(dpiScaling.find_first_of(" \t\n") != std::string::npos) else if(dpiScaling.find(' ') || dpiScaling.find('\t') || dpiScaling.find('\n'))
_commandLineDpiScaling = args.value<Vector2>("dpi-scaling"); _commandLineDpiScaling = args.value<Vector2>("dpi-scaling");
else else
_commandLineDpiScaling = Vector2{args.value<Float>("dpi-scaling")}; _commandLineDpiScaling = Vector2{args.value<Float>("dpi-scaling")};
@ -322,13 +322,15 @@ Vector2 Sdl2Application::dpiScaling(const Configuration& configuration) {
#endif #endif
} }
void Sdl2Application::setWindowTitle(const std::string& title) { void Sdl2Application::setWindowTitle(const Containers::StringView title) {
#ifndef CORRADE_TARGET_EMSCRIPTEN #ifndef CORRADE_TARGET_EMSCRIPTEN
SDL_SetWindowTitle(_window, title.data()); SDL_SetWindowTitle(_window,
Containers::String::nullTerminatedGlobalView(title).data());
#else #else
/* We don't have the _window because SDL_CreateWindow() doesn't exist in /* We don't have the _window because SDL_CreateWindow() doesn't exist in
the SDL1/2 hybrid. But it's not used anyway, so pass nullptr there. */ the SDL1/2 hybrid. But it's not used anyway, so pass nullptr there. */
SDL_SetWindowTitle(nullptr, title.data()); SDL_SetWindowTitle(nullptr,
Containers::String::nullTerminatedGlobalView(title).data());
#endif #endif
} }
@ -743,7 +745,7 @@ Vector2i Sdl2Application::framebufferSize() const {
#endif #endif
#ifdef CORRADE_TARGET_EMSCRIPTEN #ifdef CORRADE_TARGET_EMSCRIPTEN
void Sdl2Application::setContainerCssClass(const std::string& cssClass) { void Sdl2Application::setContainerCssClass(const Containers::StringView cssClass) {
#pragma GCC diagnostic push #pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wdollar-in-identifier-extension" #pragma GCC diagnostic ignored "-Wdollar-in-identifier-extension"
EM_ASM_({ EM_ASM_({
@ -752,7 +754,7 @@ void Sdl2Application::setContainerCssClass(const std::string& cssClass) {
no way to look for it anymore. */ no way to look for it anymore. */
(Module['canvas'].closest('.mn-container') || (Module['canvas'].closest('.mn-container') ||
document.getElementById('container')).className = (['mn-container', AsciiToString($0)]).join(' '); document.getElementById('container')).className = (['mn-container', AsciiToString($0)]).join(' ');
}, cssClass.data()); }, Containers::String::nullTerminatedGlobalView(cssClass).data());
#pragma GCC diagnostic pop #pragma GCC diagnostic pop
} }
#endif #endif
@ -1212,11 +1214,11 @@ Sdl2Application::Configuration::Configuration():
Sdl2Application::Configuration::~Configuration() = default; Sdl2Application::Configuration::~Configuration() = default;
std::string Sdl2Application::KeyEvent::keyName(const Key key) { Containers::StringView Sdl2Application::KeyEvent::keyName(const Key key) {
return SDL_GetKeyName(SDL_Keycode(key)); return SDL_GetKeyName(SDL_Keycode(key));
} }
std::string Sdl2Application::KeyEvent::keyName() const { Containers::StringView Sdl2Application::KeyEvent::keyName() const {
return keyName(_key); return keyName(_key);
} }

33
src/Magnum/Platform/Sdl2Application.h

@ -31,7 +31,11 @@
* @brief Class @ref Magnum::Platform::Sdl2Application, macro @ref MAGNUM_SDL2APPLICATION_MAIN() * @brief Class @ref Magnum::Platform::Sdl2Application, macro @ref MAGNUM_SDL2APPLICATION_MAIN()
*/ */
#include <string> #include <Corrade/Containers/String.h>
#ifdef MAGNUM_BUILD_DEPRECATED
/* Some APIs used to take or return a std::string before */
#include <Corrade/Containers/StringStl.h>
#endif
#include <Corrade/Containers/ArrayView.h> #include <Corrade/Containers/ArrayView.h>
#include <Corrade/Containers/EnumSet.h> #include <Corrade/Containers/EnumSet.h>
#include <Corrade/Containers/Optional.h> #include <Corrade/Containers/Optional.h>
@ -805,7 +809,7 @@ class Sdl2Application {
* *
* The @p title is expected to be encoded in UTF-8. * The @p title is expected to be encoded in UTF-8.
*/ */
void setWindowTitle(const std::string& title); void setWindowTitle(Containers::StringView title);
#if !defined(CORRADE_TARGET_EMSCRIPTEN) && (SDL_MAJOR_VERSION*1000 + SDL_MINOR_VERSION*100 + SDL_PATCHLEVEL >= 2005 || defined(DOXYGEN_GENERATING_OUTPUT)) #if !defined(CORRADE_TARGET_EMSCRIPTEN) && (SDL_MAJOR_VERSION*1000 + SDL_MINOR_VERSION*100 + SDL_PATCHLEVEL >= 2005 || defined(DOXYGEN_GENERATING_OUTPUT))
/** /**
@ -853,7 +857,7 @@ class Sdl2Application {
* @cb{.html} <div class="mn-container"> @ce is not found. This * @cb{.html} <div class="mn-container"> @ce is not found. This
* compatibility is scheduled to be removed in the future. * compatibility is scheduled to be removed in the future.
*/ */
void setContainerCssClass(const std::string& cssClass); void setContainerCssClass(Containers::StringView cssClass);
#endif #endif
/** /**
@ -1819,10 +1823,13 @@ class Sdl2Application::Configuration {
/** /**
* @brief Window title * @brief Window title
* *
* The returned string view is valid at least until the next call to
* @ref setTitle() or until the @ref Sdl2Application is destroyed.
*
* @note Not available in @ref CORRADE_TARGET_EMSCRIPTEN "Emscripten" * @note Not available in @ref CORRADE_TARGET_EMSCRIPTEN "Emscripten"
* and @ref CORRADE_TARGET_IOS "iOS". * and @ref CORRADE_TARGET_IOS "iOS".
*/ */
std::string title() const { return _title; } Containers::StringView title() const { return _title; }
#endif #endif
/** /**
@ -1840,8 +1847,8 @@ class Sdl2Application::Configuration {
* application state change) using @ref setWindowTitle(). * application state change) using @ref setWindowTitle().
*/ */
#if !defined(CORRADE_TARGET_EMSCRIPTEN) && !defined(CORRADE_TARGET_IOS) #if !defined(CORRADE_TARGET_EMSCRIPTEN) && !defined(CORRADE_TARGET_IOS)
Configuration& setTitle(std::string title) { Configuration& setTitle(const Containers::StringView title) {
_title = std::move(title); _title = Containers::String::nullTerminatedGlobalView(title);
return *this; return *this;
} }
#else #else
@ -1954,7 +1961,7 @@ class Sdl2Application::Configuration {
private: private:
#if !defined(CORRADE_TARGET_EMSCRIPTEN) && !defined(CORRADE_TARGET_IOS) #if !defined(CORRADE_TARGET_EMSCRIPTEN) && !defined(CORRADE_TARGET_IOS)
std::string _title; Containers::String _title;
#endif #endif
Vector2i _size; Vector2i _size;
DpiScalingPolicy _dpiScalingPolicy; DpiScalingPolicy _dpiScalingPolicy;
@ -2469,9 +2476,13 @@ class Sdl2Application::KeyEvent: public Sdl2Application::InputEvent {
* Human-readable localized UTF-8 name for given @p key, intended for * Human-readable localized UTF-8 name for given @p key, intended for
* displaying to the user in e.g. key binding configuration. If there * displaying to the user in e.g. key binding configuration. If there
* is no name for given key, empty string is returned. * is no name for given key, empty string is returned.
*
* The returned string view is valid at least until the next call
* to this function.
*
* @see @ref keyName(Key) * @see @ref keyName(Key)
*/ */
static std::string keyName(Key key); static Containers::StringView keyName(Key key);
/** /**
* @brief Key * @brief Key
@ -2487,9 +2498,13 @@ class Sdl2Application::KeyEvent: public Sdl2Application::InputEvent {
* @ref key(), intended for displaying to the user in e.g. * @ref key(), intended for displaying to the user in e.g.
* key binding configuration. If there is no name for that key, empty * key binding configuration. If there is no name for that key, empty
* string is returned. * string is returned.
*
* The returned string view is valid at least until the next call
* to this function.
*
* @see @ref keyName(Key) * @see @ref keyName(Key)
*/ */
std::string keyName() const; Containers::StringView keyName() const;
/** @brief Modifiers */ /** @brief Modifiers */
Modifiers modifiers() const { return _modifiers; } Modifiers modifiers() const { return _modifiers; }

Loading…
Cancel
Save