Refactoring

This commit is contained in:
Jonas Kvinge
2024-10-22 18:12:33 +02:00
parent dfcf715291
commit 8da2b9cd94
623 changed files with 9071 additions and 5126 deletions

View File

@@ -0,0 +1,182 @@
/*
* SBSystemPreferences.h
*
* Generated with:
* sdef "/Applications/System Preferences.app" | sdp -fh --basename
*SBSystemPreferences -o SBSystemPreferences.h
*/
#import <AppKit/AppKit.h>
#import <ScriptingBridge/ScriptingBridge.h>
@class SBSystemPreferencesApplication, SBSystemPreferencesDocument,
SBSystemPreferencesWindow, SBSystemPreferencesPane,
SBSystemPreferencesAnchor;
#ifdef __GNUC__
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wmultichar"
#pragma GCC diagnostic ignored "-Wfour-char-constants"
#endif
enum SBSystemPreferencesSaveOptions {
SBSystemPreferencesSaveOptionsYes = 'yes ' /* Save the file. */,
SBSystemPreferencesSaveOptionsNo = 'no ' /* Do not save the file. */,
SBSystemPreferencesSaveOptionsAsk =
'ask ' /* Ask the user whether or not to save the file. */
};
typedef enum SBSystemPreferencesSaveOptions SBSystemPreferencesSaveOptions;
enum SBSystemPreferencesPrintingErrorHandling {
SBSystemPreferencesPrintingErrorHandlingStandard =
'lwst' /* Standard PostScript error handling */,
SBSystemPreferencesPrintingErrorHandlingDetailed =
'lwdt' /* print a detailed report of PostScript errors */
};
#ifdef __GNUC__
#pragma GCC diagnostic pop
#endif
typedef enum SBSystemPreferencesPrintingErrorHandling
SBSystemPreferencesPrintingErrorHandling;
/*
* Standard Suite
*/
// The application's top-level scripting object.
@interface SBSystemPreferencesApplication : SBApplication
- (SBElementArray*)documents;
- (SBElementArray*)windows;
@property(copy, readonly) NSString* name; // The name of the application.
@property(readonly) BOOL frontmost; // Is this the active application?
@property(copy, readonly)
NSString* version; // The version number of the application.
- (id)open:(id)x; // Open a document.
- (void)print:(id)x
withProperties:(NSDictionary*)withProperties
printDialog:(BOOL)printDialog; // Print a document.
- (void)quitSaving:
(SBSystemPreferencesSaveOptions)saving; // Quit the application.
- (BOOL)exists:(id)x; // Verify that an object exists.
@end
// A document.
@interface SBSystemPreferencesDocument : SBObject
@property(copy, readonly) NSString* name; // Its name.
@property(readonly) BOOL modified; // Has it been modified since the last save?
@property(copy, readonly) NSURL* file; // Its location on disk, if it has one.
- (void)closeSaving:(SBSystemPreferencesSaveOptions)saving
savingIn:(NSURL*)savingIn; // Close a document.
- (void)saveIn:(NSURL*)in_ as:(id)as; // Save a document.
- (void)printWithProperties:(NSDictionary*)withProperties
printDialog:(BOOL)printDialog; // Print a document.
- (void) delete; // Delete an object.
- (void)duplicateTo:(SBObject*)to
withProperties:(NSDictionary*)withProperties; // Copy an object.
- (void)moveTo:(SBObject*)to; // Move an object to a new location.
@end
// A window.
@interface SBSystemPreferencesWindow : SBObject
@property(copy, readonly) NSString* name; // The title of the window.
- (NSInteger)id; // The unique identifier of the window.
@property NSInteger index; // The index of the window, ordered front to back.
@property NSRect bounds; // The bounding rectangle of the window.
@property(readonly) BOOL closeable; // Does the window have a close button?
@property(readonly)
BOOL miniaturizable; // Does the window have a minimize button?
@property BOOL miniaturized; // Is the window minimized right now?
@property(readonly) BOOL resizable; // Can the window be resized?
@property BOOL visible; // Is the window visible right now?
@property(readonly) BOOL zoomable; // Does the window have a zoom button?
@property BOOL zoomed; // Is the window zoomed right now?
@property(copy, readonly) SBSystemPreferencesDocument*
document; // The document whose contents are displayed in the window.
- (void)closeSaving:(SBSystemPreferencesSaveOptions)saving
savingIn:(NSURL*)savingIn; // Close a document.
- (void)saveIn:(NSURL*)in_ as:(id)as; // Save a document.
- (void)printWithProperties:(NSDictionary*)withProperties
printDialog:(BOOL)printDialog; // Print a document.
- (void) delete; // Delete an object.
- (void)duplicateTo:(SBObject*)to
withProperties:(NSDictionary*)withProperties; // Copy an object.
- (void)moveTo:(SBObject*)to; // Move an object to a new location.
@end
/*
* System Preferences
*/
// System Preferences top level scripting object
@interface SBSystemPreferencesApplication (SystemPreferences)
- (SBElementArray*)panes;
@property(copy)
SBSystemPreferencesPane* currentPane; // the currently selected pane
@property(copy, readonly) SBSystemPreferencesWindow*
preferencesWindow; // the main preferences window
@property BOOL showAll; // Is SystemPrefs in show all view. (Setting to false
// will do nothing)
@end
// a preference pane
@interface SBSystemPreferencesPane : SBObject
- (SBElementArray*)anchors;
- (NSString*)id; // locale independent name of the preference pane; can refer
// to a pane using the expression: pane id "<name>"
@property(copy, readonly)
NSString* localizedName; // localized name of the preference pane
@property(copy, readonly) NSString* name; // name of the preference pane as it
// appears in the title bar; can
// refer to a pane using the
// expression: pane "<name>"
- (void)closeSaving:(SBSystemPreferencesSaveOptions)saving
savingIn:(NSURL*)savingIn; // Close a document.
- (void)saveIn:(NSURL*)in_ as:(id)as; // Save a document.
- (void)printWithProperties:(NSDictionary*)withProperties
printDialog:(BOOL)printDialog; // Print a document.
- (void) delete; // Delete an object.
- (void)duplicateTo:(SBObject*)to
withProperties:(NSDictionary*)withProperties; // Copy an object.
- (void)moveTo:(SBObject*)to; // Move an object to a new location.
- (id)reveal; // Reveals an anchor within a preference pane or preference pane
// itself
@end
// an anchor within a preference pane
@interface SBSystemPreferencesAnchor : SBObject
@property(copy, readonly)
NSString* name; // name of the anchor within a preference pane
- (void)closeSaving:(SBSystemPreferencesSaveOptions)saving
savingIn:(NSURL*)savingIn; // Close a document.
- (void)saveIn:(NSURL*)in_ as:(id)as; // Save a document.
- (void)printWithProperties:(NSDictionary*)withProperties
printDialog:(BOOL)printDialog; // Print a document.
- (void) delete; // Delete an object.
- (void)duplicateTo:(SBObject*)to
withProperties:(NSDictionary*)withProperties; // Copy an object.
- (void)moveTo:(SBObject*)to; // Move an object to a new location.
- (id)reveal; // Reveals an anchor within a preference pane or preference pane
// itself
@end

