From 6625cf0e574406425c35d33110f8d99617e902df Mon Sep 17 00:00:00 2001 From: Mikko Rasa Date: Sat, 10 Dec 2022 02:15:40 +0200 Subject: [PATCH] Use default member initializers where possible --- source/http/client.cpp | 11 ----------- source/http/client.h | 17 ++++++++--------- source/http/message.cpp | 6 ------ source/http/message.h | 8 ++++---- source/http/server.cpp | 13 +++---------- source/http/server.h | 12 ++++++------ source/net/clientsocket.cpp | 6 +----- source/net/clientsocket.h | 6 +++--- source/net/communicator.cpp | 12 ++---------- source/net/communicator.h | 14 +++++++------- source/net/inet.cpp | 6 ------ source/net/inet.h | 6 +++--- source/net/inet6.cpp | 6 ------ source/net/inet6.h | 6 +++--- source/net/protocol.cpp | 5 ----- source/net/protocol.h | 6 +++--- source/net/resolve.cpp | 15 ++------------- source/net/resolve.h | 16 +++++++--------- source/net/sockaddr.cpp | 3 +-- source/net/sockaddr_private.h | 2 +- source/net/socket.cpp | 6 ++---- source/net/socket.h | 4 ++-- source/net/streamserversocket.cpp | 3 +-- source/net/streamserversocket.h | 2 +- source/net/unix.cpp | 5 ----- source/net/unix.h | 4 ++-- source/net/unix/unix.cpp | 3 +-- source/net/windows/unix.cpp | 3 +-- 28 files changed, 64 insertions(+), 142 deletions(-) diff --git a/source/http/client.cpp b/source/http/client.cpp index 705f4cb..073656d 100644 --- a/source/http/client.cpp +++ b/source/http/client.cpp @@ -11,17 +11,6 @@ using namespace std; namespace Msp { namespace Http { -Client::Client(): - sock(0), - event_disp(0), - resolver(0), - resolve_listener(0), - resolve_tag(0), - user_agent("libmsphttp/0.1"), - request(0), - response(0) -{ } - Client::~Client() { delete sock; diff --git a/source/http/client.h b/source/http/client.h index e0b9b64..c88ce32 100644 --- a/source/http/client.h +++ b/source/http/client.h @@ -30,20 +30,19 @@ private: void resolve_failed(unsigned, const std::exception &); }; - Net::StreamSocket *sock; - IO::EventDispatcher *event_disp; - Net::Resolver *resolver; - ResolveListener *resolve_listener; - unsigned resolve_tag; - std::string user_agent; - Request *request; - Response *response; + Net::StreamSocket *sock = 0; + IO::EventDispatcher *event_disp = 0; + Net::Resolver *resolver = 0; + ResolveListener *resolve_listener = 0; + unsigned resolve_tag = 0; + std::string user_agent = "libmspnet/1.0"; + Request *request = 0; + Response *response = 0; std::string in_buf; Client(const Client &); Client &operator=(const Client &); public: - Client(); ~Client(); void use_event_dispatcher(IO::EventDispatcher *); diff --git a/source/http/message.cpp b/source/http/message.cpp index 99d1497..4f17017 100644 --- a/source/http/message.cpp +++ b/source/http/message.cpp @@ -9,12 +9,6 @@ using namespace std; namespace Msp { namespace Http { -Message::Message(): - http_version(0x11), - chunk_length(0), - complete(false) -{ } - void Message::set_header(const string &hdr, const string &val) { headers[normalize_header_name(hdr)] = val; diff --git a/source/http/message.h b/source/http/message.h index 7acce90..7984372 100644 --- a/source/http/message.h +++ b/source/http/message.h @@ -14,14 +14,14 @@ class Message protected: typedef std::map HeaderMap; - Version http_version; + Version http_version = 0x11; HeaderMap headers; std::string content; - std::string::size_type chunk_length; - bool complete; + std::string::size_type chunk_length = 0; + bool complete = false; Variant user_data; - Message(); + Message() = default; public: virtual ~Message() = default; diff --git a/source/http/server.cpp b/source/http/server.cpp index c52bec4..223f6c9 100644 --- a/source/http/server.cpp +++ b/source/http/server.cpp @@ -18,13 +18,11 @@ namespace Msp { namespace Http { Server::Server(): - sock(Net::INET6), - event_disp(0) + sock(Net::INET6) { } Server::Server(unsigned port): - sock(Net::INET6), - event_disp(0) + sock(Net::INET6) { listen(port); } @@ -258,12 +256,7 @@ Server::Client &Server::get_client_by_response(Response &resp) Server::Client::Client(RefPtr s): - sock(s), - request(0), - response(0), - keepalive(false), - async(false), - stale(false) + sock(s) { } Server::Client::~Client() diff --git a/source/http/server.h b/source/http/server.h index 4835de0..d55b774 100644 --- a/source/http/server.h +++ b/source/http/server.h @@ -22,11 +22,11 @@ private: { RefPtr sock; std::string in_buf; - Request *request; - Response *response; - bool keepalive; - bool async; - bool stale; + Request *request = 0; + Response *response = 0; + bool keepalive = false; + bool async = false; + bool stale = false; Client(RefPtr); ~Client(); @@ -35,7 +35,7 @@ private: Net::StreamServerSocket sock; std::list clients; std::map responses; - IO::EventDispatcher *event_disp; + IO::EventDispatcher *event_disp = 0; public: Server(); diff --git a/source/net/clientsocket.cpp b/source/net/clientsocket.cpp index 9cabeef..c400f80 100644 --- a/source/net/clientsocket.cpp +++ b/source/net/clientsocket.cpp @@ -7,15 +7,11 @@ namespace Msp { namespace Net { ClientSocket::ClientSocket(Family af, int type, int proto): - Socket(af, type, proto), - connecting(false), - connected(false), - peer_addr(0) + Socket(af, type, proto) { } ClientSocket::ClientSocket(const Private &p, const SockAddr &paddr): Socket(p), - connecting(false), connected(true), peer_addr(paddr.copy()) { } diff --git a/source/net/clientsocket.h b/source/net/clientsocket.h index 672c32b..0406540 100644 --- a/source/net/clientsocket.h +++ b/source/net/clientsocket.h @@ -16,9 +16,9 @@ public: sigc::signal signal_connect_finished; protected: - bool connecting; - bool connected; - SockAddr *peer_addr; + bool connecting = false; + bool connected = false; + SockAddr *peer_addr = 0; ClientSocket(const Private &, const SockAddr &); ClientSocket(Family, int, int); diff --git a/source/net/communicator.cpp b/source/net/communicator.cpp index 05ef0ea..f77ada2 100644 --- a/source/net/communicator.cpp +++ b/source/net/communicator.cpp @@ -30,18 +30,13 @@ HandshakeProtocol::HandshakeProtocol(): class HandshakeReceiver: public PacketReceiver { private: - uint64_t hash; + uint64_t hash = 0; public: - HandshakeReceiver(); uint64_t get_hash() const { return hash; } void receive(const Handshake &) override; }; -HandshakeReceiver::HandshakeReceiver(): - hash(0) -{ } - void HandshakeReceiver::receive(const Handshake &shake) { hash = shake.hash; @@ -57,13 +52,10 @@ Communicator::Communicator(StreamSocket &s, const Protocol &p, ReceiverBase &r): socket(s), protocol(p), receiver(r), - handshake_status(0), - buf_size(65536), in_buf(new char[buf_size]), in_begin(in_buf), in_end(in_buf), - out_buf(new char[buf_size]), - good(true) + out_buf(new char[buf_size]) { socket.signal_data_available.connect(sigc::mem_fun(this, &Communicator::data_available)); } diff --git a/source/net/communicator.h b/source/net/communicator.h index 575ec84..322d095 100644 --- a/source/net/communicator.h +++ b/source/net/communicator.h @@ -33,13 +33,13 @@ private: StreamSocket &socket; const Protocol &protocol; ReceiverBase &receiver; - int handshake_status; - std::size_t buf_size; - char *in_buf; - char *in_begin; - char *in_end; - char *out_buf; - bool good; + int handshake_status = 0; + std::size_t buf_size = 65536; + char *in_buf = 0; + char *in_begin = 0; + char *in_end = 0; + char *out_buf = 0; + bool good = true; public: Communicator(StreamSocket &, const Protocol &, ReceiverBase &); diff --git a/source/net/inet.cpp b/source/net/inet.cpp index 8e75cd8..dbbd12d 100644 --- a/source/net/inet.cpp +++ b/source/net/inet.cpp @@ -8,12 +8,6 @@ using namespace std; namespace Msp { namespace Net { -InetAddr::InetAddr(): - port(0) -{ - fill(addr, addr+4, 0); -} - InetAddr::InetAddr(const SysAddr &sa) { const sockaddr_in &sai = reinterpret_cast(sa.addr); diff --git a/source/net/inet.h b/source/net/inet.h index 67bb610..517b111 100644 --- a/source/net/inet.h +++ b/source/net/inet.h @@ -12,11 +12,11 @@ Address class for IPv4 sockets. class InetAddr: public SockAddr { private: - unsigned char addr[4]; - unsigned port; + unsigned char addr[4] = { }; + unsigned port = 0; public: - InetAddr(); + InetAddr() = default; InetAddr(const SysAddr &); InetAddr *copy() const override { return new InetAddr(*this); } diff --git a/source/net/inet6.cpp b/source/net/inet6.cpp index 77d7570..8ddf1d5 100644 --- a/source/net/inet6.cpp +++ b/source/net/inet6.cpp @@ -8,12 +8,6 @@ using namespace std; namespace Msp { namespace Net { -Inet6Addr::Inet6Addr(): - port(0) -{ - fill(addr, addr+16, 0); -} - Inet6Addr::Inet6Addr(const SysAddr &sa) { const sockaddr_in6 &sai6 = reinterpret_cast(sa.addr); diff --git a/source/net/inet6.h b/source/net/inet6.h index fb07eec..3b48a63 100644 --- a/source/net/inet6.h +++ b/source/net/inet6.h @@ -9,11 +9,11 @@ namespace Net { class Inet6Addr: public SockAddr { private: - unsigned char addr[16]; - unsigned port; + unsigned char addr[16] = { }; + unsigned port = 0; public: - Inet6Addr(); + Inet6Addr() = default; Inet6Addr(const SysAddr &); Inet6Addr *copy() const override { return new Inet6Addr(*this); } diff --git a/source/net/protocol.cpp b/source/net/protocol.cpp index 81e9d14..bcad032 100644 --- a/source/net/protocol.cpp +++ b/source/net/protocol.cpp @@ -166,11 +166,6 @@ Protocol::PacketDefBase::PacketDefBase(unsigned i): { } -Protocol::PacketHeader::PacketHeader(): - type(0), - length(0) -{ } - Protocol::PacketHeader::PacketHeader(uint16_t t, uint16_t l): type(t), length(l) diff --git a/source/net/protocol.h b/source/net/protocol.h index e867c0a..6d15a33 100644 --- a/source/net/protocol.h +++ b/source/net/protocol.h @@ -213,10 +213,10 @@ private: struct PacketHeader { - std::uint16_t type; - std::uint16_t length; + std::uint16_t type = 0; + std::uint16_t length = 0; - PacketHeader(); + PacketHeader() = default; PacketHeader(std::uint16_t, std::uint16_t); }; diff --git a/source/net/resolve.cpp b/source/net/resolve.cpp index c30d62a..fd59cbb 100644 --- a/source/net/resolve.cpp +++ b/source/net/resolve.cpp @@ -81,9 +81,7 @@ SockAddr *resolve(const string &str, Family family) } -Resolver::Resolver(): - event_disp(0), - next_tag(1) +Resolver::Resolver() { thread.get_notify_pipe().signal_data_available.connect(sigc::mem_fun(this, &Resolver::task_done)); } @@ -147,18 +145,9 @@ void Resolver::task_done() } -Resolver::Task::Task(): - tag(0), - family(UNSPEC), - addr(0), - error(0) -{ } - - Resolver::WorkerThread::WorkerThread(): Thread("Resolver"), - sem(1), - done(false) + sem(1) { launch(); } diff --git a/source/net/resolve.h b/source/net/resolve.h index b006aff..6a0479e 100644 --- a/source/net/resolve.h +++ b/source/net/resolve.h @@ -36,14 +36,12 @@ class Resolver private: struct Task { - unsigned tag; + unsigned tag = 0; std::string host; std::string serv; - Family family; - SockAddr *addr; - std::runtime_error *error; - - Task(); + Family family = UNSPEC; + SockAddr *addr = 0; + std::runtime_error *error = 0; bool is_complete() const { return addr || error; } }; @@ -55,7 +53,7 @@ private: Mutex queue_mutex; Semaphore sem; IO::Pipe notify_pipe; - bool done; + bool done = false; public: WorkerThread(); @@ -76,9 +74,9 @@ public: sigc::signal signal_resolve_failed; private: - IO::EventDispatcher *event_disp; + IO::EventDispatcher *event_disp = 0; WorkerThread thread; - unsigned next_tag; + unsigned next_tag = 1; public: Resolver(); diff --git a/source/net/sockaddr.cpp b/source/net/sockaddr.cpp index db436fe..36e9c68 100644 --- a/source/net/sockaddr.cpp +++ b/source/net/sockaddr.cpp @@ -25,8 +25,7 @@ SockAddr *SockAddr::new_from_sys(const SysAddr &sa) } } -SockAddr::SysAddr::SysAddr(): - size(sizeof(sockaddr_storage)) +SockAddr::SysAddr::SysAddr() { addr.ss_family = AF_UNSPEC; } diff --git a/source/net/sockaddr_private.h b/source/net/sockaddr_private.h index 909ef92..5fca410 100644 --- a/source/net/sockaddr_private.h +++ b/source/net/sockaddr_private.h @@ -14,7 +14,7 @@ namespace Net { struct SockAddr::SysAddr { struct sockaddr_storage addr; - socklen_t size; + socklen_t size = sizeof(sockaddr_storage); SysAddr(); }; diff --git a/source/net/socket.cpp b/source/net/socket.cpp index 9fd468e..a983a2e 100644 --- a/source/net/socket.cpp +++ b/source/net/socket.cpp @@ -11,8 +11,7 @@ namespace Msp { namespace Net { Socket::Socket(const Private &p): - priv(new Private), - local_addr(0) + priv(new Private) { mode = IO::M_RDWR; @@ -26,8 +25,7 @@ Socket::Socket(const Private &p): } Socket::Socket(Family af, int type, int proto): - priv(new Private), - local_addr(0) + priv(new Private) { mode = IO::M_RDWR; diff --git a/source/net/socket.h b/source/net/socket.h index d917bd8..6e0a497 100644 --- a/source/net/socket.h +++ b/source/net/socket.h @@ -30,8 +30,8 @@ protected: struct Private; - Private *priv; - SockAddr *local_addr; + Private *priv = 0; + SockAddr *local_addr = 0; Socket(const Private &); Socket(Family, int, int); diff --git a/source/net/streamserversocket.cpp b/source/net/streamserversocket.cpp index 9b583a1..b91491d 100644 --- a/source/net/streamserversocket.cpp +++ b/source/net/streamserversocket.cpp @@ -14,8 +14,7 @@ namespace Msp { namespace Net { StreamServerSocket::StreamServerSocket(Family af, int proto): - ServerSocket(af, SOCK_STREAM, proto), - listening(false) + ServerSocket(af, SOCK_STREAM, proto) { } void StreamServerSocket::listen(const SockAddr &addr, unsigned backlog) diff --git a/source/net/streamserversocket.h b/source/net/streamserversocket.h index d38c2eb..bd2487d 100644 --- a/source/net/streamserversocket.h +++ b/source/net/streamserversocket.h @@ -10,7 +10,7 @@ namespace Net { class StreamServerSocket: public ServerSocket { private: - bool listening; + bool listening = false; public: StreamServerSocket(Family, int = 0); diff --git a/source/net/unix.cpp b/source/net/unix.cpp index fe00020..1da822b 100644 --- a/source/net/unix.cpp +++ b/source/net/unix.cpp @@ -5,11 +5,6 @@ using namespace std; namespace Msp { namespace Net { -UnixAddr::UnixAddr(): - abstract(false) -{ -} - string UnixAddr::str() const { string result = "unix:"; diff --git a/source/net/unix.h b/source/net/unix.h index ec65164..a7431a9 100644 --- a/source/net/unix.h +++ b/source/net/unix.h @@ -10,10 +10,10 @@ class UnixAddr: public SockAddr { private: std::string path; - bool abstract; + bool abstract = false; public: - UnixAddr(); + UnixAddr() = default; UnixAddr(const SysAddr &); UnixAddr(const std::string &, bool = false); diff --git a/source/net/unix/unix.cpp b/source/net/unix/unix.cpp index 0e88c2c..eba7fb6 100644 --- a/source/net/unix/unix.cpp +++ b/source/net/unix/unix.cpp @@ -9,8 +9,7 @@ using namespace std; namespace Msp { namespace Net { -UnixAddr::UnixAddr(const SysAddr &sa): - abstract(false) +UnixAddr::UnixAddr(const SysAddr &sa) { const sockaddr_un &sau = reinterpret_cast(sa.addr); if(static_cast(sa.size)>sizeof(sa_family_t)) diff --git a/source/net/windows/unix.cpp b/source/net/windows/unix.cpp index 052710e..6a56ee0 100644 --- a/source/net/windows/unix.cpp +++ b/source/net/windows/unix.cpp @@ -8,8 +8,7 @@ using namespace std; namespace Msp { namespace Net { -UnixAddr::UnixAddr(const SysAddr &): - abstract(false) +UnixAddr::UnixAddr(const SysAddr &) { throw unsupported("AF_UNIX"); } -- 2.43.0