From 0b57ee3a9f7a83e2f85ecd1a7c01ff5eb745fa47 Mon Sep 17 00:00:00 2001 From: Riyyi Date: Thu, 11 Mar 2021 11:37:46 +0100 Subject: [PATCH] Remove const ref std::shared_ptr Taking a const std::shared_ptr& doesnt make sense, as it doesnt increase the reference count and the object inside the pointer is still modifiable. Return std::shared_ptr by value, returning by reference wont increase the reference count and RVO (return value optimization) makes the const concern moot. --- inferno/src/inferno/render/buffer.cpp | 14 +++++++------- inferno/src/inferno/render/buffer.h | 6 +++--- inferno/src/inferno/render/font.cpp | 4 ++-- inferno/src/inferno/render/font.h | 10 +++++----- inferno/src/inferno/render/gltf.h | 4 ++-- inferno/src/inferno/render/renderer.cpp | 8 ++++---- inferno/src/inferno/render/renderer.h | 2 +- inferno/src/inferno/render/shader.cpp | 4 ++-- inferno/src/inferno/render/shader.h | 4 ++-- inferno/src/inferno/render/texture.cpp | 4 ++-- inferno/src/inferno/render/texture.h | 4 ++-- inferno/src/inferno/system/camerasystem.h | 5 +++-- inferno/src/inferno/system/rendersystem.h | 2 +- inferno/src/inferno/system/transformsystem.h | 2 +- inferno/src/inferno/window.h | 2 +- 15 files changed, 38 insertions(+), 37 deletions(-) diff --git a/inferno/src/inferno/render/buffer.cpp b/inferno/src/inferno/render/buffer.cpp index 8e0976e..3e5ac35 100644 --- a/inferno/src/inferno/render/buffer.cpp +++ b/inferno/src/inferno/render/buffer.cpp @@ -143,10 +143,10 @@ namespace Inferno { // ----------------------------------------- - BufferLayout::BufferLayout(const std::initializer_list& elements) : - m_elements(elements), m_stride(0) + BufferLayout::BufferLayout(const std::initializer_list& elements) + : m_elements(elements), m_stride(0) { - this->calculateOffsetsAndStride(); + calculateOffsetsAndStride(); } void BufferLayout::calculateOffsetsAndStride() @@ -258,7 +258,7 @@ namespace Inferno { glBindVertexArray(0); } - void VertexArray::addVertexBuffer(const std::shared_ptr& vertexBuffer) + void VertexArray::addVertexBuffer(std::shared_ptr vertexBuffer) { const auto& layout = vertexBuffer->getLayout(); ASSERT(layout.getElements().size(), "VertexBuffer has no layout"); @@ -279,18 +279,18 @@ namespace Inferno { index++; } - m_vertexBuffers.push_back(vertexBuffer); + m_vertexBuffers.push_back(std::move(vertexBuffer)); unbind(); vertexBuffer->unbind(); } - void VertexArray::setIndexBuffer(const std::shared_ptr& indexBuffer) + void VertexArray::setIndexBuffer(std::shared_ptr indexBuffer) { bind(); indexBuffer->bind(); - m_indexBuffer = indexBuffer; + m_indexBuffer = std::move(indexBuffer); unbind(); indexBuffer->unbind(); diff --git a/inferno/src/inferno/render/buffer.h b/inferno/src/inferno/render/buffer.h index 1d6b08d..abd43e3 100644 --- a/inferno/src/inferno/render/buffer.h +++ b/inferno/src/inferno/render/buffer.h @@ -132,11 +132,11 @@ namespace Inferno { void bind() const; void unbind() const; - void addVertexBuffer(const std::shared_ptr& vertexBuffer); - void setIndexBuffer(const std::shared_ptr& indexBuffer); + void addVertexBuffer(std::shared_ptr vertexBuffer); + void setIndexBuffer(std::shared_ptr indexBuffer); inline const std::vector>& getVertexBuffers() const { return m_vertexBuffers; } - inline const std::shared_ptr& getIndexBuffer() const { return m_indexBuffer; } + inline std::shared_ptr getIndexBuffer() const { return m_indexBuffer; } private: uint32_t m_id; diff --git a/inferno/src/inferno/render/font.cpp b/inferno/src/inferno/render/font.cpp index 96c92a5..499546a 100644 --- a/inferno/src/inferno/render/font.cpp +++ b/inferno/src/inferno/render/font.cpp @@ -112,7 +112,7 @@ namespace Inferno { { } - void FontManager::add(const std::string& name, const std::shared_ptr& font) + void FontManager::add(const std::string& name, std::shared_ptr font) { // Construct (key, value) pair and insert it into the unordered_map m_fontList.emplace(std::move(name), std::move(font)); @@ -146,7 +146,7 @@ namespace Inferno { } } - void FontManager::remove(const std::shared_ptr& font) + void FontManager::remove(std::shared_ptr font) { if (exists(font->name())) { m_fontList.erase(font->name()); diff --git a/inferno/src/inferno/render/font.h b/inferno/src/inferno/render/font.h index 0b9abbf..068da83 100644 --- a/inferno/src/inferno/render/font.h +++ b/inferno/src/inferno/render/font.h @@ -33,10 +33,10 @@ class Texture; inline std::string name() const { return m_name; } inline uint32_t size() const { return m_size; } - inline const std::shared_ptr& texture() const { return m_texture; } + inline std::shared_ptr texture() const { return m_texture; } - inline const std::shared_ptr& get(unsigned char c) const { return m_characterList.at(c); } - inline const std::shared_ptr& operator[](unsigned char c) const { return m_characterList.at(c); } + inline std::shared_ptr get(unsigned char c) const { return m_characterList.at(c); } + inline std::shared_ptr operator[](unsigned char c) const { return m_characterList.at(c); } private: void parseFont(const std::string& font); @@ -57,13 +57,13 @@ class Texture; FontManager(s); virtual ~FontManager(); - void add(const std::string& name, const std::shared_ptr& font); + void add(const std::string& name, std::shared_ptr font); std::shared_ptr load(const std::string& name); std::shared_ptr get(const std::string& name); bool exists(const std::string& name); void remove(const std::string& name); - void remove(const std::shared_ptr& font); + void remove(std::shared_ptr font); private: std::unordered_map> m_fontList; diff --git a/inferno/src/inferno/render/gltf.h b/inferno/src/inferno/render/gltf.h index 98186d3..3101d13 100644 --- a/inferno/src/inferno/render/gltf.h +++ b/inferno/src/inferno/render/gltf.h @@ -134,13 +134,13 @@ namespace Inferno { GltfManager(s) {} virtual ~GltfManager() {} - void add(const std::string& path, const std::shared_ptr& gltf); + void add(const std::string& path, std::shared_ptr gltf); std::shared_ptr load(const std::string& path); std::shared_ptr get(const std::string& path); bool exists(const std::string& path); void remove(const std::string& path); - void remove(const std::shared_ptr& gltf); + void remove(std::shared_ptr gltf); private: std::unordered_map> m_gltfList; diff --git a/inferno/src/inferno/render/renderer.cpp b/inferno/src/inferno/render/renderer.cpp index 46e53dc..3a4888b 100644 --- a/inferno/src/inferno/render/renderer.cpp +++ b/inferno/src/inferno/render/renderer.cpp @@ -36,9 +36,9 @@ namespace Inferno { glClearColor(color.r, color.g, color.b, color.a); } - void RenderCommand::drawIndexed(const std::shared_ptr& vertexArray, uint32_t indexCount) + void RenderCommand::drawIndexed(const VertexArray& vertexArray, uint32_t indexCount) { - uint32_t count = indexCount ? indexCount : vertexArray->getIndexBuffer()->getCount(); + uint32_t count = indexCount ? indexCount : vertexArray.getIndexBuffer()->getCount(); glDrawElements(GL_TRIANGLES, count, GL_UNSIGNED_INT, nullptr); } @@ -281,7 +281,7 @@ namespace Inferno { bind(); // Render - RenderCommand::drawIndexed(m_vertexArray, m_quadIndex * indexPerQuad); + RenderCommand::drawIndexed(*m_vertexArray, m_quadIndex * indexPerQuad); unbind(); } @@ -420,7 +420,7 @@ namespace Inferno { // Render bool depthTest = RenderCommand::depthTest(); RenderCommand::setDepthTest(false); - RenderCommand::drawIndexed(m_vertexArray, m_quadIndex * indexPerQuad); + RenderCommand::drawIndexed(*m_vertexArray, m_quadIndex * indexPerQuad); RenderCommand::setDepthTest(depthTest); unbind(); diff --git a/inferno/src/inferno/render/renderer.h b/inferno/src/inferno/render/renderer.h index 19929f0..43e18dd 100644 --- a/inferno/src/inferno/render/renderer.h +++ b/inferno/src/inferno/render/renderer.h @@ -48,7 +48,7 @@ namespace Inferno { static void clear(); static void clearColor(const glm::vec4& color); - static void drawIndexed(const std::shared_ptr& vertexArray, uint32_t indexCount = 0); + static void drawIndexed(const VertexArray& vertexArray, uint32_t indexCount = 0); static void setViewport(int32_t x, int32_t y, uint32_t width, uint32_t height); static void setDepthTest(bool enabled); diff --git a/inferno/src/inferno/render/shader.cpp b/inferno/src/inferno/render/shader.cpp index dfca699..963f468 100644 --- a/inferno/src/inferno/render/shader.cpp +++ b/inferno/src/inferno/render/shader.cpp @@ -202,7 +202,7 @@ namespace Inferno { { } - void ShaderManager::add(const std::string& name, const std::shared_ptr& shader) + void ShaderManager::add(const std::string& name, std::shared_ptr shader) { // Construct (key, value) pair and insert it into the unordered_map m_shaderList.emplace(std::move(name), std::move(shader)); @@ -243,7 +243,7 @@ namespace Inferno { } } - void ShaderManager::remove(const std::shared_ptr& shader) + void ShaderManager::remove(std::shared_ptr shader) { if (exists(shader->name())) { m_shaderList.erase(shader->name()); diff --git a/inferno/src/inferno/render/shader.h b/inferno/src/inferno/render/shader.h index 6330261..4b70a81 100644 --- a/inferno/src/inferno/render/shader.h +++ b/inferno/src/inferno/render/shader.h @@ -52,7 +52,7 @@ namespace Inferno { ShaderManager(s); virtual ~ShaderManager(); - void add(const std::string& name, const std::shared_ptr& shader); + void add(const std::string& name, std::shared_ptr shader); std::shared_ptr load(const std::string& name); std::shared_ptr load(const std::string& vertexSource, const std::string& fragmentSource); @@ -60,7 +60,7 @@ namespace Inferno { bool exists(const std::string& name); void remove(const std::string& name); - void remove(const std::shared_ptr& shader); + void remove(std::shared_ptr shader); protected: std::string computeName(const std::string& vertexSource, diff --git a/inferno/src/inferno/render/texture.cpp b/inferno/src/inferno/render/texture.cpp index e68220a..30e3bec 100644 --- a/inferno/src/inferno/render/texture.cpp +++ b/inferno/src/inferno/render/texture.cpp @@ -112,7 +112,7 @@ namespace Inferno { { } - void TextureManager::add(const std::string& path, const std::shared_ptr& texture) + void TextureManager::add(const std::string& path, std::shared_ptr texture) { // Construct (key, value) pair and insert it into the unordered_map m_textureList.emplace(std::move(path), std::move(texture)); @@ -146,7 +146,7 @@ namespace Inferno { } } - void TextureManager::remove(const std::shared_ptr& texture) + void TextureManager::remove(std::shared_ptr texture) { if (exists(texture->path())) { m_textureList.erase(texture->path()); diff --git a/inferno/src/inferno/render/texture.h b/inferno/src/inferno/render/texture.h index 12f8f3e..41aa545 100644 --- a/inferno/src/inferno/render/texture.h +++ b/inferno/src/inferno/render/texture.h @@ -44,13 +44,13 @@ namespace Inferno { TextureManager(s); virtual ~TextureManager(); - void add(const std::string& path, const std::shared_ptr& texture); + void add(const std::string& path, std::shared_ptr texture); std::shared_ptr load(const std::string& path); std::shared_ptr get(const std::string& path); bool exists(const std::string& path); void remove(const std::string& path); - void remove(const std::shared_ptr& texture); + void remove(std::shared_ptr texture); private: std::unordered_map> m_textureList; diff --git a/inferno/src/inferno/system/camerasystem.h b/inferno/src/inferno/system/camerasystem.h index ed2bb6a..3af416b 100644 --- a/inferno/src/inferno/system/camerasystem.h +++ b/inferno/src/inferno/system/camerasystem.h @@ -3,7 +3,8 @@ #include //std::shared_ptr -#include "entt/entity/registry.hpp" // entt::entity, entt::registry +#include "entt/entity/registry.hpp" // entt::entity, entt::registry +#include "glm/ext/matrix_float4x4.hpp" // glm::mat4 #include "inferno/singleton.h" @@ -24,7 +25,7 @@ namespace Inferno { glm::mat4 projectionView(); - void setRegistry(const std::shared_ptr& registry) { m_registry = registry; }; + void setRegistry(std::shared_ptr registry) { m_registry = registry; }; private: void updateOrthographic(TransformComponent& transform, CameraComponent& camera); diff --git a/inferno/src/inferno/system/rendersystem.h b/inferno/src/inferno/system/rendersystem.h index edd643c..103cc6a 100644 --- a/inferno/src/inferno/system/rendersystem.h +++ b/inferno/src/inferno/system/rendersystem.h @@ -17,7 +17,7 @@ namespace Inferno { void render(); - void setRegistry(const std::shared_ptr& registry) { m_registry = registry; }; + void setRegistry(std::shared_ptr registry) { m_registry = registry; }; private: std::shared_ptr m_registry; diff --git a/inferno/src/inferno/system/transformsystem.h b/inferno/src/inferno/system/transformsystem.h index 260fcc1..a5acc12 100644 --- a/inferno/src/inferno/system/transformsystem.h +++ b/inferno/src/inferno/system/transformsystem.h @@ -15,7 +15,7 @@ namespace Inferno { void update(); - void setRegistry(const std::shared_ptr& registry) { m_registry = registry; }; + void setRegistry(std::shared_ptr registry) { m_registry = registry; }; private: std::shared_ptr m_registry; diff --git a/inferno/src/inferno/window.h b/inferno/src/inferno/window.h index 69f51dd..5a9243d 100644 --- a/inferno/src/inferno/window.h +++ b/inferno/src/inferno/window.h @@ -47,7 +47,7 @@ namespace Inferno { inline uint32_t getHeight() const { return m_properties.height; } inline GLFWwindow* getWindow() const { return m_window; } - inline const std::shared_ptr& getContext() const { return m_context; } + inline std::shared_ptr getContext() const { return m_context; } inline void setEventCallback(const std::function& callback) { m_eventCallback = callback; }