From 8be8a76cbaae655b0edc325c5a8e494f0e01a6b0 Mon Sep 17 00:00:00 2001 From: Vittorio Romeo Date: Wed, 8 Dec 2021 01:08:11 +0000 Subject: [PATCH] Use 'auto', range-'for', and structured bindings to improve some loops --- examples/ftp/Ftp.cpp | 5 +- examples/joystick/Joystick.cpp | 24 +++++----- include/SFML/Audio/SoundFileFactory.inl | 4 +- include/SFML/Network/SocketSelector.hpp | 2 +- src/SFML/Audio/SoundBuffer.cpp | 12 ++--- src/SFML/Audio/SoundFileFactory.cpp | 24 +++++----- src/SFML/Graphics/Font.cpp | 9 ++-- src/SFML/Graphics/Image.cpp | 10 ++-- src/SFML/Graphics/ImageLoader.cpp | 5 +- src/SFML/Graphics/RenderTarget.cpp | 16 +++---- src/SFML/Graphics/RenderTextureImplFBO.cpp | 54 +++++++++++----------- src/SFML/Graphics/Shader.cpp | 7 ++- src/SFML/Network/Http.cpp | 20 ++++---- src/SFML/Network/Packet.cpp | 4 +- src/SFML/Window/GlContext.cpp | 4 +- src/SFML/Window/OSX/HIDInputManager.mm | 9 ++-- src/SFML/Window/OSX/JoystickImpl.cpp | 38 ++++++++------- src/SFML/Window/Unix/Display.cpp | 6 +-- src/SFML/Window/Unix/JoystickImpl.cpp | 38 +++++++-------- src/SFML/Window/Unix/VulkanImplX11.cpp | 6 +-- src/SFML/Window/Unix/WindowImplX11.cpp | 10 ++-- src/SFML/Window/Win32/JoystickImpl.cpp | 26 +++++------ src/SFML/Window/Win32/VulkanImplWin32.cpp | 6 +-- 23 files changed, 171 insertions(+), 168 deletions(-) diff --git a/examples/ftp/Ftp.cpp b/examples/ftp/Ftp.cpp index e4de8adc7..f6e657f3e 100644 --- a/examples/ftp/Ftp.cpp +++ b/examples/ftp/Ftp.cpp @@ -102,9 +102,8 @@ int main() // Print the contents of the current server directory sf::Ftp::ListingResponse response = server.getDirectoryListing(); std::cout << response << std::endl; - const std::vector& names = response.getListing(); - for (std::vector::const_iterator it = names.begin(); it != names.end(); ++it) - std::cout << *it << std::endl; + for (const std::string& name : response.getListing()) + std::cout << name << std::endl; break; } diff --git a/examples/joystick/Joystick.cpp b/examples/joystick/Joystick.cpp index 7d3557a9a..62f512fcf 100644 --- a/examples/joystick/Joystick.cpp +++ b/examples/joystick/Joystick.cpp @@ -142,15 +142,15 @@ int main() object.value.setString("N/A"); } - for (Texts::iterator it = texts.begin(); it != texts.end(); ++it) + for (auto& [label, joystickObject] : texts) { - it->second.label.setFont(font); - it->second.label.setCharacterSize(14); - it->second.label.setFillColor(sf::Color::White); + joystickObject.label.setFont(font); + joystickObject.label.setCharacterSize(14); + joystickObject.label.setFillColor(sf::Color::White); - it->second.value.setFont(font); - it->second.value.setCharacterSize(14); - it->second.value.setFillColor(sf::Color::White); + joystickObject.value.setFont(font); + joystickObject.value.setCharacterSize(14); + joystickObject.value.setFillColor(sf::Color::White); } // Update initially displayed joystick values if a joystick is already connected on startup @@ -187,8 +187,8 @@ int main() else if (event.type == sf::Event::JoystickDisconnected) { // Reset displayed joystick values to empty - for (Texts::iterator it = texts.begin(); it != texts.end(); ++it) - it->second.value.setString("N/A"); + for (auto& [label, joystickObject] : texts) + joystickObject.value.setString("N/A"); texts["ID"].label.setString(""); texts["ID"].value.setString(""); @@ -226,10 +226,10 @@ int main() window.clear(); // Draw the label-value sf::Text objects - for (Texts::const_iterator it = texts.begin(); it != texts.end(); ++it) + for (const auto& [label, joystickObject] : texts) { - window.draw(it->second.label); - window.draw(it->second.value); + window.draw(joystickObject.label); + window.draw(joystickObject.value); } // Display things on screen diff --git a/include/SFML/Audio/SoundFileFactory.inl b/include/SFML/Audio/SoundFileFactory.inl index 69e427965..9ca571b34 100644 --- a/include/SFML/Audio/SoundFileFactory.inl +++ b/include/SFML/Audio/SoundFileFactory.inl @@ -57,7 +57,7 @@ template void SoundFileFactory::unregisterReader() { // Remove the instance(s) of the reader from the array of factories - for (ReaderFactoryArray::iterator it = s_readers.begin(); it != s_readers.end(); ) + for (auto it = s_readers.begin(); it != s_readers.end(); /* noop */) { if (it->create == &priv::createReader) it = s_readers.erase(it); @@ -88,7 +88,7 @@ template void SoundFileFactory::unregisterWriter() { // Remove the instance(s) of the writer from the array of factories - for (WriterFactoryArray::iterator it = s_writers.begin(); it != s_writers.end(); ) + for (auto it = s_writers.begin(); it != s_writers.end(); /* noop */) { if (it->create == &priv::createWriter) it = s_writers.erase(it); diff --git a/include/SFML/Network/SocketSelector.hpp b/include/SFML/Network/SocketSelector.hpp index 693e5ffdc..f19d38786 100644 --- a/include/SFML/Network/SocketSelector.hpp +++ b/include/SFML/Network/SocketSelector.hpp @@ -240,7 +240,7 @@ private: /// else /// { /// // The listener socket is not ready, test all other sockets (the clients) -/// for (std::list::iterator it = clients.begin(); it != clients.end(); ++it) +/// for (auto it = clients.begin(); it != clients.end(); ++it) /// { /// sf::TcpSocket& client = **it; /// if (selector.isReady(client)) diff --git a/src/SFML/Audio/SoundBuffer.cpp b/src/SFML/Audio/SoundBuffer.cpp index 764c8e87e..beb79f3c5 100644 --- a/src/SFML/Audio/SoundBuffer.cpp +++ b/src/SFML/Audio/SoundBuffer.cpp @@ -72,8 +72,8 @@ SoundBuffer::~SoundBuffer() sounds.swap(m_sounds); // Detach the buffer from the sounds that use it (to avoid OpenAL errors) - for (SoundList::const_iterator it = sounds.begin(); it != sounds.end(); ++it) - (*it)->resetBuffer(); + for (Sound* soundPtr : sounds) + soundPtr->resetBuffer(); // Destroy the buffer if (m_buffer) @@ -257,8 +257,8 @@ bool SoundBuffer::update(unsigned int channelCount, unsigned int sampleRate) SoundList sounds(m_sounds); // Detach the buffer from the sounds that use it (to avoid OpenAL errors) - for (SoundList::const_iterator it = sounds.begin(); it != sounds.end(); ++it) - (*it)->resetBuffer(); + for (Sound* soundPtr : sounds) + soundPtr->resetBuffer(); // Fill the buffer ALsizei size = static_cast(m_samples.size() * sizeof(Int16)); @@ -268,8 +268,8 @@ bool SoundBuffer::update(unsigned int channelCount, unsigned int sampleRate) m_duration = seconds(static_cast(m_samples.size()) / static_cast(sampleRate) / static_cast(channelCount)); // Now reattach the buffer to the sounds that use it - for (SoundList::const_iterator it = sounds.begin(); it != sounds.end(); ++it) - (*it)->setBuffer(*this); + for (Sound* soundPtr : sounds) + soundPtr->setBuffer(*this); return true; } diff --git a/src/SFML/Audio/SoundFileFactory.cpp b/src/SFML/Audio/SoundFileFactory.cpp index 7b9c70ed0..0f60e26c1 100644 --- a/src/SFML/Audio/SoundFileFactory.cpp +++ b/src/SFML/Audio/SoundFileFactory.cpp @@ -76,11 +76,11 @@ SoundFileReader* SoundFileFactory::createReaderFromFilename(const std::string& f } // Test the filename in all the registered factories - for (ReaderFactoryArray::const_iterator it = s_readers.begin(); it != s_readers.end(); ++it) + for (const ReaderFactory& readerFactory : s_readers) { stream.seek(0); - if (it->check(stream)) - return it->create(); + if (readerFactory.check(stream)) + return readerFactory.create(); } // No suitable reader found @@ -100,11 +100,11 @@ SoundFileReader* SoundFileFactory::createReaderFromMemory(const void* data, std: stream.open(data, sizeInBytes); // Test the stream for all the registered factories - for (ReaderFactoryArray::const_iterator it = s_readers.begin(); it != s_readers.end(); ++it) + for (const ReaderFactory& readerFactory : s_readers) { stream.seek(0); - if (it->check(stream)) - return it->create(); + if (readerFactory.check(stream)) + return readerFactory.create(); } // No suitable reader found @@ -120,11 +120,11 @@ SoundFileReader* SoundFileFactory::createReaderFromStream(InputStream& stream) ensureDefaultReadersWritersRegistered(); // Test the stream for all the registered factories - for (ReaderFactoryArray::const_iterator it = s_readers.begin(); it != s_readers.end(); ++it) + for (const ReaderFactory& readerFactory : s_readers) { stream.seek(0); - if (it->check(stream)) - return it->create(); + if (readerFactory.check(stream)) + return readerFactory.create(); } // No suitable reader found @@ -140,10 +140,10 @@ SoundFileWriter* SoundFileFactory::createWriterFromFilename(const std::string& f ensureDefaultReadersWritersRegistered(); // Test the filename in all the registered factories - for (WriterFactoryArray::const_iterator it = s_writers.begin(); it != s_writers.end(); ++it) + for (const WriterFactory& writerFactory : s_writers) { - if (it->check(filename)) - return it->create(); + if (writerFactory.check(filename)) + return writerFactory.create(); } // No suitable writer found diff --git a/src/SFML/Graphics/Font.cpp b/src/SFML/Graphics/Font.cpp index 8d8ac6d3a..d7591f0c9 100644 --- a/src/SFML/Graphics/Font.cpp +++ b/src/SFML/Graphics/Font.cpp @@ -354,8 +354,7 @@ const Glyph& Font::getGlyph(Uint32 codePoint, unsigned int characterSize, bool b Uint64 key = combine(outlineThickness, bold, FT_Get_Char_Index(static_cast(m_face), codePoint)); // Search the glyph into the cache - GlyphTable::const_iterator it = glyphs.find(key); - if (it != glyphs.end()) + if (auto it = glyphs.find(key); it != glyphs.end()) { // Found: just return it return it->second; @@ -486,9 +485,9 @@ void Font::setSmooth(bool smooth) { m_isSmooth = smooth; - for (sf::Font::PageTable::iterator page = m_pages.begin(); page != m_pages.end(); ++page) + for (auto& [key, page] : m_pages) { - page->second.texture.setSmooth(m_isSmooth); + page.texture.setSmooth(m_isSmooth); } } } @@ -737,7 +736,7 @@ IntRect Font::findGlyphRect(Page& page, unsigned int width, unsigned int height) // Find the line that fits well the glyph Row* row = nullptr; float bestRatio = 0; - for (std::vector::iterator it = page.rows.begin(); it != page.rows.end() && !row; ++it) + for (auto it = page.rows.begin(); it != page.rows.end() && !row; ++it) { float ratio = static_cast(height) / static_cast(it->height); diff --git a/src/SFML/Graphics/Image.cpp b/src/SFML/Graphics/Image.cpp index f27db7265..949c27b82 100644 --- a/src/SFML/Graphics/Image.cpp +++ b/src/SFML/Graphics/Image.cpp @@ -308,8 +308,8 @@ void Image::flipHorizontally() for (std::size_t y = 0; y < m_size.y; ++y) { - std::vector::iterator left = m_pixels.begin() + static_cast::iterator::difference_type>(y * rowSize); - std::vector::iterator right = m_pixels.begin() + static_cast::iterator::difference_type>((y + 1) * rowSize - 4); + auto left = m_pixels.begin() + static_cast::iterator::difference_type>(y * rowSize); + auto right = m_pixels.begin() + static_cast::iterator::difference_type>((y + 1) * rowSize - 4); for (std::size_t x = 0; x < m_size.x / 2; ++x) { @@ -328,10 +328,10 @@ void Image::flipVertically() { if (!m_pixels.empty()) { - std::vector::iterator::difference_type rowSize = static_cast::iterator::difference_type>(m_size.x * 4); + auto rowSize = static_cast::iterator::difference_type>(m_size.x * 4); - std::vector::iterator top = m_pixels.begin(); - std::vector::iterator bottom = m_pixels.end() - rowSize; + auto top = m_pixels.begin(); + auto bottom = m_pixels.end() - rowSize; for (std::size_t y = 0; y < m_size.y / 2; ++y) { diff --git a/src/SFML/Graphics/ImageLoader.cpp b/src/SFML/Graphics/ImageLoader.cpp index 4cfc9e4b3..619806f0f 100644 --- a/src/SFML/Graphics/ImageLoader.cpp +++ b/src/SFML/Graphics/ImageLoader.cpp @@ -41,8 +41,9 @@ namespace // Convert a string to lower case std::string toLower(std::string str) { - for (std::string::iterator i = str.begin(); i != str.end(); ++i) - *i = static_cast(std::tolower(*i)); + for (char& c : str) + c = static_cast(std::tolower(c)); + return str; } diff --git a/src/SFML/Graphics/RenderTarget.cpp b/src/SFML/Graphics/RenderTarget.cpp index dc4dd11d6..e7ac0e69e 100644 --- a/src/SFML/Graphics/RenderTarget.cpp +++ b/src/SFML/Graphics/RenderTarget.cpp @@ -77,9 +77,9 @@ namespace // Check if a RenderTarget with the given ID is active in the current context bool isActive(sf::Uint64 id) { - ContextRenderTargetMap::iterator iter = contextRenderTargetMap.find(sf::Context::getActiveContextId()); + auto it = contextRenderTargetMap.find(sf::Context::getActiveContextId()); - if ((iter == contextRenderTargetMap.end()) || (iter->second != id)) + if ((it == contextRenderTargetMap.end()) || (it->second != id)) return false; return true; @@ -433,28 +433,28 @@ bool RenderTarget::setActive(bool active) Uint64 contextId = Context::getActiveContextId(); using RenderTargetImpl::contextRenderTargetMap; - RenderTargetImpl::ContextRenderTargetMap::iterator iter = contextRenderTargetMap.find(contextId); + auto it = contextRenderTargetMap.find(contextId); if (active) { - if (iter == contextRenderTargetMap.end()) + if (it == contextRenderTargetMap.end()) { contextRenderTargetMap[contextId] = m_id; m_cache.glStatesSet = false; m_cache.enable = false; } - else if (iter->second != m_id) + else if (it->second != m_id) { - iter->second = m_id; + it->second = m_id; m_cache.enable = false; } } else { - if (iter != contextRenderTargetMap.end()) - contextRenderTargetMap.erase(iter); + if (it != contextRenderTargetMap.end()) + contextRenderTargetMap.erase(it); m_cache.enable = false; } diff --git a/src/SFML/Graphics/RenderTextureImplFBO.cpp b/src/SFML/Graphics/RenderTextureImplFBO.cpp index ee6edcc2d..c3592867d 100644 --- a/src/SFML/Graphics/RenderTextureImplFBO.cpp +++ b/src/SFML/Graphics/RenderTextureImplFBO.cpp @@ -62,18 +62,18 @@ namespace { sf::Uint64 contextId = sf::Context::getActiveContextId(); - for (std::set >::iterator iter = staleFrameBuffers.begin(); iter != staleFrameBuffers.end();) + for (auto it = staleFrameBuffers.begin(); it != staleFrameBuffers.end();) { - if (iter->first == contextId) + if (it->first == contextId) { - GLuint frameBuffer = static_cast(iter->second); + GLuint frameBuffer = static_cast(it->second); glCheck(GLEXT_glDeleteFramebuffers(1, &frameBuffer)); - staleFrameBuffers.erase(iter++); + staleFrameBuffers.erase(it++); } else { - ++iter; + ++it; } } } @@ -86,17 +86,17 @@ namespace sf::Uint64 contextId = sf::Context::getActiveContextId(); // Destroy active frame buffer objects - for (std::unordered_set*>::iterator frameBuffersIter = frameBuffers.begin(); frameBuffersIter != frameBuffers.end(); ++frameBuffersIter) + for (auto* frameBuffer : frameBuffers) { - for (std::unordered_map::iterator iter = (*frameBuffersIter)->begin(); iter != (*frameBuffersIter)->end(); ++iter) + for (auto it = frameBuffer->begin(); it != frameBuffer->end(); ++it) { - if (iter->first == contextId) + if (it->first == contextId) { - GLuint frameBuffer = iter->second; - glCheck(GLEXT_glDeleteFramebuffers(1, &frameBuffer)); + GLuint frameBufferId = it->second; + glCheck(GLEXT_glDeleteFramebuffers(1, &frameBufferId)); // Erase the entry from the RenderTextureImplFBO's map - (*frameBuffersIter)->erase(iter); + frameBuffer->erase(it); break; } @@ -162,11 +162,11 @@ RenderTextureImplFBO::~RenderTextureImplFBO() } // Move all frame buffer objects to stale set - for (std::unordered_map::iterator iter = m_frameBuffers.begin(); iter != m_frameBuffers.end(); ++iter) - staleFrameBuffers.emplace(iter->first, iter->second); + for (auto& [contextId, frameBufferId] : m_frameBuffers) + staleFrameBuffers.emplace(contextId, frameBufferId); - for (std::unordered_map::iterator iter = m_multisampleFrameBuffers.begin(); iter != m_multisampleFrameBuffers.end(); ++iter) - staleFrameBuffers.emplace(iter->first, iter->second); + for (auto& [contextId, multisampleFrameBufferId] : m_multisampleFrameBuffers) + staleFrameBuffers.emplace(contextId, multisampleFrameBufferId); // Clean up FBOs destroyStaleFBOs(); @@ -542,26 +542,26 @@ bool RenderTextureImplFBO::activate(bool active) { Lock lock(mutex); - std::unordered_map::iterator iter; + std::unordered_map::iterator it; if (m_multisample) { - iter = m_multisampleFrameBuffers.find(contextId); + it = m_multisampleFrameBuffers.find(contextId); - if (iter != m_multisampleFrameBuffers.end()) + if (it != m_multisampleFrameBuffers.end()) { - glCheck(GLEXT_glBindFramebuffer(GLEXT_GL_FRAMEBUFFER, iter->second)); + glCheck(GLEXT_glBindFramebuffer(GLEXT_GL_FRAMEBUFFER, it->second)); return true; } } else { - iter = m_frameBuffers.find(contextId); + it = m_frameBuffers.find(contextId); - if (iter != m_frameBuffers.end()) + if (it != m_frameBuffers.end()) { - glCheck(GLEXT_glBindFramebuffer(GLEXT_GL_FRAMEBUFFER, iter->second)); + glCheck(GLEXT_glBindFramebuffer(GLEXT_GL_FRAMEBUFFER, it->second)); return true; } @@ -596,15 +596,15 @@ void RenderTextureImplFBO::updateTexture(unsigned int) Lock lock(mutex); - std::unordered_map::iterator iter = m_frameBuffers.find(contextId); - std::unordered_map::iterator multisampleIter = m_multisampleFrameBuffers.find(contextId); + auto frameBufferIt = m_frameBuffers.find(contextId); + auto multisampleIt = m_multisampleFrameBuffers.find(contextId); - if ((iter != m_frameBuffers.end()) && (multisampleIter != m_multisampleFrameBuffers.end())) + if ((frameBufferIt != m_frameBuffers.end()) && (multisampleIt != m_multisampleFrameBuffers.end())) { // Set up the blit target (draw framebuffer) and blit (from the read framebuffer, our multisample FBO) - glCheck(GLEXT_glBindFramebuffer(GLEXT_GL_DRAW_FRAMEBUFFER, iter->second)); + glCheck(GLEXT_glBindFramebuffer(GLEXT_GL_DRAW_FRAMEBUFFER, frameBufferIt->second)); glCheck(GLEXT_glBlitFramebuffer(0, 0, static_cast(m_width), static_cast(m_height), 0, 0, static_cast(m_width), static_cast(m_height), GL_COLOR_BUFFER_BIT, GL_NEAREST)); - glCheck(GLEXT_glBindFramebuffer(GLEXT_GL_DRAW_FRAMEBUFFER, multisampleIter->second)); + glCheck(GLEXT_glBindFramebuffer(GLEXT_GL_DRAW_FRAMEBUFFER, multisampleIt->second)); } } diff --git a/src/SFML/Graphics/Shader.cpp b/src/SFML/Graphics/Shader.cpp index ab4b86b4d..5f16020f6 100644 --- a/src/SFML/Graphics/Shader.cpp +++ b/src/SFML/Graphics/Shader.cpp @@ -552,7 +552,7 @@ void Shader::setUniform(const std::string& name, const Texture& texture) if (location != -1) { // Store the location -> texture mapping - TextureTable::iterator it = m_textures.find(location); + auto it = m_textures.find(location); if (it == m_textures.end()) { // New entry, make sure there are enough texture units @@ -970,7 +970,7 @@ bool Shader::compile(const char* vertexShaderCode, const char* geometryShaderCod //////////////////////////////////////////////////////////// void Shader::bindTextures() const { - TextureTable::const_iterator it = m_textures.begin(); + auto it = m_textures.begin(); for (std::size_t i = 0; i < m_textures.size(); ++i) { GLint index = static_cast(i + 1); @@ -989,8 +989,7 @@ void Shader::bindTextures() const int Shader::getUniformLocation(const std::string& name) { // Check the cache - UniformTable::const_iterator it = m_uniforms.find(name); - if (it != m_uniforms.end()) + if (auto it = m_uniforms.find(name); it != m_uniforms.end()) { // Already in cache, return it return it->second; diff --git a/src/SFML/Network/Http.cpp b/src/SFML/Network/Http.cpp index 9e9d0aea8..f6c2ff4e1 100644 --- a/src/SFML/Network/Http.cpp +++ b/src/SFML/Network/Http.cpp @@ -39,8 +39,9 @@ namespace // Convert a string to lower case std::string toLower(std::string str) { - for (std::string::iterator i = str.begin(); i != str.end(); ++i) - *i = static_cast(std::tolower(*i)); + for (char& c : str) + c = static_cast(std::tolower(c)); + return str; } } @@ -119,9 +120,9 @@ std::string Http::Request::prepare() const out << "HTTP/" << m_majorVersion << "." << m_minorVersion << "\r\n"; // Write fields - for (FieldTable::const_iterator i = m_fields.begin(); i != m_fields.end(); ++i) + for (const auto& [fieldKey, fieldValue] : m_fields) { - out << i->first << ": " << i->second << "\r\n"; + out << fieldKey << ": " << fieldValue << "\r\n"; } // Use an extra \r\n to separate the header from the body @@ -154,16 +155,13 @@ m_minorVersion(0) //////////////////////////////////////////////////////////// const std::string& Http::Response::getField(const std::string& field) const { - FieldTable::const_iterator it = m_fields.find(toLower(field)); - if (it != m_fields.end()) + if (auto it = m_fields.find(toLower(field)); it != m_fields.end()) { return it->second; } - else - { - static const std::string empty = ""; - return empty; - } + + static const std::string empty; + return empty; } diff --git a/src/SFML/Network/Packet.cpp b/src/SFML/Network/Packet.cpp index d1cd52abd..bdaf782a4 100644 --- a/src/SFML/Network/Packet.cpp +++ b/src/SFML/Network/Packet.cpp @@ -557,8 +557,8 @@ Packet& Packet::operator <<(const std::wstring& data) // Then insert characters if (length > 0) { - for (std::wstring::const_iterator c = data.begin(); c != data.end(); ++c) - *this << static_cast(*c); + for (wchar_t c : data) + *this << static_cast(c); } return *this; diff --git a/src/SFML/Window/GlContext.cpp b/src/SFML/Window/GlContext.cpp index c9b75a7eb..149d773e4 100644 --- a/src/SFML/Window/GlContext.cpp +++ b/src/SFML/Window/GlContext.cpp @@ -734,8 +734,8 @@ void GlContext::cleanupUnsharedResources() setActive(true); // Call the registered destruction callbacks - for (ContextDestroyCallbacks::iterator iter = contextDestroyCallbacks.begin(); iter != contextDestroyCallbacks.end(); ++iter) - iter->first(iter->second); + for (auto& [callback, ptr] : contextDestroyCallbacks) + callback(ptr); // Make the originally active context active again if (contextToRestore) diff --git a/src/SFML/Window/OSX/HIDInputManager.mm b/src/SFML/Window/OSX/HIDInputManager.mm index 2df085deb..1faa933e2 100644 --- a/src/SFML/Window/OSX/HIDInputManager.mm +++ b/src/SFML/Window/OSX/HIDInputManager.mm @@ -314,16 +314,19 @@ void HIDInputManager::freeUp() if (m_layoutData != 0) CFRelease(m_layoutData); + m_layoutData = 0; + // Do not release m_layout! It is owned by m_layoutData. if (m_manager != 0) CFRelease(m_manager); + m_manager = 0; for (unsigned int i = 0; i < Keyboard::KeyCount; ++i) { - for (IOHIDElements::iterator it = m_keys[i].begin(); it != m_keys[i].end(); ++it) - CFRelease(*it); + for (IOHIDElementRef iohidElementRef : m_keys[i]) + CFRelease(iohidElementRef); m_keys[i].clear(); } @@ -364,7 +367,7 @@ bool HIDInputManager::isPressed(IOHIDElements& elements) // state = true if at least one corresponding HID button is pressed bool state = false; - for (IOHIDElements::iterator it = elements.begin(); it != elements.end(); /* noop */) + for (auto it = elements.begin(); it != elements.end(); /* noop */) { IOHIDValueRef value = 0; diff --git a/src/SFML/Window/OSX/JoystickImpl.cpp b/src/SFML/Window/OSX/JoystickImpl.cpp index 041d175da..546940c00 100644 --- a/src/SFML/Window/OSX/JoystickImpl.cpp +++ b/src/SFML/Window/OSX/JoystickImpl.cpp @@ -307,10 +307,12 @@ bool JoystickImpl::open(unsigned int index) std::sort(m_buttons.begin(), m_buttons.end(), JoystickButtonSortPredicate); // Retain all these objects for personal use - for (ButtonsVector::iterator it(m_buttons.begin()); it != m_buttons.end(); ++it) - CFRetain(*it); - for (AxisMap::iterator it(m_axis.begin()); it != m_axis.end(); ++it) - CFRetain(it->second); + for (IOHIDElementRef iohidElementRef : m_buttons) + CFRetain(iohidElementRef); + + for (const auto& [axis, iohidElementRef] : m_axis) + CFRetain(iohidElementRef); + if (m_hat != nullptr) CFRetain(m_hat); @@ -329,16 +331,20 @@ bool JoystickImpl::open(unsigned int index) void JoystickImpl::close() { AutoreleasePool pool; - for (ButtonsVector::iterator it(m_buttons.begin()); it != m_buttons.end(); ++it) - CFRelease(*it); + + for (IOHIDElementRef iohidElementRef : m_buttons) + CFRelease(iohidElementRef); + m_buttons.clear(); - for (AxisMap::iterator it(m_axis.begin()); it != m_axis.end(); ++it) - CFRelease(it->second); + for (const auto& [axis, iohidElementRef] : m_axis) + CFRelease(iohidElementRef); + m_axis.clear(); if (m_hat != nullptr) CFRelease(m_hat); + m_hat = nullptr; // And we unregister this joystick @@ -356,8 +362,8 @@ JoystickCaps JoystickImpl::getCapabilities() const caps.buttonCount = m_buttons.size(); // Axis: - for (AxisMap::const_iterator it(m_axis.begin()); it != m_axis.end(); ++it) - caps.axes[it->first] = true; + for (const auto& [axis, iohidElementRef] : m_axis) + caps.axes[axis] = true; if (m_hat != nullptr) caps.axes[Joystick::PovX] = caps.axes[Joystick::PovY] = true; @@ -416,7 +422,7 @@ JoystickState JoystickImpl::update() // Update buttons' state unsigned int i = 0; - for (ButtonsVector::iterator it(m_buttons.begin()); it != m_buttons.end(); ++it, ++i) + for (auto it = m_buttons.begin(); it != m_buttons.end(); ++it, ++i) { IOHIDValueRef value = 0; IOHIDDeviceGetValue(IOHIDElementGetDevice(*it), *it, &value); @@ -432,10 +438,10 @@ JoystickState JoystickImpl::update() } // Update axes' state - for (AxisMap::iterator it = m_axis.begin(); it != m_axis.end(); ++it) + for (const auto& [axis, iohidElementRef] : m_axis) { IOHIDValueRef value = 0; - IOHIDDeviceGetValue(IOHIDElementGetDevice(it->second), it->second, &value); + IOHIDDeviceGetValue(IOHIDElementGetDevice(iohidElementRef), iohidElementRef, &value); // Check for plug out. if (!value) @@ -453,13 +459,13 @@ JoystickState JoystickImpl::update() // This method might not be very accurate (the "0 position" can be // slightly shift with some device) but we don't care because most // of devices are so sensitive that this is not relevant. - double physicalMax = IOHIDElementGetPhysicalMax(it->second); - double physicalMin = IOHIDElementGetPhysicalMin(it->second); + double physicalMax = IOHIDElementGetPhysicalMax(iohidElementRef); + double physicalMin = IOHIDElementGetPhysicalMin(iohidElementRef); double scaledMin = -100; double scaledMax = 100; double physicalValue = IOHIDValueGetScaledValue(value, kIOHIDValueScaleTypePhysical); float scaledValue = (((physicalValue - physicalMin) * (scaledMax - scaledMin)) / (physicalMax - physicalMin)) + scaledMin; - state.axes[it->first] = scaledValue; + state.axes[axis] = scaledValue; } // Update POV/Hat state. Assuming model described in `open`, values are: diff --git a/src/SFML/Window/Unix/Display.cpp b/src/SFML/Window/Unix/Display.cpp index fb6b3d5c1..f2e82fd31 100644 --- a/src/SFML/Window/Unix/Display.cpp +++ b/src/SFML/Window/Unix/Display.cpp @@ -89,10 +89,8 @@ void CloseDisplay(Display* display) //////////////////////////////////////////////////////////// Atom getAtom(const std::string& name, bool onlyIfExists) { - AtomMap::const_iterator iter = atoms.find(name); - - if (iter != atoms.end()) - return iter->second; + if (auto it = atoms.find(name); it != atoms.end()) + return it->second; Display* display = OpenDisplay(); diff --git a/src/SFML/Window/Unix/JoystickImpl.cpp b/src/SFML/Window/Unix/JoystickImpl.cpp index 1af2ef259..013a089d1 100644 --- a/src/SFML/Window/Unix/JoystickImpl.cpp +++ b/src/SFML/Window/Unix/JoystickImpl.cpp @@ -125,30 +125,30 @@ namespace // Since isJoystick returned true, this has to succeed const char* devnode = udev_device_get_devnode(udevDevice); - JoystickList::iterator record; + JoystickList::iterator recordIt; - for (record = joystickList.begin(); record != joystickList.end(); ++record) + for (recordIt = joystickList.begin(); recordIt != joystickList.end(); ++recordIt) { - if (record->deviceNode == devnode) + if (recordIt->deviceNode == devnode) { if (std::strstr(action, "add")) { // The system path might have changed so update it const char* syspath = udev_device_get_syspath(udevDevice); - record->plugged = true; - record->systemPath = syspath ? syspath : ""; + recordIt->plugged = true; + recordIt->systemPath = syspath ? syspath : ""; break; } else if (std::strstr(action, "remove")) { - record->plugged = false; + recordIt->plugged = false; break; } } } - if (record == joystickList.end()) + if (recordIt == joystickList.end()) { if (std::strstr(action, "add")) { @@ -177,8 +177,8 @@ namespace } // Reset the plugged status of each mapping since we are doing a full rescan - for (JoystickList::iterator record = joystickList.begin(); record != joystickList.end(); ++record) - record->plugged = false; + for (JoystickRecord& record : joystickList) + record.plugged = false; udev_enumerate* udevEnumerator = udev_enumerate_new(udevContext); @@ -218,27 +218,27 @@ namespace // Since isJoystick returned true, this has to succeed const char* devnode = udev_device_get_devnode(newUdevDevice); - JoystickList::iterator record; + JoystickList::iterator recordIt; // Check if the device node has been mapped before - for (record = joystickList.begin(); record != joystickList.end(); ++record) + for (recordIt = joystickList.begin(); recordIt != joystickList.end(); ++recordIt) { - if (record->deviceNode == devnode) + if (recordIt->deviceNode == devnode) { - record->plugged = true; + recordIt->plugged = true; break; } } // If not mapped before, map it now - if (record == joystickList.end()) + if (recordIt == joystickList.end()) { - JoystickRecord nweRecord; - nweRecord.deviceNode = devnode; - nweRecord.systemPath = syspath; - nweRecord.plugged = true; + JoystickRecord newRecord; + newRecord.deviceNode = devnode; + newRecord.systemPath = syspath; + newRecord.plugged = true; - joystickList.push_back(nweRecord); + joystickList.push_back(newRecord); } } diff --git a/src/SFML/Window/Unix/VulkanImplX11.cpp b/src/SFML/Window/Unix/VulkanImplX11.cpp index 4ad12e9cb..ab1370a30 100644 --- a/src/SFML/Window/Unix/VulkanImplX11.cpp +++ b/src/SFML/Window/Unix/VulkanImplX11.cpp @@ -143,13 +143,13 @@ bool VulkanImplX11::isAvailable(bool requireGraphics) bool has_VK_KHR_surface = false; bool has_VK_KHR_platform_surface = false; - for (std::vector::const_iterator iter = extensionProperties.begin(); iter != extensionProperties.end(); ++iter) + for (const VkExtensionProperties& properties : extensionProperties) { - if (!std::strcmp(iter->extensionName, VK_KHR_SURFACE_EXTENSION_NAME)) + if (!std::strcmp(properties.extensionName, VK_KHR_SURFACE_EXTENSION_NAME)) { has_VK_KHR_surface = true; } - else if (!std::strcmp(iter->extensionName, VK_KHR_XLIB_SURFACE_EXTENSION_NAME)) + else if (!std::strcmp(properties.extensionName, VK_KHR_XLIB_SURFACE_EXTENSION_NAME)) { has_VK_KHR_platform_surface = true; } diff --git a/src/SFML/Window/Unix/WindowImplX11.cpp b/src/SFML/Window/Unix/WindowImplX11.cpp index 514a8e683..68bd38c09 100644 --- a/src/SFML/Window/Unix/WindowImplX11.cpp +++ b/src/SFML/Window/Unix/WindowImplX11.cpp @@ -1200,9 +1200,9 @@ void WindowImplX11::requestFocus() { Lock lock(allWindowsMutex); - for (std::vector::iterator itr = allWindows.begin(); itr != allWindows.end(); ++itr) + for (sf::priv::WindowImplX11* windowPtr : allWindows) { - if ((*itr)->hasFocus()) + if (windowPtr->hasFocus()) { sfmlWindowFocused = true; break; @@ -1733,17 +1733,17 @@ bool WindowImplX11::processEvent(XEvent& windowEvent) if (windowEvent.type == KeyRelease) { // Find the next KeyPress event with matching keycode and time - std::deque::iterator iter = std::find_if( + auto it = std::find_if( m_events.begin(), m_events.end(), KeyRepeatFinder(windowEvent.xkey.keycode, windowEvent.xkey.time) ); - if (iter != m_events.end()) + if (it != m_events.end()) { // If we don't want repeated events, remove the next KeyPress from the queue if (!m_keyRepeat) - m_events.erase(iter); + m_events.erase(it); // This KeyRelease is a repeated event and we don't want it return false; diff --git a/src/SFML/Window/Win32/JoystickImpl.cpp b/src/SFML/Window/Win32/JoystickImpl.cpp index 78879ca4b..86ac1baa4 100644 --- a/src/SFML/Window/Win32/JoystickImpl.cpp +++ b/src/SFML/Window/Win32/JoystickImpl.cpp @@ -451,9 +451,9 @@ void JoystickImpl::cleanupDInput() bool JoystickImpl::isConnectedDInput(unsigned int index) { // Check if a joystick with the given index is in the connected list - for (std::vector::iterator i = joystickList.begin(); i != joystickList.end(); ++i) + for (const JoystickRecord& record : joystickList) { - if (i->index == index) + if (record.index == index) return true; } @@ -472,7 +472,7 @@ void JoystickImpl::updateConnectionsDInput() HRESULT result = directInput->EnumDevices(DI8DEVCLASS_GAMECTRL, &JoystickImpl::deviceEnumerationCallback, nullptr, DIEDFL_ATTACHEDONLY); // Remove devices that were not connected during the enumeration - for (std::vector::iterator i = joystickList.begin(); i != joystickList.end();) + for (auto i = joystickList.begin(); i != joystickList.end();) { if (!i->plugged) i = joystickList.erase(i); @@ -490,14 +490,14 @@ void JoystickImpl::updateConnectionsDInput() // Assign unused joystick indices to devices that were newly connected for (unsigned int i = 0; i < Joystick::Count; ++i) { - for (std::vector::iterator j = joystickList.begin(); j != joystickList.end(); ++j) + for (JoystickRecord& record : joystickList) { - if (j->index == i) + if (record.index == i) break; - if (j->index == Joystick::Count) + if (record.index == Joystick::Count) { - j->index = i; + record.index = i; break; } } @@ -523,12 +523,12 @@ bool JoystickImpl::openDInput(unsigned int index) m_buffered = false; // Search for a joystick with the given index in the connected list - for (std::vector::iterator it = joystickList.begin(); it != joystickList.end(); ++it) + for (const JoystickRecord& record : joystickList) { - if (it->index == index) + if (record.index == index) { // Create device - HRESULT result = directInput->CreateDevice(it->guid, &m_device, nullptr); + HRESULT result = directInput->CreateDevice(record.guid, &m_device, nullptr); if (FAILED(result)) { @@ -552,10 +552,10 @@ bool JoystickImpl::openDInput(unsigned int index) // Check if device is already blacklisted if (m_identification.productId && m_identification.vendorId) { - for (JoystickBlacklist::const_iterator iter = joystickBlacklist.begin(); iter != joystickBlacklist.end(); ++iter) + for (const JoystickBlacklistEntry& blacklistEntry : joystickBlacklist) { - if ((m_identification.productId == iter->productId) && - (m_identification.vendorId == iter->vendorId)) + if ((m_identification.productId == blacklistEntry.productId) && + (m_identification.vendorId == blacklistEntry.vendorId)) { // Device is blacklisted m_device->Release(); diff --git a/src/SFML/Window/Win32/VulkanImplWin32.cpp b/src/SFML/Window/Win32/VulkanImplWin32.cpp index 5d675592f..5f17933f8 100644 --- a/src/SFML/Window/Win32/VulkanImplWin32.cpp +++ b/src/SFML/Window/Win32/VulkanImplWin32.cpp @@ -145,13 +145,13 @@ bool VulkanImplWin32::isAvailable(bool requireGraphics) bool has_VK_KHR_surface = false; bool has_VK_KHR_platform_surface = false; - for (std::vector::const_iterator iter = extensionProperties.begin(); iter != extensionProperties.end(); ++iter) + for (const VkExtensionProperties& properties : extensionProperties) { - if (!std::strcmp(iter->extensionName, VK_KHR_SURFACE_EXTENSION_NAME)) + if (!std::strcmp(properties.extensionName, VK_KHR_SURFACE_EXTENSION_NAME)) { has_VK_KHR_surface = true; } - else if (!std::strcmp(iter->extensionName, VK_KHR_WIN32_SURFACE_EXTENSION_NAME)) + else if (!std::strcmp(properties.extensionName, VK_KHR_WIN32_SURFACE_EXTENSION_NAME)) { has_VK_KHR_platform_surface = true; }