34
src/includes/arraysize.h Normal file
View File

@@ -0,0 +1,34 @@
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// From Chromium src/base/macros.h
#include <cstddef> // For size_t.
// The arraysize(arr) macro returns the # of elements in an array arr.
// The expression is a compile-time constant, and therefore can be
// used in defining new arrays, for example. If you use arraysize on
// a pointer by mistake, you will get a compile-time error.
//
// One caveat is that arraysize() doesn't accept any array of an
// anonymous type or a type defined inside a function. In these rare
// cases, you have to use the unsafe ARRAYSIZE_UNSAFE() macro below. This is
// due to a limitation in C++'s template system. The limitation might
// eventually be removed, but it hasn't happened yet.
// This template function declaration is used in defining arraysize.
// Note that the function doesn't need an implementation, as we only
// use its type.
template<typename T, size_t N>
char (&ArraySizeHelper(T (&array)[N]))[N];
// That gcc wants both of these prototypes seems mysterious. VC, for
// its part, can't decide which to use (another mystery). Matching of
// template overloads: the final frontier.
#ifndef _MSC_VER
template<typename T, size_t N>
char (&ArraySizeHelper(const T (&array)[N]))[N];
#endif
#define arraysize(array) (sizeof(ArraySizeHelper(array)))

View File

