Skip to content

Commit

Permalink
Change C style numeric cast to static_cast (#128)
Browse files Browse the repository at this point in the history
* Change C style numeric cast '(T)val to C++ style 'static_cast<T>(val)'

* Add /.vscode/ to gitignore

Signed-off-by: Michael Franceski <[email protected]>
  • Loading branch information
mfrandev authored Oct 25, 2023
1 parent 708d49b commit 08a6d16
Show file tree
Hide file tree
Showing 15 changed files with 60 additions and 45 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ compile_commands.json
/.cache/
CMakeUserPresets.json
/thirdparty/
/.vscode/
4 changes: 2 additions & 2 deletions src/groups/bmq/bmqt/bmqt_version.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ Version::print(bsl::ostream& stream, int level, int spacesPerLevel) const

bslim::Printer printer(&stream, level, spacesPerLevel);
printer.start();
printer.printAttribute("major", int(d_major));
printer.printAttribute("minor", int(d_minor));
printer.printAttribute("major", static_cast<int>(d_major));
printer.printAttribute("minor", static_cast<int>(d_minor));
printer.end();

return stream;
Expand Down
3 changes: 2 additions & 1 deletion src/groups/mqb/mqbc/mqbc_storageutil.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2721,7 +2721,8 @@ int StorageUtil::processCommand(mqbcmd::StorageResult* result,
else if (command.isPartitionValue()) {
const int partitionId = command.partition().partitionId();

if (partitionId < 0 || partitionId >= int(fileStores->size())) {
if (partitionId < 0 ||
partitionId >= static_cast<int>(fileStores->size())) {
bdlma::LocalSequentialAllocator<256> localAllocator(allocator);
mwcu::MemOutStream os(&localAllocator);
os << "Invalid partitionId value: '" << partitionId << "'";
Expand Down
7 changes: 4 additions & 3 deletions src/groups/mwc/mwcst/mwcst_basictableinfoprovider.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -404,7 +404,8 @@ BasicTableInfoProvider::addColumn(const bslstl::StringRef& tableColumnName,
ColumnFormat& column = d_columns.back();

if (!d_columnGroups.empty()) {
column.d_columnGroupIndex = (int)d_columnGroups.size() - 1;
column.d_columnGroupIndex = static_cast<int>(d_columnGroups.size()) -
1;
}

return column;
Expand All @@ -421,10 +422,10 @@ int BasicTableInfoProvider::numRows() const
int BasicTableInfoProvider::numColumns(int level) const
{
if (level == 0) {
return (int)d_columns.size();
return static_cast<int>(d_columns.size());
}
else {
return (int)d_columnGroups.size();
return static_cast<int>(d_columnGroups.size());
}
}

Expand Down
23 changes: 12 additions & 11 deletions src/groups/mwc/mwcst/mwcst_printutil.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,9 @@ const char* timeIntervalNsHelper(bsls::Types::Int64* num,
}

*num = ns / div;
*remainder = (int)bsl::floor(
((double)(ns - *num * div) / static_cast<double>(div)) *
bsl::pow(10.0, precision));
*remainder = static_cast<int>(bsl::floor(
(static_cast<double>(ns - *num * div) / static_cast<double>(div)) *
bsl::pow(10.0, precision)));
return TIME_INTERVAL_NS_UNITS[level];
}

Expand Down Expand Up @@ -117,9 +117,10 @@ const char* memoryHelper(bsls::Types::Int64* num,

bsls::Types::Int64 div = (((bsls::Types::Int64)1) << shift);
*num = bytes >> shift;
*remainder = (int)bsl::floor(
(((double)(bytes - (*num << shift)) / static_cast<double>(div)) *
bsl::pow(10.0, precision)));
*remainder = static_cast<int>(
bsl::floor(((static_cast<double>(bytes - (*num << shift)) /
static_cast<double>(div)) *
bsl::pow(10.0, precision))));

if (negative) {
*num = -*num;
Expand Down Expand Up @@ -257,8 +258,8 @@ bsl::ostream& PrintUtil::printValueWithSeparator(bsl::ostream& stream,

if (precision > 0) {
double absValue = bsl::abs(value);
int remainder = (int)bsl::floor((absValue - bsl::floor(absValue)) *
bsl::pow(10.0, precision));
int remainder = static_cast<int>(bsl::floor(
(absValue - bsl::floor(absValue)) * bsl::pow(10.0, precision)));

snprintf(pos - precision - 2,
precision + 2,
Expand All @@ -279,7 +280,7 @@ bsl::ostream& PrintUtil::printStringCentered(bsl::ostream& stream,
const bslstl::StringRef& str)
{
bsl::streamsize width = stream.width();
if (width > (int)str.length()) {
if (width > static_cast<int>(str.length())) {
bsl::streamsize left = (width + str.length()) / 2;
stream.width(left);
stream << str;
Expand Down Expand Up @@ -395,8 +396,8 @@ bsl::ostream& PrintUtil::printOrdinal(bsl::ostream& stream,
{
stream << num;

int mod100 = (int)(num % 100);
int mod10 = (int)(num % 10);
int mod100 = static_cast<int>(num % 100);
int mod10 = static_cast<int>(num % 10);

if (mod100 == 11 || mod100 == 12 || mod100 == 13) {
return stream << "th";
Expand Down
4 changes: 3 additions & 1 deletion src/groups/mwc/mwcst/mwcst_printutil.t.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -401,7 +401,9 @@ int main(int argc, char* argv[])
PrintUtil::printedMemoryLength(data.d_arg, data.d_precision);

LOOP_ASSERT_EQUALS(LINE, ss.str(), data.d_expected_p);
LOOP_ASSERT_EQUALS(LINE, (int)ss.str().length(), expectedLength);
LOOP_ASSERT_EQUALS(LINE,
static_cast<int>(ss.str().length()),
expectedLength);
}

#undef TO_I64
Expand Down
2 changes: 1 addition & 1 deletion src/groups/mwc/mwcst/mwcst_statcontext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -727,7 +727,7 @@ int StatContext::valueIndex(const bslstl::StringRef& name) const
{
for (size_t i = 0; i < d_valueDefs_p->size(); ++i) {
if ((*d_valueDefs_p)[i].d_name == name) {
return (int)i;
return static_cast<int>(i);
}
}

Expand Down
12 changes: 6 additions & 6 deletions src/groups/mwc/mwcst/mwcst_statcontexttableinfoprovider.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,7 @@ void StatContextTableInfoProvider::addColumn(const bslstl::StringRef& name,
{
BSLS_ASSERT(!d_columnGroups.empty());
d_columns.push_back(
ColumnInfo((int)d_columnGroups.size() - 1, name, column));
ColumnInfo(static_cast<int>(d_columnGroups.size()) - 1, name, column));
}

void StatContextTableInfoProvider::addColumn(const bslstl::StringRef& name,
Expand All @@ -342,7 +342,7 @@ void StatContextTableInfoProvider::addColumn(const bslstl::StringRef& name,
const IntValueFunctor& func)
{
BSLS_ASSERT(!d_columnGroups.empty());
d_columns.push_back(ColumnInfo((int)d_columnGroups.size() - 1,
d_columns.push_back(ColumnInfo(static_cast<int>(d_columnGroups.size()) - 1,
name,
statValueIndex,
printType,
Expand All @@ -355,7 +355,7 @@ void StatContextTableInfoProvider::addColumn(const bslstl::StringRef& name,
const DoubleValueFunctor& func)
{
BSLS_ASSERT(!d_columnGroups.empty());
d_columns.push_back(ColumnInfo((int)d_columnGroups.size() - 1,
d_columns.push_back(ColumnInfo(static_cast<int>(d_columnGroups.size()) - 1,
name,
statValueIndex,
printType,
Expand Down Expand Up @@ -427,16 +427,16 @@ void StatContextTableInfoProvider::addColumn(
// ACCESSORS
int StatContextTableInfoProvider::numRows() const
{
return (int)d_rows.size();
return static_cast<int>(d_rows.size());
}

int StatContextTableInfoProvider::numColumns(int level) const
{
if (level == 0) {
return (int)d_columns.size();
return static_cast<int>(d_columns.size());
}
else {
return (int)d_columnGroups.size();
return static_cast<int>(d_columnGroups.size());
}
}

Expand Down
6 changes: 3 additions & 3 deletions src/groups/mwc/mwcst/mwcst_statutil.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ double StatUtil::incrementsPerSecond(
value.snapshot(secondSnapshot).snapshotTime() -
value.snapshot(firstSnapshot).snapshotTime();

double divisor = (double)duration / DMCST_NS_PER_SEC;
double divisor = static_cast<double>(duration) / DMCST_NS_PER_SEC;
return diff / divisor;
}

Expand Down Expand Up @@ -145,7 +145,7 @@ double StatUtil::decrementsPerSecond(
value.snapshot(secondSnapshot).snapshotTime() -
value.snapshot(firstSnapshot).snapshotTime();

double divisor = (double)duration / DMCST_NS_PER_SEC;
double divisor = static_cast<double>(duration) / DMCST_NS_PER_SEC;
return diff / divisor;
}

Expand Down Expand Up @@ -198,7 +198,7 @@ StatUtil::ratePerSecond(const StatValue& value,
value.snapshot(secondSnapshot).snapshotTime() -
value.snapshot(firstSnapshot).snapshotTime();

double divisor = (double)duration / DMCST_NS_PER_SEC;
double divisor = static_cast<double>(duration) / DMCST_NS_PER_SEC;
return diff / divisor;
}

Expand Down
2 changes: 1 addition & 1 deletion src/groups/mwc/mwcst/mwcst_statvalue.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ bsls::Types::Int64 MIN_INT = bsl::numeric_limits<bsls::Types::Int64>::min();
// PRIVATE MANIPULATORS
void StatValue::aggregateLevel(int level, bsls::Types::Int64 snapshotTime)
{
if (level + 1 >= (int)d_levelStartIndices.size() - 1) {
if (level + 1 >= static_cast<int>(d_levelStartIndices.size()) - 1) {
// There is no higher aggregation level
return;
}
Expand Down
4 changes: 2 additions & 2 deletions src/groups/mwc/mwcst/mwcst_tablerecords.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -200,12 +200,12 @@ const StatContext* TableRecords::context() const

int TableRecords::numRecords() const
{
return (int)d_records.size();
return static_cast<int>(d_records.size());
}

const TableRecords::Record& TableRecords::record(int index) const
{
BSLS_ASSERT_SAFE(index < (int)d_records.size());
BSLS_ASSERT_SAFE(index < static_cast<int>(d_records.size()));
return d_records[index];
}

Expand Down
2 changes: 1 addition & 1 deletion src/groups/mwc/mwcst/mwcst_tableschema.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,7 @@ TableSchema::addColumn(const bslstl::StringRef& name,
// ACCESSORS
int TableSchema::numColumns() const
{
return (int)d_columns.size();
return static_cast<int>(d_columns.size());
}

const TableSchemaColumn& TableSchema::column(int index) const
Expand Down
27 changes: 18 additions & 9 deletions src/groups/mwc/mwcst/mwcst_tableutil.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,17 +57,18 @@ int TableUtil::printTable(bsl::ostream& stream, const TableInfoProvider& info)

columnWidths[0].resize(info.numColumns(0), 0);
for (size_t i = 0; i < columnWidths[0].size(); ++i) {
columnWidths[0][i] = info.getHeaderSize(0, (int)i);
columnWidths[0][i] = info.getHeaderSize(0, static_cast<int>(i));

for (int row = 0; row < numRows; ++row) {
columnWidths[0][i] = bsl::max(columnWidths[0][i],
info.getValueSize(row, (int)i));
columnWidths[0][i] = bsl::max(
columnWidths[0][i],
info.getValueSize(row, static_cast<int>(i)));
}
}

// Figure out widths of additional header cells
for (size_t levelIdx = 1; levelIdx < columnWidths.size(); ++levelIdx) {
int level = (int)levelIdx;
int level = static_cast<int>(levelIdx);
bsl::vector<int>& levelHeaders = columnWidths[levelIdx];
bsl::vector<int>& lastLevelHeaders = columnWidths[levelIdx - 1];

Expand All @@ -76,7 +77,8 @@ int TableUtil::printTable(bsl::ostream& stream, const TableInfoProvider& info)
int lastUpdatedHeader = -1;
int numSubHeaders = 0;
for (size_t col = 0; col < lastLevelHeaders.size(); ++col) {
int headerIdx = info.getParentHeader(level - 1, (int)col);
int headerIdx = info.getParentHeader(level - 1,
static_cast<int>(col));
levelHeaders[headerIdx] += lastLevelHeaders[col];

if (lastUpdatedHeader == headerIdx) {
Expand All @@ -94,11 +96,14 @@ int TableUtil::printTable(bsl::ostream& stream, const TableInfoProvider& info)
int mod = excess % numSubHeaders;

levelHeaders[lastUpdatedHeader] = headerSize;
for (size_t hIdx = 0; (int)hIdx < numSubHeaders; ++hIdx) {
for (size_t hIdx = 0;
static_cast<int>(hIdx) < numSubHeaders;
++hIdx) {
// Add 'excessPerSubHeader' to each subHeader, and an
// extra '1' to 'mod' of them
lastLevelHeaders[col - hIdx - 1] +=
excessPerSubHeader + ((int)hIdx < mod ? 1 : 0);
excessPerSubHeader +
(static_cast<int>(hIdx) < mod ? 1 : 0);
}
}

Expand Down Expand Up @@ -131,7 +136,8 @@ int TableUtil::printTable(bsl::ostream& stream, const TableInfoProvider& info)
stream << bsl::setfill(' ');

// Print the header rows
for (int level = (int)columnWidths.size() - 1; level >= 0; --level) {
for (int level = static_cast<int>(columnWidths.size()) - 1; level >= 0;
--level) {
stream << bsl::endl;

for (size_t i = 0; i < columnWidths[level].size(); ++i) {
Expand Down Expand Up @@ -169,7 +175,10 @@ int TableUtil::printTable(bsl::ostream& stream, const TableInfoProvider& info)
}

stream << bsl::setw(columnWidths[0][col]);
info.printValue(stream, row, (int)col, columnWidths[0][col]);
info.printValue(stream,
row,
static_cast<int>(col),
columnWidths[0][col]);
}
stream << bsl::endl;
}
Expand Down
6 changes: 3 additions & 3 deletions src/groups/mwc/mwcst/mwcst_testtableinfoprovider.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,13 @@ void TestTableInfoProvider::addHeaderLevel(const Row& row)
// ACCESSORS
int TestTableInfoProvider::numRows() const
{
return (int)d_table.size();
return static_cast<int>(d_table.size());
}

int TestTableInfoProvider::numColumns(int /*level*/) const
{
BSLS_ASSERT(d_headers.size() > 0);
return (int)d_headers[0].size();
return static_cast<int>(d_headers[0].size());
}

bool TestTableInfoProvider::hasTitle() const
Expand All @@ -71,7 +71,7 @@ bool TestTableInfoProvider::hasTitle() const

int TestTableInfoProvider::numHeaderLevels() const
{
return (int)d_headers.size();
return static_cast<int>(d_headers.size());
}

int TestTableInfoProvider::getValueSize(int row, int column) const
Expand Down
2 changes: 1 addition & 1 deletion src/groups/mwc/mwcu/mwcu_operationchain.t.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ struct ThrowException {

// ACCESSORS
BSLS_ANNOTATION_NORETURN
void operator()() const { throw int(42); }
void operator()() const { throw static_cast<int>(42); }
};

// ====================
Expand Down

0 comments on commit 08a6d16

Please sign in to comment.