1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-26 04:57:44 +00:00

AK: Simplify constructors and conversions from nullptr_t

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.
This commit is contained in:
Lenny Maiorani 2021-01-10 16:29:28 -07:00 committed by Andreas Kling
parent 9dc44bf8c4
commit e6f907a155
105 changed files with 300 additions and 244 deletions

View file

@ -24,6 +24,7 @@
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <AK/WeakPtr.h>
#include <LibCore/Event.h>
#include <LibCore/Object.h>
@ -32,7 +33,7 @@ namespace Core {
ChildEvent::ChildEvent(Type type, Object& child, Object* insertion_before_child)
: Core::Event(type)
, m_child(child.make_weak_ptr())
, m_insertion_before_child(insertion_before_child ? insertion_before_child->make_weak_ptr() : nullptr)
, m_insertion_before_child(AK::try_make_weak_ptr(insertion_before_child))
{
}

View file

@ -503,7 +503,7 @@ bool SignalHandlers::remove(int handler_id)
auto it = m_handlers.find(handler_id);
if (it != m_handlers.end()) {
// Mark pending remove
m_handlers_pending.set(handler_id, nullptr);
m_handlers_pending.set(handler_id, {});
return true;
}
it = m_handlers_pending.find(handler_id);

View file

@ -256,7 +256,7 @@ const LogStream& operator<<(const LogStream&, const Object&);
register_property( \
property_name, \
[this] { return this->getter(); }, \
nullptr);
{});
#define REGISTER_RECT_PROPERTY(property_name, getter, setter) \
register_property( \

View file

@ -37,7 +37,7 @@ OwnPtr<Reader> Reader::create(const String& path)
{
auto file_or_error = MappedFile::map(path);
if (file_or_error.is_error())
return nullptr;
return {};
return adopt_own(*new Reader(file_or_error.release_value()));
}

View file

@ -90,12 +90,12 @@ OwnPtr<DebugSession> DebugSession::exec_and_attach(const String& command, String
if (waitpid(pid, nullptr, WSTOPPED) != pid) {
perror("waitpid");
return nullptr;
return {};
}
if (ptrace(PT_ATTACH, pid, 0, 0) < 0) {
perror("PT_ATTACH");
return nullptr;
return {};
}
// We want to continue until the exit from the 'execve' sycsall.
@ -105,7 +105,7 @@ OwnPtr<DebugSession> DebugSession::exec_and_attach(const String& command, String
if (waitpid(pid, nullptr, WSTOPPED) != pid) {
perror("wait_pid");
return nullptr;
return {};
}
auto debug_session = adopt_own(*new DebugSession(pid, source_root));
@ -114,7 +114,7 @@ OwnPtr<DebugSession> DebugSession::exec_and_attach(const String& command, String
int wstatus = debug_session->continue_debuggee_and_wait();
if (WSTOPSIG(wstatus) != SIGTRAP) {
dbgln("expected SIGTRAP");
return nullptr;
return {};
}
// At this point, libraries should have been loaded

View file

@ -24,6 +24,7 @@
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <AK/WeakPtr.h>
#include <LibGUI/AboutDialog.h>
#include <LibGUI/Action.h>
#include <LibGUI/ActionGroup.h>
@ -39,7 +40,7 @@ namespace CommonActions {
NonnullRefPtr<Action> make_about_action(const String& app_name, const Icon& app_icon, Window* parent)
{
WeakPtr<Window> weak_parent = parent ? parent->make_weak_ptr<Window>() : nullptr;
auto weak_parent = AK::try_make_weak_ptr<Window>(parent);
return Action::create(String::formatted("About {}", app_name), app_icon.bitmap_for_size(16), [=](auto&) {
AboutDialog::show(app_name, app_icon.bitmap_for_size(32), weak_parent.ptr());
});
@ -288,7 +289,7 @@ void Action::set_checked(bool checked)
void Action::set_group(Badge<ActionGroup>, ActionGroup* group)
{
m_action_group = group ? group->make_weak_ptr() : nullptr;
m_action_group = AK::try_make_weak_ptr(group);
}
void Action::set_icon(const Gfx::Bitmap* icon)

View file

@ -171,7 +171,7 @@ protected:
if (!drain_messages_from_peer())
break;
}
return nullptr;
return {};
}
bool drain_messages_from_peer()

View file

@ -43,7 +43,7 @@ void ArrayBufferPrototype::initialize(GlobalObject& global_object)
u8 attr = Attribute::Writable | Attribute::Configurable;
define_native_function(vm.names.slice, slice, 2, attr);
// FIXME: This should be an accessor property
define_native_property(vm.names.byteLength, byte_length_getter, nullptr, Attribute::Configurable);
define_native_property(vm.names.byteLength, byte_length_getter, {}, Attribute::Configurable);
define_property(vm.well_known_symbol_to_string_tag(), js_string(vm.heap(), "ArrayBuffer"), Attribute::Configurable);
}

View file

@ -45,7 +45,7 @@ void ErrorPrototype::initialize(GlobalObject& global_object)
Object::initialize(global_object);
u8 attr = Attribute::Writable | Attribute::Configurable;
define_native_property(vm.names.name, name_getter, name_setter, attr);
define_native_property(vm.names.message, message_getter, nullptr, attr);
define_native_property(vm.names.message, message_getter, {}, attr);
define_native_function(vm.names.toString, to_string, 0, attr);
}

View file

@ -50,11 +50,11 @@ void RegExpPrototype::initialize(GlobalObject& global_object)
define_native_function(vm.names.exec, exec, 1, attr);
u8 readable_attr = Attribute::Configurable;
define_native_property(vm.names.flags, flags, nullptr, readable_attr);
define_native_property(vm.names.source, source, nullptr, readable_attr);
define_native_property(vm.names.flags, flags, {}, readable_attr);
define_native_property(vm.names.source, source, {}, readable_attr);
#define __JS_ENUMERATE(flagName, flag_name, flag_char, ECMAScriptFlagName) \
define_native_property(vm.names.flagName, flag_name, nullptr, readable_attr);
define_native_property(vm.names.flagName, flag_name, {}, readable_attr);
JS_ENUMERATE_REGEXP_FLAGS
#undef __JS_ENUMERATE
}

View file

@ -73,8 +73,8 @@ void ScriptFunction::initialize(GlobalObject& global_object)
prototype->define_property(vm.names.constructor, this, Attribute::Writable | Attribute::Configurable);
define_property(vm.names.prototype, prototype, Attribute::Writable);
}
define_native_property(vm.names.length, length_getter, nullptr, Attribute::Configurable);
define_native_property(vm.names.name, name_getter, nullptr, Attribute::Configurable);
define_native_property(vm.names.length, length_getter, {}, Attribute::Configurable);
define_native_property(vm.names.name, name_getter, {}, Attribute::Configurable);
}
ScriptFunction::~ScriptFunction()

