From 7892541383ce69000da0d6643c5dfbcfc444b6b4 Mon Sep 17 00:00:00 2001 From: sfan5 Date: Sat, 1 Mar 2025 11:53:37 +0100 Subject: [PATCH] Various random code cleanups --- irr/include/irrMath.h | 58 +++++++-------------- irr/include/vector3d.h | 6 +++ irr/src/CMakeLists.txt | 9 ++++ irr/src/CNullDriver.cpp | 4 +- src/client/client.cpp | 14 ++--- src/client/content_mapblock.cpp | 2 +- src/client/event_manager.h | 4 +- src/client/game.cpp | 9 ++-- src/client/hud.cpp | 4 +- src/client/imagefilters.cpp | 4 +- src/client/mesh_generator_thread.cpp | 7 ++- src/client/minimap.cpp | 12 ++--- src/client/particles.cpp | 4 +- src/client/shadows/dynamicshadowsrender.cpp | 15 +++--- src/client/shadows/dynamicshadowsrender.h | 3 +- src/client/wieldmesh.cpp | 3 +- src/database/database-dummy.cpp | 3 +- src/database/database-files.cpp | 3 +- src/emerge.cpp | 3 +- src/face_position_cache.cpp | 2 +- src/gui/guiFormSpecMenu.cpp | 9 ++-- src/gui/guiHyperText.cpp | 6 +-- src/gui/guiTable.cpp | 7 ++- src/gui/profilergraph.cpp | 2 +- src/main.cpp | 2 +- src/map.cpp | 14 ++--- src/map.h | 2 +- src/mapgen/mapgen.cpp | 4 +- src/mapgen/mapgen.h | 4 +- src/mapgen/mapgen_carpathian.cpp | 2 +- src/mapgen/mapgen_carpathian.h | 2 +- src/mapgen/mapgen_flat.cpp | 2 +- src/mapgen/mapgen_flat.h | 2 +- src/mapgen/mapgen_fractal.cpp | 2 +- src/mapgen/mapgen_fractal.h | 2 +- src/mapgen/mapgen_v5.cpp | 2 +- src/mapgen/mapgen_v5.h | 2 +- src/mapgen/mapgen_v6.cpp | 2 +- src/mapgen/mapgen_v6.h | 2 +- src/mapgen/mapgen_v7.cpp | 2 +- src/mapgen/mapgen_v7.h | 2 +- src/mapgen/mapgen_valleys.cpp | 2 +- src/mapgen/mapgen_valleys.h | 2 +- src/mapgen/mg_decoration.cpp | 2 +- src/mapgen/mg_decoration.h | 2 +- src/mapgen/mg_ore.cpp | 4 +- src/mapgen/mg_ore.h | 2 +- src/mapgen/mg_schematic.cpp | 5 +- src/network/clientpackethandler.cpp | 2 +- src/nodedef.cpp | 3 +- src/nodetimer.cpp | 2 +- src/nodetimer.h | 8 ++- src/noise.cpp | 4 +- src/noise.h | 2 +- src/pathfinder.cpp | 8 ++- src/script/common/c_content.cpp | 11 ++-- src/script/common/c_content.h | 10 ++-- src/script/lua_api/l_craft.cpp | 4 +- src/script/lua_api/l_mapgen.cpp | 2 +- src/script/lua_api/l_rollback.cpp | 5 +- src/server.cpp | 3 +- src/server/rollback.cpp | 12 ++--- src/serverenvironment.cpp | 7 ++- src/tileanimation.cpp | 4 +- src/unittest/test_random.cpp | 4 +- src/unittest/test_utilities.cpp | 4 +- src/unittest/test_voxelmanipulator.cpp | 6 +-- src/util/areastore.cpp | 4 +- src/util/areastore.h | 2 +- src/util/numeric.cpp | 22 +++----- src/util/numeric.h | 53 +++++++++---------- src/util/string.cpp | 41 +++++---------- src/util/thread.h | 16 +++--- 73 files changed, 216 insertions(+), 285 deletions(-) diff --git a/irr/include/irrMath.h b/irr/include/irrMath.h index 3a1471a02..e9c86156d 100644 --- a/irr/include/irrMath.h +++ b/irr/include/irrMath.h @@ -18,47 +18,38 @@ namespace core //! Rounding error constant often used when comparing f32 values. -const f32 ROUNDING_ERROR_f32 = 0.000001f; -const f64 ROUNDING_ERROR_f64 = 0.00000001; +constexpr f32 ROUNDING_ERROR_f32 = 0.000001f; +constexpr f64 ROUNDING_ERROR_f64 = 0.00000001; #ifdef PI // make sure we don't collide with a define #undef PI #endif //! Constant for PI. -const f32 PI = 3.14159265359f; - -//! Constant for reciprocal of PI. -const f32 RECIPROCAL_PI = 1.0f / PI; - -//! Constant for half of PI. -const f32 HALF_PI = PI / 2.0f; +constexpr f32 PI = M_PI; #ifdef PI64 // make sure we don't collide with a define #undef PI64 #endif //! Constant for 64bit PI. -const f64 PI64 = 3.1415926535897932384626433832795028841971693993751; - -//! Constant for 64bit reciprocal of PI. -const f64 RECIPROCAL_PI64 = 1.0 / PI64; +constexpr f64 PI64 = M_PI; //! 32bit Constant for converting from degrees to radians -const f32 DEGTORAD = PI / 180.0f; +constexpr f32 DEGTORAD = PI / 180.0f; //! 32bit constant for converting from radians to degrees (formally known as GRAD_PI) -const f32 RADTODEG = 180.0f / PI; +constexpr f32 RADTODEG = 180.0f / PI; //! 64bit constant for converting from degrees to radians (formally known as GRAD_PI2) -const f64 DEGTORAD64 = PI64 / 180.0; +constexpr f64 DEGTORAD64 = PI64 / 180.0; //! 64bit constant for converting from radians to degrees -const f64 RADTODEG64 = 180.0 / PI64; +constexpr f64 RADTODEG64 = 180.0 / PI64; //! Utility function to convert a radian value to degrees /** Provided as it can be clearer to write radToDeg(X) than RADTODEG * X \param radians The radians value to convert to degrees. */ -inline f32 radToDeg(f32 radians) +inline constexpr f32 radToDeg(f32 radians) { return RADTODEG * radians; } @@ -67,7 +58,7 @@ inline f32 radToDeg(f32 radians) /** Provided as it can be clearer to write radToDeg(X) than RADTODEG * X \param radians The radians value to convert to degrees. */ -inline f64 radToDeg(f64 radians) +inline constexpr f64 radToDeg(f64 radians) { return RADTODEG64 * radians; } @@ -76,7 +67,7 @@ inline f64 radToDeg(f64 radians) /** Provided as it can be clearer to write degToRad(X) than DEGTORAD * X \param degrees The degrees value to convert to radians. */ -inline f32 degToRad(f32 degrees) +inline constexpr f32 degToRad(f32 degrees) { return DEGTORAD * degrees; } @@ -85,44 +76,44 @@ inline f32 degToRad(f32 degrees) /** Provided as it can be clearer to write degToRad(X) than DEGTORAD * X \param degrees The degrees value to convert to radians. */ -inline f64 degToRad(f64 degrees) +inline constexpr f64 degToRad(f64 degrees) { return DEGTORAD64 * degrees; } -//! returns minimum of two values. Own implementation to get rid of the STL (VS6 problems) +//! returns minimum of two values. template inline const T &min_(const T &a, const T &b) { return a < b ? a : b; } -//! returns minimum of three values. Own implementation to get rid of the STL (VS6 problems) +//! returns minimum of three values. template inline const T &min_(const T &a, const T &b, const T &c) { return a < b ? min_(a, c) : min_(b, c); } -//! returns maximum of two values. Own implementation to get rid of the STL (VS6 problems) +//! returns maximum of two values. template inline const T &max_(const T &a, const T &b) { return a < b ? b : a; } -//! returns maximum of three values. Own implementation to get rid of the STL (VS6 problems) +//! returns maximum of three values. template inline const T &max_(const T &a, const T &b, const T &c) { return a < b ? max_(b, c) : max_(a, c); } -//! returns abs of two values. Own implementation to get rid of STL (VS6 problems) +//! returns abs of two values. template inline T abs_(const T &a) { - return a < (T)0 ? -a : a; + return std::abs(a); } //! returns linear interpolation of a and b with ratio t @@ -140,19 +131,6 @@ inline const T clamp(const T &value, const T &low, const T &high) return min_(max_(value, low), high); } -//! swaps the content of the passed parameters -// Note: We use the same trick as boost and use two template arguments to -// avoid ambiguity when swapping objects of an Irrlicht type that has not -// it's own swap overload. Otherwise we get conflicts with some compilers -// in combination with stl. -template -inline void swap(T1 &a, T2 &b) -{ - T1 c(a); - a = b; - b = c; -} - template inline T roundingError(); diff --git a/irr/include/vector3d.h b/irr/include/vector3d.h index a69f17e16..fd788e734 100644 --- a/irr/include/vector3d.h +++ b/irr/include/vector3d.h @@ -33,6 +33,12 @@ public: explicit constexpr vector3d(T n) : X(n), Y(n), Z(n) {} + template + constexpr static vector3d from(const vector3d &other) + { + return {static_cast(other.X), static_cast(other.Y), static_cast(other.Z)}; + } + // operators vector3d operator-() const { return vector3d(-X, -Y, -Z); } diff --git a/irr/src/CMakeLists.txt b/irr/src/CMakeLists.txt index 5ac78a17e..820af4fe9 100644 --- a/irr/src/CMakeLists.txt +++ b/irr/src/CMakeLists.txt @@ -33,6 +33,15 @@ if(CMAKE_CXX_COMPILER_ID MATCHES "^(GNU|Clang|AppleClang)$") elseif(MSVC) string(APPEND CMAKE_CXX_STANDARD_LIBRARIES " msvcrt.lib") # ???? fuck off + add_compile_definitions( + # Suppress some useless warnings + _CRT_SECURE_NO_DEPRECATE + # Get M_PI to work + _USE_MATH_DEFINES + # Don't define min/max macros in minwindef.h + NOMINMAX + ) + add_compile_options(/Zl) # Enable SSE for floating point math on 32-bit x86 by default diff --git a/irr/src/CNullDriver.cpp b/irr/src/CNullDriver.cpp index c87d5ae93..6f44fc4e6 100644 --- a/irr/src/CNullDriver.cpp +++ b/irr/src/CNullDriver.cpp @@ -1455,9 +1455,9 @@ void CNullDriver::setMaterialRendererName(u32 idx, const char *name) void CNullDriver::swapMaterialRenderers(u32 idx1, u32 idx2, bool swapNames) { if (idx1 < MaterialRenderers.size() && idx2 < MaterialRenderers.size()) { - irr::core::swap(MaterialRenderers[idx1].Renderer, MaterialRenderers[idx2].Renderer); + std::swap(MaterialRenderers[idx1].Renderer, MaterialRenderers[idx2].Renderer); if (swapNames) - irr::core::swap(MaterialRenderers[idx1].Name, MaterialRenderers[idx2].Name); + std::swap(MaterialRenderers[idx1].Name, MaterialRenderers[idx2].Name); } } diff --git a/src/client/client.cpp b/src/client/client.cpp index 3c7716cbd..08fd2a215 100644 --- a/src/client/client.cpp +++ b/src/client/client.cpp @@ -1044,8 +1044,8 @@ void Client::Send(NetworkPacket* pkt) // Will fill up 12 + 12 + 4 + 4 + 4 + 1 + 1 + 1 + 4 + 4 bytes void writePlayerPos(LocalPlayer *myplayer, ClientMap *clientMap, NetworkPacket *pkt, bool camera_inverted) { - v3f pf = myplayer->getPosition() * 100; - v3f sf = myplayer->getSpeed() * 100; + v3s32 position = v3s32::from(myplayer->getPosition() * 100); + v3s32 speed = v3s32::from(myplayer->getSpeed() * 100); s32 pitch = myplayer->getPitch() * 100; s32 yaw = myplayer->getYaw() * 100; u32 keyPressed = myplayer->control.getKeysPressed(); @@ -1056,9 +1056,6 @@ void writePlayerPos(LocalPlayer *myplayer, ClientMap *clientMap, NetworkPacket * f32 movement_speed = myplayer->control.movement_speed; f32 movement_dir = myplayer->control.movement_direction; - v3s32 position(pf.X, pf.Y, pf.Z); - v3s32 speed(sf.X, sf.Y, sf.Z); - /* Format: [0] v3s32 position*100 @@ -1755,12 +1752,7 @@ void Client::addUpdateMeshTaskWithEdge(v3s16 blockpos, bool ack_to_server, bool void Client::addUpdateMeshTaskForNode(v3s16 nodepos, bool ack_to_server, bool urgent) { - { - v3s16 p = nodepos; - infostream<<"Client::addUpdateMeshTaskForNode(): " - <<"("<light_source && (normal != v3s16(0, 0, 0)); - v3f normal2(normal.X, normal.Y, normal.Z); + v3f normal2 = v3f::from(normal); for (int j = 0; j < 4; j++) { vertices[j].Pos = coords[j] + cur_node.origin; vertices[j].Normal = normal2; diff --git a/src/client/event_manager.h b/src/client/event_manager.h index 35b36adce..1fee6781c 100644 --- a/src/client/event_manager.h +++ b/src/client/event_manager.h @@ -33,7 +33,7 @@ public: void put(MtEvent *e) override { - std::map::iterator i = m_dest.find(e->getType()); + auto i = m_dest.find(e->getType()); if (i != m_dest.end()) { std::list &funcs = i->second.funcs; for (FuncSpec &func : funcs) { @@ -44,7 +44,7 @@ public: } void reg(MtEvent::Type type, event_receive_func f, void *data) override { - std::map::iterator i = m_dest.find(type); + auto i = m_dest.find(type); if (i != m_dest.end()) { i->second.funcs.emplace_back(f, data); } else { diff --git a/src/client/game.cpp b/src/client/game.cpp index fbe81ff66..7588fedc7 100644 --- a/src/client/game.cpp +++ b/src/client/game.cpp @@ -3245,10 +3245,8 @@ PointedThing Game::updatePointedThing( n.getSelectionBoxes(nodedef, &boxes, n.getNeighbors(result.node_undersurface, &map)); - f32 d = 0.002 * BS; - for (std::vector::const_iterator i = boxes.begin(); - i != boxes.end(); ++i) { - aabb3f box = *i; + f32 d = 0.002f * BS; + for (aabb3f box : boxes) { box.MinEdge -= v3f(d, d, d); box.MaxEdge += v3f(d, d, d); selectionboxes->push_back(box); @@ -3450,9 +3448,8 @@ bool Game::nodePlacement(const ItemDefinition &selected_def, u8 predicted_param2 = dir.Y < 0 ? 1 : 0; if (selected_def.wallmounted_rotate_vertical) { bool rotate90 = false; - v3f fnodepos = v3f(neighborpos.X, neighborpos.Y, neighborpos.Z); v3f ppos = client->getEnv().getLocalPlayer()->getPosition() / BS; - v3f pdir = fnodepos - ppos; + v3f pdir = v3f::from(neighborpos) - ppos; switch (predicted_f.drawtype) { case NDT_TORCHLIKE: { rotate90 = !((pdir.X < 0 && pdir.Z > 0) || diff --git a/src/client/hud.cpp b/src/client/hud.cpp index aa9544486..4ef64fedc 100644 --- a/src/client/hud.cpp +++ b/src/client/hud.cpp @@ -965,8 +965,8 @@ void Hud::drawBlockBounds() v3f pmax = v3f(x, y, 1 + radius) * MAP_BLOCKSIZE * BS; driver->draw3DLine( - base_corner + v3f(pmin.X, pmin.Y, pmin.Z), - base_corner + v3f(pmax.X, pmax.Y, pmax.Z), + base_corner + pmin, + base_corner + pmax, choose_color(block_pos.X, block_pos.Y) ); driver->draw3DLine( diff --git a/src/client/imagefilters.cpp b/src/client/imagefilters.cpp index 09a1198ea..cd7205ff9 100644 --- a/src/client/imagefilters.cpp +++ b/src/client/imagefilters.cpp @@ -285,13 +285,13 @@ void imageScaleNNAA(video::IImage *src, const core::rect &srcrect, video::I maxsx = minsx + sw / dim.Width; maxsx = rangelim(maxsx, 0, sox + sw); if (minsx > maxsx) - SWAP(double, minsx, maxsx); + std::swap(minsx, maxsx); minsy = soy + (dy * sh / dim.Height); minsy = rangelim(minsy, 0, soy + sh); maxsy = minsy + sh / dim.Height; maxsy = rangelim(maxsy, 0, soy + sh); if (minsy > maxsy) - SWAP(double, minsy, maxsy); + std::swap(minsy, maxsy); // Total area, and integral of r, g, b values over that area, // initialized to zero, to be summed up in next loops. diff --git a/src/client/mesh_generator_thread.cpp b/src/client/mesh_generator_thread.cpp index e511bc62c..712c785ce 100644 --- a/src/client/mesh_generator_thread.cpp +++ b/src/client/mesh_generator_thread.cpp @@ -145,8 +145,7 @@ QueuedMeshUpdate *MeshUpdateQueue::pop() MutexAutoLock lock(m_mutex); bool must_be_urgent = !m_urgents.empty(); - for (std::vector::iterator i = m_queue.begin(); - i != m_queue.end(); ++i) { + for (auto i = m_queue.begin(); i != m_queue.end(); ++i) { QueuedMeshUpdate *q = *i; if (must_be_urgent && m_urgents.count(q->p) == 0) continue; @@ -264,8 +263,8 @@ void MeshUpdateManager::updateBlock(Map *map, v3s16 p, bool ack_block_to_server, g_settings->getBool("smooth_lighting") && !g_settings->getFlag("performance_tradeoffs"); if (!m_queue_in.addBlock(map, p, ack_block_to_server, urgent)) { - warningstream << "Update requested for non-existent block at (" - << p.X << ", " << p.Y << ", " << p.Z << ")" << std::endl; + warningstream << "Update requested for non-existent block at " + << p << std::endl; return; } if (update_neighbors) { diff --git a/src/client/minimap.cpp b/src/client/minimap.cpp index a37790f71..e60608688 100644 --- a/src/client/minimap.cpp +++ b/src/client/minimap.cpp @@ -78,15 +78,13 @@ void MinimapUpdateThread::doUpdate() while (popBlockUpdate(&update)) { if (update.data) { // Swap two values in the map using single lookup - std::pair::iterator, bool> - result = m_blocks_cache.insert(std::make_pair(update.pos, update.data)); + auto result = m_blocks_cache.insert(std::make_pair(update.pos, update.data)); if (!result.second) { delete result.first->second; result.first->second = update.data; } } else { - std::map::iterator it; - it = m_blocks_cache.find(update.pos); + auto it = m_blocks_cache.find(update.pos); if (it != m_blocks_cache.end()) { delete it->second; m_blocks_cache.erase(it); @@ -124,8 +122,7 @@ void MinimapUpdateThread::getMap(v3s16 pos, s16 size, s16 height) for (blockpos.Z = blockpos_min.Z; blockpos.Z <= blockpos_max.Z; ++blockpos.Z) for (blockpos.Y = blockpos_min.Y; blockpos.Y <= blockpos_max.Y; ++blockpos.Y) for (blockpos.X = blockpos_min.X; blockpos.X <= blockpos_max.X; ++blockpos.X) { - std::map::const_iterator pblock = - m_blocks_cache.find(blockpos); + auto pblock = m_blocks_cache.find(blockpos); if (pblock == m_blocks_cache.end()) continue; const MinimapMapblock &block = *pblock->second; @@ -647,8 +644,7 @@ void Minimap::drawMinimap(core::rect rect) f32 sin_angle = std::sin(m_angle * core::DEGTORAD); f32 cos_angle = std::cos(m_angle * core::DEGTORAD); s32 marker_size2 = 0.025 * (float)rect.getWidth();; - for (std::list::const_iterator - i = m_active_markers.begin(); + for (auto i = m_active_markers.begin(); i != m_active_markers.end(); ++i) { v2f posf = *i; if (data->minimap_shape_round) { diff --git a/src/client/particles.cpp b/src/client/particles.cpp index bcb3cf207..179c9c465 100644 --- a/src/client/particles.cpp +++ b/src/client/particles.cpp @@ -193,7 +193,7 @@ void Particle::updateVertices(ClientEnvironment *env, video::SColor color) video::S3DVertex *vertices = m_buffer->getVertices(m_index); if (m_texture.tex != nullptr) - scale = m_texture.tex -> scale.blend(m_time / (m_expiration+0.1)); + scale = m_texture.tex -> scale.blend(m_time / (m_expiration+0.1f)); else scale = v2f(1.f, 1.f); @@ -203,7 +203,7 @@ void Particle::updateVertices(ClientEnvironment *env, video::SColor color) v2u32 framesize; texcoord = m_p.animation.getTextureCoords(texsize, m_animation_frame); m_p.animation.determineParams(texsize, NULL, NULL, &framesize); - framesize_f = v2f(framesize.X / (float) texsize.X, framesize.Y / (float) texsize.Y); + framesize_f = v2f::from(framesize) / v2f::from(texsize); tx0 = m_texpos.X + texcoord.X; tx1 = m_texpos.X + texcoord.X + framesize_f.X * m_texsize.X; diff --git a/src/client/shadows/dynamicshadowsrender.cpp b/src/client/shadows/dynamicshadowsrender.cpp index 9898b08e6..835f995f1 100644 --- a/src/client/shadows/dynamicshadowsrender.cpp +++ b/src/client/shadows/dynamicshadowsrender.cpp @@ -177,14 +177,15 @@ void ShadowRenderer::removeNodeFromShadowList(scene::ISceneNode *node) node->forEachMaterial([] (auto &mat) { mat.setTexture(TEXTURE_LAYER_SHADOW, nullptr); }); - for (auto it = m_shadow_node_array.begin(); it != m_shadow_node_array.end();) { - if (it->node == node) { - it = m_shadow_node_array.erase(it); - break; - } else { - ++it; - } + + auto it = std::find(m_shadow_node_array.begin(), m_shadow_node_array.end(), node); + if (it == m_shadow_node_array.end()) { + infostream << "removeNodeFromShadowList: " << node << " not found" << std::endl; + return; } + // swap with last, then remove + *it = m_shadow_node_array.back(); + m_shadow_node_array.pop_back(); } void ShadowRenderer::updateSMTextures() diff --git a/src/client/shadows/dynamicshadowsrender.h b/src/client/shadows/dynamicshadowsrender.h index 5854dc4ed..1c7b6e482 100644 --- a/src/client/shadows/dynamicshadowsrender.h +++ b/src/client/shadows/dynamicshadowsrender.h @@ -28,7 +28,8 @@ struct NodeToApply E_SHADOW_MODE m = E_SHADOW_MODE::ESM_BOTH) : node(n), shadowMode(m){}; - bool operator<(const NodeToApply &other) const { return node < other.node; }; + + bool operator==(scene::ISceneNode *n) const { return node == n; } scene::ISceneNode *node; diff --git a/src/client/wieldmesh.cpp b/src/client/wieldmesh.cpp index 7b87f7bdf..9e8d72cf2 100644 --- a/src/client/wieldmesh.cpp +++ b/src/client/wieldmesh.cpp @@ -154,8 +154,7 @@ public: int maxdim = MYMAX(dim.Width, dim.Height); - std::map::iterator - it = m_extrusion_meshes.lower_bound(maxdim); + auto it = m_extrusion_meshes.lower_bound(maxdim); if (it == m_extrusion_meshes.end()) { // no viable resolution found; use largest one diff --git a/src/database/database-dummy.cpp b/src/database/database-dummy.cpp index f23734b6f..280745711 100644 --- a/src/database/database-dummy.cpp +++ b/src/database/database-dummy.cpp @@ -36,8 +36,7 @@ bool Database_Dummy::deleteBlock(const v3s16 &pos) void Database_Dummy::listAllLoadableBlocks(std::vector &dst) { dst.reserve(m_database.size()); - for (std::map::const_iterator x = m_database.begin(); - x != m_database.end(); ++x) { + for (auto x = m_database.begin(); x != m_database.end(); ++x) { dst.push_back(getIntegerAsBlock(x->first)); } } diff --git a/src/database/database-files.cpp b/src/database/database-files.cpp index 5001a2810..84684299d 100644 --- a/src/database/database-files.cpp +++ b/src/database/database-files.cpp @@ -234,8 +234,7 @@ void PlayerDatabaseFiles::listPlayers(std::vector &res) { std::vector files = fs::GetDirListing(m_savedir); // list files into players directory - for (std::vector::const_iterator it = files.begin(); it != - files.end(); ++it) { + for (auto it = files.begin(); it != files.end(); ++it) { // Ignore directories if (it->dir) continue; diff --git a/src/emerge.cpp b/src/emerge.cpp index 2dfead5b4..71e1ed7c5 100644 --- a/src/emerge.cpp +++ b/src/emerge.cpp @@ -375,8 +375,7 @@ bool EmergeManager::pushBlockEmergeData( } } - std::pair::iterator, bool> findres; - findres = m_blocks_enqueued.insert(std::make_pair(pos, BlockEmergeData())); + auto findres = m_blocks_enqueued.insert(std::make_pair(pos, BlockEmergeData())); BlockEmergeData &bedata = findres.first->second; *entry_already_exists = !findres.second; diff --git a/src/face_position_cache.cpp b/src/face_position_cache.cpp index 65a66a37c..b85ebb048 100644 --- a/src/face_position_cache.cpp +++ b/src/face_position_cache.cpp @@ -13,7 +13,7 @@ std::mutex FacePositionCache::cache_mutex; const std::vector &FacePositionCache::getFacePositions(u16 d) { MutexAutoLock lock(cache_mutex); - std::unordered_map>::const_iterator it = cache.find(d); + auto it = cache.find(d); if (it != cache.end()) return it->second; diff --git a/src/gui/guiFormSpecMenu.cpp b/src/gui/guiFormSpecMenu.cpp index 0595832a1..621eba1c2 100644 --- a/src/gui/guiFormSpecMenu.cpp +++ b/src/gui/guiFormSpecMenu.cpp @@ -3194,7 +3194,7 @@ void GUIFormSpecMenu::regenerateGui(v2u32 screensize) pos_offset = v2f32(); // used for formspec versions < 3 - std::list::iterator legacy_sort_start = std::prev(Children.end()); // last element + auto legacy_sort_start = std::prev(Children.end()); // last element if (enable_prepends) { // Backup the coordinates so that prepends can use the coordinates of choice. @@ -3308,7 +3308,7 @@ void GUIFormSpecMenu::legacySortElements(std::list::iterator from else ++from; - std::list::iterator to = Children.end(); + auto to = Children.end(); // 1: Copy into a sortable container std::vector elements(from, to); @@ -4899,8 +4899,7 @@ bool GUIFormSpecMenu::OnEvent(const SEvent& event) if (s.ftype == f_Unknown && s.fid == event.GUIEvent.Caller->getID()) { current_field_enter_pending = s.fname; - std::unordered_map::const_iterator it = - field_close_on_enter.find(s.fname); + auto it = field_close_on_enter.find(s.fname); if (it != field_close_on_enter.end()) close_on_enter = (*it).second; @@ -5085,7 +5084,7 @@ double GUIFormSpecMenu::calculateImgsize(const parserData &data) ((15.0 / 13.0) * (0.85 + data.invsize.Y)); } - double prefer_imgsize = getImgsize(v2u32(padded_screensize.X, padded_screensize.Y), + double prefer_imgsize = getImgsize(v2u32::from(padded_screensize), screen_dpi, gui_scaling); // Try to use the preferred imgsize, but if that's bigger than the maximum diff --git a/src/gui/guiHyperText.cpp b/src/gui/guiHyperText.cpp index 5446038b0..0f887ec4e 100644 --- a/src/gui/guiHyperText.cpp +++ b/src/gui/guiHyperText.cpp @@ -734,7 +734,7 @@ void TextDrawer::place(const core::rect &dest_rect) ymargin = p.margin; // Place non floating stuff - std::vector::iterator el = p.elements.begin(); + auto el = p.elements.begin(); while (el != p.elements.end()) { // Determine line width and y pos @@ -807,8 +807,8 @@ void TextDrawer::place(const core::rect &dest_rect) el++; } - std::vector::iterator linestart = el; - std::vector::iterator lineend = p.elements.end(); + auto linestart = el; + auto lineend = p.elements.end(); // First pass, find elements fitting into line // (or at least one element) diff --git a/src/gui/guiTable.cpp b/src/gui/guiTable.cpp index 0c10a2b64..0bc480da7 100644 --- a/src/gui/guiTable.cpp +++ b/src/gui/guiTable.cpp @@ -362,8 +362,7 @@ void GUITable::setTable(const TableOptions &options, // Find content_index. Image indices are defined in // column options so check active_image_indices. s32 image_index = stoi(content[i * colcount + j]); - std::map::iterator image_iter = - active_image_indices.find(image_index); + auto image_iter =active_image_indices.find(image_index); if (image_iter != active_image_indices.end()) row->content_index = image_iter->second; @@ -965,7 +964,7 @@ bool GUITable::OnEvent(const SEvent &event) s32 GUITable::allocString(const std::string &text) { - std::map::iterator it = m_alloc_strings.find(text); + auto it = m_alloc_strings.find(text); if (it == m_alloc_strings.end()) { s32 id = m_strings.size(); std::wstring wtext = utf8_to_wide(text); @@ -979,7 +978,7 @@ s32 GUITable::allocString(const std::string &text) s32 GUITable::allocImage(const std::string &imagename) { - std::map::iterator it = m_alloc_images.find(imagename); + auto it = m_alloc_images.find(imagename); if (it == m_alloc_images.end()) { s32 id = m_images.size(); m_images.push_back(m_tsrc->getTexture(imagename)); diff --git a/src/gui/profilergraph.cpp b/src/gui/profilergraph.cpp index 22e6608b8..4e633ea5d 100644 --- a/src/gui/profilergraph.cpp +++ b/src/gui/profilergraph.cpp @@ -27,7 +27,7 @@ void ProfilerGraph::draw(s32 x_left, s32 y_bottom, video::IVideoDriver *driver, for (const auto &i : piece.values) { const std::string &id = i.first; const float &value = i.second; - std::map::iterator j = m_meta.find(id); + auto j = m_meta.find(id); if (j == m_meta.end()) { m_meta[id] = Meta(value); diff --git a/src/main.cpp b/src/main.cpp index d93803306..d3e698f33 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1223,7 +1223,7 @@ static bool migrate_map_database(const GameParams &game_params, const Settings & std::vector blocks; old_db->listAllLoadableBlocks(blocks); new_db->beginSave(); - for (std::vector::const_iterator it = blocks.begin(); it != blocks.end(); ++it) { + for (auto it = blocks.begin(); it != blocks.end(); ++it) { if (kill) return false; std::string data; diff --git a/src/map.cpp b/src/map.cpp index e8ccac0cc..d88200846 100644 --- a/src/map.cpp +++ b/src/map.cpp @@ -292,12 +292,13 @@ void Map::timerUpdate(float dtime, float unload_timeout, s32 max_loaded_blocks, // If there is no practical limit, we spare creation of mapblock_queue if (max_loaded_blocks < 0) { + MapBlockVect blocks; for (auto §or_it : m_sectors) { MapSector *sector = sector_it.second; bool all_blocks_deleted = true; - MapBlockVect blocks; + blocks.clear(); sector->getBlocks(blocks); for (MapBlock *block : blocks) { @@ -336,10 +337,11 @@ void Map::timerUpdate(float dtime, float unload_timeout, s32 max_loaded_blocks, } } else { std::priority_queue mapblock_queue; + MapBlockVect blocks; for (auto §or_it : m_sectors) { MapSector *sector = sector_it.second; - MapBlockVect blocks; + blocks.clear(); sector->getBlocks(blocks); for (MapBlock *block : blocks) { @@ -417,16 +419,16 @@ void Map::timerUpdate(float dtime, float unload_timeout, s32 max_loaded_blocks, void Map::unloadUnreferencedBlocks(std::vector *unloaded_blocks) { - timerUpdate(0.0, -1.0, 0, unloaded_blocks); + timerUpdate(0, -1, 0, unloaded_blocks); } -void Map::deleteSectors(std::vector §orList) +void Map::deleteSectors(const std::vector §orList) { for (v2s16 j : sectorList) { MapSector *sector = m_sectors[j]; // If sector is in sector cache, remove it from there - if(m_sector_cache == sector) - m_sector_cache = NULL; + if (m_sector_cache == sector) + m_sector_cache = nullptr; // Remove from map and delete m_sectors.erase(j); delete sector; diff --git a/src/map.h b/src/map.h index a4f0e4524..db4d8be28 100644 --- a/src/map.h +++ b/src/map.h @@ -191,7 +191,7 @@ public: // Deletes sectors and their blocks from memory // Takes cache into account // If deleted sector is in sector cache, clears cache - void deleteSectors(std::vector &list); + void deleteSectors(const std::vector &list); // For debug printing. Prints "Map: ", "ServerMap: " or "ClientMap: " virtual void PrintInfo(std::ostream &out); diff --git a/src/mapgen/mapgen.cpp b/src/mapgen/mapgen.cpp index 1917c484c..83c56ee8d 100644 --- a/src/mapgen/mapgen.cpp +++ b/src/mapgen/mapgen.cpp @@ -37,7 +37,7 @@ #include "cavegen.h" #include "dungeongen.h" -FlagDesc flagdesc_mapgen[] = { +const FlagDesc flagdesc_mapgen[] = { {"caves", MG_CAVES}, {"dungeons", MG_DUNGEONS}, {"light", MG_LIGHT}, @@ -47,7 +47,7 @@ FlagDesc flagdesc_mapgen[] = { {NULL, 0} }; -FlagDesc flagdesc_gennotify[] = { +const FlagDesc flagdesc_gennotify[] = { {"dungeon", 1 << GENNOTIFY_DUNGEON}, {"temple", 1 << GENNOTIFY_TEMPLE}, {"cave_begin", 1 << GENNOTIFY_CAVE_BEGIN}, diff --git a/src/mapgen/mapgen.h b/src/mapgen/mapgen.h index a81b9a361..31313b35f 100644 --- a/src/mapgen/mapgen.h +++ b/src/mapgen/mapgen.h @@ -29,8 +29,8 @@ class Settings; class MMVManip; class NodeDefManager; -extern FlagDesc flagdesc_mapgen[]; -extern FlagDesc flagdesc_gennotify[]; +extern const FlagDesc flagdesc_mapgen[]; +extern const FlagDesc flagdesc_gennotify[]; class Biome; class BiomeGen; diff --git a/src/mapgen/mapgen_carpathian.cpp b/src/mapgen/mapgen_carpathian.cpp index 46048b51f..5c7f9e1b1 100644 --- a/src/mapgen/mapgen_carpathian.cpp +++ b/src/mapgen/mapgen_carpathian.cpp @@ -24,7 +24,7 @@ #include "mapgen_carpathian.h" -FlagDesc flagdesc_mapgen_carpathian[] = { +const FlagDesc flagdesc_mapgen_carpathian[] = { {"caverns", MGCARPATHIAN_CAVERNS}, {"rivers", MGCARPATHIAN_RIVERS}, {NULL, 0} diff --git a/src/mapgen/mapgen_carpathian.h b/src/mapgen/mapgen_carpathian.h index c2c0d48fe..754634af8 100644 --- a/src/mapgen/mapgen_carpathian.h +++ b/src/mapgen/mapgen_carpathian.h @@ -12,7 +12,7 @@ class BiomeManager; -extern FlagDesc flagdesc_mapgen_carpathian[]; +extern const FlagDesc flagdesc_mapgen_carpathian[]; struct MapgenCarpathianParams : public MapgenParams diff --git a/src/mapgen/mapgen_flat.cpp b/src/mapgen/mapgen_flat.cpp index 56346432e..c0face7b9 100644 --- a/src/mapgen/mapgen_flat.cpp +++ b/src/mapgen/mapgen_flat.cpp @@ -23,7 +23,7 @@ #include "mapgen_flat.h" -FlagDesc flagdesc_mapgen_flat[] = { +const FlagDesc flagdesc_mapgen_flat[] = { {"lakes", MGFLAT_LAKES}, {"hills", MGFLAT_HILLS}, {"caverns", MGFLAT_CAVERNS}, diff --git a/src/mapgen/mapgen_flat.h b/src/mapgen/mapgen_flat.h index 46ff46154..6eb303765 100644 --- a/src/mapgen/mapgen_flat.h +++ b/src/mapgen/mapgen_flat.h @@ -14,7 +14,7 @@ class BiomeManager; -extern FlagDesc flagdesc_mapgen_flat[]; +extern const FlagDesc flagdesc_mapgen_flat[]; struct MapgenFlatParams : public MapgenParams { diff --git a/src/mapgen/mapgen_fractal.cpp b/src/mapgen/mapgen_fractal.cpp index 3e28fbcac..0ac72ac08 100644 --- a/src/mapgen/mapgen_fractal.cpp +++ b/src/mapgen/mapgen_fractal.cpp @@ -24,7 +24,7 @@ #include "mapgen_fractal.h" -FlagDesc flagdesc_mapgen_fractal[] = { +const FlagDesc flagdesc_mapgen_fractal[] = { {"terrain", MGFRACTAL_TERRAIN}, {NULL, 0} }; diff --git a/src/mapgen/mapgen_fractal.h b/src/mapgen/mapgen_fractal.h index 0a71bd1d1..1070f7f25 100644 --- a/src/mapgen/mapgen_fractal.h +++ b/src/mapgen/mapgen_fractal.h @@ -12,7 +12,7 @@ class BiomeManager; -extern FlagDesc flagdesc_mapgen_fractal[]; +extern const FlagDesc flagdesc_mapgen_fractal[]; struct MapgenFractalParams : public MapgenParams diff --git a/src/mapgen/mapgen_v5.cpp b/src/mapgen/mapgen_v5.cpp index 07d1abda7..9cfd0cf9d 100644 --- a/src/mapgen/mapgen_v5.cpp +++ b/src/mapgen/mapgen_v5.cpp @@ -23,7 +23,7 @@ #include "mapgen_v5.h" -FlagDesc flagdesc_mapgen_v5[] = { +const FlagDesc flagdesc_mapgen_v5[] = { {"caverns", MGV5_CAVERNS}, {NULL, 0} }; diff --git a/src/mapgen/mapgen_v5.h b/src/mapgen/mapgen_v5.h index 8c8c1c27f..7901347dc 100644 --- a/src/mapgen/mapgen_v5.h +++ b/src/mapgen/mapgen_v5.h @@ -12,7 +12,7 @@ class BiomeManager; -extern FlagDesc flagdesc_mapgen_v5[]; +extern const FlagDesc flagdesc_mapgen_v5[]; struct MapgenV5Params : public MapgenParams { diff --git a/src/mapgen/mapgen_v6.cpp b/src/mapgen/mapgen_v6.cpp index 44243618e..a698494cd 100644 --- a/src/mapgen/mapgen_v6.cpp +++ b/src/mapgen/mapgen_v6.cpp @@ -25,7 +25,7 @@ #include "mapgen_v6.h" -FlagDesc flagdesc_mapgen_v6[] = { +const FlagDesc flagdesc_mapgen_v6[] = { {"jungles", MGV6_JUNGLES}, {"biomeblend", MGV6_BIOMEBLEND}, {"mudflow", MGV6_MUDFLOW}, diff --git a/src/mapgen/mapgen_v6.h b/src/mapgen/mapgen_v6.h index 6d776665a..820577187 100644 --- a/src/mapgen/mapgen_v6.h +++ b/src/mapgen/mapgen_v6.h @@ -27,7 +27,7 @@ #define MGV6_TEMPLES 0x40 -extern FlagDesc flagdesc_mapgen_v6[]; +extern const FlagDesc flagdesc_mapgen_v6[]; enum BiomeV6Type diff --git a/src/mapgen/mapgen_v7.cpp b/src/mapgen/mapgen_v7.cpp index fe052f3b7..8fc5b0c6f 100644 --- a/src/mapgen/mapgen_v7.cpp +++ b/src/mapgen/mapgen_v7.cpp @@ -24,7 +24,7 @@ #include "mapgen_v7.h" -FlagDesc flagdesc_mapgen_v7[] = { +const FlagDesc flagdesc_mapgen_v7[] = { {"mountains", MGV7_MOUNTAINS}, {"ridges", MGV7_RIDGES}, {"floatlands", MGV7_FLOATLANDS}, diff --git a/src/mapgen/mapgen_v7.h b/src/mapgen/mapgen_v7.h index 49e036b82..10a0aa12e 100644 --- a/src/mapgen/mapgen_v7.h +++ b/src/mapgen/mapgen_v7.h @@ -16,7 +16,7 @@ class BiomeManager; -extern FlagDesc flagdesc_mapgen_v7[]; +extern const FlagDesc flagdesc_mapgen_v7[]; struct MapgenV7Params : public MapgenParams { diff --git a/src/mapgen/mapgen_valleys.cpp b/src/mapgen/mapgen_valleys.cpp index 55185c445..0d91765b6 100644 --- a/src/mapgen/mapgen_valleys.cpp +++ b/src/mapgen/mapgen_valleys.cpp @@ -32,7 +32,7 @@ Licensing changed by permission of Gael de Sailly. #include -FlagDesc flagdesc_mapgen_valleys[] = { +const FlagDesc flagdesc_mapgen_valleys[] = { {"altitude_chill", MGVALLEYS_ALT_CHILL}, {"humid_rivers", MGVALLEYS_HUMID_RIVERS}, {"vary_river_depth", MGVALLEYS_VARY_RIVER_DEPTH}, diff --git a/src/mapgen/mapgen_valleys.h b/src/mapgen/mapgen_valleys.h index c0e3bd129..7e318bb59 100644 --- a/src/mapgen/mapgen_valleys.h +++ b/src/mapgen/mapgen_valleys.h @@ -24,7 +24,7 @@ Licensing changed by permission of Gael de Sailly. class BiomeManager; class BiomeGenOriginal; -extern FlagDesc flagdesc_mapgen_valleys[]; +extern const FlagDesc flagdesc_mapgen_valleys[]; struct MapgenValleysParams : public MapgenParams { diff --git a/src/mapgen/mg_decoration.cpp b/src/mapgen/mg_decoration.cpp index dcf32acb7..9d6b73070 100644 --- a/src/mapgen/mg_decoration.cpp +++ b/src/mapgen/mg_decoration.cpp @@ -15,7 +15,7 @@ #include "mapgen/treegen.h" -FlagDesc flagdesc_deco[] = { +const FlagDesc flagdesc_deco[] = { {"place_center_x", DECO_PLACE_CENTER_X}, {"place_center_y", DECO_PLACE_CENTER_Y}, {"place_center_z", DECO_PLACE_CENTER_Z}, diff --git a/src/mapgen/mg_decoration.h b/src/mapgen/mg_decoration.h index 40829b689..15f38c394 100644 --- a/src/mapgen/mg_decoration.h +++ b/src/mapgen/mg_decoration.h @@ -33,7 +33,7 @@ enum DecorationType { #define DECO_ALL_FLOORS 0x40 #define DECO_ALL_CEILINGS 0x80 -extern FlagDesc flagdesc_deco[]; +extern const FlagDesc flagdesc_deco[]; class Decoration : public ObjDef, public NodeResolver { diff --git a/src/mapgen/mg_ore.cpp b/src/mapgen/mg_ore.cpp index d5817f682..3ab908d75 100644 --- a/src/mapgen/mg_ore.cpp +++ b/src/mapgen/mg_ore.cpp @@ -13,7 +13,7 @@ #include -FlagDesc flagdesc_ore[] = { +const FlagDesc flagdesc_ore[] = { {"absheight", OREFLAG_ABSHEIGHT}, // Non-functional {"puff_cliffs", OREFLAG_PUFF_CLIFFS}, {"puff_additive_composition", OREFLAG_PUFF_ADDITIVE}, @@ -324,7 +324,7 @@ void OrePuff::generate(MMVManip *vm, int mapseed, u32 blockseed, int y1 = ymid + ntop; if ((flags & OREFLAG_PUFF_ADDITIVE) && (y0 > y1)) - SWAP(int, y0, y1); + std::swap(y0, y1); for (int y = y0; y <= y1; y++) { u32 i = vm->m_area.index(x, y, z); diff --git a/src/mapgen/mg_ore.h b/src/mapgen/mg_ore.h index eed13ebfc..ae9faee11 100644 --- a/src/mapgen/mg_ore.h +++ b/src/mapgen/mg_ore.h @@ -33,7 +33,7 @@ enum OreType { ORE_STRATUM, }; -extern FlagDesc flagdesc_ore[]; +extern const FlagDesc flagdesc_ore[]; class Ore : public ObjDef, public NodeResolver { public: diff --git a/src/mapgen/mg_schematic.cpp b/src/mapgen/mg_schematic.cpp index 8caed8157..9e5c078c8 100644 --- a/src/mapgen/mg_schematic.cpp +++ b/src/mapgen/mg_schematic.cpp @@ -127,7 +127,7 @@ void Schematic::blitToVManip(MMVManip *vm, v3s16 p, Rotation rot, bool force_pla i_start = sx - 1; i_step_x = zstride; i_step_z = -xstride; - SWAP(s16, sx, sz); + std::swap(sx, sz); break; case ROTATE_180: i_start = zstride * (sz - 1) + sx - 1; @@ -138,7 +138,7 @@ void Schematic::blitToVManip(MMVManip *vm, v3s16 p, Rotation rot, bool force_pla i_start = zstride * (sz - 1); i_step_x = -zstride; i_step_z = xstride; - SWAP(s16, sx, sz); + std::swap(sx, sz); break; default: i_start = 0; @@ -222,7 +222,6 @@ void Schematic::placeOnMap(ServerMap *map, v3s16 p, u32 flags, Rotation rot, bool force_place) { std::map modified_blocks; - std::map::iterator it; assert(map != NULL); assert(schemdata != NULL); diff --git a/src/network/clientpackethandler.cpp b/src/network/clientpackethandler.cpp index f589396c6..fe490dd8c 100644 --- a/src/network/clientpackethandler.cpp +++ b/src/network/clientpackethandler.cpp @@ -568,7 +568,7 @@ void Client::handleCommand_MovePlayer(NetworkPacket* pkt) player->setPosition(pos); infostream << "Client got TOCLIENT_MOVE_PLAYER" - << " pos=(" << pos.X << "," << pos.Y << "," << pos.Z << ")" + << " pos=" << pos << " pitch=" << pitch << " yaw=" << yaw << std::endl; diff --git a/src/nodedef.cpp b/src/nodedef.cpp index 6994464ed..ffc5503b1 100644 --- a/src/nodedef.cpp +++ b/src/nodedef.cpp @@ -1067,8 +1067,7 @@ void NodeDefManager::clear() bool NodeDefManager::getId(const std::string &name, content_t &result) const { - std::unordered_map::const_iterator - i = m_name_id_mapping_with_aliases.find(name); + auto i = m_name_id_mapping_with_aliases.find(name); if(i == m_name_id_mapping_with_aliases.end()) return false; result = i->second; diff --git a/src/nodetimer.cpp b/src/nodetimer.cpp index 04ea3d5bc..bdf842a26 100644 --- a/src/nodetimer.cpp +++ b/src/nodetimer.cpp @@ -117,7 +117,7 @@ std::vector NodeTimerList::step(float dtime) if (m_next_trigger_time == -1. || m_time < m_next_trigger_time) { return elapsed_timers; } - std::multimap::iterator i = m_timers.begin(); + auto i = m_timers.begin(); // Process timers for (; i != m_timers.end() && i->first <= m_time; ++i) { NodeTimer t = i->second; diff --git a/src/nodetimer.h b/src/nodetimer.h index a61abf172..c2990c166 100644 --- a/src/nodetimer.h +++ b/src/nodetimer.h @@ -50,8 +50,7 @@ public: // Get timer NodeTimer get(const v3s16 &p) { - std::map::iterator>::iterator n = - m_iterators.find(p); + auto n = m_iterators.find(p); if (n == m_iterators.end()) return NodeTimer(); NodeTimer t = n->second->second; @@ -60,8 +59,7 @@ public: } // Deletes timer void remove(v3s16 p) { - std::map::iterator>::iterator n = - m_iterators.find(p); + auto n = m_iterators.find(p); if(n != m_iterators.end()) { double removed_time = n->second->first; m_timers.erase(n->second); @@ -81,7 +79,7 @@ public: void insert(const NodeTimer &timer) { v3s16 p = timer.position; double trigger_time = m_time + (double)(timer.timeout - timer.elapsed); - std::multimap::iterator it = m_timers.emplace(trigger_time, timer); + auto it = m_timers.emplace(trigger_time, timer); m_iterators.emplace(p, it); if (m_next_trigger_time == -1. || trigger_time < m_next_trigger_time) m_next_trigger_time = trigger_time; diff --git a/src/noise.cpp b/src/noise.cpp index bfd29e4ee..d81e0bbba 100644 --- a/src/noise.cpp +++ b/src/noise.cpp @@ -38,7 +38,9 @@ // Unsigned magic seed prevents undefined behavior. #define NOISE_MAGIC_SEED 1013U -FlagDesc flagdesc_noiseparams[] = { +#define myfloor(x) ((x) < 0 ? (int)(x) - 1 : (int)(x)) + +const FlagDesc flagdesc_noiseparams[] = { {"defaults", NOISE_FLAG_DEFAULTS}, {"eased", NOISE_FLAG_EASED}, {"absvalue", NOISE_FLAG_ABSVALUE}, diff --git a/src/noise.h b/src/noise.h index c864c8fbb..acd8d555d 100644 --- a/src/noise.h +++ b/src/noise.h @@ -36,7 +36,7 @@ #undef RANDOM_MAX #endif -extern FlagDesc flagdesc_noiseparams[]; +extern const FlagDesc flagdesc_noiseparams[]; // Note: this class is not polymorphic so that its high level of // optimizability may be preserved in the common use case diff --git a/src/pathfinder.cpp b/src/pathfinder.cpp index 656abf901..9509ba88a 100644 --- a/src/pathfinder.cpp +++ b/src/pathfinder.cpp @@ -578,7 +578,7 @@ MapGridNodeContainer::MapGridNodeContainer(Pathfinder *pathf) PathGridnode &MapGridNodeContainer::access(v3s16 p) { - std::map::iterator it = m_nodes.find(p); + auto it = m_nodes.find(p); if (it != m_nodes.end()) { return it->second; } @@ -758,8 +758,7 @@ std::vector Pathfinder::getPath(v3s16 source, } //convert all index positions to "normal" positions and insert //them into full_path in reverse - std::vector::reverse_iterator rit = index_path.rbegin(); - for (; rit != index_path.rend(); ++rit) { + for (auto rit = index_path.rbegin(); rit != index_path.rend(); ++rit) { full_path.push_back(getIndexElement(*rit).pos); } //manually add true_destination to end of path, if needed @@ -1419,8 +1418,7 @@ std::string Pathfinder::dirToName(PathDirections dir) void Pathfinder::printPath(const std::vector &path) { unsigned int current = 0; - for (std::vector::iterator i = path.begin(); - i != path.end(); ++i) { + for (auto i = path.begin(); i != path.end(); ++i) { std::cout << std::setw(3) << current << ":" << *i << std::endl; current++; } diff --git a/src/script/common/c_content.cpp b/src/script/common/c_content.cpp index fb4762eec..7efbb7abf 100644 --- a/src/script/common/c_content.cpp +++ b/src/script/common/c_content.cpp @@ -1175,8 +1175,7 @@ void push_palette(lua_State *L, const std::vector *palette) lua_createtable(L, palette->size(), 0); int newTable = lua_gettop(L); int index = 1; - std::vector::const_iterator iter; - for (iter = palette->begin(); iter != palette->end(); ++iter) { + for (auto iter = palette->begin(); iter != palette->end(); ++iter) { push_ARGB8(L, (*iter)); lua_rawseti(L, newTable, index); index++; @@ -1829,7 +1828,7 @@ void push_hit_params(lua_State *L,const HitParams ¶ms) /******************************************************************************/ bool getflagsfield(lua_State *L, int table, const char *fieldname, - FlagDesc *flagdesc, u32 *flags, u32 *flagmask) + const FlagDesc *flagdesc, u32 *flags, u32 *flagmask) { lua_getfield(L, table, fieldname); @@ -1840,7 +1839,7 @@ bool getflagsfield(lua_State *L, int table, const char *fieldname, return success; } -bool read_flags(lua_State *L, int index, FlagDesc *flagdesc, +bool read_flags(lua_State *L, int index, const FlagDesc *flagdesc, u32 *flags, u32 *flagmask) { if (lua_isstring(L, index)) { @@ -1855,7 +1854,7 @@ bool read_flags(lua_State *L, int index, FlagDesc *flagdesc, return true; } -u32 read_flags_table(lua_State *L, int table, FlagDesc *flagdesc, u32 *flagmask) +u32 read_flags_table(lua_State *L, int table, const FlagDesc *flagdesc, u32 *flagmask) { u32 flags = 0, mask = 0; char fnamebuf[64] = "no"; @@ -1880,7 +1879,7 @@ u32 read_flags_table(lua_State *L, int table, FlagDesc *flagdesc, u32 *flagmask) return flags; } -void push_flags_string(lua_State *L, FlagDesc *flagdesc, u32 flags, u32 flagmask) +void push_flags_string(lua_State *L, const FlagDesc *flagdesc, u32 flags, u32 flagmask) { std::string flagstring = writeFlagString(flags, flagdesc, flagmask); lua_pushlstring(L, flagstring.c_str(), flagstring.size()); diff --git a/src/script/common/c_content.h b/src/script/common/c_content.h index f05a51f24..f57c15797 100644 --- a/src/script/common/c_content.h +++ b/src/script/common/c_content.h @@ -120,21 +120,21 @@ void read_groups(lua_State *L, int index, ItemGroupList &result); void push_groups(lua_State *L, const ItemGroupList &groups); -//TODO rename to "read_enum_field" +// TODO: rename to "read_enum_field" and replace with type-safe template int getenumfield(lua_State *L, int table, const char *fieldname, const EnumString *spec, int default_); bool getflagsfield(lua_State *L, int table, const char *fieldname, - FlagDesc *flagdesc, u32 *flags, u32 *flagmask); + const FlagDesc *flagdesc, u32 *flags, u32 *flagmask); -bool read_flags(lua_State *L, int index, FlagDesc *flagdesc, +bool read_flags(lua_State *L, int index, const FlagDesc *flagdesc, u32 *flags, u32 *flagmask); -void push_flags_string(lua_State *L, FlagDesc *flagdesc, +void push_flags_string(lua_State *L, const FlagDesc *flagdesc, u32 flags, u32 flagmask); u32 read_flags_table(lua_State *L, int table, - FlagDesc *flagdesc, u32 *flagmask); + const FlagDesc *flagdesc, u32 *flagmask); void push_items(lua_State *L, const std::vector &items); diff --git a/src/script/lua_api/l_craft.cpp b/src/script/lua_api/l_craft.cpp index be6eb4102..f3b6fd41c 100644 --- a/src/script/lua_api/l_craft.cpp +++ b/src/script/lua_api/l_craft.cpp @@ -411,7 +411,7 @@ static void push_craft_recipe(lua_State *L, IGameDef *gdef, CraftOutput output = recipe->getOutput(input, gdef); lua_newtable(L); // items - std::vector::const_iterator iter = input.items.begin(); + auto iter = input.items.begin(); for (u16 j = 1; iter != input.items.end(); ++iter, j++) { if (iter->empty()) continue; @@ -457,7 +457,7 @@ static void push_craft_recipes(lua_State *L, IGameDef *gdef, lua_createtable(L, recipes.size(), 0); - std::vector::const_iterator it = recipes.begin(); + auto it = recipes.begin(); for (unsigned i = 0; it != recipes.end(); ++it) { lua_newtable(L); push_craft_recipe(L, gdef, *it, output); diff --git a/src/script/lua_api/l_mapgen.cpp b/src/script/lua_api/l_mapgen.cpp index 3b547f5da..8d3df7213 100644 --- a/src/script/lua_api/l_mapgen.cpp +++ b/src/script/lua_api/l_mapgen.cpp @@ -250,7 +250,7 @@ bool read_schematic_def(lua_State *L, int index, u8 param2 = getintfield_default(L, -1, "param2", 0); //// Find or add new nodename-to-ID mapping - std::unordered_map::iterator it = name_id_map.find(name); + auto it = name_id_map.find(name); content_t name_index; if (it != name_id_map.end()) { name_index = it->second; diff --git a/src/script/lua_api/l_rollback.cpp b/src/script/lua_api/l_rollback.cpp index 93137263c..9a4a3ca64 100644 --- a/src/script/lua_api/l_rollback.cpp +++ b/src/script/lua_api/l_rollback.cpp @@ -36,7 +36,7 @@ int ModApiRollback::l_rollback_get_node_actions(lua_State *L) } std::list actions = rollback->getNodeActors(pos, range, seconds, limit); - std::list::iterator iter = actions.begin(); + auto iter = actions.begin(); lua_createtable(L, actions.size(), 0); for (unsigned int i = 1; iter != actions.end(); ++iter, ++i) { @@ -86,8 +86,7 @@ int ModApiRollback::l_rollback_revert_actions_by(lua_State *L) lua_pushboolean(L, success); lua_createtable(L, log.size(), 0); unsigned long i = 0; - for(std::list::const_iterator iter = log.begin(); - iter != log.end(); ++i, ++iter) { + for(auto iter = log.begin(); iter != log.end(); ++i, ++iter) { lua_pushnumber(L, i); lua_pushstring(L, iter->c_str()); lua_settable(L, -3); diff --git a/src/server.cpp b/src/server.cpp index 116bbfe77..16611843f 100644 --- a/src/server.cpp +++ b/src/server.cpp @@ -1986,9 +1986,8 @@ void Server::SendMovePlayer(PlayerSAO *sao) pkt << sao->getBasePosition() << sao->getLookPitch() << sao->getRotation().Y; { - v3f pos = sao->getBasePosition(); verbosestream << "Server: Sending TOCLIENT_MOVE_PLAYER" - << " pos=(" << pos.X << "," << pos.Y << "," << pos.Z << ")" + << " pos=" << sao->getBasePosition() << " pitch=" << sao->getLookPitch() << " yaw=" << sao->getRotation().Y << std::endl; diff --git a/src/server/rollback.cpp b/src/server/rollback.cpp index a20469aa5..43ca70bb3 100644 --- a/src/server/rollback.cpp +++ b/src/server/rollback.cpp @@ -121,7 +121,7 @@ void RollbackManager::registerNewNode(const int id, const std::string &name) int RollbackManager::getActorId(const std::string &name) { - for (std::vector::const_iterator iter = knownActors.begin(); + for (auto iter = knownActors.begin(); iter != knownActors.end(); ++iter) { if (iter->name == name) { return iter->id; @@ -141,7 +141,7 @@ int RollbackManager::getActorId(const std::string &name) int RollbackManager::getNodeId(const std::string &name) { - for (std::vector::const_iterator iter = knownNodes.begin(); + for (auto iter = knownNodes.begin(); iter != knownNodes.end(); ++iter) { if (iter->name == name) { return iter->id; @@ -161,7 +161,7 @@ int RollbackManager::getNodeId(const std::string &name) const char * RollbackManager::getActorName(const int id) { - for (std::vector::const_iterator iter = knownActors.begin(); + for (auto iter = knownActors.begin(); iter != knownActors.end(); ++iter) { if (iter->id == id) { return iter->name.c_str(); @@ -174,7 +174,7 @@ const char * RollbackManager::getActorName(const int id) const char * RollbackManager::getNodeName(const int id) { - for (std::vector::const_iterator iter = knownNodes.begin(); + for (auto iter = knownNodes.begin(); iter != knownNodes.end(); ++iter) { if (iter->id == id) { return iter->name.c_str(); @@ -771,9 +771,7 @@ void RollbackManager::flush() { sqlite3_exec(db, "BEGIN", NULL, NULL, NULL); - std::list::const_iterator iter; - - for (iter = action_todisk_buffer.begin(); + for (auto iter = action_todisk_buffer.begin(); iter != action_todisk_buffer.end(); ++iter) { if (iter->actor.empty()) { diff --git a/src/serverenvironment.cpp b/src/serverenvironment.cpp index 3de1f2168..bab243713 100644 --- a/src/serverenvironment.cpp +++ b/src/serverenvironment.cpp @@ -43,7 +43,7 @@ // A number that is much smaller than the timeout for particle spawners should/could ever be #define PARTICLE_SPAWNER_NO_EXPIRY -1024.f -static constexpr s16 ACTIVE_OBJECT_RESAVE_DISTANCE_SQ = 3 * 3; +static constexpr s16 ACTIVE_OBJECT_RESAVE_DISTANCE_SQ = sqr(3); /* ABMWithState @@ -627,8 +627,7 @@ void ServerEnvironment::addPlayer(RemotePlayer *player) void ServerEnvironment::removePlayer(RemotePlayer *player) { - for (std::vector::iterator it = m_players.begin(); - it != m_players.end(); ++it) { + for (auto it = m_players.begin(); it != m_players.end(); ++it) { if ((*it) == player) { delete *it; m_players.erase(it); @@ -2412,7 +2411,7 @@ bool ServerEnvironment::migratePlayersDatabase(const GameParams &game_params, std::vector player_list; srcdb->listPlayers(player_list); - for (std::vector::const_iterator it = player_list.begin(); + for (auto it = player_list.begin(); it != player_list.end(); ++it) { actionstream << "Migrating player " << it->c_str() << std::endl; RemotePlayer player(it->c_str(), NULL); diff --git a/src/tileanimation.cpp b/src/tileanimation.cpp index 311fa4c84..a3dc6965d 100644 --- a/src/tileanimation.cpp +++ b/src/tileanimation.cpp @@ -57,7 +57,7 @@ void TileAnimationParams::determineParams(v2u32 texture_size, int *frame_count, if (frame_count) *frame_count = _frame_count; if (frame_length_ms) - *frame_length_ms = 1000.0 * vertical_frames.length / _frame_count; + *frame_length_ms = 1000 * vertical_frames.length / _frame_count; if (frame_size) *frame_size = v2u32(texture_size.X, frame_height); } else if (type == TAT_SHEET_2D) { @@ -104,5 +104,5 @@ v2f TileAnimationParams::getTextureCoords(v2u32 texture_size, int frame) const r = frame % sheet_2d.frames_w; ret = v2u32(r * frame_size.X, q * frame_size.Y); } - return v2f(ret.X / (float) texture_size.X, ret.Y / (float) texture_size.Y); + return v2f::from(ret) / v2f::from(texture_size); } diff --git a/src/unittest/test_random.cpp b/src/unittest/test_random.cpp index da5c2cf90..314192166 100644 --- a/src/unittest/test_random.cpp +++ b/src/unittest/test_random.cpp @@ -80,7 +80,7 @@ void TestRandom::testPseudoRandomRange() s32 min = (pr.next() % 3000) - 500; s32 max = (pr.next() % 3000) - 500; if (min > max) - SWAP(s32, min, max); + std::swap(min, max); s32 randval = pr.range(min, max); UASSERT(randval >= min); @@ -120,7 +120,7 @@ void TestRandom::testPcgRandomRange() s32 min = (pr.next() % 3000) - 500; s32 max = (pr.next() % 3000) - 500; if (min > max) - SWAP(s32, min, max); + std::swap(min, max); s32 randval = pr.range(min, max); UASSERT(randval >= min); diff --git a/src/unittest/test_utilities.cpp b/src/unittest/test_utilities.cpp index b7d4965f9..ac1f29b30 100644 --- a/src/unittest/test_utilities.cpp +++ b/src/unittest/test_utilities.cpp @@ -656,8 +656,6 @@ C apply_all(const C &co, F functor) return ret; } -#define cast_v3(T, other) T((other).X, (other).Y, (other).Z) - void TestUtilities::testIsBlockInSight() { const std::vector testdata1 = { @@ -674,7 +672,7 @@ void TestUtilities::testIsBlockInSight() auto test1 = [] (const std::vector &data) { float range = BS * MAP_BLOCKSIZE * 4; float fov = 72 * core::DEGTORAD; - v3f cam_pos = cast_v3(v3f, data[0]), cam_dir = cast_v3(v3f, data[1]); + v3f cam_pos = v3f::from(data[0]), cam_dir = v3f::from(data[1]); UASSERT( isBlockInSight(data[2], cam_pos, cam_dir, fov, range)); UASSERT(!isBlockInSight(data[3], cam_pos, cam_dir, fov, range)); UASSERT(!isBlockInSight(data[4], cam_pos, cam_dir, fov, range)); diff --git a/src/unittest/test_voxelmanipulator.cpp b/src/unittest/test_voxelmanipulator.cpp index abfb5a5ec..09d4b5781 100644 --- a/src/unittest/test_voxelmanipulator.cpp +++ b/src/unittest/test_voxelmanipulator.cpp @@ -52,13 +52,11 @@ void TestVoxelManipulator::testVoxelArea() UASSERT(aa.size() == results.size()); infostream<<"Result of diff:"<::const_iterator - it = aa.begin(); it != aa.end(); ++it) { + for (auto it = aa.begin(); it != aa.end(); ++it) { it->print(infostream); infostream << std::endl; - std::vector::iterator j; - j = std::find(results.begin(), results.end(), *it); + auto j = std::find(results.begin(), results.end(), *it); UASSERT(j != results.end()); results.erase(j); } diff --git a/src/util/areastore.cpp b/src/util/areastore.cpp index 8d5dfa7d4..67bf4113a 100644 --- a/src/util/areastore.cpp +++ b/src/util/areastore.cpp @@ -194,7 +194,7 @@ bool VectorAreaStore::removeArea(u32 id) if (it == areas_map.end()) return false; Area *a = &it->second; - for (std::vector::iterator v_it = m_areas.begin(); + for (auto v_it = m_areas.begin(); v_it != m_areas.end(); ++v_it) { if (*v_it == a) { m_areas.erase(v_it); @@ -259,7 +259,7 @@ bool SpatialAreaStore::insertArea(Area *a) bool SpatialAreaStore::removeArea(u32 id) { - std::map::iterator itr = areas_map.find(id); + auto itr = areas_map.find(id); if (itr != areas_map.end()) { Area *a = &itr->second; bool result = m_tree->deleteData(get_spatial_region(a->minedge, diff --git a/src/util/areastore.h b/src/util/areastore.h index 7f6c24815..c377c2ec9 100644 --- a/src/util/areastore.h +++ b/src/util/areastore.h @@ -162,7 +162,7 @@ private: { u32 id = in.getIdentifier(); - std::map::iterator itr = m_store->areas_map.find(id); + auto itr = m_store->areas_map.find(id); assert(itr != m_store->areas_map.end()); m_result->push_back(&itr->second); } diff --git a/src/util/numeric.cpp b/src/util/numeric.cpp index 2ed351fbf..7dc3d4dea 100644 --- a/src/util/numeric.cpp +++ b/src/util/numeric.cpp @@ -7,7 +7,6 @@ #include "log.h" #include "constants.h" // BS, MAP_BLOCKSIZE #include "noise.h" // PseudoRandom, PcgRandom -#include "threading/mutex_auto_lock.h" #include #include @@ -73,15 +72,14 @@ u64 murmur_hash_64_ua(const void *key, int len, unsigned int seed) h *= m; } - const unsigned char *data2 = (const unsigned char *)data; switch (len & 7) { - case 7: h ^= (u64)data2[6] << 48; [[fallthrough]]; - case 6: h ^= (u64)data2[5] << 40; [[fallthrough]]; - case 5: h ^= (u64)data2[4] << 32; [[fallthrough]]; - case 4: h ^= (u64)data2[3] << 24; [[fallthrough]]; - case 3: h ^= (u64)data2[2] << 16; [[fallthrough]]; - case 2: h ^= (u64)data2[1] << 8; [[fallthrough]]; - case 1: h ^= (u64)data2[0]; + case 7: h ^= (u64)data[6] << 48; [[fallthrough]]; + case 6: h ^= (u64)data[5] << 40; [[fallthrough]]; + case 5: h ^= (u64)data[4] << 32; [[fallthrough]]; + case 4: h ^= (u64)data[3] << 24; [[fallthrough]]; + case 3: h ^= (u64)data[2] << 16; [[fallthrough]]; + case 2: h ^= (u64)data[1] << 8; [[fallthrough]]; + case 1: h ^= (u64)data[0]; h *= m; } @@ -105,11 +103,7 @@ bool isBlockInSight(v3s16 blockpos_b, v3f camera_pos, v3f camera_dir, v3s16 blockpos_nodes = blockpos_b * MAP_BLOCKSIZE; // Block center position - v3f blockpos( - ((float)blockpos_nodes.X + MAP_BLOCKSIZE/2) * BS, - ((float)blockpos_nodes.Y + MAP_BLOCKSIZE/2) * BS, - ((float)blockpos_nodes.Z + MAP_BLOCKSIZE/2) * BS - ); + v3f blockpos = v3f::from(blockpos_nodes + MAP_BLOCKSIZE / 2) * BS; // Block position relative to camera v3f blockpos_relative = blockpos - camera_pos; diff --git a/src/util/numeric.h b/src/util/numeric.h index 3082adb31..5c0d2cebf 100644 --- a/src/util/numeric.h +++ b/src/util/numeric.h @@ -15,14 +15,16 @@ #include #include -#define rangelim(d, min, max) ((d) < (min) ? (min) : ((d) > (max) ? (max) : (d))) -#define myfloor(x) ((x) < 0.0 ? (int)(x) - 1 : (int)(x)) -// The naive swap performs better than the xor version -#define SWAP(t, x, y) do { \ - t temp = x; \ - x = y; \ - y = temp; \ -} while (0) +// Like std::clamp but allows mismatched types +template +inline constexpr T rangelim(const T &d, const T2 &min, const T3 &max) +{ + if (d < (T)min) + return (T)min; + if (d > (T)max) + return (T)max; + return d; +} // Maximum radius of a block. The magic number is // sqrt(3.0) / 2.0 in literal form. @@ -113,23 +115,24 @@ inline bool isInArea(v3s16 p, v3s16 d) ); } -inline void sortBoxVerticies(v3s16 &p1, v3s16 &p2) { +inline void sortBoxVerticies(v3s16 &p1, v3s16 &p2) +{ if (p1.X > p2.X) - SWAP(s16, p1.X, p2.X); + std::swap(p1.X, p2.X); if (p1.Y > p2.Y) - SWAP(s16, p1.Y, p2.Y); + std::swap(p1.Y, p2.Y); if (p1.Z > p2.Z) - SWAP(s16, p1.Z, p2.Z); + std::swap(p1.Z, p2.Z); } inline v3s16 componentwise_min(const v3s16 &a, const v3s16 &b) { - return v3s16(MYMIN(a.X, b.X), MYMIN(a.Y, b.Y), MYMIN(a.Z, b.Z)); + return v3s16(std::min(a.X, b.X), std::min(a.Y, b.Y), std::min(a.Z, b.Z)); } inline v3s16 componentwise_max(const v3s16 &a, const v3s16 &b) { - return v3s16(MYMAX(a.X, b.X), MYMAX(a.Y, b.Y), MYMAX(a.Z, b.Z)); + return v3s16(std::max(a.X, b.X), std::max(a.Y, b.Y), std::max(a.Z, b.Z)); } /// @brief Describes a grid with given step, oirginating at (0,0,0) @@ -290,7 +293,8 @@ inline s32 myround(f32 f) return (s32)(f < 0.f ? (f - 0.5f) : (f + 0.5f)); } -inline constexpr f32 sqr(f32 f) +template +inline constexpr T sqr(T f) { return f * f; } @@ -322,23 +326,15 @@ inline v3s16 doubleToInt(v3d p, double d) */ inline v3f intToFloat(v3s16 p, f32 d) { - return v3f( - (f32)p.X * d, - (f32)p.Y * d, - (f32)p.Z * d - ); + return v3f::from(p) * d; } // Random helper. Usually d=BS inline aabb3f getNodeBox(v3s16 p, float d) { return aabb3f( - (float)p.X * d - 0.5f * d, - (float)p.Y * d - 0.5f * d, - (float)p.Z * d - 0.5f * d, - (float)p.X * d + 0.5f * d, - (float)p.Y * d + 0.5f * d, - (float)p.Z * d + 0.5f * d + v3f::from(p) * d - 0.5f * d, + v3f::from(p) * d + 0.5f * d ); } @@ -410,14 +406,15 @@ inline float cycle_shift(float value, float by = 0, float max = 1) return value + by; } -inline bool is_power_of_two(u32 n) +constexpr inline bool is_power_of_two(u32 n) { return n != 0 && (n & (n - 1)) == 0; } // Compute next-higher power of 2 efficiently, e.g. for power-of-2 texture sizes. // Public Domain: https://graphics.stanford.edu/~seander/bithacks.html#RoundUpPowerOf2 -inline u32 npot2(u32 orig) { +constexpr inline u32 npot2(u32 orig) +{ orig--; orig |= orig >> 1; orig |= orig >> 2; diff --git a/src/util/string.cpp b/src/util/string.cpp index 0dbb9c0d3..c08421d55 100644 --- a/src/util/string.cpp +++ b/src/util/string.cpp @@ -139,16 +139,6 @@ std::string wide_to_utf8(std::wstring_view input) return out; } -void wide_add_codepoint(std::wstring &result, char32_t codepoint) -{ - if ((0xD800 <= codepoint && codepoint <= 0xDFFF) || (0x10FFFF < codepoint)) { - // Invalid codepoint, replace with unicode replacement character - result.push_back(0xFFFD); - return; - } - result.push_back(codepoint); -} - #else // _WIN32 std::wstring utf8_to_wide(std::string_view input) @@ -175,32 +165,25 @@ std::string wide_to_utf8(std::wstring_view input) return out; } +#endif // _WIN32 + void wide_add_codepoint(std::wstring &result, char32_t codepoint) { - if (codepoint < 0x10000) { - if (0xD800 <= codepoint && codepoint <= 0xDFFF) { - // Invalid codepoint, part of a surrogate pair - // Replace with unicode replacement character - result.push_back(0xFFFD); - return; - } - result.push_back((wchar_t) codepoint); - return; - } - codepoint -= 0x10000; - if (codepoint >= 0x100000) { - // original codepoint was above 0x10FFFF, so invalid - // replace with unicode replacement character + if ((0xD800 <= codepoint && codepoint <= 0xDFFF) || codepoint > 0x10FFFF) { + // Invalid codepoint, replace with unicode replacement character result.push_back(0xFFFD); return; } - result.push_back((wchar_t) ((codepoint >> 10) | 0xD800)); - result.push_back((wchar_t) ((codepoint & 0x3FF) | 0xDC00)); + if constexpr (sizeof(wchar_t) == 2) { // Surrogate encoding needed? + if (codepoint > 0xffff) { + result.push_back((wchar_t) ((codepoint >> 10) | 0xD800)); + result.push_back((wchar_t) ((codepoint & 0x3FF) | 0xDC00)); + return; + } + } + result.push_back((wchar_t) codepoint); } -#endif // _WIN32 - - std::string urlencode(std::string_view str) { // Encodes reserved URI characters by a percent sign diff --git a/src/util/thread.h b/src/util/thread.h index 1bc91e726..8b28bf72e 100644 --- a/src/util/thread.h +++ b/src/util/thread.h @@ -92,22 +92,19 @@ public: void add(const Key &key, Caller caller, CallerData callerdata, ResultQueue *dest) { - typename std::deque >::iterator i; - typename std::list >::iterator j; - { MutexAutoLock lock(m_queue.getMutex()); /* If the caller is already on the list, only update CallerData */ - for (i = m_queue.getQueue().begin(); i != m_queue.getQueue().end(); ++i) { - GetRequest &request = *i; + for (auto i = m_queue.getQueue().begin(); i != m_queue.getQueue().end(); ++i) { + auto &request = *i; if (request.key != key) continue; - for (j = request.callers.begin(); j != request.callers.end(); ++j) { - CallerInfo &ca = *j; + for (auto j = request.callers.begin(); j != request.callers.end(); ++j) { + auto &ca = *j; if (ca.caller == caller) { ca.data = callerdata; return; @@ -150,10 +147,9 @@ public: void pushResult(GetRequest req, T res) { - for (typename std::list >::iterator - i = req.callers.begin(); + for (auto i = req.callers.begin(); i != req.callers.end(); ++i) { - CallerInfo &ca = *i; + auto &ca = *i; GetResult result;