From 88b436e6a9c98af7215bd115e1b7a3f1a1db99d3 Mon Sep 17 00:00:00 2001 From: Loïc Blot Date: Sat, 19 Aug 2017 22:23:47 +0200 Subject: Code modernization: subfolders (#6283) * Code modernization: subfolders Modernize various code on subfolders client, network, script, threading, unittests, util * empty function * default constructor/destructor * for range-based loops * use emplace_back instead of push_back * C++ STL header style * Make connection.cpp readable in a pointed place + typo --- src/util/auth.cpp | 2 +- src/util/base64.cpp | 5 +++-- src/util/container.h | 21 +++++++++++---------- src/util/numeric.cpp | 2 +- src/util/pointedthing.h | 2 +- src/util/serialize.cpp | 20 +++++++++++--------- src/util/serialize.h | 2 +- src/util/sha1.cpp | 14 +++++++------- src/util/srp.cpp | 9 +++++---- src/util/thread.h | 6 +++--- 10 files changed, 44 insertions(+), 39 deletions(-) (limited to 'src/util') diff --git a/src/util/auth.cpp b/src/util/auth.cpp index c329e36e6..3dd5a9afa 100644 --- a/src/util/auth.cpp +++ b/src/util/auth.cpp @@ -23,7 +23,7 @@ with this program; if not, write to the Free Software Foundation, Inc., #include "base64.h" #include "sha1.h" #include "srp.h" -#include "string.h" +#include "util/string.h" #include "debug.h" // Get an sha-1 hash of the player's name combined with diff --git a/src/util/base64.cpp b/src/util/base64.cpp index e14de7de2..c75f98598 100644 --- a/src/util/base64.cpp +++ b/src/util/base64.cpp @@ -40,8 +40,9 @@ static inline bool is_base64(unsigned char c) { bool base64_is_valid(std::string const& s) { - for(size_t i=0; i class MutexedMap { public: - MutexedMap() {} + MutexedMap() = default; void set(const Key &name, const Value &value) { @@ -128,7 +128,8 @@ public: template friend class RequestQueue; - MutexedQueue() {} + MutexedQueue() = default; + bool empty() const { MutexAutoLock lock(m_mutex); @@ -153,9 +154,9 @@ public: T t = m_queue.front(); m_queue.pop_front(); return t; - } else { - return T(); } + + return T(); } T pop_front(u32 wait_time_max_ms) @@ -166,9 +167,9 @@ public: T t = m_queue.front(); m_queue.pop_front(); return t; - } else { - throw ItemNotFoundException("MutexedQueue: queue is empty"); } + + throw ItemNotFoundException("MutexedQueue: queue is empty"); } T pop_frontNoEx() @@ -190,9 +191,9 @@ public: T t = m_queue.back(); m_queue.pop_back(); return t; - } else { - throw ItemNotFoundException("MutexedQueue: queue is empty"); } + + throw ItemNotFoundException("MutexedQueue: queue is empty"); } /* this version of pop_back returns a empty element of T on timeout. @@ -206,9 +207,9 @@ public: T t = m_queue.back(); m_queue.pop_back(); return t; - } else { - return T(); } + + return T(); } T pop_backNoEx() diff --git a/src/util/numeric.cpp b/src/util/numeric.cpp index e6a9cb75e..a48a72a8a 100644 --- a/src/util/numeric.cpp +++ b/src/util/numeric.cpp @@ -23,7 +23,7 @@ with this program; if not, write to the Free Software Foundation, Inc., #include "../constants.h" // BS, MAP_BLOCKSIZE #include "../noise.h" // PseudoRandom, PcgRandom #include "../threading/mutex_auto_lock.h" -#include +#include // myrand diff --git a/src/util/pointedthing.h b/src/util/pointedthing.h index c13436fcd..bd95ef1f3 100644 --- a/src/util/pointedthing.h +++ b/src/util/pointedthing.h @@ -81,7 +81,7 @@ struct PointedThing f32 distanceSq = 0; //! Constructor for POINTEDTHING_NOTHING - PointedThing() {}; + PointedThing() = default; //! Constructor for POINTEDTHING_NODE PointedThing(const v3s16 &under, const v3s16 &above, const v3s16 &real_under, const v3f &point, const v3s16 &normal, diff --git a/src/util/serialize.cpp b/src/util/serialize.cpp index 75843cb1b..49db9cf85 100644 --- a/src/util/serialize.cpp +++ b/src/util/serialize.cpp @@ -156,8 +156,8 @@ std::string serializeWideString(const std::wstring &plain) writeU16((u8 *)buf, plain.size()); s.append(buf, 2); - for (u32 i = 0; i < plain.size(); i++) { - writeU16((u8 *)buf, plain[i]); + for (wchar_t i : plain) { + writeU16((u8 *)buf, i); s.append(buf, 2); } return s; @@ -246,8 +246,7 @@ std::string serializeJsonString(const std::string &plain) std::ostringstream os(std::ios::binary); os << "\""; - for (size_t i = 0; i < plain.size(); i++) { - char c = plain[i]; + for (char c : plain) { switch (c) { case '"': os << "\\\""; @@ -308,7 +307,9 @@ std::string deSerializeJsonString(std::istream &is) if (c == '"') { return os.str(); - } else if (c == '\\') { + } + + if (c == '\\') { c2 = is.get(); if (is.eof()) throw SerializationError("JSON string ended prematurely"); @@ -390,17 +391,18 @@ std::string deSerializeJsonStringIfNeeded(std::istream &is) // Found end of word is.unget(); break; - } else { - tmp_os << c; } + + tmp_os << c; } expect_initial_quote = false; } if (is_json) { std::istringstream tmp_is(tmp_os.str(), std::ios::binary); return deSerializeJsonString(tmp_is); - } else - return tmp_os.str(); + } + + return tmp_os.str(); } //// diff --git a/src/util/serialize.h b/src/util/serialize.h index 0607ff37a..0a1c96205 100644 --- a/src/util/serialize.h +++ b/src/util/serialize.h @@ -37,7 +37,7 @@ with this program; if not, write to the Free Software Foundation, Inc., #include #endif #endif -#include // for memcpy +#include // for memcpy #include #include #include diff --git a/src/util/sha1.cpp b/src/util/sha1.cpp index c04b6c0c0..d61b262af 100644 --- a/src/util/sha1.cpp +++ b/src/util/sha1.cpp @@ -24,10 +24,10 @@ SOFTWARE. */ -#include -#include -#include -#include +#include +#include +#include +#include #include "sha1.h" @@ -96,7 +96,7 @@ void SHA1::process() +(bytes[t*4 + 2] << 8) + bytes[t*4 + 3]; for(; t< 80; t++ ) W[t] = lrot( W[t-3]^W[t-8]^W[t-14]^W[t-16], 1 ); - + /* main loop */ Uint32 temp; for( t = 0; t < 80; t++ ) @@ -154,7 +154,7 @@ void SHA1::addBytes( const char* data, int num ) num -= toCopy; data += toCopy; unprocessedBytes += toCopy; - + // there is a full block if( unprocessedBytes == 64 ) process(); } @@ -168,7 +168,7 @@ unsigned char* SHA1::getDigest() Uint32 totalBitsH = size >> 29; // add 0x80 to the message addBytes( "\x80", 1 ); - + unsigned char footer[64] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, diff --git a/src/util/srp.cpp b/src/util/srp.cpp index e0ddb9020..aa8dcb4a7 100644 --- a/src/util/srp.cpp +++ b/src/util/srp.cpp @@ -31,13 +31,14 @@ #include #include #else - #include + #include + #endif // clang-format on -#include -#include -#include +#include +#include +#include #include diff --git a/src/util/thread.h b/src/util/thread.h index fbd04c08e..52c6c6aba 100644 --- a/src/util/thread.h +++ b/src/util/thread.h @@ -78,8 +78,8 @@ public: template class GetRequest { public: - GetRequest() {} - ~GetRequest() {} + GetRequest() = default; + ~GetRequest() = default; GetRequest(const Key &a_key): key(a_key) { @@ -189,7 +189,7 @@ class UpdateThread : public Thread { public: UpdateThread(const std::string &name) : Thread(name + "Update") {} - ~UpdateThread() {} + ~UpdateThread() = default; void deferUpdate() { m_update_sem.post(); } -- cgit v1.2.3