View file

@ -82,7 +82,7 @@ void StringPrototype::initialize(GlobalObject& global_object)
StringObject::initialize(global_object);
u8 attr = Attribute::Writable | Attribute::Configurable;
define_native_property(vm.names.length, length_getter, nullptr, 0);
define_native_property(vm.names.length, length_getter, {}, 0);
define_native_function(vm.names.charAt, char_at, 1, attr);
define_native_function(vm.names.charCodeAt, char_code_at, 1, attr);
define_native_function(vm.names.repeat, repeat, 1, attr);

View file

@ -47,7 +47,7 @@ void SymbolPrototype::initialize(GlobalObject& global_object)
{
auto& vm = this->vm();
Object::initialize(global_object);
define_native_property(vm.names.description, description_getter, nullptr, Attribute::Configurable);
define_native_property(vm.names.description, description_getter, {}, Attribute::Configurable);
define_native_function(vm.names.toString, to_string, 0, Attribute::Writable | Attribute::Configurable);
define_native_function(vm.names.valueOf, value_of, 0, Attribute::Writable | Attribute::Configurable);

View file

@ -40,7 +40,7 @@ void TypedArrayPrototype::initialize(GlobalObject& object)
auto& vm = this->vm();
Object::initialize(object);
// FIXME: This should be an accessor property
define_native_property(vm.names.length, length_getter, nullptr, Attribute::Configurable);
define_native_property(vm.names.length, length_getter, {}, Attribute::Configurable);
}
TypedArrayPrototype::~TypedArrayPrototype()