@@ -0,0 +1,19 @@
#ifndef DBUS_METATYPES_H
#define DBUS_METATYPES_H
#include <QMetaType>
#include <QByteArray>
#include <QByteArrayList>
#include <QMap>
#include <QString>
#include <QDBusObjectPath>
Q_DECLARE_METATYPE(QByteArrayList)
using InterfacesAndProperties = QMap<QString, QVariantMap>;
using ManagedObjectList = QMap<QDBusObjectPath, InterfacesAndProperties>;
Q_DECLARE_METATYPE(InterfacesAndProperties)
Q_DECLARE_METATYPE(ManagedObjectList)
#endif // DBUS_METATYPES_H

142
src/includes/iconmapper.h Normal file
View File

@@ -0,0 +1,142 @@
/*
* Strawberry Music Player
* Copyright 2019, Jonas Kvinge <jonas@jkvinge.net>
*
* Strawberry is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Strawberry is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Strawberry. If not, see <http://www.gnu.org/licenses/>.
*
*/
#ifndef ICONMAPPER_H
#define ICONMAPPER_H
#include "config.h"
#include <QtGlobal>
#include <QMap>
namespace IconMapper {
using namespace Qt::Literals::StringLiterals;
struct IconProperties {
explicit IconProperties() : min_size(0), max_size(0), allow_system_icon(true) {}
IconProperties(const QStringList &_names, const int _min_size = 16, const int _max_size = 512, const bool _allow_system_icon = true) : names(_names), min_size(_min_size), max_size(_max_size), allow_system_icon(_allow_system_icon) {}
QStringList names;
int min_size;
int max_size;
bool allow_system_icon;
};
static const QMap<QString, IconProperties> iconmapper_ = { // clazy:exclude=non-pod-global-static
{ u"albums"_s, { {u"media-optical"_s}} },
{ u"alsa"_s, { {}} },
{ u"application-exit"_s, { {}} },
{ u"applications-internet"_s, { {}} },
{ u"bluetooth"_s, { {u"preferences-system-bluetooth"_s, u"bluetooth-active"_s}} },
{ u"cdcase"_s, { {u"cdcover"_s, u"media-optical"_s}} },
{ u"media-optical"_s, { {u"cd"_s}} },
{ u"configure"_s, { {}} },
{ u"device-ipod-nano"_s, { {}} },
{ u"device-ipod"_s, { {}} },
{ u"device-phone"_s, { {}} },
{ u"device"_s, { {u"drive-removable-media-usb-pendrive"_s}} },
{ u"device-usb-drive"_s, { {}} },
{ u"device-usb-flash"_s, { {}} },
{ u"dialog-error"_s, { {}} },
{ u"dialog-information"_s, { {}} },
{ u"dialog-ok-apply"_s, { {}} },
{ u"dialog-password"_s, { {}} },
{ u"dialog-warning"_s, { {}} },
{ u"document-download"_s, { {u"download"_s}} },
{ u"document-new"_s, { {}} },
{ u"document-open-folder"_s, { {}} },
{ u"document-open"_s, { {}} },
{ u"document-save"_s, { {}} },
{ u"document-search"_s, { {}} },
{ u"document-open-remote"_s, { {}} },
{ u"download"_s, { {u"applications-internet"_s, u"network-workgroup"_s}} },
{ u"edit-clear-list"_s, { {u"edit-clear-list"_s, u"edit-clear-all"_s}} },
{ u"edit-clear-locationbar-ltr"_s, { {u"edit-clear-locationbar-ltr"_s}} },
{ u"edit-copy"_s, { {}} },
{ u"edit-delete"_s, { {}} },
{ u"edit-find"_s, { {}} },
{ u"edit-redo"_s, { {}} },
{ u"edit-rename"_s, { {}} },
{ u"edit-undo"_s, { {}} },
{ u"electrocompaniet"_s, { {}} },
{ u"equalizer"_s, { {u"view-media-equalizer"_s}} },
{ u"folder-new"_s, { {}} },
{ u"folder"_s, { {}} },
{ u"folder-sound"_s, { {u"folder-music"_s}} },
{ u"footsteps"_s, { {u"go-jump"_s}} },
{ u"go-down"_s, { {}} },
{ u"go-home"_s, { {}} },
{ u"go-jump"_s, { {}} },
{ u"go-next"_s, { {}} },
{ u"go-previous"_s, { {}} },
{ u"go-up"_s, { {}} },
{ u"gstreamer"_s, { {u"phonon-gstreamer"_s}} },
{ u"headset"_s, { {u"audio-headset"_s}} },
{ u"help-hint"_s, { {}} },
{ u"intel"_s, { {}} },
{ u"jack"_s, { {u"audio-input-line"_s}} },
{ u"keyboard"_s, { {u"input-keyboard"_s}} },
{ u"list-add"_s, { {}} },
{ u"list-remove"_s, { {}} },
{ u"love"_s, { {u"heart"_s, u"emblem-favorite"_s}} },
{ u"mcintosh-player"_s, { {}} },
{ u"mcintosh"_s, { {}} },
{ u"mcintosh-text"_s, { {}} },
{ u"media-eject"_s, { {}} },
{ u"media-playback-pause"_s, { {u"media-pause"_s}} },
{ u"media-playlist-repeat"_s, { {}} },
{ u"media-playlist-shuffle"_s, { {""_L1}} },
{ u"media-playback-start"_s, { {u"media-play"_s, u"media-playback-playing"_s}} },
{ u"media-seek-backward"_s, { {}} },
{ u"media-seek-forward"_s, { {}} },
{ u"media-skip-backward"_s, { {}} },
{ u"media-skip-forward"_s, { {}} },
{ u"media-playback-stop"_s, { {u"media-stop"_s}} },
{ u"moodbar"_s, { {u"preferences-desktop-icons"_s}} },
{ u"nvidia"_s, { {}} },
{ u"pulseaudio"_s, { {}} },
{ u"realtek"_s, { {}} },
{ u"scrobble-disabled"_s, { {}} },
{ u"scrobble"_s, { {u"love"_s}} },
{ u"search"_s, { {}} },
{ u"soundcard"_s, { {u"audiocard"_s, u"audio-card"_s}} },
{ u"speaker"_s, { {}} },
{ u"star-grey"_s, { {}} },
{ u"star"_s, { {}} },
{ u"strawberry"_s, { {}} },
{ u"subsonic"_s, { {}} },
{ u"tidal"_s, { {}} },
{ u"tools-wizard"_s, { {}} },
{ u"view-choose"_s, { {}} },
{ u"view-fullscreen"_s, { {}} },
{ u"view-media-lyrics"_s, { {}} },
{ u"view-media-playlist"_s, { {}} },
{ u"view-media-visualization"_s, { {u"preferences-desktop-theme"_s}} },
{ u"view-refresh"_s, { {}} },
{ u"library-music"_s, { {u"vinyl"_s}} },
{ u"vlc"_s, { {}} },
{ u"zoom-in"_s, { {}} },
{ u"zoom-out"_s, { {}, 0, 0 } }
};
} // namespace IconMapper
#endif // ICONMAPPER_H

