Skip to content

Commit

Permalink
3751: Convert for loops with pairs to structured bindings (TrenchBroo…
Browse files Browse the repository at this point in the history
…m#3752)

* 3751: convert for loops with pairs to structured bindings

* 3751: disable gcc7 unused variable warning - it warns about structured bindings

* 3751: also add -Wno-unused-variable to lib/kdl

* 3751: fix typo
  • Loading branch information
ericwa authored Feb 23, 2021
1 parent f5e7a11 commit b63cd06
Show file tree
Hide file tree
Showing 27 changed files with 85 additions and 141 deletions.
5 changes: 5 additions & 0 deletions cmake/Utils.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,11 @@ macro(set_compiler_config TARGET)

# FIXME: enable -Wcpp once we found a workaround for glew / QOpenGLWindow problem, see RenderView.h
target_compile_options(${TARGET} PRIVATE -Wno-cpp)

# gcc <= 7 warns about unused structured bindings, see https://github.com/TrenchBroom/TrenchBroom/issues/3751
if (CMAKE_CXX_COMPILER_VERSION VERSION_LESS 8)
target_compile_options(${TARGET} PRIVATE -Wno-unused-variable)
endif()
elseif(COMPILER_IS_MSVC)
target_compile_definitions(${TARGET} PRIVATE _CRT_SECURE_NO_DEPRECATE _CRT_NONSTDC_NO_DEPRECATE)
target_compile_options(${TARGET} PRIVATE /W4 /EHsc /MP)
Expand Down
3 changes: 1 addition & 2 deletions common/src/Assets/EntityModelManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -172,8 +172,7 @@ namespace TrenchBroom {

void EntityModelManager::resetTextureMode() {
if (m_resetTextureMode) {
for (const auto& entry : m_models) {
auto& model = entry.second;
for (const auto& [path, model] : m_models) {
model->setTextureMode(m_minFilter, m_magFilter);
}
m_resetTextureMode = false;
Expand Down
3 changes: 1 addition & 2 deletions common/src/EL/Expression.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -280,8 +280,7 @@ namespace TrenchBroom {
std::optional<LiteralExpression> MapExpression::optimize() {
bool allOptimized = true;

for (auto& entry : m_elements) {
auto& expression = entry.second;
for (auto& [key, expression] : m_elements) {
allOptimized &= expression.optimize();
}

Expand Down
6 changes: 3 additions & 3 deletions common/src/EL/Value.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -640,9 +640,9 @@ namespace TrenchBroom {
}

size_t i = 0;
for (const auto& entry : m) {
str << childIndent << "\"" << entry.first << "\"" << ": ";
entry.second.appendToStream(str, multiline, childIndent);
for (const auto& [key, value] : m) {
str << childIndent << "\"" << key << "\"" << ": ";
value.appendToStream(str, multiline, childIndent);
if (i++ < m.size() - 1) {
str << ",";
if (!multiline) {
Expand Down
8 changes: 4 additions & 4 deletions common/src/IO/ImageFileSystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -132,12 +132,12 @@ namespace TrenchBroom {
std::vector<Path> ImageFileSystemBase::Directory::contents() const {
std::vector<Path> contents;

for (const auto& entry : m_directories) {
contents.push_back(Path(entry.first));
for (const auto& [path, directory] : m_directories) {
contents.push_back(Path(path));
}

for (const auto& entry : m_files) {
contents.push_back(Path(entry.first));
for (const auto& [path, file] : m_files) {
contents.push_back(Path(path));
}

return contents;
Expand Down
5 changes: 1 addition & 4 deletions common/src/IO/MapReader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -401,10 +401,7 @@ namespace TrenchBroom {
* Resolves cases when a child is parsed before its parent; called after the whole map is parsed.
*/
void MapReader::resolveNodes(ParserStatus& status) {
for (const auto& entry : m_unresolvedNodes) {
Model::Node* node = entry.first;
const ParentInfo& info = entry.second;

for (const auto& [node, info] : m_unresolvedNodes) {
Model::Node* parent = resolveParent(info);
if (parent == nullptr)
onUnresolvedNode(info, node, status);
Expand Down
4 changes: 1 addition & 3 deletions common/src/IO/Parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,7 @@ namespace TrenchBroom {
m_tokenNames = tokenNames();

std::vector<std::string> names;
for (const auto& entry : m_tokenNames) {
const TokenType type = entry.first;
const std::string& name = entry.second;
for (const auto& [type, name] : m_tokenNames) {
if ((typeMask & type) != 0)
names.push_back(name);
}
Expand Down
11 changes: 5 additions & 6 deletions common/src/Model/ModelUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -148,8 +148,7 @@ namespace TrenchBroom {
template <typename T>
static std::vector<Node*> doCollectParents(const T& nodes) {
std::vector<Node*> result;
for (const auto& entry : nodes) {
Node* parent = entry.first;
for (const auto& [parent, children] : nodes) {
collectWithParents(parent, result);
}
return kdl::vec_sort_and_remove_duplicates(std::move(result));
Expand All @@ -165,16 +164,16 @@ namespace TrenchBroom {

std::vector<Node*> collectChildren(const std::map<Node*, std::vector<Node*>>& nodes) {
std::vector<Node*> result;
for (const auto& entry : nodes) {
result = kdl::vec_concat(std::move(result), entry.second);
for (const auto& [parent, children] : nodes) {
result = kdl::vec_concat(std::move(result), children);
}
return result;
}

std::vector<Node*> collectChildren(const std::vector<std::pair<Model::Node*, std::vector<std::unique_ptr<Model::Node>>>>& nodes) {
std::vector<Node*> result;
for (const auto& entry : nodes) {
result = kdl::vec_concat(std::move(result), kdl::vec_transform(entry.second, [](auto& child) { return child.get(); }));
for (const auto& [parent, children] : nodes) {
result = kdl::vec_concat(std::move(result), kdl::vec_transform(children, [](auto& child) { return child.get(); }));
}
return result;
}
Expand Down
5 changes: 1 addition & 4 deletions common/src/Model/Polyhedron_Matcher.h
Original file line number Diff line number Diff line change
Expand Up @@ -303,10 +303,7 @@ namespace TrenchBroom {
static VertexRelation buildVertexRelation(const P& left, const P& right, const VMap& vertexMap) {
VertexRelation result;

for (const auto& entry : vertexMap) {
const auto& leftPosition = entry.first;
const auto& rightPosition = entry.second;

for (const auto& [leftPosition, rightPosition] : vertexMap) {
auto* leftVertex = left.findVertexByPosition(leftPosition);
auto* rightVertex = right.findVertexByPosition(rightPosition);

Expand Down
5 changes: 1 addition & 4 deletions common/src/Renderer/EntityModelRenderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -143,14 +143,11 @@ namespace TrenchBroom {
glAssert(glEnable(GL_TEXTURE_2D));
glAssert(glActiveTexture(GL_TEXTURE0));

for (const auto& entry : m_entities) {
auto* entityNode = entry.first;
for (const auto& [entityNode, renderer] : m_entities) {
if (!m_showHiddenEntities && !m_editorContext.visible(entityNode)) {
continue;
}

auto* renderer = entry.second;

const auto transformation = entityNode->entity().modelTransformation();
MultiplyModelMatrix multMatrix(renderContext.transformation(), vm::mat4x4f(transformation));

Expand Down
3 changes: 1 addition & 2 deletions common/src/Renderer/FaceRenderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,7 @@ namespace TrenchBroom {
void FaceRenderer::prepareVerticesAndIndices(VboManager& vboManager) {
m_vertexArray->prepare(vboManager);

for (const auto& pair : *m_indexArrayMap) {
const auto& brushIndexHolderPtr = pair.second;
for (const auto& [texture, brushIndexHolderPtr] : *m_indexArrayMap) {
brushIndexHolderPtr->prepare(vboManager);
}
}
Expand Down
7 changes: 3 additions & 4 deletions common/src/Renderer/PointHandleRenderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,11 +88,10 @@ namespace TrenchBroom {
const Camera& camera = renderContext.camera();
ActiveShader shader(renderContext.shaderManager(), Shaders::HandleShader);

for (const auto& entry : map) {
const Color color = mixAlpha(entry.first, opacity);
shader.set("Color", color);
for (const auto& [color, positions] : map) {
shader.set("Color", mixAlpha(color, opacity));

for (const vm::vec3f& position : entry.second) {
for (const vm::vec3f& position : positions) {
vm::vec3f nudgeTowardsCamera;

// In 3D view, nudge towards camera by the handle radius, to prevent lines (brush edges, etc.) from clipping into the handle
Expand Down
16 changes: 4 additions & 12 deletions common/src/Renderer/PrimitiveRenderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -246,18 +246,14 @@ namespace TrenchBroom {
}

void PrimitiveRenderer::prepareLines(VboManager& vboManager) {
for (auto& entry : m_lineMeshes) {
const LineRenderAttributes& attributes = entry.first;
IndexRangeMapBuilder<Vertex::Type>& mesh = entry.second;
for (auto& [attributes, mesh] : m_lineMeshes) {
IndexRangeRenderer& renderer = m_lineMeshRenderers.insert(std::make_pair(attributes, IndexRangeRenderer(mesh))).first->second;
renderer.prepare(vboManager);
}
}

void PrimitiveRenderer::prepareTriangles(VboManager& vboManager) {
for (auto& entry : m_triangleMeshes) {
const TriangleRenderAttributes& attributes = entry.first;
IndexRangeMapBuilder<Vertex::Type>& mesh = entry.second;
for (auto& [attributes, mesh] : m_triangleMeshes) {
IndexRangeRenderer& renderer = m_triangleMeshRenderers.insert(std::make_pair(attributes, IndexRangeRenderer(mesh))).first->second;
renderer.prepare(vboManager);
}
Expand All @@ -271,9 +267,7 @@ namespace TrenchBroom {
void PrimitiveRenderer::renderLines(RenderContext& renderContext) {
ActiveShader shader(renderContext.shaderManager(), Shaders::VaryingPUniformCShader);

for (auto& entry : m_lineMeshRenderers) {
const LineRenderAttributes& attributes = entry.first;
IndexRangeRenderer& renderer = entry.second;
for (auto& [attributes, renderer] : m_lineMeshRenderers) {
attributes.render(renderer, shader);
}
glAssert(glLineWidth(1.0f))
Expand All @@ -282,9 +276,7 @@ namespace TrenchBroom {
void PrimitiveRenderer::renderTriangles(RenderContext& renderContext) {
ActiveShader shader(renderContext.shaderManager(), Shaders::VaryingPUniformCShader);

for (auto& entry : m_triangleMeshRenderers) {
const TriangleRenderAttributes& attributes = entry.first;
IndexRangeRenderer& renderer = entry.second;
for (auto& [attributes, renderer] : m_triangleMeshRenderers) {
attributes.render(renderer, shader);
}
}
Expand Down
17 changes: 4 additions & 13 deletions common/src/Renderer/TexturedIndexRangeMap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,7 @@ namespace TrenchBroom {
}

void TexturedIndexRangeMap::Size::inc(const TexturedIndexRangeMap::Size& other) {
for (const auto& entry : other.m_sizes) {
auto* texture = entry.first;
const auto& indexRange = entry.second;
for (const auto& [texture, indexRange] : other.m_sizes) {
auto& sizeForKey = findCurrent(texture);
sizeForKey.inc(indexRange);
}
Expand All @@ -61,9 +59,7 @@ namespace TrenchBroom {
}

void TexturedIndexRangeMap::Size::initialize(TextureToIndexRangeMap& data) const {
for (const auto& entry : m_sizes) {
const auto* texture = entry.first;
const auto& size = entry.second;
for (const auto& [texture, size] : m_sizes) {
data.insert(std::make_pair(texture, IndexRangeMap(size)));
}
}
Expand Down Expand Up @@ -100,9 +96,7 @@ namespace TrenchBroom {
}

void TexturedIndexRangeMap::add(const TexturedIndexRangeMap& other) {
for (const auto& entry : *other.m_data) {
auto* texture = entry.first;
const auto& indexRangeMap = entry.second;
for (const auto& [texture, indexRangeMap] : *other.m_data) {
auto& current = findCurrent(texture);
current.add(indexRangeMap);
}
Expand All @@ -114,10 +108,7 @@ namespace TrenchBroom {
}

void TexturedIndexRangeMap::render(VertexArray& vertexArray, TextureRenderFunc& func) {
for (const auto& entry : *m_data) {
const auto* texture = entry.first;
const auto& indexArray = entry.second;

for (const auto& [texture, indexArray] : *m_data) {
func.before(texture);
indexArray.render(vertexArray);
func.after(texture);
Expand Down
4 changes: 2 additions & 2 deletions common/src/View/BrushVertexCommands.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ namespace TrenchBroom {

static auto collectBrushNodes(const std::vector<std::pair<Model::Node*, Model::NodeContents>> nodes) {
auto result = std::vector<Model::BrushNode*>{};
for (const auto& pair : nodes) {
if (auto* brushNode = dynamic_cast<Model::BrushNode*>(pair.first)) {
for (const auto& [node, contents] : nodes) {
if (auto* brushNode = dynamic_cast<Model::BrushNode*>(node)) {
result.push_back(brushNode);
}
}
Expand Down
3 changes: 1 addition & 2 deletions common/src/View/ClipTool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -827,8 +827,7 @@ namespace TrenchBroom {
void ClipTool::addBrushesToRenderer(const std::map<Model::Node*, std::vector<Model::Node*>>& map, Renderer::BrushRenderer& renderer) {
std::vector<Model::BrushNode*> brushes;

for (const auto& entry : map) {
const auto& nodes = entry.second;
for (const auto& [parent, nodes] : map) {
for (auto* node : nodes) {
node->accept(kdl::overload(
[] (const Model::WorldNode*) {},
Expand Down
4 changes: 1 addition & 3 deletions common/src/View/EntityBrowserView.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -368,9 +368,7 @@ namespace TrenchBroom {

{ // create and upload all vertex arrays
const auto stringVertices = collectStringVertices(layout, y, height);
for (const auto& entry : stringVertices) {
const auto& fontDescriptor = entry.first;
const auto& vertices = entry.second;
for (const auto& [fontDescriptor, vertices] : stringVertices) {
stringRenderers[fontDescriptor] = Renderer::VertexArray::ref(vertices);
stringRenderers[fontDescriptor].prepare(vboManager());
}
Expand Down
4 changes: 2 additions & 2 deletions common/src/View/EntityPropertyEditor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -146,8 +146,8 @@ namespace TrenchBroom {
// Concatenate the flag descriptions and return.
QString result;
QTextStream stream(&result);
for (const auto& flagDescriptor : flagDescriptors) {
stream << flagDescriptor.second << "\n";
for (const auto& [value, description] : flagDescriptors) {
stream << description << "\n";
}
return result;
}
Expand Down
17 changes: 6 additions & 11 deletions common/src/View/MapDocument.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1039,8 +1039,8 @@ namespace TrenchBroom {

std::vector<std::string> getLinkedGroupIdsRecursively(const std::map<Model::Node*, std::vector<Model::Node*>>& parentChildrenMap) {
std::vector<std::string> linkedGroupIds;
for (const auto& pair : parentChildrenMap) {
Model::Node::visitAll(pair.second, kdl::overload(
for (const auto& [parent, children] : parentChildrenMap) {
Model::Node::visitAll(children, kdl::overload(
[] (auto&& thisLambda, const Model::WorldNode* worldNode) { worldNode->visitChildren(thisLambda); },
[] (auto&& thisLambda, const Model::LayerNode* layerNode) { layerNode->visitChildren(thisLambda); },
[&](auto&& thisLambda, const Model::GroupNode* groupNode) {
Expand Down Expand Up @@ -1089,8 +1089,7 @@ namespace TrenchBroom {

std::map<Model::Node*, std::vector<Model::Node*>> MapDocument::collectRemovableParents(const std::map<Model::Node*, std::vector<Model::Node*>>& nodes) const {
std::map<Model::Node*, std::vector<Model::Node*>> result;
for (const auto& entry : nodes) {
Model::Node* node = entry.first;
for (const auto& [node, children] : nodes) {
if (node->removeIfEmpty() && !node->hasChildren()) {
Model::Node* parent = node->parent();
ensure(parent != nullptr, "parent is null");
Expand Down Expand Up @@ -1126,8 +1125,7 @@ namespace TrenchBroom {
}

void MapDocument::closeRemovedGroups(const std::map<Model::Node*, std::vector<Model::Node*>>& toRemove) {
for (const auto& entry : toRemove) {
const std::vector<Model::Node*>& nodes = entry.second;
for (const auto& [parent, nodes] : toRemove) {
for (const Model::Node* node : nodes) {
if (node == currentGroup()) {
closeGroup();
Expand All @@ -1143,8 +1141,7 @@ namespace TrenchBroom {
return false;

std::map<Model::Node*, std::vector<Model::Node*>> nodesToRemove;
for (const auto& entry : nodesToAdd) {
const std::vector<Model::Node*>& children = entry.second;
for (const auto& [newParent, children] : nodesToAdd) {
nodesToRemove = kdl::map_merge(nodesToRemove, Model::parentChildrenMap(children));
}

Expand Down Expand Up @@ -1181,9 +1178,7 @@ namespace TrenchBroom {
}

bool MapDocument::checkReparenting(const std::map<Model::Node*, std::vector<Model::Node*>>& nodesToAdd) const {
for (const auto& entry : nodesToAdd) {
const Model::Node* newParent = entry.first;
const std::vector<Model::Node*>& children = entry.second;
for (const auto& [newParent, children] : nodesToAdd) {
if (!newParent->canAddChildren(std::begin(children), std::end(children)))
return false;
}
Expand Down
Loading

0 comments on commit b63cd06

Please sign in to comment.