View file

@ -42,7 +42,7 @@ Uint8ClampedArray::Uint8ClampedArray(u32 length, Object& prototype)
, m_length(length)
{
auto& vm = this->vm();
define_native_property(vm.names.length, length_getter, nullptr);
define_native_property(vm.names.length, length_getter, {});
m_data = (u8*)calloc(m_length, 1);
}

View file

@ -43,7 +43,7 @@ Function<bool(Editor&)> Editor::find_internal_function(const StringView& name)
ENUMERATE_EDITOR_INTERNAL_FUNCTIONS(__ENUMERATE)
return nullptr;
return {};
}
void Editor::search_forwards()

View file

@ -112,13 +112,13 @@ String CodeBlock::render_for_terminal(size_t) const
OwnPtr<CodeBlock> CodeBlock::parse(Vector<StringView>::ConstIterator& lines)
{
if (lines.is_end())
return nullptr;
return {};
constexpr auto tick_tick_tick = "```";
StringView line = *lines;
if (!line.starts_with(tick_tick_tick))
return nullptr;
return {};
// Our Markdown extension: we allow
// specifying a style and a language
@ -134,7 +134,7 @@ OwnPtr<CodeBlock> CodeBlock::parse(Vector<StringView>::ConstIterator& lines)
StringView style_spec = line.substring_view(3, line.length() - 3);
auto spec = Text::parse(style_spec);
if (!spec.has_value())
return nullptr;
return {};
++lines;

View file

@ -119,7 +119,7 @@ OwnPtr<Document> Document::parse(const StringView& str)
auto line = Paragraph::Line::parse(lines);
if (!line)
return nullptr;
return {};
paragraph_lines.append(line.release_nonnull());
}

View file

@ -62,7 +62,7 @@ String Heading::render_for_terminal(size_t) const
OwnPtr<Heading> Heading::parse(Vector<StringView>::ConstIterator& lines)
{
if (lines.is_end())
return nullptr;
return {};
const StringView& line = *lines;
size_t level;
@ -73,12 +73,12 @@ OwnPtr<Heading> Heading::parse(Vector<StringView>::ConstIterator& lines)
}
if (!level || level >= line.length() || line[level] != ' ')
return nullptr;
return {};
StringView title_view = line.substring_view(level + 1, line.length() - level - 1);
auto text = Text::parse(title_view);
if (!text.has_value())
return nullptr;
return {};
auto heading = make<Heading>(move(text.value()), level);

View file

@ -47,19 +47,19 @@ String HorizontalRule::render_for_terminal(size_t view_width) const
OwnPtr<HorizontalRule> HorizontalRule::parse(Vector<StringView>::ConstIterator& lines)
{
if (lines.is_end())
return nullptr;
return {};
const StringView& line = *lines;
if (line.length() < 3)
return nullptr;
return {};
if (!line.starts_with('-') && !line.starts_with('_') && !line.starts_with('*'))
return nullptr;
return {};
auto first_character = line.characters_without_null_termination()[0];
for (auto ch : line) {
if (ch != first_character)
return nullptr;
return {};
}
++lines;

View file

@ -122,20 +122,20 @@ OwnPtr<List> List::parse(Vector<StringView>::ConstIterator& lines)
if (first)
is_ordered = appears_ordered;
else if (is_ordered != appears_ordered)
return nullptr;
return {};
if (!flush_item_if_needed())
return nullptr;
return {};
while (offset + 1 < line.length() && line[offset + 1] == ' ')
offset++;
} else {
if (first)
return nullptr;
return {};
for (size_t i = 0; i < offset; i++) {
if (line[i] != ' ')
return nullptr;
return {};
}
}
@ -149,7 +149,7 @@ OwnPtr<List> List::parse(Vector<StringView>::ConstIterator& lines)
}
if (!flush_item_if_needed() || first)
return nullptr;
return {};
return make<List>(move(items), is_ordered);
}