75
src/includes/lazy.h Normal file
View File

@@ -0,0 +1,75 @@
/* This file is part of Strawberry.
Copyright 2016, John Maguire <john.maguire@gmail.com>
Strawberry is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Strawberry is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Strawberry. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef LAZY_H
#define LAZY_H
#include <functional>
#include <type_traits>
#include "core/logging.h"
#include "shared_ptr.h"
// Helper for lazy initialization of objects.
// Usage:
// Lazy<Foo> my_lazy_object([]() { return new Foo; });
template<typename T>
class Lazy {
public:
explicit Lazy(std::function<T*()> init) : init_(init) {}
// Convenience constructor that will lazily default construct the object.
Lazy() : init_([]() { return new T; }) {}
T* get() const {
CheckInitialized();
return ptr_.get();
}
SharedPtr<T> ptr() const {
CheckInitialized();
return ptr_;
}
typename std::add_lvalue_reference<T>::type operator*() const {
CheckInitialized();
return *ptr_;
}
T* operator->() const { return get(); }
// Returns true if the object is not yet initialized.
explicit operator bool() const { return ptr_; }
// Deletes the underlying object and will re-run the initialization function if the object is requested again.
void reset() { ptr_.reset(); }
private:
void CheckInitialized() const {
if (!ptr_) {
ptr_ = SharedPtr<T>(init_(), [](T*obj) { qLog(Debug) << obj << "deleted"; delete obj; });
qLog(Debug) << &*ptr_ << "created";
}
}
const std::function<T*()> init_;
mutable SharedPtr<T> ptr_;
};
#endif // LAZY_H

View File

@@ -0,0 +1,33 @@
#import <AppKit/NSApplication.h>
#include "config.h"
#include "globalshortcuts/globalshortcutsbackend-macos.h"
class PlatformInterface;
@class SPMediaKeyTap;
@interface AppDelegate : NSObject<NSApplicationDelegate, NSUserNotificationCenterDelegate> {
PlatformInterface *application_handler_;
NSMenu *dock_menu_;
GlobalShortcutsBackendMacOS *shortcut_handler_;
SPMediaKeyTap *key_tap_;
}
- (id) initWithHandler: (PlatformInterface*)handler;
// NSApplicationDelegate
- (BOOL) applicationShouldHandleReopen: (NSApplication*)app hasVisibleWindows:(BOOL)flag;
- (NSMenu*) applicationDockMenu: (NSApplication*)sender;
- (void)applicationDidFinishLaunching:(NSNotification*)aNotification;
- (NSApplicationTerminateReply) applicationShouldTerminate:(NSApplication*)sender;
// NSUserNotificationCenterDelegate
- (BOOL) userNotificationCenter: (id)center
shouldPresentNotification: (id)notification;
- (void) setDockMenu: (NSMenu*)menu;
- (GlobalShortcutsBackendMacOS*) shortcut_handler;
- (void) setShortcutHandler: (GlobalShortcutsBackendMacOS*)backend;
- (void) mediaKeyTap: (SPMediaKeyTap*)keyTap receivedMediaKeyEvent:(NSEvent*)event;
@end

View File

@@ -0,0 +1,65 @@
/*
* Strawberry Music Player
* Copyright 2024, Jonas Kvinge <jonas@jkvinge.net>
*
* Strawberry is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Strawberry is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Strawberry. If not, see <http://www.gnu.org/licenses/>.
*
*/
#ifndef MUTEX_PROTECTED_H
#define MUTEX_PROTECTED_H
#include <boost/noncopyable.hpp>
#include <QMutex>
#include <QMutexLocker>
template<typename T>
class mutex_protected : public boost::noncopyable {
public:
mutex_protected(const mutex_protected &value) : value_(value.value()) {}
mutex_protected(const T value) : value_(value) {}
~mutex_protected() {}
T value() const {
QMutexLocker l(&mutex_);
return value_;
}
T operator==(const mutex_protected &value) const {
QMutexLocker l(&mutex_);
return value == value_;
}
T operator==(const T value) const {
QMutexLocker l(&mutex_);
return value == value_;
}
void operator=(const mutex_protected &value) {
QMutexLocker l(&mutex_);
value_ = value.value();
}
void operator=(const T value) {
QMutexLocker l(&mutex_);
value_ = value;
}
private:
T value_;
mutable QMutex mutex_;
};
#endif // MUTEX_PROTECTED_H

