Skip to content

Commit 326315f

Browse files
committed
Implicit conversions clean-up
1 parent c94b8cb commit 326315f

File tree

9 files changed

+120
-98
lines changed

9 files changed

+120
-98
lines changed

trantor/net/inner/Socket.cc

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -113,16 +113,15 @@ void Socket::closeWrite()
113113
int Socket::read(char *buffer, uint64_t len)
114114
{
115115
#ifndef _WIN32
116-
return ::read(sockFd_, buffer, len);
116+
return static_cast<int>(::read(sockFd_, buffer, len));
117117
#else
118118
return recv(sockFd_, buffer, static_cast<int>(len), 0);
119119
#endif
120120
}
121121

122122
struct sockaddr_in6 Socket::getLocalAddr(int sockfd)
123123
{
124-
struct sockaddr_in6 localaddr;
125-
memset(&localaddr, 0, sizeof(localaddr));
124+
struct sockaddr_in6 localaddr{};
126125
socklen_t addrlen = static_cast<socklen_t>(sizeof localaddr);
127126
if (::getsockname(sockfd,
128127
static_cast<struct sockaddr *>((void *)(&localaddr)),
@@ -135,8 +134,7 @@ struct sockaddr_in6 Socket::getLocalAddr(int sockfd)
135134

136135
struct sockaddr_in6 Socket::getPeerAddr(int sockfd)
137136
{
138-
struct sockaddr_in6 peeraddr;
139-
memset(&peeraddr, 0, sizeof(peeraddr));
137+
struct sockaddr_in6 peeraddr{};
140138
socklen_t addrlen = static_cast<socklen_t>(sizeof peeraddr);
141139
if (::getpeername(sockfd,
142140
static_cast<struct sockaddr *>((void *)(&peeraddr)),

trantor/utils/Date.cc

Lines changed: 24 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -53,20 +53,21 @@ const Date Date::date()
5353
{
5454
#ifndef _WIN32
5555
struct timeval tv;
56-
gettimeofday(&tv, NULL);
56+
gettimeofday(&tv, nullptr);
5757
int64_t seconds = tv.tv_sec;
5858
return Date(seconds * MICRO_SECONDS_PER_SEC + tv.tv_usec);
5959
#else
6060
timeval tv;
61-
gettimeofday(&tv, NULL);
61+
gettimeofday(&tv, nullptr);
6262
int64_t seconds = tv.tv_sec;
6363
return Date(seconds * MICRO_SECONDS_PER_SEC + tv.tv_usec);
6464
#endif
6565
}
6666
const Date Date::after(double second) const
6767
{
68-
return Date(static_cast<int64_t>(microSecondsSinceEpoch_ +
69-
second * MICRO_SECONDS_PER_SEC));
68+
return Date(microSecondsSinceEpoch_ +
69+
static_cast<int64_t>(
70+
second * static_cast<double>(MICRO_SECONDS_PER_SEC)));
7071
}
7172
const Date Date::roundSecond() const
7273
{
@@ -86,7 +87,7 @@ const Date Date::roundDay() const
8687
t.tm_hour = 0;
8788
t.tm_min = 0;
8889
t.tm_sec = 0;
89-
return Date(mktime(&t) * MICRO_SECONDS_PER_SEC);
90+
return Date(static_cast<int64_t>(mktime(&t)) * MICRO_SECONDS_PER_SEC);
9091
}
9192
struct tm Date::tmStruct() const
9293
{
@@ -281,8 +282,8 @@ std::string Date::toDbString() const
281282

282283
Date Date::fromDbStringLocal(const std::string &datetime)
283284
{
284-
unsigned int year = {0}, month = {0}, day = {0}, hour = {0}, minute = {0},
285-
second = {0}, microSecond = {0};
285+
unsigned int year = 0U, month = 0U, day = 0U, hour = 0U, minute = 0U,
286+
second = 0U, microSecond = 0U;
286287
std::vector<std::string> &&v = splitString(datetime, " ");
287288

288289
if (v.size() == 0)
@@ -315,16 +316,16 @@ Date Date::fromDbStringLocal(const std::string &datetime)
315316
// Format YYYY-MM-DD HH:MM:SS[.UUUUUU] is given
316317
try
317318
{
318-
year = std::stol(date[0]);
319-
month = std::stol(date[1]);
320-
day = std::stol(date[2]);
319+
year = static_cast<unsigned int>(std::stoul(date[0]));
320+
month = static_cast<unsigned int>(std::stoul(date[1]));
321+
day = static_cast<unsigned int>(std::stoul(date[2]));
321322
std::vector<std::string> time = splitString(v[1], ":");
322323
if (2 < time.size())
323324
{
324-
hour = std::stol(time[0]);
325-
minute = std::stol(time[1]);
325+
hour = static_cast<unsigned int>(std::stoul(time[0]));
326+
minute = static_cast<unsigned int>(std::stoul(time[1]));
326327
auto seconds = splitString(time[2], ".");
327-
second = std::stol(seconds[0]);
328+
second = static_cast<unsigned int>(std::stoul(seconds[0]));
328329
if (1 < seconds.size())
329330
{
330331
if (seconds[1].length() > 6)
@@ -335,7 +336,8 @@ Date Date::fromDbStringLocal(const std::string &datetime)
335336
{
336337
seconds[1].append(6 - seconds[1].length(), '0');
337338
}
338-
microSecond = std::stol(seconds[1]);
339+
microSecond =
340+
static_cast<unsigned int>(std::stoul(seconds[1]));
339341
}
340342
}
341343
}
@@ -358,7 +360,7 @@ Date Date::fromDbString(const std::string &datetime)
358360
std::string Date::toCustomFormattedStringLocal(const std::string &fmtStr,
359361
bool showMicroseconds) const
360362
{
361-
char buf[256] = {0};
363+
char buf[256]{};
362364
time_t seconds =
363365
static_cast<time_t>(microSecondsSinceEpoch_ / MICRO_SECONDS_PER_SEC);
364366
struct tm tm_time;
@@ -370,7 +372,7 @@ std::string Date::toCustomFormattedStringLocal(const std::string &fmtStr,
370372
strftime(buf, sizeof(buf), fmtStr.c_str(), &tm_time);
371373
if (!showMicroseconds)
372374
return std::string(buf);
373-
char decimals[12] = {0};
375+
char decimals[12]{};
374376
int microseconds =
375377
static_cast<int>(microSecondsSinceEpoch_ % MICRO_SECONDS_PER_SEC);
376378
snprintf(decimals, sizeof(decimals), ".%06d", microseconds);
@@ -388,12 +390,12 @@ Date::Date(unsigned int year,
388390
memset(&tm, 0, sizeof(tm));
389391
tm.tm_isdst = -1;
390392
time_t epoch;
391-
tm.tm_year = year - 1900;
392-
tm.tm_mon = month - 1;
393-
tm.tm_mday = day;
394-
tm.tm_hour = hour;
395-
tm.tm_min = minute;
396-
tm.tm_sec = second;
393+
tm.tm_year = static_cast<int>(year - 1900);
394+
tm.tm_mon = static_cast<int>(month - 1);
395+
tm.tm_mday = static_cast<int>(day);
396+
tm.tm_hour = static_cast<int>(hour);
397+
tm.tm_min = static_cast<int>(minute);
398+
tm.tm_sec = static_cast<int>(second);
397399
epoch = mktime(&tm);
398400
microSecondsSinceEpoch_ =
399401
static_cast<int64_t>(epoch) * MICRO_SECONDS_PER_SEC + microSecond;

trantor/utils/LogStream.cc

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
#include <string.h>
2222
#include <stdint.h>
2323
#include <stdio.h>
24-
#include <iostream>
2524

2625
using namespace trantor;
2726
using namespace trantor::detail;
@@ -30,33 +29,32 @@ namespace trantor
3029
{
3130
namespace detail
3231
{
33-
const char digits[] = "9876543210123456789";
34-
const char *zero = digits + 9;
32+
constexpr char digits[] = "9876543210123456789";
33+
constexpr const char *const zero = digits + 9;
3534

36-
const char digitsHex[] = "0123456789ABCDEF";
35+
constexpr const char digitsHex[] = "0123456789ABCDEF";
3736

3837
// Efficient Integer to String Conversions, by Matthew Wilson.
3938
template <typename T>
40-
size_t convert(char buf[], T value)
39+
size_t convert(char *buf, T value)
4140
{
42-
T i = value;
41+
if (value < 0)
42+
{
43+
*buf++ = '-';
44+
}
4345
char *p = buf;
4446

4547
do
4648
{
47-
int lsd = static_cast<int>(i % 10);
48-
i /= 10;
49+
int lsd = static_cast<int>(value % 10);
50+
value /= 10;
4951
*p++ = zero[lsd];
50-
} while (i != 0);
52+
} while (value != 0);
5153

52-
if (value < 0)
53-
{
54-
*p++ = '-';
55-
}
5654
*p = '\0';
5755
std::reverse(buf, p);
5856

59-
return p - buf;
57+
return static_cast<size_t>(p - buf);
6058
}
6159

6260
size_t convertHex(char buf[], uintptr_t value)
@@ -74,7 +72,7 @@ size_t convertHex(char buf[], uintptr_t value)
7472
*p = '\0';
7573
std::reverse(buf, p);
7674

77-
return p - buf;
75+
return static_cast<size_t>(p - buf);
7876
}
7977

8078
template class FixedBuffer<kSmallBuffer>;
@@ -211,7 +209,7 @@ LogStream &LogStream::operator<<(const double &v)
211209
if (buffer_.avail() >= kMaxNumericSize)
212210
{
213211
int len = snprintf(buffer_.current(), kMaxNumericSize, "%.12g", v);
214-
buffer_.add(len);
212+
buffer_.add(static_cast<size_t>(len));
215213
return *this;
216214
}
217215
else
@@ -222,7 +220,7 @@ LogStream &LogStream::operator<<(const double &v)
222220
auto oldLen = exBuffer_.length();
223221
exBuffer_.resize(oldLen + kMaxNumericSize);
224222
int len = snprintf(&(exBuffer_[oldLen]), kMaxNumericSize, "%.12g", v);
225-
exBuffer_.resize(oldLen + len);
223+
exBuffer_.resize(oldLen + static_cast<size_t>(len));
226224
return *this;
227225
}
228226

@@ -234,7 +232,7 @@ LogStream &LogStream::operator<<(const long double &v)
234232
if (buffer_.avail() >= kMaxNumericSize)
235233
{
236234
int len = snprintf(buffer_.current(), kMaxNumericSize, "%.12Lg", v);
237-
buffer_.add(len);
235+
buffer_.add(static_cast<size_t>(len));
238236
return *this;
239237
}
240238
else
@@ -245,15 +243,16 @@ LogStream &LogStream::operator<<(const long double &v)
245243
auto oldLen = exBuffer_.length();
246244
exBuffer_.resize(oldLen + kMaxNumericSize);
247245
int len = snprintf(&(exBuffer_[oldLen]), kMaxNumericSize, "%.12Lg", v);
248-
exBuffer_.resize(oldLen + len);
246+
exBuffer_.resize(oldLen + static_cast<size_t>(len));
249247
return *this;
250248
}
251249

252250
template <typename T>
253251
Fmt::Fmt(const char *fmt, T val)
254252
{
255-
length_ = snprintf(buf_, sizeof buf_, fmt, val);
256-
assert(static_cast<size_t>(length_) < sizeof buf_);
253+
length_ =
254+
static_cast<decltype(length_)>(snprintf(buf_, sizeof(buf_), fmt, val));
255+
assert(static_cast<size_t>(length_) < sizeof(buf_));
257256
}
258257

259258
// Explicit instantiations

trantor/utils/LogStream.h

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,20 @@
2121
#include <assert.h>
2222
#include <string.h> // memcpy
2323
#include <string>
24+
#include <cstdint>
2425

2526
namespace trantor
2627
{
2728
namespace detail
2829
{
30+
#if (__cplusplus >= 201703L)
31+
// Note: in C++17, static should be replaced by inline
32+
inline constexpr size_t kSmallBuffer{4000};
33+
inline constexpr size_t kLargeBuffer{4000 * 1000};
34+
#else
2935
static constexpr size_t kSmallBuffer{4000};
3036
static constexpr size_t kLargeBuffer{4000 * 1000};
37+
#endif
3138

3239
template <int SIZE>
3340
class TRANTOR_EXPORT FixedBuffer : NonCopyable
@@ -38,14 +45,19 @@ class TRANTOR_EXPORT FixedBuffer : NonCopyable
3845
setCookie(cookieStart);
3946
}
4047

48+
FixedBuffer(const FixedBuffer &) = delete;
49+
FixedBuffer &operator=(const FixedBuffer &) = delete;
50+
FixedBuffer(FixedBuffer &&) = delete;
51+
FixedBuffer &operator=(FixedBuffer &&) = delete;
52+
4153
~FixedBuffer()
4254
{
4355
setCookie(cookieEnd);
4456
}
4557

4658
bool append(const char * /*restrict*/ buf, size_t len)
4759
{
48-
if ((size_t)(avail()) > len)
60+
if (static_cast<size_t>(avail()) > len)
4961
{
5062
memcpy(cur_, buf, len);
5163
cur_ += len;
@@ -58,9 +70,9 @@ class TRANTOR_EXPORT FixedBuffer : NonCopyable
5870
{
5971
return data_;
6072
}
61-
int length() const
73+
size_t length() const
6274
{
63-
return static_cast<int>(cur_ - data_);
75+
return static_cast<size_t>(cur_ - data_);
6476
}
6577

6678
// write to data_ directly
@@ -97,8 +109,6 @@ class TRANTOR_EXPORT FixedBuffer : NonCopyable
97109
{
98110
return std::string(data_, length());
99111
}
100-
// StringPiece toStringPiece() const { return StringPiece(data_, length());
101-
// }
102112

103113
private:
104114
const char *end() const
@@ -154,8 +164,6 @@ class TRANTOR_EXPORT LogStream : NonCopyable
154164
return *this;
155165
}
156166

157-
// self& operator<<(signed char);
158-
// self& operator<<(unsigned char);
159167
template <int N>
160168
self &operator<<(const char (&buf)[N])
161169
{
@@ -248,7 +256,7 @@ class TRANTOR_EXPORT LogStream : NonCopyable
248256
std::string exBuffer_;
249257
};
250258

251-
class TRANTOR_EXPORT Fmt // : boost::noncopyable
259+
class TRANTOR_EXPORT Fmt
252260
{
253261
public:
254262
template <typename T>
@@ -258,14 +266,14 @@ class TRANTOR_EXPORT Fmt // : boost::noncopyable
258266
{
259267
return buf_;
260268
}
261-
int length() const
269+
uint8_t length() const
262270
{
263271
return length_;
264272
}
265273

266274
private:
267275
char buf_[48];
268-
int length_;
276+
uint8_t length_;
269277
};
270278

271279
inline LogStream &operator<<(LogStream &s, const Fmt &fmt)

trantor/utils/Logger.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ inline LogStream &operator<<(LogStream &s, T v)
8585

8686
inline LogStream &operator<<(LogStream &s, const Logger::SourceFile &v)
8787
{
88-
s.append(v.data_, v.size_);
88+
s.append(v.data_, static_cast<size_t>(v.size_));
8989
return s;
9090
}
9191

@@ -444,7 +444,7 @@ RawLogger::~RawLogger()
444444
}
445445
else
446446
{
447-
auto &oFunc = Logger::outputFunc_(index_);
447+
auto &oFunc = Logger::outputFunc_(static_cast<size_t>(index_));
448448
if (!oFunc)
449449
return;
450450
oFunc(logStream_.bufferData(), logStream_.bufferLength());

trantor/utils/Logger.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,8 +133,8 @@ class TRANTOR_EXPORT Logger : public NonCopyable
133133
}
134134
else
135135
{
136-
outputFunc_(index) = outputFunc;
137-
flushFunc_(index) = flushFunc;
136+
outputFunc_(static_cast<size_t>(index)) = outputFunc;
137+
flushFunc_(static_cast<size_t>(index)) = flushFunc;
138138
}
139139
}
140140

0 commit comments

Comments
 (0)