Skip to content

Commit 27b4018

Browse files
authored
Use keyword nullptr where possible (etr#247)
Replace every usage of 0x0 or NULL with nullptr, which is available since C++11. NULL, or 0x0, or nullptr, or even 0 was used over the entiry code base before for expressing the concept of a null pointer. Use the C++ special word nullptr instead to unify the code base and minimize friction with expectations of modern C++ developers. References: etr#241 Signed-off-by: Alexander Dahl <post@lespocky.de>
1 parent 6b195cf commit 27b4018

13 files changed

Lines changed: 67 additions & 67 deletions

examples/service.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ int main(int argc, char **argv) {
158158
while ((c = getopt(argc, argv, "p:k:c:sv?")) != EOF) {
159159
switch (c) {
160160
case 'p':
161-
port = strtoul(optarg, NULL, 10);
161+
port = strtoul(optarg, nullptr, 10);
162162
break;
163163
case 'k':
164164
key = optarg;

src/deferred_response.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ namespace httpserver {
2929
namespace details {
3030

3131
MHD_Response* get_raw_response_helper(void* cls, ssize_t (*cb)(void*, uint64_t, char*, size_t)) {
32-
return MHD_create_response_from_callback(MHD_SIZE_UNKNOWN, 1024, cb, cls, NULL);
32+
return MHD_create_response_from_callback(MHD_SIZE_UNKNOWN, 1024, cb, cls, nullptr);
3333
}
3434

3535
} // namespace details

src/file_response.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ MHD_Response* file_response::get_raw_response() {
3535
if (size) {
3636
return MHD_create_response_from_fd(size, fd);
3737
} else {
38-
return MHD_create_response_from_buffer(0, 0x0, MHD_RESPMEM_PERSISTENT);
38+
return MHD_create_response_from_buffer(0, nullptr, MHD_RESPMEM_PERSISTENT);
3939
}
4040
}
4141

src/http_request.cpp

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ bool http_request::check_digest_auth(const std::string& realm, const std::string
5858
const std::string http_request::get_connection_value(const std::string& key, enum MHD_ValueKind kind) const {
5959
const char* header_c = MHD_lookup_connection_value(underlying_connection, kind, key.c_str());
6060

61-
if (header_c == NULL) return EMPTY;
61+
if (header_c == nullptr) return EMPTY;
6262

6363
return header_c;
6464
}
@@ -140,7 +140,7 @@ MHD_Result http_request::build_request_args(void *cls, enum MHD_ValueKind kind,
140140
std::ignore = kind;
141141

142142
arguments_accumulator* aa = static_cast<arguments_accumulator*>(cls);
143-
std::string value = ((arg_value == NULL) ? "" : arg_value);
143+
std::string value = ((arg_value == nullptr) ? "" : arg_value);
144144

145145
http::base_unescaper(&value, aa->unescaper);
146146
(*aa->arguments)[key] = value;
@@ -152,7 +152,7 @@ MHD_Result http_request::build_request_querystring(void *cls, enum MHD_ValueKind
152152
std::ignore = kind;
153153

154154
std::string* querystring = static_cast<std::string*>(cls);
155-
std::string value = ((arg_value == NULL) ? "" : arg_value);
155+
std::string value = ((arg_value == nullptr) ? "" : arg_value);
156156

157157
int buffer_size = std::string(key).size() + value.size() + 3;
158158
char* buf = new char[buffer_size];
@@ -170,41 +170,41 @@ MHD_Result http_request::build_request_querystring(void *cls, enum MHD_ValueKind
170170
}
171171

172172
const std::string http_request::get_user() const {
173-
char* username = 0x0;
174-
char* password = 0x0;
173+
char* username = nullptr;
174+
char* password = nullptr;
175175

176176
username = MHD_basic_auth_get_username_password(underlying_connection, &password);
177-
if (password != 0x0) free(password);
177+
if (password != nullptr) free(password);
178178

179179
std::string user;
180-
if (username != 0x0) user = username;
180+
if (username != nullptr) user = username;
181181

182182
free(username);
183183

184184
return user;
185185
}
186186

187187
const std::string http_request::get_pass() const {
188-
char* username = 0x0;
189-
char* password = 0x0;
188+
char* username = nullptr;
189+
char* password = nullptr;
190190

191191
username = MHD_basic_auth_get_username_password(underlying_connection, &password);
192-
if (username != 0x0) free(username);
192+
if (username != nullptr) free(username);
193193

194194
std::string pass;
195-
if (password != 0x0) pass = password;
195+
if (password != nullptr) pass = password;
196196

197197
free(password);
198198

199199
return pass;
200200
}
201201

202202
const std::string http_request::get_digested_user() const {
203-
char* digested_user_c = 0x0;
203+
char* digested_user_c = nullptr;
204204
digested_user_c = MHD_digest_auth_get_username(underlying_connection);
205205

206206
std::string digested_user = EMPTY;
207-
if (digested_user_c != 0x0) {
207+
if (digested_user_c != nullptr) {
208208
digested_user = digested_user_c;
209209
free(digested_user_c);
210210
}

src/http_response.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
namespace httpserver {
2828

2929
MHD_Response* http_response::get_raw_response() {
30-
return MHD_create_response_from_buffer(0, 0x0, MHD_RESPMEM_PERSISTENT);
30+
return MHD_create_response_from_buffer(0, nullptr, MHD_RESPMEM_PERSISTENT);
3131
}
3232

3333
void http_response::decorate_response(MHD_Response* response) {

src/http_utils.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -345,8 +345,8 @@ ip_representation::ip_representation(const std::string& ip) {
345345
}
346346

347347
if (parts[i].size() == 4) {
348-
pieces[y] = strtol((parts[i].substr(0, 2)).c_str(), NULL, 16);
349-
pieces[y+1] = strtol((parts[i].substr(2, 2)).c_str(), NULL, 16);
348+
pieces[y] = strtol((parts[i].substr(0, 2)).c_str(), nullptr, 16);
349+
pieces[y+1] = strtol((parts[i].substr(2, 2)).c_str(), nullptr, 16);
350350

351351
y += 2;
352352
} else {
@@ -371,7 +371,7 @@ ip_representation::ip_representation(const std::string& ip) {
371371

372372
for (unsigned int ii = 0; ii < subparts.size(); ii++) {
373373
if (subparts[ii] != "*") {
374-
pieces[y+ii] = strtol(subparts[ii].c_str(), NULL, 10);
374+
pieces[y+ii] = strtol(subparts[ii].c_str(), nullptr, 10);
375375
if (pieces[y+ii] > 255) throw std::invalid_argument("IP is badly formatted. 255 is max value for ip part.");
376376
} else {
377377
CLEAR_BIT(mask, y+ii);
@@ -396,7 +396,7 @@ ip_representation::ip_representation(const std::string& ip) {
396396
if (parts.size() == 4) {
397397
for (unsigned int i = 0; i < parts.size(); i++) {
398398
if (parts[i] != "*") {
399-
pieces[12+i] = strtol(parts[i].c_str(), NULL, 10);
399+
pieces[12+i] = strtol(parts[i].c_str(), nullptr, 10);
400400
if (pieces[12+i] > 255) throw std::invalid_argument("IP is badly formatted. 255 is max value for ip part.");
401401
} else {
402402
CLEAR_BIT(mask, 12+i);
@@ -481,7 +481,7 @@ void dump_arg_map(std::ostream &os, const std::string &prefix, const std::map<st
481481
size_t base_unescaper(std::string* s, unescaper_ptr unescaper) {
482482
if ((*s)[0] == 0) return 0;
483483

484-
if (unescaper != 0x0) {
484+
if (unescaper != nullptr) {
485485
unescaper(*s);
486486
return s->size();
487487
}

src/httpserver/create_webserver.hpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -335,11 +335,11 @@ class create_webserver {
335335
size_t _content_size_limit = static_cast<size_t>(-1);
336336
int _connection_timeout = DEFAULT_WS_TIMEOUT;
337337
int _per_IP_connection_limit = 0;
338-
log_access_ptr _log_access = 0x0;
339-
log_error_ptr _log_error = 0x0;
340-
validator_ptr _validator = 0x0;
341-
unescaper_ptr _unescaper = 0x0;
342-
const struct sockaddr* _bind_address = 0x0;
338+
log_access_ptr _log_access = nullptr;
339+
log_error_ptr _log_error = nullptr;
340+
validator_ptr _validator = nullptr;
341+
unescaper_ptr _unescaper = nullptr;
342+
const struct sockaddr* _bind_address = nullptr;
343343
int _bind_socket = 0;
344344
int _max_thread_stack_size = 0;
345345
bool _use_ssl = false;
@@ -363,9 +363,9 @@ class create_webserver {
363363
bool _deferred_enabled = false;
364364
bool _single_resource = false;
365365
bool _tcp_nodelay = false;
366-
render_ptr _not_found_resource = 0x0;
367-
render_ptr _method_not_allowed_resource = 0x0;
368-
render_ptr _internal_error_resource = 0x0;
366+
render_ptr _not_found_resource = nullptr;
367+
render_ptr _method_not_allowed_resource = nullptr;
368+
render_ptr _internal_error_resource = nullptr;
369369

370370
friend class webserver;
371371
};

src/httpserver/details/modded_request.hpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,14 @@ namespace httpserver {
3535
namespace details {
3636

3737
struct modded_request {
38-
struct MHD_PostProcessor *pp = 0x0;
39-
std::string* complete_uri = 0x0;
40-
std::string* standardized_url = 0x0;
41-
webserver* ws = 0x0;
38+
struct MHD_PostProcessor *pp = nullptr;
39+
std::string* complete_uri = nullptr;
40+
std::string* standardized_url = nullptr;
41+
webserver* ws = nullptr;
4242

4343
const std::shared_ptr<http_response> (httpserver::http_resource::*callback)(const httpserver::http_request&);
4444

45-
http_request* dhr = 0x0;
45+
http_request* dhr = nullptr;
4646
std::shared_ptr<http_response> dhrs;
4747
bool second = false;
4848
bool has_body = false;
@@ -56,7 +56,7 @@ struct modded_request {
5656
modded_request& operator=(modded_request&& b) = default;
5757

5858
~modded_request() {
59-
if (NULL != pp) {
59+
if (nullptr != pp) {
6060
MHD_destroy_post_processor(pp);
6161
}
6262
if (second) {

src/httpserver/http_request.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -244,9 +244,9 @@ class http_request {
244244
size_t content_size_limit = static_cast<size_t>(-1);
245245
std::string version;
246246

247-
struct MHD_Connection* underlying_connection = 0x0;
247+
struct MHD_Connection* underlying_connection = nullptr;
248248

249-
unescaper_ptr unescaper = 0x0;
249+
unescaper_ptr unescaper = nullptr;
250250

251251
static MHD_Result build_request_header(void *cls, enum MHD_ValueKind kind, const char *key, const char *value);
252252

src/webserver.cpp

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -159,8 +159,8 @@ webserver::webserver(const create_webserver& params):
159159
method_not_allowed_resource(params._method_not_allowed_resource),
160160
internal_error_resource(params._internal_error_resource) {
161161
ignore_sigpipe();
162-
pthread_mutex_init(&mutexwait, NULL);
163-
pthread_cond_init(&mutexcond, NULL);
162+
pthread_mutex_init(&mutexwait, nullptr);
163+
pthread_cond_init(&mutexcond, nullptr);
164164
}
165165

166166
webserver::~webserver() {
@@ -180,10 +180,10 @@ void webserver::request_completed(void *cls, struct MHD_Connection *connection,
180180
std::ignore = toe;
181181

182182
details::modded_request* mr = static_cast<details::modded_request*>(*con_cls);
183-
if (mr == 0x0) return;
183+
if (mr == nullptr) return;
184184

185185
delete mr;
186-
mr = 0x0;
186+
mr = nullptr;
187187
}
188188

189189
bool webserver::register_resource(const std::string& resource, http_resource* hrm, bool family) {
@@ -204,14 +204,14 @@ bool webserver::register_resource(const std::string& resource, http_resource* hr
204204

205205
bool webserver::start(bool blocking) {
206206
struct {
207-
MHD_OptionItem operator ()(enum MHD_OPTION opt, intptr_t val, void *ptr = 0) {
207+
MHD_OptionItem operator ()(enum MHD_OPTION opt, intptr_t val, void *ptr = nullptr) {
208208
MHD_OptionItem x = {opt, val, ptr};
209209
return x;
210210
}
211211
} gen;
212212
vector<struct MHD_OptionItem> iov;
213213

214-
iov.push_back(gen(MHD_OPTION_NOTIFY_COMPLETED, (intptr_t) &request_completed, NULL));
214+
iov.push_back(gen(MHD_OPTION_NOTIFY_COMPLETED, (intptr_t) &request_completed, nullptr));
215215
iov.push_back(gen(MHD_OPTION_URI_LOG_CALLBACK, (intptr_t) &uri_log, this));
216216
iov.push_back(gen(MHD_OPTION_EXTERNAL_LOGGER, (intptr_t) &error_log, this));
217217
iov.push_back(gen(MHD_OPTION_UNESCAPE_CALLBACK, (intptr_t) &unescaper_func, this));
@@ -279,7 +279,7 @@ bool webserver::start(bool blocking) {
279279
}
280280
#endif // HAVE_GNUTLS
281281

282-
iov.push_back(gen(MHD_OPTION_END, 0, NULL));
282+
iov.push_back(gen(MHD_OPTION_END, 0, nullptr));
283283

284284
int start_conf = start_method;
285285

@@ -310,8 +310,8 @@ bool webserver::start(bool blocking) {
310310
start_conf |= MHD_USE_TCP_FASTOPEN;
311311
#endif
312312

313-
daemon = NULL;
314-
if (bind_address == 0x0) {
313+
daemon = nullptr;
314+
if (bind_address == nullptr) {
315315
daemon = MHD_start_daemon(start_conf, port, &policy_callback, this,
316316
&answer_to_connection, this, MHD_OPTION_ARRAY,
317317
&iov[0], MHD_OPTION_END);
@@ -321,7 +321,7 @@ bool webserver::start(bool blocking) {
321321
&iov[0], MHD_OPTION_SOCK_ADDR, bind_address, MHD_OPTION_END);
322322
}
323323

324-
if (daemon == NULL) {
324+
if (daemon == nullptr) {
325325
throw std::invalid_argument("Unable to connect daemon to port: " + std::to_string(port));
326326
}
327327

@@ -430,11 +430,11 @@ void error_log(void* cls, const char* fmt, va_list ap) {
430430
std::ignore = ap;
431431

432432
webserver* dws = static_cast<webserver*>(cls);
433-
if (dws->log_error != 0x0) dws->log_error(fmt);
433+
if (dws->log_error != nullptr) dws->log_error(fmt);
434434
}
435435

436436
void access_log(webserver* dws, string uri) {
437-
if (dws->log_access != 0x0) dws->log_access(uri);
437+
if (dws->log_access != nullptr) dws->log_access(uri);
438438
}
439439

440440
size_t unescaper_func(void * cls, struct MHD_Connection *c, char *s) {
@@ -472,23 +472,23 @@ void webserver::upgrade_handler(void *cls, struct MHD_Connection* connection, vo
472472
}
473473

474474
const std::shared_ptr<http_response> webserver::not_found_page(details::modded_request* mr) const {
475-
if (not_found_resource != 0x0) {
475+
if (not_found_resource != nullptr) {
476476
return not_found_resource(*mr->dhr);
477477
} else {
478478
return std::shared_ptr<http_response>(new string_response(NOT_FOUND_ERROR, http_utils::http_not_found));
479479
}
480480
}
481481

482482
const std::shared_ptr<http_response> webserver::method_not_allowed_page(details::modded_request* mr) const {
483-
if (method_not_allowed_resource != 0x0) {
483+
if (method_not_allowed_resource != nullptr) {
484484
return method_not_allowed_resource(*mr->dhr);
485485
} else {
486486
return std::shared_ptr<http_response>(new string_response(METHOD_ERROR, http_utils::http_method_not_allowed));
487487
}
488488
}
489489

490490
const std::shared_ptr<http_response> webserver::internal_error_page(details::modded_request* mr, bool force_our) const {
491-
if (internal_error_resource != 0x0 && !force_our) {
491+
if (internal_error_resource != nullptr && !force_our) {
492492
return internal_error_resource(*mr->dhr);
493493
} else {
494494
return std::shared_ptr<http_response>(new string_response(GENERIC_ERROR, http_utils::http_internal_server_error, "text/plain"));
@@ -507,13 +507,13 @@ MHD_Result webserver::requests_answer_first_step(MHD_Connection* connection, str
507507
const char *encoding = MHD_lookup_connection_value(connection, MHD_HEADER_KIND, http_utils::http_header_content_type);
508508

509509
if (post_process_enabled &&
510-
(0x0 != encoding &&
510+
(nullptr != encoding &&
511511
((0 == strncasecmp(http_utils::http_post_encoding_form_urlencoded, encoding, strlen(http_utils::http_post_encoding_form_urlencoded))) ||
512512
(0 == strncasecmp(http_utils::http_post_encoding_multipart_formdata, encoding, strlen(http_utils::http_post_encoding_multipart_formdata)))))) {
513513
const size_t post_memory_limit(32 * 1024); // Same as #MHD_POOL_SIZE_DEFAULT
514514
mr->pp = MHD_create_post_processor(connection, post_memory_limit, &post_iterator, mr);
515515
} else {
516-
mr->pp = NULL;
516+
mr->pp = nullptr;
517517
}
518518
return MHD_YES;
519519
}
@@ -529,7 +529,7 @@ MHD_Result webserver::requests_answer_second_step(MHD_Connection* connection, co
529529
#endif // DEBUG
530530
mr->dhr->grow_content(upload_data, *upload_data_size);
531531

532-
if (mr->pp != NULL) MHD_post_process(mr->pp, upload_data, *upload_data_size);
532+
if (mr->pp != nullptr) MHD_post_process(mr->pp, upload_data, *upload_data_size);
533533
}
534534

535535
*upload_data_size = 0;

0 commit comments

Comments
 (0)