View File

@@ -0,0 +1,29 @@
/*
* Strawberry Music Player
* This file was part of Clementine.
* Copyright 2011, David Sansome <me@davidsansome.com>
*
* Strawberry is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Strawberry is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Strawberry. If not, see <http://www.gnu.org/licenses/>.
*
*/
#ifndef QT_BLURIMAGE_H
#define QT_BLURIMAGE_H
#include "config.h"
// Exported by QtGui
void qt_blurImage(QPainter *p, QImage &blurImage, qreal radius, bool quality, bool alphaOnly, int transposed = 0);
#endif // QT_BLURIMAGE_H

View File

@@ -0,0 +1,65 @@
// Copyright (c) 2010 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef BASE_MAC_SCOPED_CFTYPEREF_H_
#define BASE_MAC_SCOPED_CFTYPEREF_H_
#include <CoreFoundation/CoreFoundation.h>
// ScopedCFTypeRef<> is patterned after scoped_ptr<>, but maintains ownership
// of a CoreFoundation object: any object that can be represented as a
// CFTypeRef. Style deviations here are solely for compatibility with
// scoped_ptr<>'s interface, with which everyone is already familiar.
//
// When ScopedCFTypeRef<> takes ownership of an object (in the constructor or
// in reset()), it takes over the caller's existing ownership claim. The
// caller must own the object it gives to ScopedCFTypeRef<>, and relinquishes
// an ownership claim to that object. ScopedCFTypeRef<> does not call
// CFRetain().
template<typename CFT>
class ScopedCFTypeRef {
public:
using element_type = CFT;
explicit ScopedCFTypeRef(CFT object = nullptr) : object_(object) {}
~ScopedCFTypeRef() {
if (object_) CFRelease(object_);
}
void reset(CFT object = nullptr) {
if (object_) CFRelease(object_);
object_ = object;
}
bool operator==(CFT that) const { return object_ == that; }
bool operator!=(CFT that) const { return object_ != that; }
operator CFT() const { return object_; }
CFT get() const { return object_; }
void swap(ScopedCFTypeRef &that) {
CFT temp = that.object_;
that.object_ = object_;
object_ = temp;
}
// ScopedCFTypeRef<>::release() is like scoped_ptr<>::release. It is NOT
// a wrapper for CFRelease(). To force a ScopedCFTypeRef<> object to call
// CFRelease(), use ScopedCFTypeRef<>::reset().
CFT release() __attribute__((warn_unused_result)) {
CFT temp = object_;
object_ = nullptr;
return temp;
}
private:
CFT object_;
Q_DISABLE_COPY(ScopedCFTypeRef);
};
#endif // BASE_MAC_SCOPED_CFTYPEREF_H_

