1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-26 01:27:43 +00:00

AK: Add Eternal<T> and use it in various places.

This is useful for static locals that never need to be destroyed:

Thing& Thing::the()
{
    static Eternal<Thing> the;
    return the;
}

The object will be allocated in data segment memory and will never have
its destructor invoked.
This commit is contained in:
Andreas Kling 2019-04-03 16:50:08 +02:00
parent 528054d192
commit c02c9880b6
23 changed files with 78 additions and 81 deletions

View file

@ -26,7 +26,7 @@ void GButton::paint_event(GPaintEvent& event)
GPainter painter(*this);
painter.add_clip_rect(event.rect());
StylePainter::the().paint_button(painter, rect(), m_button_style, m_being_pressed, m_hovered);
StylePainter::paint_button(painter, rect(), m_button_style, m_being_pressed, m_hovered);
if (!caption().is_empty() || m_icon) {
auto content_rect = rect();

View file

@ -3,19 +3,7 @@
#include <WindowServer/WSAPITypes.h>
#include <LibC/SharedBuffer.h>
GClipboard& GClipboard::the()
{
static GClipboard* s_the;
if (!s_the)
s_the = new GClipboard;
return *s_the;
}
GClipboard::GClipboard()
{
}
String GClipboard::data() const
String GClipboard::data()
{
WSAPI_ClientMessage request;
request.type = WSAPI_ClientMessage::Type::GetClipboardContents;

View file

@ -4,11 +4,6 @@
class GClipboard {
public:
static GClipboard& the();
String data() const;
void set_data(const String&);
private:
GClipboard();
static String data();
static void set_data(const String&);
};

View file

@ -1,13 +1,12 @@
#include <LibGUI/GDesktop.h>
#include <LibGUI/GEventLoop.h>
#include <AK/Eternal.h>
#include <string.h>
GDesktop& GDesktop::the()
{
static GDesktop* s_the;
if (!s_the)
s_the = new GDesktop;
return *s_the;
static Eternal<GDesktop> the;
return the;
}
GDesktop::GDesktop()

View file

@ -6,12 +6,11 @@
class GDesktop {
public:
static GDesktop& the();
GDesktop();
String wallpaper() const;
bool set_wallpaper(const String& path);
private:
GDesktop();
Rect m_rect;
};

View file

@ -1,16 +1,14 @@
#include <LibGUI/GFontDatabase.h>
#include <SharedGraphics/Font.h>
#include <AK/Eternal.h>
#include <dirent.h>
#include <stdio.h>
#include <stdlib.h>
static GFontDatabase* s_the;
GFontDatabase& GFontDatabase::the()
{
if (!s_the)
s_the = new GFontDatabase;
return *s_the;
static Eternal<GFontDatabase> the;
return the;
}
GFontDatabase::GFontDatabase()

View file

@ -9,13 +9,13 @@ class Font;
class GFontDatabase {
public:
static GFontDatabase& the();
GFontDatabase();
RetainPtr<Font> get_by_name(const String&);
void for_each_font(Function<void(const String&)>);
void for_each_fixed_width_font(Function<void(const String&)>);
private:
GFontDatabase();
~GFontDatabase();
struct Metadata {

View file

@ -197,14 +197,14 @@ void GScrollBar::paint_event(GPaintEvent& event)
painter.fill_rect(rect(), Color::from_rgb(0xd6d2ce));
StylePainter::the().paint_button(painter, up_button_rect(), ButtonStyle::Normal, false);
StylePainter::paint_button(painter, up_button_rect(), ButtonStyle::Normal, false);
painter.draw_bitmap(up_button_rect().location().translated(3, 3), orientation() == Orientation::Vertical ? *s_up_arrow_bitmap : *s_left_arrow_bitmap, has_scrubber() ? Color::Black : Color::MidGray);
StylePainter::the().paint_button(painter, down_button_rect(), ButtonStyle::Normal, false);
StylePainter::paint_button(painter, down_button_rect(), ButtonStyle::Normal, false);
painter.draw_bitmap(down_button_rect().location().translated(3, 3), orientation() == Orientation::Vertical ? *s_down_arrow_bitmap : *s_right_arrow_bitmap, has_scrubber() ? Color::Black : Color::MidGray);
if (has_scrubber())
StylePainter::the().paint_button(painter, scrubber_rect(), ButtonStyle::Normal, false);
StylePainter::paint_button(painter, scrubber_rect(), ButtonStyle::Normal, false);
}
void GScrollBar::mousedown_event(GMouseEvent& event)

View file

@ -37,5 +37,5 @@ void GStatusBar::paint_event(GPaintEvent& event)
{
GPainter painter(*this);
painter.add_clip_rect(event.rect());
StylePainter::the().paint_surface(painter, rect(), !spans_entire_window_horizontally());
StylePainter::paint_surface(painter, rect(), !spans_entire_window_horizontally());
}

View file

@ -785,7 +785,7 @@ void GTextEditor::cut()
{
auto selected_text = this->selected_text();
printf("Cut: \"%s\"\n", selected_text.characters());
GClipboard::the().set_data(selected_text);
GClipboard::set_data(selected_text);
delete_selection();
}
@ -793,12 +793,12 @@ void GTextEditor::copy()
{
auto selected_text = this->selected_text();
printf("Copy: \"%s\"\n", selected_text.characters());
GClipboard::the().set_data(selected_text);
GClipboard::set_data(selected_text);
}
void GTextEditor::paste()
{
auto paste_text = GClipboard::the().data();
auto paste_text = GClipboard::data();
printf("Paste: \"%s\"\n", paste_text.characters());
insert_at_cursor_or_replace_selection(paste_text);
}

View file

@ -79,5 +79,5 @@ void GToolBar::paint_event(GPaintEvent& event)
{
GPainter painter(*this);
painter.add_clip_rect(event.rect());
StylePainter::the().paint_surface(painter, rect(), !spans_entire_window_horizontally());
StylePainter::paint_surface(painter, rect(), !spans_entire_window_horizontally());
}