Skip to content

Commit

Permalink
AK: Simplify constructors and conversions from nullptr_t
Browse files Browse the repository at this point in the history
Problem:
- Many constructors are defined as `{}` rather than using the ` =
  default` compiler-provided constructor.
- Some types provide an implicit conversion operator from `nullptr_t`
  instead of requiring the caller to default construct. This violates
  the C++ Core Guidelines suggestion to declare single-argument
  constructors explicit
  (https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#c46-by-default-declare-single-argument-constructors-explicit).

Solution:
- Change default constructors to use the compiler-provided default
  constructor.
- Remove implicit conversion operators from `nullptr_t` and change
  usage to enforce type consistency without conversion.
  • Loading branch information
ldm5180 authored and awesomekling committed Jan 12, 2021
1 parent 9dc44bf commit e6f907a
Show file tree
Hide file tree
Showing 105 changed files with 300 additions and 244 deletions.
2 changes: 1 addition & 1 deletion AK/Badge.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ namespace AK {
template<typename T>
class Badge {
friend T;
Badge() { }
constexpr Badge() = default;

Badge(const Badge&) = delete;
Badge& operator=(const Badge&) = delete;
Expand Down
38 changes: 31 additions & 7 deletions AK/ByteBuffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
#pragma once

#include <AK/NonnullRefPtr.h>
#include <AK/OwnPtr.h>
#include <AK/RefCounted.h>
#include <AK/RefPtr.h>
#include <AK/Span.h>
Expand All @@ -42,6 +43,7 @@ class ByteBufferImpl : public RefCounted<ByteBufferImpl> {
static NonnullRefPtr<ByteBufferImpl> create_zeroed(size_t);
static NonnullRefPtr<ByteBufferImpl> copy(const void*, size_t);

ByteBufferImpl() = delete;
~ByteBufferImpl() { clear(); }

void clear()
Expand Down Expand Up @@ -92,16 +94,14 @@ class ByteBufferImpl : public RefCounted<ByteBufferImpl> {
private:
explicit ByteBufferImpl(size_t);
ByteBufferImpl(const void*, size_t);
ByteBufferImpl() { }

u8* m_data { nullptr };
size_t m_size { 0 };
};

class ByteBuffer {
public:
ByteBuffer() { }
ByteBuffer(std::nullptr_t) { }
ByteBuffer() = default;
ByteBuffer(const ByteBuffer& other)
: m_impl(other.m_impl)
{
Expand Down Expand Up @@ -159,11 +159,35 @@ class ByteBuffer {
u8* data() { return m_impl ? m_impl->data() : nullptr; }
const u8* data() const { return m_impl ? m_impl->data() : nullptr; }

Bytes bytes() { return m_impl ? m_impl->bytes() : nullptr; }
ReadonlyBytes bytes() const { return m_impl ? m_impl->bytes() : nullptr; }
Bytes bytes()
{
if (m_impl) {
return m_impl->bytes();
}
return {};
}
ReadonlyBytes bytes() const
{
if (m_impl) {
return m_impl->bytes();
}
return {};
}

Span<u8> span() { return m_impl ? m_impl->span() : nullptr; }
Span<const u8> span() const { return m_impl ? m_impl->span() : nullptr; }
Span<u8> span()
{
if (m_impl) {
return m_impl->span();
}
return {};
}
Span<const u8> span() const
{
if (m_impl) {
return m_impl->span();
}
return {};
}

u8* offset_pointer(int offset) { return m_impl ? m_impl->offset_pointer(offset) : nullptr; }
const u8* offset_pointer(int offset) const { return m_impl ? m_impl->offset_pointer(offset) : nullptr; }
Expand Down
2 changes: 1 addition & 1 deletion AK/DoublyLinkedList.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ class DoublyLinkedList {
};

public:
DoublyLinkedList() { }
DoublyLinkedList() = default;
~DoublyLinkedList() { clear(); }

bool is_empty() const { return !m_head; }
Expand Down
2 changes: 1 addition & 1 deletion AK/Endian.h
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ class [[gnu::packed]] LittleEndian {
friend InputStream& operator>><T>(InputStream&, LittleEndian<T>&);
friend OutputStream& operator<<<T>(OutputStream&, LittleEndian<T>);

constexpr LittleEndian() { }
constexpr LittleEndian() = default;

constexpr LittleEndian(T value)
: m_value(convert_between_host_and_little_endian(value))
Expand Down
2 changes: 1 addition & 1 deletion AK/FlyString.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ namespace AK {

class FlyString {
public:
FlyString() { }
FlyString() = default;
FlyString(const FlyString& other)
: m_impl(other.impl())
{
Expand Down
6 changes: 3 additions & 3 deletions AK/Format.h
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ struct StandardFormatter {

template<typename T>
struct Formatter<T, typename EnableIf<IsIntegral<T>::value>::Type> : StandardFormatter {
Formatter() { }
Formatter() = default;
explicit Formatter(StandardFormatter formatter)
: StandardFormatter(formatter)
{
Expand All @@ -277,7 +277,7 @@ struct Formatter<T, typename EnableIf<IsIntegral<T>::value>::Type> : StandardFor

template<>
struct Formatter<StringView> : StandardFormatter {
Formatter() { }
Formatter() = default;
explicit Formatter(StandardFormatter formatter)
: StandardFormatter(formatter)
{
Expand Down Expand Up @@ -338,7 +338,7 @@ struct Formatter<float> : StandardFormatter {
};
template<>
struct Formatter<double> : StandardFormatter {
Formatter() { }
Formatter() = default;
explicit Formatter(StandardFormatter formatter)
: StandardFormatter(formatter)
{
Expand Down
23 changes: 17 additions & 6 deletions AK/Function.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,10 @@

#pragma once

#include "Assertions.h"
#include "OwnPtr.h"
#include "StdLibExtras.h"
#include <AK/Assertions.h>
#include <AK/OwnPtr.h>
#include <AK/StdLibExtras.h>
#include <AK/Traits.h>

namespace AK {

Expand All @@ -38,7 +39,6 @@ template<typename Out, typename... In>
class Function<Out(In...)> {
public:
Function() = default;
Function(std::nullptr_t) { }

template<typename CallableType, class = typename EnableIf<!(IsPointer<CallableType>::value && IsFunction<typename RemovePointer<CallableType>::Type>::value) && IsRvalueReference<CallableType&&>::value>::Type>
Function(CallableType&& callable)
Expand Down Expand Up @@ -83,7 +83,7 @@ class Function<Out(In...)> {
private:
class CallableWrapperBase {
public:
virtual ~CallableWrapperBase() { }
virtual ~CallableWrapperBase() = default;
virtual Out call(In...) const = 0;
};

Expand All @@ -98,7 +98,18 @@ class Function<Out(In...)> {
CallableWrapper(const CallableWrapper&) = delete;
CallableWrapper& operator=(const CallableWrapper&) = delete;

Out call(In... in) const final override { return m_callable(forward<In>(in)...); }
Out call(In... in) const final override
{
if constexpr (requires { m_callable(forward<In>(in)...); }) {
return m_callable(forward<In>(in)...);
} else if constexpr (requires { m_callable(); }) {
return m_callable();
} else if constexpr (IsSame<void, Out>::value) {
return;
} else {
return {};
}
}

private:
CallableType m_callable;
Expand Down
2 changes: 1 addition & 1 deletion AK/HashMap.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ class HashMap {
};

public:
HashMap() { }
HashMap() = default;

#ifndef SERENITY_LIBC_BUILD
HashMap(std::initializer_list<Entry> list)
Expand Down
2 changes: 1 addition & 1 deletion AK/HashTable.h
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ class HashTable {
};

public:
HashTable() { }
HashTable() = default;
HashTable(size_t capacity) { rehash(capacity); }

~HashTable()
Expand Down
4 changes: 2 additions & 2 deletions AK/IDAllocator.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ namespace AK {
class IDAllocator {

public:
IDAllocator() { }
~IDAllocator() { }
IDAllocator() = default;
~IDAllocator() = default;

int allocate()
{
Expand Down
2 changes: 1 addition & 1 deletion AK/InlineLinkedList.h
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ inline T* InlineLinkedListNode<T>::next() const
template<typename T>
class InlineLinkedList {
public:
InlineLinkedList() { }
InlineLinkedList() = default;

bool is_empty() const { return !m_head; }
size_t size_slow() const;
Expand Down
4 changes: 2 additions & 2 deletions AK/JsonArray.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ namespace AK {

class JsonArray {
public:
JsonArray() { }
~JsonArray() { }
JsonArray() = default;
~JsonArray() = default;

JsonArray(const JsonArray& other)
: m_values(other.m_values)
Expand Down
4 changes: 2 additions & 2 deletions AK/JsonObject.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ namespace AK {

class JsonObject {
public:
JsonObject() { }
~JsonObject() { }
JsonObject() = default;
~JsonObject() = default;

JsonObject(const JsonObject& other)
: m_order(other.m_order)
Expand Down
2 changes: 1 addition & 1 deletion AK/LexicalPath.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ namespace AK {

class LexicalPath {
public:
LexicalPath() { }
LexicalPath() = default;
explicit LexicalPath(const StringView&);

bool is_valid() const { return m_is_valid; }
Expand Down
8 changes: 4 additions & 4 deletions AK/LogStream.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ class LogStream {
#endif
{
}
virtual ~LogStream() { }
virtual ~LogStream() = default;

virtual void write(const char*, int) const = 0;

Expand Down Expand Up @@ -94,7 +94,7 @@ class BufferedLogStream : public LogStream {
bool empty() const { return m_size == 0; }

public:
BufferedLogStream() { }
BufferedLogStream() = default;

virtual ~BufferedLogStream() override
{
Expand All @@ -114,7 +114,7 @@ class BufferedLogStream : public LogStream {

class DebugLogStream final : public BufferedLogStream {
public:
DebugLogStream() { }
DebugLogStream() = default;
virtual ~DebugLogStream() override;

// DebugLogStream only checks `enabled` and possibly generates output while the destructor runs.
Expand All @@ -128,7 +128,7 @@ class DebugLogStream final : public BufferedLogStream {
#ifdef KERNEL
class KernelLogStream final : public BufferedLogStream {
public:
KernelLogStream() { }
KernelLogStream() = default;
virtual ~KernelLogStream() override;
};
#endif
Expand Down
2 changes: 1 addition & 1 deletion AK/Optional.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ namespace AK {
template<typename T>
class alignas(T) [[nodiscard]] Optional {
public:
Optional() { }
Optional() = default;

Optional(const T& value)
: m_has_value(true)
Expand Down
3 changes: 1 addition & 2 deletions AK/OwnPtr.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ namespace AK {
template<typename T>
class OwnPtr {
public:
OwnPtr() { }
OwnPtr() = default;
explicit OwnPtr(T* ptr)
: m_ptr(ptr)
{
Expand All @@ -57,7 +57,6 @@ class OwnPtr {
: m_ptr(other.leak_ptr())
{
}
OwnPtr(std::nullptr_t) {};
~OwnPtr()
{
clear();
Expand Down
4 changes: 2 additions & 2 deletions AK/Queue.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ namespace AK {
template<typename T, int segment_size = 1000>
class Queue {
public:
Queue() { }
~Queue() { }
Queue() = default;
~Queue() = default;

size_t size() const { return m_size; }
bool is_empty() const { return m_size == 0; }
Expand Down
2 changes: 1 addition & 1 deletion AK/RefCounted.h
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ class RefCountedBase {
}

protected:
RefCountedBase() { }
RefCountedBase() = default;
ALWAYS_INLINE ~RefCountedBase()
{
ASSERT(m_ref_count.load(AK::MemoryOrder::memory_order_relaxed) == 0);
Expand Down
3 changes: 1 addition & 2 deletions AK/RefPtr.h
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ class RefPtr {
Adopt
};

RefPtr() { }
RefPtr() = default;
RefPtr(const T* ptr)
: m_bits(PtrTraits::as_bits(const_cast<T*>(ptr)))
{
Expand Down Expand Up @@ -205,7 +205,6 @@ class RefPtr {
m_bits.store(0xe0e0e0e0, AK::MemoryOrder::memory_order_relaxed);
#endif
}
RefPtr(std::nullptr_t) { }

template<typename U>
RefPtr(const OwnPtr<U>&) = delete;
Expand Down
4 changes: 2 additions & 2 deletions AK/SinglyLinkedList.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ namespace AK {
template<typename ListType, typename ElementType>
class SinglyLinkedListIterator {
public:
SinglyLinkedListIterator() { }
SinglyLinkedListIterator() = default;
bool operator!=(const SinglyLinkedListIterator& other) const { return m_node != other.m_node; }
SinglyLinkedListIterator& operator++()
{
Expand Down Expand Up @@ -78,7 +78,7 @@ class SinglyLinkedList {
};

public:
SinglyLinkedList() { }
SinglyLinkedList() = default;
~SinglyLinkedList() { clear(); }

bool is_empty() const { return !head(); }
Expand Down
4 changes: 2 additions & 2 deletions AK/SinglyLinkedListWithCount.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ template<typename T>
class SinglyLinkedListWithCount : private SinglyLinkedList<T> {

public:
SinglyLinkedListWithCount() { }
~SinglyLinkedListWithCount() { }
SinglyLinkedListWithCount() = default;
~SinglyLinkedListWithCount() = default;

using List = SinglyLinkedList<T>;

Expand Down
5 changes: 1 addition & 4 deletions AK/Span.h
Original file line number Diff line number Diff line change
Expand Up @@ -105,10 +105,7 @@ class Span : public Detail::Span<T> {
public:
using Detail::Span<T>::Span;

ALWAYS_INLINE constexpr Span(std::nullptr_t)
: Span()
{
}
constexpr Span() = default;

ALWAYS_INLINE constexpr Span(const Span& other)
: Span(other.m_values, other.m_size)
Expand Down
Loading

0 comments on commit e6f907a

Please sign in to comment.