View File

@@ -0,0 +1,143 @@
// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef BASE_MEMORY_SCOPED_NSOBJECT_H_
#define BASE_MEMORY_SCOPED_NSOBJECT_H_
#import <Foundation/Foundation.h>
// scoped_nsobject<> is patterned after scoped_ptr<>, but maintains ownership
// of an NSObject subclass object. Style deviations here are solely for
// compatibility with scoped_ptr<>'s interface, with which everyone is already
// familiar.
//
// When scoped_nsobject<> takes ownership of an object (in the constructor or
// in reset()), it takes over the caller's existing ownership claim. The
// caller must own the object it gives to scoped_nsobject<>, and relinquishes
// an ownership claim to that object. scoped_nsobject<> does not call
// -retain.
//
// scoped_nsobject<> is not to be used for NSAutoreleasePools. For
// NSAutoreleasePools use ScopedNSAutoreleasePool from
// scoped_nsautorelease_pool.h instead.
// We check for bad uses of scoped_nsobject and NSAutoreleasePool at compile
// time with a template specialization (see below).
template <typename NST>
class scoped_nsobject {
public:
explicit scoped_nsobject(NST* object = nil) : object_(object) {}
~scoped_nsobject() { [object_ release]; }
void reset(NST* object = nil) {
// We intentionally do not check that object != object_ as the caller must
// already have an ownership claim over whatever it gives to
// scoped_nsobject and ScopedCFTypeRef, whether it's in the constructor or
// in a call to reset(). In either case, it relinquishes that claim and
// the scoper assumes it.
[object_ release];
object_ = object;
}
bool operator==(NST* that) const { return object_ == that; }
bool operator!=(NST* that) const { return object_ != that; }
operator NST*() const { return object_; }
NST* get() const { return object_; }
void swap(scoped_nsobject &that) {
NST* temp = that.object_;
that.object_ = object_;
object_ = temp;
}
// scoped_nsobject<>::release() is like scoped_ptr<>::release. It is NOT
// a wrapper for [object_ release]. To force a scoped_nsobject<> object to
// call [object_ release], use scoped_nsobject<>::reset().
NST* release() __attribute__((warn_unused_result)) {
NST* temp = object_;
object_ = nil;
return temp;
}
private:
NST* object_;
Q_DISABLE_COPY(scoped_nsobject);
};
// Free functions
template <class C>
void swap(scoped_nsobject<C> &p1, scoped_nsobject<C> &p2) {
p1.swap(p2);
}
template <class C>
bool operator==(C* p1, const scoped_nsobject<C> &p2) {
return p1 == p2.get();
}
template <class C>
bool operator!=(C* p1, const scoped_nsobject<C> &p2) {
return p1 != p2.get();
}
// Specialization to make scoped_nsobject<id> work.
template <>
class scoped_nsobject<id> {
public:
explicit scoped_nsobject(id object = nil) : object_(object) {}
~scoped_nsobject() { [object_ release]; }
void reset(id object = nil) {
// We intentionally do not check that object != object_ as the caller must
// already have an ownership claim over whatever it gives to
// scoped_nsobject and ScopedCFTypeRef, whether it's in the constructor or
// in a call to reset(). In either case, it relinquishes that claim and
// the scoper assumes it.
[object_ release];
object_ = object;
}
bool operator==(id that) const { return object_ == that; }
bool operator!=(id that) const { return object_ != that; }
operator id() const { return object_; }
id get() const { return object_; }
void swap(scoped_nsobject &that) {
id temp = that.object_;
that.object_ = object_;
object_ = temp;
}
// scoped_nsobject<>::release() is like scoped_ptr<>::release. It is NOT
// a wrapper for [object_ release]. To force a scoped_nsobject<> object to
// call [object_ release], use scoped_nsobject<>::reset().
id release() __attribute__((warn_unused_result)) {
id temp = object_;
object_ = nil;
return temp;
}
private:
id object_;
Q_DISABLE_COPY(scoped_nsobject);
};
// Do not use scoped_nsobject for NSAutoreleasePools, use
// ScopedNSAutoreleasePool instead. This is a compile time check. See details
// at top of header.
template <>
class scoped_nsobject<NSAutoreleasePool> {
private:
explicit scoped_nsobject(NSAutoreleasePool* object = nil);
Q_DISABLE_COPY(scoped_nsobject);
};
#endif // BASE_MEMORY_SCOPED_NSOBJECT_H_

