Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 3 additions & 5 deletions trantor/net/inner/Socket.cc
Original file line number Diff line number Diff line change
Expand Up @@ -113,16 +113,15 @@ void Socket::closeWrite()
int Socket::read(char *buffer, uint64_t len)
{
#ifndef _WIN32
return ::read(sockFd_, buffer, len);
return static_cast<int>(::read(sockFd_, buffer, len));
#else
return recv(sockFd_, buffer, static_cast<int>(len), 0);
#endif
}

struct sockaddr_in6 Socket::getLocalAddr(int sockfd)
{
struct sockaddr_in6 localaddr;
memset(&localaddr, 0, sizeof(localaddr));
struct sockaddr_in6 localaddr{};
socklen_t addrlen = static_cast<socklen_t>(sizeof localaddr);
if (::getsockname(sockfd,
static_cast<struct sockaddr *>((void *)(&localaddr)),
Expand All @@ -135,8 +134,7 @@ struct sockaddr_in6 Socket::getLocalAddr(int sockfd)

struct sockaddr_in6 Socket::getPeerAddr(int sockfd)
{
struct sockaddr_in6 peeraddr;
memset(&peeraddr, 0, sizeof(peeraddr));
struct sockaddr_in6 peeraddr{};
socklen_t addrlen = static_cast<socklen_t>(sizeof peeraddr);
if (::getpeername(sockfd,
static_cast<struct sockaddr *>((void *)(&peeraddr)),
Expand Down
46 changes: 24 additions & 22 deletions trantor/utils/Date.cc
Original file line number Diff line number Diff line change
Expand Up @@ -53,20 +53,21 @@ const Date Date::date()
{
#ifndef _WIN32
struct timeval tv;
gettimeofday(&tv, NULL);
gettimeofday(&tv, nullptr);
int64_t seconds = tv.tv_sec;
return Date(seconds * MICRO_SECONDS_PER_SEC + tv.tv_usec);
#else
timeval tv;
gettimeofday(&tv, NULL);
gettimeofday(&tv, nullptr);
int64_t seconds = tv.tv_sec;
return Date(seconds * MICRO_SECONDS_PER_SEC + tv.tv_usec);
#endif
}
const Date Date::after(double second) const
{
return Date(static_cast<int64_t>(microSecondsSinceEpoch_ +
second * MICRO_SECONDS_PER_SEC));
return Date(microSecondsSinceEpoch_ +
static_cast<int64_t>(
second * static_cast<double>(MICRO_SECONDS_PER_SEC)));
}
const Date Date::roundSecond() const
{
Expand All @@ -86,7 +87,7 @@ const Date Date::roundDay() const
t.tm_hour = 0;
t.tm_min = 0;
t.tm_sec = 0;
return Date(mktime(&t) * MICRO_SECONDS_PER_SEC);
return Date(static_cast<int64_t>(mktime(&t)) * MICRO_SECONDS_PER_SEC);
}
struct tm Date::tmStruct() const
{
Expand Down Expand Up @@ -281,8 +282,8 @@ std::string Date::toDbString() const

Date Date::fromDbStringLocal(const std::string &datetime)
{
unsigned int year = {0}, month = {0}, day = {0}, hour = {0}, minute = {0},
second = {0}, microSecond = {0};
unsigned int year = 0U, month = 0U, day = 0U, hour = 0U, minute = 0U,
second = 0U, microSecond = 0U;
std::vector<std::string> &&v = splitString(datetime, " ");

if (v.size() == 0)
Expand Down Expand Up @@ -315,16 +316,16 @@ Date Date::fromDbStringLocal(const std::string &datetime)
// Format YYYY-MM-DD HH:MM:SS[.UUUUUU] is given
try
{
year = std::stol(date[0]);
month = std::stol(date[1]);
day = std::stol(date[2]);
year = static_cast<unsigned int>(std::stoul(date[0]));
month = static_cast<unsigned int>(std::stoul(date[1]));
day = static_cast<unsigned int>(std::stoul(date[2]));
std::vector<std::string> time = splitString(v[1], ":");
if (2 < time.size())
{
hour = std::stol(time[0]);
minute = std::stol(time[1]);
hour = static_cast<unsigned int>(std::stoul(time[0]));
minute = static_cast<unsigned int>(std::stoul(time[1]));
auto seconds = splitString(time[2], ".");
second = std::stol(seconds[0]);
second = static_cast<unsigned int>(std::stoul(seconds[0]));
if (1 < seconds.size())
{
if (seconds[1].length() > 6)
Expand All @@ -335,7 +336,8 @@ Date Date::fromDbStringLocal(const std::string &datetime)
{
seconds[1].append(6 - seconds[1].length(), '0');
}
microSecond = std::stol(seconds[1]);
microSecond =
static_cast<unsigned int>(std::stoul(seconds[1]));
}
}
}
Expand All @@ -358,7 +360,7 @@ Date Date::fromDbString(const std::string &datetime)
std::string Date::toCustomFormattedStringLocal(const std::string &fmtStr,
bool showMicroseconds) const
{
char buf[256] = {0};
char buf[256]{};
time_t seconds =
static_cast<time_t>(microSecondsSinceEpoch_ / MICRO_SECONDS_PER_SEC);
struct tm tm_time;
Expand All @@ -370,7 +372,7 @@ std::string Date::toCustomFormattedStringLocal(const std::string &fmtStr,
strftime(buf, sizeof(buf), fmtStr.c_str(), &tm_time);
if (!showMicroseconds)
return std::string(buf);
char decimals[12] = {0};
char decimals[12]{};
int microseconds =
static_cast<int>(microSecondsSinceEpoch_ % MICRO_SECONDS_PER_SEC);
snprintf(decimals, sizeof(decimals), ".%06d", microseconds);
Expand All @@ -388,12 +390,12 @@ Date::Date(unsigned int year,
memset(&tm, 0, sizeof(tm));
tm.tm_isdst = -1;
time_t epoch;
tm.tm_year = year - 1900;
tm.tm_mon = month - 1;
tm.tm_mday = day;
tm.tm_hour = hour;
tm.tm_min = minute;
tm.tm_sec = second;
tm.tm_year = static_cast<int>(year - 1900);
tm.tm_mon = static_cast<int>(month - 1);
tm.tm_mday = static_cast<int>(day);
tm.tm_hour = static_cast<int>(hour);
tm.tm_min = static_cast<int>(minute);
tm.tm_sec = static_cast<int>(second);
epoch = mktime(&tm);
microSecondsSinceEpoch_ =
static_cast<int64_t>(epoch) * MICRO_SECONDS_PER_SEC + microSecond;
Expand Down
41 changes: 20 additions & 21 deletions trantor/utils/LogStream.cc
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
#include <string.h>
#include <stdint.h>
#include <stdio.h>
#include <iostream>

using namespace trantor;
using namespace trantor::detail;
Expand All @@ -30,33 +29,32 @@ namespace trantor
{
namespace detail
{
const char digits[] = "9876543210123456789";
const char *zero = digits + 9;
constexpr char digits[] = "9876543210123456789";
constexpr const char *const zero = digits + 9;

const char digitsHex[] = "0123456789ABCDEF";
constexpr const char digitsHex[] = "0123456789ABCDEF";

// Efficient Integer to String Conversions, by Matthew Wilson.
template <typename T>
size_t convert(char buf[], T value)
size_t convert(char *buf, T value)
{
T i = value;
if (value < 0)
{
*buf++ = '-';
}
char *p = buf;

do
{
int lsd = static_cast<int>(i % 10);
i /= 10;
int lsd = static_cast<int>(value % 10);
value /= 10;
*p++ = zero[lsd];
} while (i != 0);
} while (value != 0);

if (value < 0)
{
*p++ = '-';
}
*p = '\0';
std::reverse(buf, p);

return p - buf;
return static_cast<size_t>(p - buf);
}

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

return p - buf;
return static_cast<size_t>(p - buf);
}

template class FixedBuffer<kSmallBuffer>;
Expand Down Expand Up @@ -211,7 +209,7 @@ LogStream &LogStream::operator<<(const double &v)
if (buffer_.avail() >= kMaxNumericSize)
{
int len = snprintf(buffer_.current(), kMaxNumericSize, "%.12g", v);
buffer_.add(len);
buffer_.add(static_cast<size_t>(len));
return *this;
}
else
Expand All @@ -222,7 +220,7 @@ LogStream &LogStream::operator<<(const double &v)
auto oldLen = exBuffer_.length();
exBuffer_.resize(oldLen + kMaxNumericSize);
int len = snprintf(&(exBuffer_[oldLen]), kMaxNumericSize, "%.12g", v);
exBuffer_.resize(oldLen + len);
exBuffer_.resize(oldLen + static_cast<size_t>(len));
return *this;
}

Expand All @@ -234,7 +232,7 @@ LogStream &LogStream::operator<<(const long double &v)
if (buffer_.avail() >= kMaxNumericSize)
{
int len = snprintf(buffer_.current(), kMaxNumericSize, "%.12Lg", v);
buffer_.add(len);
buffer_.add(static_cast<size_t>(len));
return *this;
}
else
Expand All @@ -245,15 +243,16 @@ LogStream &LogStream::operator<<(const long double &v)
auto oldLen = exBuffer_.length();
exBuffer_.resize(oldLen + kMaxNumericSize);
int len = snprintf(&(exBuffer_[oldLen]), kMaxNumericSize, "%.12Lg", v);
exBuffer_.resize(oldLen + len);
exBuffer_.resize(oldLen + static_cast<size_t>(len));
return *this;
}

template <typename T>
Fmt::Fmt(const char *fmt, T val)
{
length_ = snprintf(buf_, sizeof buf_, fmt, val);
assert(static_cast<size_t>(length_) < sizeof buf_);
length_ =
static_cast<decltype(length_)>(snprintf(buf_, sizeof(buf_), fmt, val));
assert(static_cast<size_t>(length_) < sizeof(buf_));
}

// Explicit instantiations
Expand Down
28 changes: 18 additions & 10 deletions trantor/utils/LogStream.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,20 @@
#include <assert.h>
#include <string.h> // memcpy
#include <string>
#include <cstdint>

namespace trantor
{
namespace detail
{
#if (__cplusplus >= 201703L)
// Note: in C++17, static should be replaced by inline
inline constexpr size_t kSmallBuffer{4000};
inline constexpr size_t kLargeBuffer{4000 * 1000};
#else
static constexpr size_t kSmallBuffer{4000};
static constexpr size_t kLargeBuffer{4000 * 1000};
#endif

template <int SIZE>
class TRANTOR_EXPORT FixedBuffer : NonCopyable
Expand All @@ -38,14 +45,19 @@ class TRANTOR_EXPORT FixedBuffer : NonCopyable
setCookie(cookieStart);
}

FixedBuffer(const FixedBuffer &) = delete;
FixedBuffer &operator=(const FixedBuffer &) = delete;
FixedBuffer(FixedBuffer &&) = delete;
FixedBuffer &operator=(FixedBuffer &&) = delete;

~FixedBuffer()
{
setCookie(cookieEnd);
}

bool append(const char * /*restrict*/ buf, size_t len)
{
if ((size_t)(avail()) > len)
if (static_cast<size_t>(avail()) > len)
{
memcpy(cur_, buf, len);
cur_ += len;
Expand All @@ -58,9 +70,9 @@ class TRANTOR_EXPORT FixedBuffer : NonCopyable
{
return data_;
}
int length() const
size_t length() const
{
return static_cast<int>(cur_ - data_);
return static_cast<size_t>(cur_ - data_);
}

// write to data_ directly
Expand Down Expand Up @@ -97,8 +109,6 @@ class TRANTOR_EXPORT FixedBuffer : NonCopyable
{
return std::string(data_, length());
}
// StringPiece toStringPiece() const { return StringPiece(data_, length());
// }

private:
const char *end() const
Expand Down Expand Up @@ -154,8 +164,6 @@ class TRANTOR_EXPORT LogStream : NonCopyable
return *this;
}

// self& operator<<(signed char);
// self& operator<<(unsigned char);
template <int N>
self &operator<<(const char (&buf)[N])
{
Expand Down Expand Up @@ -248,7 +256,7 @@ class TRANTOR_EXPORT LogStream : NonCopyable
std::string exBuffer_;
};

class TRANTOR_EXPORT Fmt // : boost::noncopyable
class TRANTOR_EXPORT Fmt
{
public:
template <typename T>
Expand All @@ -258,14 +266,14 @@ class TRANTOR_EXPORT Fmt // : boost::noncopyable
{
return buf_;
}
int length() const
uint8_t length() const
{
return length_;
}

private:
char buf_[48];
int length_;
uint8_t length_;
};

inline LogStream &operator<<(LogStream &s, const Fmt &fmt)
Expand Down
4 changes: 2 additions & 2 deletions trantor/utils/Logger.cc
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ inline LogStream &operator<<(LogStream &s, T v)

inline LogStream &operator<<(LogStream &s, const Logger::SourceFile &v)
{
s.append(v.data_, v.size_);
s.append(v.data_, static_cast<size_t>(v.size_));
return s;
}

Expand Down Expand Up @@ -444,7 +444,7 @@ RawLogger::~RawLogger()
}
else
{
auto &oFunc = Logger::outputFunc_(index_);
auto &oFunc = Logger::outputFunc_(static_cast<size_t>(index_));
if (!oFunc)
return;
oFunc(logStream_.bufferData(), logStream_.bufferLength());
Expand Down
4 changes: 2 additions & 2 deletions trantor/utils/Logger.h
Original file line number Diff line number Diff line change
Expand Up @@ -133,8 +133,8 @@ class TRANTOR_EXPORT Logger : public NonCopyable
}
else
{
outputFunc_(index) = outputFunc;
flushFunc_(index) = flushFunc;
outputFunc_(static_cast<size_t>(index)) = outputFunc;
flushFunc_(static_cast<size_t>(index)) = flushFunc;
}
}

Expand Down
Loading
Loading