From 8d5a546f234401fd73fd4382eee29a6e60ab706c Mon Sep 17 00:00:00 2001 From: Rasmus Karlsson Date: Mon, 24 Jun 2024 01:40:22 +0200 Subject: [PATCH] mess around with ast matching and some refactoring --- scripts/get-app-uses.sh | 41 +++++++++++++++++++ src/Application.cpp | 36 +++++++++------- src/Application.hpp | 14 +++---- .../notifications/NotificationController.cpp | 6 +-- .../notifications/NotificationController.hpp | 10 ++--- src/providers/seventv/SeventvAPI.hpp | 6 +-- src/providers/seventv/SeventvBadges.hpp | 3 +- 7 files changed, 80 insertions(+), 36 deletions(-) create mode 100755 scripts/get-app-uses.sh diff --git a/scripts/get-app-uses.sh b/scripts/get-app-uses.sh new file mode 100755 index 000000000..2eb562759 --- /dev/null +++ b/scripts/get-app-uses.sh @@ -0,0 +1,41 @@ +#!/usr/bin/env bash + +# This script will print any usages of IApplication - can be used to figure out +# dependencies of an file. e.g. if used on SeventvEmotes.cpp, you'll see it uses SeventvAPI + +set -e + +command_file="/tmp/c2-get-app-uses-command" + +usage() { + >&2 echo "usage: $0 - prints the Application dependencies this file has" + exit +} + +file="$1" + +if [ -z "$file" ]; then + usage +fi + +if [ ! -f "$file" ]; then + >&2 echo "error: file '$file' does not exist" + exit 1 +fi + +echo 'set output print +match cxxMemberCallExpr(on(hasType(asString("IApplication *"))))' >"$command_file" + +next=0 +usages=() +while read -r l; do + # echo "l: '$l'" + if [ "$next" = "1" ]; then + echo "$l" + usages+=("$l") + fi + next=0 + if [[ $l = 'Binding for "root":' ]]; then + next=1 + fi +done <<< "$(clang-query "$file" -f="$command_file" 2>/dev/null)" diff --git a/src/Application.cpp b/src/Application.cpp index 529b2481b..32f41d5f4 100644 --- a/src/Application.cpp +++ b/src/Application.cpp @@ -121,29 +121,29 @@ Application::Application(Settings &_settings, const Paths &paths, , themes(&this->emplace()) , fonts(new Fonts(_settings)) , emotes(&this->emplace()) - , accounts(&this->emplace()) + , accounts(new AccountController) + , bttvEmotes(new BttvEmotes) + , ffzEmotes(new FfzEmotes) + , seventvAPI(new SeventvAPI) + , seventvEmotes(new SeventvEmotes) + , seventvBadges(new SeventvBadges) , hotkeys(&this->emplace()) , windows(&this->emplace(new WindowManager(paths))) , toasts(&this->emplace()) , imageUploader(&this->emplace()) - , seventvAPI(&this->emplace()) , crashHandler(&this->emplace(new CrashHandler(paths))) , commands(&this->emplace()) - , notifications(&this->emplace()) , highlights(&this->emplace()) , twitch(new TwitchIrcServer) , ffzBadges(&this->emplace()) - , seventvBadges(&this->emplace()) , userData(new UserDataController(paths)) , sound(makeSoundController(_settings)) , twitchLiveController(&this->emplace()) + , notifications(new NotificationController) , twitchPubSub(new PubSub(TWITCH_PUBSUB_URL)) , twitchBadges(new TwitchBadges) , chatterinoBadges(new ChatterinoBadges) - , bttvEmotes(new BttvEmotes) - , ffzEmotes(new FfzEmotes) - , seventvEmotes(new SeventvEmotes) , logging(new Logging(_settings)) , linkResolver(new LinkResolver) , streamerMode(new StreamerMode) @@ -168,13 +168,17 @@ void Application::fakeDtor() this->twitchPubSub.reset(); this->twitchBadges.reset(); this->chatterinoBadges.reset(); - this->bttvEmotes.reset(); - this->ffzEmotes.reset(); - this->seventvEmotes.reset(); // this->twitch.reset(); this->fonts.reset(); this->sound.reset(); this->userData.reset(); + + this->seventvBadges.reset(); + this->seventvEmotes.reset(); + this->seventvAPI.reset(); + this->ffzEmotes.reset(); + this->bttvEmotes.reset(); + this->accounts.reset(); } void Application::initialize(Settings &settings, const Paths &paths) @@ -357,8 +361,9 @@ IEmotes *Application::getEmotes() AccountController *Application::getAccounts() { assertInGuiThread(); + assert(this->accounts); - return this->accounts; + return this->accounts.get(); } HotkeyController *Application::getHotkeys() @@ -400,8 +405,9 @@ CommandController *Application::getCommands() NotificationController *Application::getNotifications() { assertInGuiThread(); + assert(this->notifications); - return this->notifications; + return this->notifications.get(); } HighlightController *Application::getHighlights() @@ -421,8 +427,9 @@ FfzBadges *Application::getFfzBadges() SeventvBadges *Application::getSeventvBadges() { // SeventvBadges handles its own locks, so we don't need to assert that this is called in the GUI thread + assert(this->seventvBadges); - return this->seventvBadges; + return this->seventvBadges.get(); } IUserDataController *Application::getUserData() @@ -472,8 +479,9 @@ ImageUploader *Application::getImageUploader() SeventvAPI *Application::getSeventvAPI() { assertInGuiThread(); + assert(this->seventvAPI); - return this->seventvAPI; + return this->seventvAPI.get(); } #ifdef CHATTERINO_HAVE_PLUGINS diff --git a/src/Application.hpp b/src/Application.hpp index af1c563a8..6ea7d9a7e 100644 --- a/src/Application.hpp +++ b/src/Application.hpp @@ -147,28 +147,28 @@ private: Theme *const themes{}; std::unique_ptr fonts{}; Emotes *const emotes{}; - AccountController *const accounts{}; + std::unique_ptr accounts; + std::unique_ptr bttvEmotes; + std::unique_ptr ffzEmotes; + std::unique_ptr seventvAPI; + std::unique_ptr seventvEmotes; + std::unique_ptr seventvBadges; HotkeyController *const hotkeys{}; WindowManager *const windows{}; Toasts *const toasts{}; ImageUploader *const imageUploader{}; - SeventvAPI *const seventvAPI{}; CrashHandler *const crashHandler{}; CommandController *const commands{}; - NotificationController *const notifications{}; HighlightController *const highlights{}; std::unique_ptr twitch; FfzBadges *const ffzBadges{}; - SeventvBadges *const seventvBadges{}; std::unique_ptr userData; std::unique_ptr sound; TwitchLiveController *const twitchLiveController{}; + std::unique_ptr notifications; std::unique_ptr twitchPubSub; std::unique_ptr twitchBadges; std::unique_ptr chatterinoBadges; - std::unique_ptr bttvEmotes; - std::unique_ptr ffzEmotes; - std::unique_ptr seventvEmotes; const std::unique_ptr logging; std::unique_ptr linkResolver; std::unique_ptr streamerMode; diff --git a/src/controllers/notifications/NotificationController.cpp b/src/controllers/notifications/NotificationController.cpp index cfab7242f..336a5e848 100644 --- a/src/controllers/notifications/NotificationController.cpp +++ b/src/controllers/notifications/NotificationController.cpp @@ -27,9 +27,9 @@ namespace chatterino { -void NotificationController::initialize(Settings &settings, const Paths &paths) +NotificationController::NotificationController() + : liveStatusTimer_(new QTimer(this)) { - this->initialized_ = true; for (const QString &channelName : this->twitchSetting_.getValue()) { this->channelMap[Platform::Twitch].append(channelName); @@ -43,8 +43,6 @@ void NotificationController::initialize(Settings &settings, const Paths &paths) this->channelMap[Platform::Twitch].raw()); }); - liveStatusTimer_ = new QTimer(); - this->fetchFakeChannels(); QObject::connect(this->liveStatusTimer_, &QTimer::timeout, [this] { diff --git a/src/controllers/notifications/NotificationController.hpp b/src/controllers/notifications/NotificationController.hpp index ad70e029f..01f69ccc9 100644 --- a/src/controllers/notifications/NotificationController.hpp +++ b/src/controllers/notifications/NotificationController.hpp @@ -2,7 +2,6 @@ #include "common/ChatterinoSetting.hpp" #include "common/SignalVector.hpp" -#include "common/Singleton.hpp" #include @@ -17,10 +16,13 @@ enum class Platform : uint8_t { Twitch, // 0 }; -class NotificationController final : public Singleton, private QObject +/** + * NotificationController is responsible for ? + */ +class NotificationController final : private QObject { public: - void initialize(Settings &settings, const Paths &paths) override; + NotificationController(); bool isChannelNotified(const QString &channelName, Platform p); void updateChannelNotification(const QString &channelName, Platform p); @@ -36,8 +38,6 @@ public: NotificationModel *createModel(QObject *parent, Platform p); private: - bool initialized_ = false; - void fetchFakeChannels(); void removeFakeChannel(const QString channelName); void checkStream(bool live, QString channelName); diff --git a/src/providers/seventv/SeventvAPI.hpp b/src/providers/seventv/SeventvAPI.hpp index c47f9dd33..5ee5d9b61 100644 --- a/src/providers/seventv/SeventvAPI.hpp +++ b/src/providers/seventv/SeventvAPI.hpp @@ -1,7 +1,5 @@ #pragma once -#include "common/Singleton.hpp" - #include class QString; @@ -11,7 +9,7 @@ namespace chatterino { class NetworkResult; -class SeventvAPI : public Singleton +class SeventvAPI final { using ErrorCallback = std::function; template @@ -19,7 +17,7 @@ class SeventvAPI : public Singleton public: SeventvAPI() = default; - ~SeventvAPI() override = default; + ~SeventvAPI() = default; SeventvAPI(const SeventvAPI &) = delete; SeventvAPI(SeventvAPI &&) = delete; diff --git a/src/providers/seventv/SeventvBadges.hpp b/src/providers/seventv/SeventvBadges.hpp index 725528916..c9d0efad5 100644 --- a/src/providers/seventv/SeventvBadges.hpp +++ b/src/providers/seventv/SeventvBadges.hpp @@ -1,7 +1,6 @@ #pragma once #include "common/Aliases.hpp" -#include "common/Singleton.hpp" #include "util/QStringHash.hpp" #include @@ -16,7 +15,7 @@ namespace chatterino { struct Emote; using EmotePtr = std::shared_ptr; -class SeventvBadges : public Singleton +class SeventvBadges { public: // Return the badge, if any, that is assigned to the user