28
src/includes/scoped_ptr.h Normal file
View File

@@ -0,0 +1,28 @@
/*
* Strawberry Music Player
* Copyright 2023, Jonas Kvinge <jonas@jkvinge.net>
*
* Strawberry is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Strawberry is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Strawberry. If not, see <http://www.gnu.org/licenses/>.
*
*/
#ifndef SCOPED_PTR_H
#define SCOPED_PTR_H
#include <memory>
template <typename T, typename D = std::default_delete<T>>
using ScopedPtr = std::unique_ptr<T, D>;
#endif // SCOPED_PTR_H

View File

@@ -0,0 +1,68 @@
/*
* Strawberry Music Player
* This file was part of Clementine.
* Copyright 2010, David Sansome <me@davidsansome.com>
*
* Strawberry is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Strawberry is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Strawberry. If not, see <http://www.gnu.org/licenses/>.
*
*/
#ifndef SCOPEDGOBJECT_H
#define SCOPEDGOBJECT_H
#include "config.h"
#include <glib-object.h>
template<typename T>
class ScopedGObject {
public:
ScopedGObject() : object_(nullptr) {}
ScopedGObject(const ScopedGObject &other) : object_(nullptr) {
reset(other.object_);
}
~ScopedGObject() { reset(); }
ScopedGObject &operator=(const ScopedGObject &other) {
reset(other.object_);
return *this;
}
void reset(T *new_object = nullptr) {
if (new_object) g_object_ref(new_object);
reset_without_add(new_object);
}
void reset_without_add(T *new_object = nullptr) {
if (object_) g_object_unref(object_);
object_ = new_object;
}
T *get() const { return object_; }
operator T*() const { return get(); }
T *operator*() const { return get(); }
operator bool() const { return get(); }
bool operator==(const ScopedGObject &other) const {
return object_ == other.object_;
}
private:
T *object_;
};
#endif // SCOPEDGOBJECT_H

28
src/includes/shared_ptr.h Normal file
View File

@@ -0,0 +1,28 @@
/*
* Strawberry Music Player
* Copyright 2023, Jonas Kvinge <jonas@jkvinge.net>
*
* Strawberry is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Strawberry is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Strawberry. If not, see <http://www.gnu.org/licenses/>.
*
*/
#ifndef SHARED_PTR_H
#define SHARED_PTR_H
#include <memory>
template<typename T>
using SharedPtr = std::shared_ptr<T>;
#endif // SHARED_PTR_H