View file

@ -61,11 +61,11 @@ String Paragraph::render_for_terminal(size_t) const
OwnPtr<Paragraph::Line> Paragraph::Line::parse(Vector<StringView>::ConstIterator& lines)
{
if (lines.is_end())
return nullptr;
return {};
auto text = Text::parse(*lines++);
if (!text.has_value())
return nullptr;
return {};
return make<Paragraph::Line>(text.release_value());
}

View file

@ -118,12 +118,12 @@ OwnPtr<Table> Table::parse(Vector<StringView>::ConstIterator& lines)
auto peek_it = lines;
auto first_line = *peek_it;
if (!first_line.starts_with('|'))
return nullptr;
return {};
++peek_it;
if (peek_it.is_end())
return nullptr;
return {};
auto header_segments = first_line.split_view('|', true);
auto header_delimiters = peek_it->split_view('|', true);
@ -141,10 +141,10 @@ OwnPtr<Table> Table::parse(Vector<StringView>::ConstIterator& lines)
++peek_it;
if (header_delimiters.size() != header_segments.size())
return nullptr;
return {};
if (header_delimiters.is_empty())
return nullptr;
return {};
size_t total_width = 0;
@ -154,7 +154,7 @@ OwnPtr<Table> Table::parse(Vector<StringView>::ConstIterator& lines)
for (size_t i = 0; i < header_segments.size(); ++i) {
auto text_option = Text::parse(header_segments[i]);
if (!text_option.has_value())
return nullptr; // An invalid 'text' in the header should just fail the table parse.
return {}; // An invalid 'text' in the header should just fail the table parse.
auto text = text_option.release_value();
auto& column = table->m_columns[i];

View file

@ -138,10 +138,10 @@ int Database::init()
ParseMode mode = ParseMode::UnknownMode;
OwnPtr<Vendor> current_vendor = nullptr;
OwnPtr<Device> current_device = nullptr;
OwnPtr<Class> current_class = nullptr;
OwnPtr<Subclass> current_subclass = nullptr;
OwnPtr<Vendor> current_vendor {};
OwnPtr<Device> current_device {};
OwnPtr<Class> current_class {};
OwnPtr<Subclass> current_subclass {};
auto commit_device = [&]() {
if (current_device && current_vendor) {

View file

@ -69,7 +69,7 @@ int regcomp(regex_t* reg, const char* pattern, int cflags)
// Note that subsequent uses of regcomp() without regfree() _will_ leak memory
// This could've been prevented if libc provided a reginit() or similar, but it does not.
reg->__data = new internal_regex_t { 0, 0, nullptr, 0, ReError::REG_NOERR, {}, 0 };
reg->__data = new internal_regex_t { 0, 0, {}, 0, ReError::REG_NOERR, {}, 0 };
auto preg = impl_from(reg);

View file

@ -142,11 +142,11 @@ void CanvasRenderingContext2D::did_draw(const Gfx::FloatRect&)
OwnPtr<Gfx::Painter> CanvasRenderingContext2D::painter()
{
if (!m_element)
return nullptr;
return {};
if (!m_element->bitmap()) {
if (!m_element->create_bitmap())
return nullptr;
return {};
}
return make<Gfx::Painter>(*m_element->bitmap());
@ -208,7 +208,7 @@ RefPtr<ImageData> CanvasRenderingContext2D::create_image_data(int width, int hei
{
if (!wrapper()) {
dbgln("Hmm! Attempted to create ImageData for wrapper-less CRC2D.");
return nullptr;
return {};
}
return ImageData::create_with_size(wrapper()->global_object(), width, height);
}