THRIFT-4762: Applied some C++11 refactorings to the runtime library and compiler (#1719)
* make use of C++11 override keyword
* added const specifier to TTransport::getOrigin()
* added more const correctness to the compiler
* make use of auto keyword
* replaced usage of NULL with nullptr
* make use of explicitly-defaulted function definition
* extended changelog
diff --git a/lib/cpp/test/TransportTest.cpp b/lib/cpp/test/TransportTest.cpp
index 0d900d9..b0c84b6 100644
--- a/lib/cpp/test/TransportTest.cpp
+++ b/lib/cpp/test/TransportTest.cpp
@@ -57,7 +57,7 @@
class SizeGenerator {
public:
- virtual ~SizeGenerator() {}
+ virtual ~SizeGenerator() = default;
virtual uint32_t nextSize() = 0;
virtual std::string describe() const = 0;
};
@@ -65,8 +65,8 @@
class ConstantSizeGenerator : public SizeGenerator {
public:
ConstantSizeGenerator(uint32_t value) : value_(value) {}
- uint32_t nextSize() { return value_; }
- std::string describe() const {
+ uint32_t nextSize() override { return value_; }
+ std::string describe() const override {
std::ostringstream desc;
desc << value_;
return desc.str();
@@ -81,9 +81,9 @@
RandomSizeGenerator(uint32_t min, uint32_t max)
: generator_(rng, boost::uniform_int<int>(min, max)) {}
- uint32_t nextSize() { return generator_(); }
+ uint32_t nextSize() override { return generator_(); }
- std::string describe() const {
+ std::string describe() const override {
std::ostringstream desc;
desc << "rand(" << getMin() << ", " << getMax() << ")";
return desc.str();
@@ -109,8 +109,8 @@
GenericSizeGenerator(uint32_t min, uint32_t max)
: generator_(new RandomSizeGenerator(min, max)) {}
- uint32_t nextSize() { return generator_->nextSize(); }
- std::string describe() const { return generator_->describe(); }
+ uint32_t nextSize() override { return generator_->nextSize(); }
+ std::string describe() const override { return generator_->describe(); }
private:
std::shared_ptr<SizeGenerator> generator_;
@@ -131,7 +131,7 @@
template <class Transport_>
class CoupledTransports {
public:
- virtual ~CoupledTransports() {}
+ virtual ~CoupledTransports() = default;
typedef Transport_ TransportType;
CoupledTransports() : in(), out() {}
@@ -282,7 +282,7 @@
out.reset(new TFileTransport(filename));
}
- ~CoupledFileTransports() { remove(filename.c_str()); }
+ ~CoupledFileTransports() override { remove(filename.c_str()); }
std::string filename;
};
@@ -341,7 +341,7 @@
struct TriggerInfo {
TriggerInfo(int seconds, const std::shared_ptr<TTransport>& transport, uint32_t writeLength)
- : timeoutSeconds(seconds), transport(transport), writeLength(writeLength), next(NULL) {}
+ : timeoutSeconds(seconds), transport(transport), writeLength(writeLength), next(nullptr) {}
int timeoutSeconds;
std::shared_ptr<TTransport> transport;
@@ -355,7 +355,7 @@
bool g_teardown = false;
void alarm_handler() {
- TriggerInfo* info = NULL;
+ TriggerInfo* info = nullptr;
{
apache::thrift::concurrency::Synchronized s(g_alarm_monitor);
// The alarm timed out, which almost certainly means we're stuck
@@ -366,7 +366,7 @@
// tools/test/runner only records stdout messages in the failure messages for
// boost tests. (boost prints its test info to stdout.)
printf("Timeout alarm expired; attempting to unblock transport\n");
- if (g_triggerInfo == NULL) {
+ if (g_triggerInfo == nullptr) {
printf(" trigger stack is empty!\n");
}
@@ -377,7 +377,7 @@
}
// Write some data to the transport to hopefully unblock it.
- uint8_t* buf = new uint8_t[info->writeLength];
+ auto* buf = new uint8_t[info->writeLength];
memset(buf, 'b', info->writeLength);
boost::scoped_array<uint8_t> array(buf);
info->transport->write(buf, info->writeLength);
@@ -395,7 +395,7 @@
if (g_teardown)
return;
// calculate timeout
- if (g_triggerInfo == NULL) {
+ if (g_triggerInfo == nullptr) {
timeout = 0;
} else {
timeout = g_triggerInfo->timeoutSeconds * 1000;
@@ -421,10 +421,10 @@
void add_trigger(unsigned int seconds,
const std::shared_ptr<TTransport>& transport,
uint32_t write_len) {
- TriggerInfo* info = new TriggerInfo(seconds, transport, write_len);
+ auto* info = new TriggerInfo(seconds, transport, write_len);
{
apache::thrift::concurrency::Synchronized s(g_alarm_monitor);
- if (g_triggerInfo == NULL) {
+ if (g_triggerInfo == nullptr) {
// This is the first trigger.
// Set g_triggerInfo, and schedule the alarm
g_triggerInfo = info;
@@ -441,17 +441,17 @@
}
void clear_triggers() {
- TriggerInfo* info = NULL;
+ TriggerInfo* info = nullptr;
{
apache::thrift::concurrency::Synchronized s(g_alarm_monitor);
info = g_triggerInfo;
- g_triggerInfo = NULL;
+ g_triggerInfo = nullptr;
g_numTriggersFired = 0;
g_alarm_monitor.notify();
}
- while (info != NULL) {
+ while (info != nullptr) {
TriggerInfo* next = info->next;
delete info;
info = next;
@@ -500,8 +500,8 @@
SizeGenerator& rChunkGenerator,
uint32_t maxOutstanding) {
CoupledTransports transports;
- BOOST_REQUIRE(transports.in != NULL);
- BOOST_REQUIRE(transports.out != NULL);
+ BOOST_REQUIRE(transports.in != nullptr);
+ BOOST_REQUIRE(transports.out != nullptr);
boost::shared_array<uint8_t> wbuf = boost::shared_array<uint8_t>(new uint8_t[totalSize]);
boost::shared_array<uint8_t> rbuf = boost::shared_array<uint8_t>(new uint8_t[totalSize]);
@@ -593,8 +593,8 @@
template <class CoupledTransports>
void test_read_part_available() {
CoupledTransports transports;
- BOOST_REQUIRE(transports.in != NULL);
- BOOST_REQUIRE(transports.out != NULL);
+ BOOST_REQUIRE(transports.in != nullptr);
+ BOOST_REQUIRE(transports.out != nullptr);
uint8_t write_buf[16];
uint8_t read_buf[16];
@@ -615,8 +615,8 @@
template <class CoupledTransports>
void test_read_part_available_in_chunks() {
CoupledTransports transports;
- BOOST_REQUIRE(transports.in != NULL);
- BOOST_REQUIRE(transports.out != NULL);
+ BOOST_REQUIRE(transports.in != nullptr);
+ BOOST_REQUIRE(transports.out != nullptr);
uint8_t write_buf[16];
uint8_t read_buf[16];
@@ -642,8 +642,8 @@
template <class CoupledTransports>
void test_read_partial_midframe() {
CoupledTransports transports;
- BOOST_REQUIRE(transports.in != NULL);
- BOOST_REQUIRE(transports.out != NULL);
+ BOOST_REQUIRE(transports.in != nullptr);
+ BOOST_REQUIRE(transports.out != nullptr);
uint8_t write_buf[16];
uint8_t read_buf[16];
@@ -700,8 +700,8 @@
template <class CoupledTransports>
void test_borrow_part_available() {
CoupledTransports transports;
- BOOST_REQUIRE(transports.in != NULL);
- BOOST_REQUIRE(transports.out != NULL);
+ BOOST_REQUIRE(transports.in != nullptr);
+ BOOST_REQUIRE(transports.out != nullptr);
uint8_t write_buf[16];
uint8_t read_buf[16];
@@ -715,7 +715,7 @@
uint32_t borrow_len = 10;
const uint8_t* borrowed_buf = transports.in->borrow(read_buf, &borrow_len);
BOOST_CHECK_EQUAL(g_numTriggersFired, (unsigned int)0);
- BOOST_CHECK(borrowed_buf == NULL);
+ BOOST_CHECK(borrowed_buf == nullptr);
clear_triggers();
}
@@ -723,8 +723,8 @@
template <class CoupledTransports>
void test_read_none_available() {
CoupledTransports transports;
- BOOST_REQUIRE(transports.in != NULL);
- BOOST_REQUIRE(transports.out != NULL);
+ BOOST_REQUIRE(transports.in != nullptr);
+ BOOST_REQUIRE(transports.out != nullptr);
uint8_t read_buf[16];
@@ -751,8 +751,8 @@
template <class CoupledTransports>
void test_borrow_none_available() {
CoupledTransports transports;
- BOOST_REQUIRE(transports.in != NULL);
- BOOST_REQUIRE(transports.out != NULL);
+ BOOST_REQUIRE(transports.in != nullptr);
+ BOOST_REQUIRE(transports.out != nullptr);
uint8_t write_buf[16];
memset(write_buf, 'a', sizeof(write_buf));
@@ -760,8 +760,8 @@
// Attempting to borrow when no data is available should fail immediately
set_trigger(1, transports.out, 10);
uint32_t borrow_len = 10;
- const uint8_t* borrowed_buf = transports.in->borrow(NULL, &borrow_len);
- BOOST_CHECK(borrowed_buf == NULL);
+ const uint8_t* borrowed_buf = transports.in->borrow(nullptr, &borrow_len);
+ BOOST_CHECK(borrowed_buf == nullptr);
BOOST_CHECK_EQUAL(g_numTriggersFired, (unsigned int)0);
clear_triggers();
@@ -1055,7 +1055,7 @@
#ifdef BOOST_TEST_DYN_LINK
bool init_unit_test_suite() {
struct timeval tv;
- THRIFT_GETTIMEOFDAY(&tv, NULL);
+ THRIFT_GETTIMEOFDAY(&tv, nullptr);
int seed = tv.tv_sec ^ tv.tv_usec;
initrand(seed);