Skip to content

Commit

Permalink
src: use if constexpr where appropriate
Browse files Browse the repository at this point in the history
Doesn't change much but communicates to readers that these
are compile-time conditionals.

PR-URL: #44291
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Feng Yu <F3n67u@outlook.com>
Reviewed-By: Rich Trott <rtrott@gmail.com>
  • Loading branch information
addaleax authored and RafaelGSS committed Sep 5, 2022
1 parent b670954 commit 7f16177
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 8 deletions.
10 changes: 5 additions & 5 deletions src/base_object-inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ bool BaseObject::has_pointer_data() const {
template <typename T, bool kIsWeak>
BaseObject::PointerData*
BaseObjectPtrImpl<T, kIsWeak>::pointer_data() const {
if (kIsWeak) {
if constexpr (kIsWeak) {
return data_.pointer_data;
}
if (get_base_object() == nullptr) {
Expand All @@ -126,7 +126,7 @@ BaseObjectPtrImpl<T, kIsWeak>::pointer_data() const {

template <typename T, bool kIsWeak>
BaseObject* BaseObjectPtrImpl<T, kIsWeak>::get_base_object() const {
if (kIsWeak) {
if constexpr (kIsWeak) {
if (pointer_data() == nullptr) {
return nullptr;
}
Expand All @@ -137,7 +137,7 @@ BaseObject* BaseObjectPtrImpl<T, kIsWeak>::get_base_object() const {

template <typename T, bool kIsWeak>
BaseObjectPtrImpl<T, kIsWeak>::~BaseObjectPtrImpl() {
if (kIsWeak) {
if constexpr (kIsWeak) {
if (pointer_data() != nullptr &&
--pointer_data()->weak_ptr_count == 0 &&
pointer_data()->self == nullptr) {
Expand All @@ -157,7 +157,7 @@ template <typename T, bool kIsWeak>
BaseObjectPtrImpl<T, kIsWeak>::BaseObjectPtrImpl(T* target)
: BaseObjectPtrImpl() {
if (target == nullptr) return;
if (kIsWeak) {
if constexpr (kIsWeak) {
data_.pointer_data = target->pointer_data();
CHECK_NOT_NULL(pointer_data());
pointer_data()->weak_ptr_count++;
Expand Down Expand Up @@ -198,7 +198,7 @@ BaseObjectPtrImpl<T, kIsWeak>& BaseObjectPtrImpl<T, kIsWeak>::operator=(
template <typename T, bool kIsWeak>
BaseObjectPtrImpl<T, kIsWeak>::BaseObjectPtrImpl(BaseObjectPtrImpl&& other)
: data_(other.data_) {
if (kIsWeak)
if constexpr (kIsWeak)
other.data_.target = nullptr;
else
other.data_.pointer_data = nullptr;
Expand Down
2 changes: 1 addition & 1 deletion src/json_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ class JSONWriter {
typename test_for_number = typename std::
enable_if<std::numeric_limits<T>::is_specialized, bool>::type>
inline void write_value(T number) {
if (std::is_same<T, bool>::value)
if constexpr (std::is_same<T, bool>::value)
out_ << (number ? "true" : "false");
else
out_ << number;
Expand Down
2 changes: 1 addition & 1 deletion src/node_http_parser.cc
Original file line number Diff line number Diff line change
Expand Up @@ -674,7 +674,7 @@ class Parser : public AsyncWrap, public StreamListener {
// Should always be called from the same context.
CHECK_EQ(env, parser->env());

if (should_pause) {
if constexpr (should_pause) {
llhttp_pause(&parser->parser_);
} else {
llhttp_resume(&parser->parser_);
Expand Down
2 changes: 1 addition & 1 deletion src/node_zlib.cc
Original file line number Diff line number Diff line change
Expand Up @@ -361,7 +361,7 @@ class CompressionStream : public AsyncWrap, public ThreadPoolWork {
ctx_.SetBuffers(in, in_len, out, out_len);
ctx_.SetFlush(flush);

if (!async) {
if constexpr (!async) {
// sync version
AsyncWrap::env()->PrintSyncTrace();
DoThreadPoolWork();
Expand Down

0 comments on commit 7f16177

Please sign in to comment.