mirror of
https://github.com/RGBCube/serenity
synced 2025-05-31 04:48:14 +00:00
AK: Make MappedFile heap-allocated and ref-counted
Let's adapt this class a bit better to how it's actually being used. Instead of having valid/invalid states and storing an error in case it's invalid, a MappedFile is now always valid, and the factory function that creates it will return an OSError if mapping fails.
This commit is contained in:
parent
70fce5c4c7
commit
2f3b901f7f
36 changed files with 184 additions and 199 deletions
|
@ -79,22 +79,24 @@ String ManualModel::page_path(const GUI::ModelIndex& index) const
|
|||
return page->path();
|
||||
}
|
||||
|
||||
Result<StringView, int> ManualModel::page_view(const String& path) const
|
||||
Result<StringView, OSError> ManualModel::page_view(const String& path) const
|
||||
{
|
||||
if (path.is_empty())
|
||||
return StringView {};
|
||||
|
||||
auto mapped_file = m_mapped_files.get(path);
|
||||
if (mapped_file.has_value())
|
||||
return StringView { (const char*)mapped_file.value()->data(), mapped_file.value()->size() };
|
||||
{
|
||||
// Check if we've got it cached already.
|
||||
auto mapped_file = m_mapped_files.get(path);
|
||||
if (mapped_file.has_value())
|
||||
return StringView { mapped_file.value()->bytes() };
|
||||
}
|
||||
|
||||
auto map = make<MappedFile>(path);
|
||||
if (!map->is_valid())
|
||||
return map->errno_if_invalid();
|
||||
|
||||
StringView view { (const char*)map->data(), map->size() };
|
||||
m_mapped_files.set(path, move(map));
|
||||
auto file_or_error = MappedFile::map(path);
|
||||
if (file_or_error.is_error())
|
||||
return file_or_error.error();
|
||||
|
||||
StringView view { file_or_error.value()->bytes() };
|
||||
m_mapped_files.set(path, file_or_error.release_value());
|
||||
return view;
|
||||
}
|
||||
|
||||
|
|
|
@ -45,7 +45,7 @@ public:
|
|||
|
||||
String page_path(const GUI::ModelIndex&) const;
|
||||
String page_and_section(const GUI::ModelIndex&) const;
|
||||
Result<StringView, int> page_view(const String& path) const;
|
||||
Result<StringView, OSError> page_view(const String& path) const;
|
||||
|
||||
void update_section_node_on_toggle(const GUI::ModelIndex&, const bool);
|
||||
virtual int row_count(const GUI::ModelIndex& = GUI::ModelIndex()) const override;
|
||||
|
@ -62,5 +62,5 @@ private:
|
|||
GUI::Icon m_section_open_icon;
|
||||
GUI::Icon m_section_icon;
|
||||
GUI::Icon m_page_icon;
|
||||
mutable HashMap<String, OwnPtr<MappedFile>> m_mapped_files;
|
||||
mutable HashMap<String, NonnullRefPtr<MappedFile>> m_mapped_files;
|
||||
};
|
||||
|
|
|
@ -159,7 +159,7 @@ int main(int argc, char* argv[])
|
|||
|
||||
auto source_result = model->page_view(path);
|
||||
if (source_result.is_error()) {
|
||||
GUI::MessageBox::show(window, strerror(source_result.error()), "Failed to open man page", GUI::MessageBox::Type::Error);
|
||||
GUI::MessageBox::show(window, source_result.error().string(), "Failed to open man page", GUI::MessageBox::Type::Error);
|
||||
return;
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue