From 5fee1aad7d13cdc6cc763c35d998e3dcdcc16b70 Mon Sep 17 00:00:00 2001 From: Vittorio Romeo Date: Tue, 15 Feb 2022 13:36:22 +0000 Subject: [PATCH] Add '[[nodiscard]]' in more useful/ambiguous places, fix usages --- include/SFML/Audio/SoundFileReader.hpp | 4 ++-- include/SFML/Audio/SoundFileWriter.hpp | 4 ++-- include/SFML/Audio/SoundStream.hpp | 5 +++-- src/SFML/Audio/SoundFileReaderFlac.hpp | 6 +++--- src/SFML/Audio/SoundFileReaderMp3.hpp | 6 +++--- src/SFML/Audio/SoundFileReaderOgg.hpp | 6 +++--- src/SFML/Audio/SoundFileReaderWav.hpp | 6 +++--- src/SFML/Audio/SoundFileWriterFlac.hpp | 4 ++-- src/SFML/Audio/SoundFileWriterOgg.hpp | 4 ++-- src/SFML/Audio/SoundFileWriterWav.hpp | 6 +++--- src/SFML/Window/Android/JoystickImpl.hpp | 4 ++-- src/SFML/Window/Android/SensorImpl.hpp | 4 ++-- src/SFML/Window/FreeBSD/JoystickImpl.hpp | 4 ++-- src/SFML/Window/NetBSD/JoystickImpl.hpp | 4 ++-- src/SFML/Window/OSX/JoystickImpl.hpp | 4 ++-- src/SFML/Window/OSX/SensorImpl.hpp | 4 ++-- src/SFML/Window/OpenBSD/JoystickImpl.hpp | 4 ++-- src/SFML/Window/SensorManager.cpp | 11 +++++++++-- src/SFML/Window/Unix/JoystickImpl.hpp | 4 ++-- src/SFML/Window/Unix/SensorImpl.hpp | 4 ++-- src/SFML/Window/Win32/JoystickImpl.hpp | 10 +++++----- src/SFML/Window/Win32/SensorImpl.hpp | 4 ++-- src/SFML/Window/iOS/JoystickImpl.hpp | 4 ++-- src/SFML/Window/iOS/SensorImpl.hpp | 4 ++-- 24 files changed, 64 insertions(+), 56 deletions(-) diff --git a/include/SFML/Audio/SoundFileReader.hpp b/include/SFML/Audio/SoundFileReader.hpp index 7343e677b..32158944a 100644 --- a/include/SFML/Audio/SoundFileReader.hpp +++ b/include/SFML/Audio/SoundFileReader.hpp @@ -128,13 +128,13 @@ public: /// { /// public: /// -/// static bool check(sf::InputStream& stream) +/// [[nodiscard]] static bool check(sf::InputStream& stream) /// { /// // typically, read the first few header bytes and check fields that identify the format /// // return true if the reader can handle the format /// } /// -/// bool open(sf::InputStream& stream, Info& info) override +/// [[nodiscard]] bool open(sf::InputStream& stream, Info& info) override /// { /// // read the sound file header and fill the sound attributes /// // (channel count, sample count and sample rate) diff --git a/include/SFML/Audio/SoundFileWriter.hpp b/include/SFML/Audio/SoundFileWriter.hpp index 79a7d309a..f2d72f7e1 100644 --- a/include/SFML/Audio/SoundFileWriter.hpp +++ b/include/SFML/Audio/SoundFileWriter.hpp @@ -97,13 +97,13 @@ public: /// { /// public: /// -/// static bool check(const std::string& filename) +/// [[nodiscard]] static bool check(const std::string& filename) /// { /// // typically, check the extension /// // return true if the writer can handle the format /// } /// -/// bool open(const std::string& filename, unsigned int sampleRate, unsigned int channelCount) override +/// [[nodiscard]] bool open(const std::string& filename, unsigned int sampleRate, unsigned int channelCount) override /// { /// // open the file 'filename' for writing, /// // write the given sample rate and channel count to the file header diff --git a/include/SFML/Audio/SoundStream.hpp b/include/SFML/Audio/SoundStream.hpp index 30f8c17f3..8a9d3cb43 100644 --- a/include/SFML/Audio/SoundStream.hpp +++ b/include/SFML/Audio/SoundStream.hpp @@ -396,7 +396,7 @@ private: /// { /// public: /// -/// bool open(const std::string& location) +/// [[nodiscard]] bool open(const std::string& location) /// { /// // Open the source and get audio settings /// ... @@ -405,6 +405,7 @@ private: /// /// // Initialize the stream -- important! /// initialize(channelCount, sampleRate); +/// return true; /// } /// /// private: @@ -414,9 +415,9 @@ private: /// // Fill the chunk with audio data from the stream source /// // (note: must not be empty if you want to continue playing) /// data.samples = ...; -/// data.sampleCount = ...; /// /// // Return true to continue playing +/// data.sampleCount = ...; /// return true; /// } /// diff --git a/src/SFML/Audio/SoundFileReaderFlac.hpp b/src/SFML/Audio/SoundFileReaderFlac.hpp index 93ffcebf1..f7732bd23 100644 --- a/src/SFML/Audio/SoundFileReaderFlac.hpp +++ b/src/SFML/Audio/SoundFileReaderFlac.hpp @@ -53,7 +53,7 @@ public: /// \return True if the file is supported by this reader /// //////////////////////////////////////////////////////////// - static bool check(InputStream& stream); + [[nodiscard]] static bool check(InputStream& stream); public: @@ -76,7 +76,7 @@ public: /// \param info Structure to fill with the attributes of the loaded sound /// //////////////////////////////////////////////////////////// - bool open(sf::InputStream& stream, Info& info) override; + [[nodiscard]] bool open(sf::InputStream& stream, Info& info) override; //////////////////////////////////////////////////////////// /// \brief Change the current read position to the given sample offset @@ -102,7 +102,7 @@ public: /// \return Number of samples actually read (may be less than \a maxCount) /// //////////////////////////////////////////////////////////// - Uint64 read(Int16* samples, Uint64 maxCount) override; + [[nodiscard]] Uint64 read(Int16* samples, Uint64 maxCount) override; public: diff --git a/src/SFML/Audio/SoundFileReaderMp3.hpp b/src/SFML/Audio/SoundFileReaderMp3.hpp index 648a159b9..a9591135c 100644 --- a/src/SFML/Audio/SoundFileReaderMp3.hpp +++ b/src/SFML/Audio/SoundFileReaderMp3.hpp @@ -71,7 +71,7 @@ public: /// \return True if the file is supported by this reader /// //////////////////////////////////////////////////////////// - static bool check(InputStream& stream); + [[nodiscard]] static bool check(InputStream& stream); public: @@ -96,7 +96,7 @@ public: /// \return True if the file was successfully opened /// //////////////////////////////////////////////////////////// - bool open(InputStream& stream, Info& info) override; + [[nodiscard]] bool open(InputStream& stream, Info& info) override; //////////////////////////////////////////////////////////// /// \brief Change the current read position to the given sample offset @@ -122,7 +122,7 @@ public: /// \return Number of samples actually read (may be less than \a maxCount) /// //////////////////////////////////////////////////////////// - Uint64 read(Int16* samples, Uint64 maxCount) override; + [[nodiscard]] Uint64 read(Int16* samples, Uint64 maxCount) override; private: diff --git a/src/SFML/Audio/SoundFileReaderOgg.hpp b/src/SFML/Audio/SoundFileReaderOgg.hpp index fc6293b6d..771bc05cb 100644 --- a/src/SFML/Audio/SoundFileReaderOgg.hpp +++ b/src/SFML/Audio/SoundFileReaderOgg.hpp @@ -52,7 +52,7 @@ public: /// \return True if the file is supported by this reader /// //////////////////////////////////////////////////////////// - static bool check(InputStream& stream); + [[nodiscard]] static bool check(InputStream& stream); public: @@ -77,7 +77,7 @@ public: /// \return True if the file was successfully opened /// //////////////////////////////////////////////////////////// - bool open(InputStream& stream, Info& info) override; + [[nodiscard]] bool open(InputStream& stream, Info& info) override; //////////////////////////////////////////////////////////// /// \brief Change the current read position to the given sample offset @@ -103,7 +103,7 @@ public: /// \return Number of samples actually read (may be less than \a maxCount) /// //////////////////////////////////////////////////////////// - Uint64 read(Int16* samples, Uint64 maxCount) override; + [[nodiscard]] Uint64 read(Int16* samples, Uint64 maxCount) override; private: diff --git a/src/SFML/Audio/SoundFileReaderWav.hpp b/src/SFML/Audio/SoundFileReaderWav.hpp index 47d5eeadf..dadf2e878 100644 --- a/src/SFML/Audio/SoundFileReaderWav.hpp +++ b/src/SFML/Audio/SoundFileReaderWav.hpp @@ -51,7 +51,7 @@ public: /// \return True if the file is supported by this reader /// //////////////////////////////////////////////////////////// - static bool check(InputStream& stream); + [[nodiscard]] static bool check(InputStream& stream); public: @@ -68,7 +68,7 @@ public: /// \param info Structure to fill with the attributes of the loaded sound /// //////////////////////////////////////////////////////////// - bool open(sf::InputStream& stream, Info& info) override; + [[nodiscard]] bool open(sf::InputStream& stream, Info& info) override; //////////////////////////////////////////////////////////// /// \brief Change the current read position to the given sample offset @@ -94,7 +94,7 @@ public: /// \return Number of samples actually read (may be less than \a maxCount) /// //////////////////////////////////////////////////////////// - Uint64 read(Int16* samples, Uint64 maxCount) override; + [[nodiscard]] Uint64 read(Int16* samples, Uint64 maxCount) override; private: diff --git a/src/SFML/Audio/SoundFileWriterFlac.hpp b/src/SFML/Audio/SoundFileWriterFlac.hpp index 518637ff5..76cead032 100644 --- a/src/SFML/Audio/SoundFileWriterFlac.hpp +++ b/src/SFML/Audio/SoundFileWriterFlac.hpp @@ -53,7 +53,7 @@ public: /// \return True if the file can be written by this writer /// //////////////////////////////////////////////////////////// - static bool check(const std::string& filename); + [[nodiscard]] static bool check(const std::string& filename); public: @@ -79,7 +79,7 @@ public: /// \return True if the file was successfully opened /// //////////////////////////////////////////////////////////// - bool open(const std::string& filename, unsigned int sampleRate, unsigned int channelCount) override; + [[nodiscard]] bool open(const std::string& filename, unsigned int sampleRate, unsigned int channelCount) override; //////////////////////////////////////////////////////////// /// \brief Write audio samples to the open file diff --git a/src/SFML/Audio/SoundFileWriterOgg.hpp b/src/SFML/Audio/SoundFileWriterOgg.hpp index 96cbbca3e..46f2e40ab 100644 --- a/src/SFML/Audio/SoundFileWriterOgg.hpp +++ b/src/SFML/Audio/SoundFileWriterOgg.hpp @@ -53,7 +53,7 @@ public: /// \return True if the file can be written by this writer /// //////////////////////////////////////////////////////////// - static bool check(const std::string& filename); + [[nodiscard]] static bool check(const std::string& filename); public: @@ -79,7 +79,7 @@ public: /// \return True if the file was successfully opened /// //////////////////////////////////////////////////////////// - bool open(const std::string& filename, unsigned int sampleRate, unsigned int channelCount) override; + [[nodiscard]] bool open(const std::string& filename, unsigned int sampleRate, unsigned int channelCount) override; //////////////////////////////////////////////////////////// /// \brief Write audio samples to the open file diff --git a/src/SFML/Audio/SoundFileWriterWav.hpp b/src/SFML/Audio/SoundFileWriterWav.hpp index 06b0a3e0a..4e8cb84f9 100644 --- a/src/SFML/Audio/SoundFileWriterWav.hpp +++ b/src/SFML/Audio/SoundFileWriterWav.hpp @@ -53,7 +53,7 @@ public: /// \return True if the file can be written by this writer /// //////////////////////////////////////////////////////////// - static bool check(const std::string& filename); + [[nodiscard]] static bool check(const std::string& filename); public: @@ -79,7 +79,7 @@ public: /// \return True if the file was successfully opened /// //////////////////////////////////////////////////////////// - bool open(const std::string& filename, unsigned int sampleRate, unsigned int channelCount) override; + [[nodiscard]] bool open(const std::string& filename, unsigned int sampleRate, unsigned int channelCount) override; //////////////////////////////////////////////////////////// /// \brief Write audio samples to the open file @@ -101,7 +101,7 @@ private: /// \return True on success, false on error /// //////////////////////////////////////////////////////////// - bool writeHeader(unsigned int sampleRate, unsigned int channelCount); + [[nodiscard]] bool writeHeader(unsigned int sampleRate, unsigned int channelCount); //////////////////////////////////////////////////////////// /// \brief Close the file diff --git a/src/SFML/Window/Android/JoystickImpl.hpp b/src/SFML/Window/Android/JoystickImpl.hpp index bad9932fa..91384e18d 100644 --- a/src/SFML/Window/Android/JoystickImpl.hpp +++ b/src/SFML/Window/Android/JoystickImpl.hpp @@ -68,7 +68,7 @@ public: /// \return True on success, false on failure /// //////////////////////////////////////////////////////////// - bool open(unsigned int index); + [[nodiscard]] bool open(unsigned int index); //////////////////////////////////////////////////////////// /// \brief Close the joystick @@ -98,7 +98,7 @@ public: /// \return Joystick state /// //////////////////////////////////////////////////////////// - JoystickState update(); + [[nodiscard]] JoystickState update(); private: diff --git a/src/SFML/Window/Android/SensorImpl.hpp b/src/SFML/Window/Android/SensorImpl.hpp index 317137bc0..d74953f0b 100644 --- a/src/SFML/Window/Android/SensorImpl.hpp +++ b/src/SFML/Window/Android/SensorImpl.hpp @@ -74,7 +74,7 @@ public: /// \return True on success, false on failure /// //////////////////////////////////////////////////////////// - bool open(Sensor::Type sensor); + [[nodiscard]] bool open(Sensor::Type sensor); //////////////////////////////////////////////////////////// /// \brief Close the sensor @@ -88,7 +88,7 @@ public: /// \return Sensor value /// //////////////////////////////////////////////////////////// - Vector3f update(); + [[nodiscard]] Vector3f update(); //////////////////////////////////////////////////////////// /// \brief Enable or disable the sensor diff --git a/src/SFML/Window/FreeBSD/JoystickImpl.hpp b/src/SFML/Window/FreeBSD/JoystickImpl.hpp index fd135cb2a..15c2535d5 100644 --- a/src/SFML/Window/FreeBSD/JoystickImpl.hpp +++ b/src/SFML/Window/FreeBSD/JoystickImpl.hpp @@ -75,7 +75,7 @@ public: /// \return True on success, false on failure /// //////////////////////////////////////////////////////////// - bool open(unsigned int index); + [[nodiscard]] bool open(unsigned int index); //////////////////////////////////////////////////////////// /// \brief Close the joystick @@ -105,7 +105,7 @@ public: /// \return Joystick state /// //////////////////////////////////////////////////////////// - JoystickState update(); + [[nodiscard]] JoystickState update(); private: diff --git a/src/SFML/Window/NetBSD/JoystickImpl.hpp b/src/SFML/Window/NetBSD/JoystickImpl.hpp index f196e6d8b..9700d182f 100644 --- a/src/SFML/Window/NetBSD/JoystickImpl.hpp +++ b/src/SFML/Window/NetBSD/JoystickImpl.hpp @@ -76,7 +76,7 @@ public: /// \return True on success, false on failure /// //////////////////////////////////////////////////////////// - bool open(unsigned int index); + [[nodiscard]] bool open(unsigned int index); //////////////////////////////////////////////////////////// /// \brief Close the joystick @@ -106,7 +106,7 @@ public: /// \return Joystick state /// //////////////////////////////////////////////////////////// - JoystickState update(); + [[nodiscard]] JoystickState update(); private: diff --git a/src/SFML/Window/OSX/JoystickImpl.hpp b/src/SFML/Window/OSX/JoystickImpl.hpp index 93dd0bf94..deb45a2b9 100644 --- a/src/SFML/Window/OSX/JoystickImpl.hpp +++ b/src/SFML/Window/OSX/JoystickImpl.hpp @@ -78,7 +78,7 @@ public: /// \return True on success, false on failure /// //////////////////////////////////////////////////////////// - bool open(unsigned int index); + [[nodiscard]] bool open(unsigned int index); //////////////////////////////////////////////////////////// /// \brief Close the joystick @@ -108,7 +108,7 @@ public: /// \return Joystick state /// //////////////////////////////////////////////////////////// - JoystickState update(); + [[nodiscard]] JoystickState update(); private: diff --git a/src/SFML/Window/OSX/SensorImpl.hpp b/src/SFML/Window/OSX/SensorImpl.hpp index 2c8eee43c..d257e9765 100644 --- a/src/SFML/Window/OSX/SensorImpl.hpp +++ b/src/SFML/Window/OSX/SensorImpl.hpp @@ -68,7 +68,7 @@ public: /// \return True on success, false on failure /// //////////////////////////////////////////////////////////// - bool open(Sensor::Type sensor); + [[nodiscard]] bool open(Sensor::Type sensor); //////////////////////////////////////////////////////////// /// \brief Close the sensor @@ -82,7 +82,7 @@ public: /// \return Sensor value /// //////////////////////////////////////////////////////////// - Vector3f update(); + [[nodiscard]] Vector3f update(); //////////////////////////////////////////////////////////// /// \brief Enable or disable the sensor diff --git a/src/SFML/Window/OpenBSD/JoystickImpl.hpp b/src/SFML/Window/OpenBSD/JoystickImpl.hpp index af7ef5c7a..b39fdb65f 100644 --- a/src/SFML/Window/OpenBSD/JoystickImpl.hpp +++ b/src/SFML/Window/OpenBSD/JoystickImpl.hpp @@ -68,7 +68,7 @@ public: /// \return True on success, false on failure /// //////////////////////////////////////////////////////////// - bool open(unsigned int index); + [[nodiscard]] bool open(unsigned int index); //////////////////////////////////////////////////////////// /// \brief Close the joystick @@ -98,7 +98,7 @@ public: /// \return Joystick state /// //////////////////////////////////////////////////////////// - JoystickState update(); + [[nodiscard]] JoystickState update(); private: diff --git a/src/SFML/Window/SensorManager.cpp b/src/SFML/Window/SensorManager.cpp index 988bc588c..254fb9a43 100644 --- a/src/SFML/Window/SensorManager.cpp +++ b/src/SFML/Window/SensorManager.cpp @@ -105,8 +105,15 @@ SensorManager::SensorManager() // Open the available sensors if (m_sensors[i].available) { - m_sensors[i].sensor.open(static_cast(i)); - m_sensors[i].sensor.setEnabled(false); + if (m_sensors[i].sensor.open(static_cast(i))) + { + m_sensors[i].sensor.setEnabled(false); + } + else + { + m_sensors[i].available = false; + err() << "Warning: sensor " << i << " failed to open, will not be available" << std::endl; + } } } } diff --git a/src/SFML/Window/Unix/JoystickImpl.hpp b/src/SFML/Window/Unix/JoystickImpl.hpp index b48d648c5..ab3049022 100644 --- a/src/SFML/Window/Unix/JoystickImpl.hpp +++ b/src/SFML/Window/Unix/JoystickImpl.hpp @@ -80,7 +80,7 @@ public: /// \return True on success, false on failure /// //////////////////////////////////////////////////////////// - bool open(unsigned int index); + [[nodiscard]] bool open(unsigned int index); //////////////////////////////////////////////////////////// /// \brief Close the joystick @@ -110,7 +110,7 @@ public: /// \return Joystick state /// //////////////////////////////////////////////////////////// - JoystickState update(); + [[nodiscard]] JoystickState update(); private: diff --git a/src/SFML/Window/Unix/SensorImpl.hpp b/src/SFML/Window/Unix/SensorImpl.hpp index e361219ff..bf8e0c9b4 100644 --- a/src/SFML/Window/Unix/SensorImpl.hpp +++ b/src/SFML/Window/Unix/SensorImpl.hpp @@ -68,7 +68,7 @@ public: /// \return True on success, false on failure /// //////////////////////////////////////////////////////////// - bool open(Sensor::Type sensor); + [[nodiscard]] bool open(Sensor::Type sensor); //////////////////////////////////////////////////////////// /// \brief Close the sensor @@ -82,7 +82,7 @@ public: /// \return Sensor value /// //////////////////////////////////////////////////////////// - Vector3f update(); + [[nodiscard]] Vector3f update(); //////////////////////////////////////////////////////////// /// \brief Enable or disable the sensor diff --git a/src/SFML/Window/Win32/JoystickImpl.hpp b/src/SFML/Window/Win32/JoystickImpl.hpp index c4558affc..caa7f8ef2 100644 --- a/src/SFML/Window/Win32/JoystickImpl.hpp +++ b/src/SFML/Window/Win32/JoystickImpl.hpp @@ -91,7 +91,7 @@ public: /// \return True on success, false on failure /// //////////////////////////////////////////////////////////// - bool open(unsigned int index); + [[nodiscard]] bool open(unsigned int index); //////////////////////////////////////////////////////////// /// \brief Close the joystick @@ -121,7 +121,7 @@ public: /// \return Joystick state /// //////////////////////////////////////////////////////////// - JoystickState update(); + [[nodiscard]] JoystickState update(); //////////////////////////////////////////////////////////// /// \brief Perform the global initialization of the joystick module (DInput) @@ -159,7 +159,7 @@ public: /// \return True on success, false on failure /// //////////////////////////////////////////////////////////// - bool openDInput(unsigned int index); + [[nodiscard]] bool openDInput(unsigned int index); //////////////////////////////////////////////////////////// /// \brief Close the joystick (DInput) @@ -181,7 +181,7 @@ public: /// \return Joystick state /// //////////////////////////////////////////////////////////// - JoystickState updateDInputBuffered(); + [[nodiscard]] JoystickState updateDInputBuffered(); //////////////////////////////////////////////////////////// /// \brief Update the joystick and get its new state (DInput, Polled) @@ -189,7 +189,7 @@ public: /// \return Joystick state /// //////////////////////////////////////////////////////////// - JoystickState updateDInputPolled(); + [[nodiscard]] JoystickState updateDInputPolled(); private: diff --git a/src/SFML/Window/Win32/SensorImpl.hpp b/src/SFML/Window/Win32/SensorImpl.hpp index ed5adbc1e..cb9218173 100644 --- a/src/SFML/Window/Win32/SensorImpl.hpp +++ b/src/SFML/Window/Win32/SensorImpl.hpp @@ -68,7 +68,7 @@ public: /// \return True on success, false on failure /// //////////////////////////////////////////////////////////// - bool open(Sensor::Type sensor); + [[nodiscard]] bool open(Sensor::Type sensor); //////////////////////////////////////////////////////////// /// \brief Close the sensor @@ -82,7 +82,7 @@ public: /// \return Sensor value /// //////////////////////////////////////////////////////////// - Vector3f update(); + [[nodiscard]] Vector3f update(); //////////////////////////////////////////////////////////// /// \brief Enable or disable the sensor diff --git a/src/SFML/Window/iOS/JoystickImpl.hpp b/src/SFML/Window/iOS/JoystickImpl.hpp index e5f99ae80..f78734555 100644 --- a/src/SFML/Window/iOS/JoystickImpl.hpp +++ b/src/SFML/Window/iOS/JoystickImpl.hpp @@ -73,7 +73,7 @@ public: /// \return True on success, false on failure /// //////////////////////////////////////////////////////////// - bool open(unsigned int index); + [[nodiscard]] bool open(unsigned int index); //////////////////////////////////////////////////////////// /// \brief Close the joystick @@ -103,7 +103,7 @@ public: /// \return Joystick state /// //////////////////////////////////////////////////////////// - JoystickState update(); + [[nodiscard]] JoystickState update(); }; } // namespace priv diff --git a/src/SFML/Window/iOS/SensorImpl.hpp b/src/SFML/Window/iOS/SensorImpl.hpp index 7e80b9835..e9f7dcf44 100644 --- a/src/SFML/Window/iOS/SensorImpl.hpp +++ b/src/SFML/Window/iOS/SensorImpl.hpp @@ -73,7 +73,7 @@ public: /// \return True on success, false on failure /// //////////////////////////////////////////////////////////// - bool open(Sensor::Type sensor); + [[nodiscard]] bool open(Sensor::Type sensor); //////////////////////////////////////////////////////////// /// \brief Close the sensor @@ -87,7 +87,7 @@ public: /// \return Sensor value /// //////////////////////////////////////////////////////////// - Vector3f update(); + [[nodiscard]] Vector3f update(); //////////////////////////////////////////////////////////// /// \brief Enable or disable the sensor