strawberry-audio-player-win.../src/core/scopedgobject.h

71 lines
1.7 KiB
C
Raw Normal View History

2018-02-27 18:06:05 +01:00
/*
* 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/>.
2018-08-09 18:39:44 +02:00
*
2018-02-27 18:06:05 +01:00
*/
#ifndef SCOPEDGOBJECT_H
#define SCOPEDGOBJECT_H
#include "config.h"
#include <glib-object.h>
#include <QtDebug>
template <typename T>
class ScopedGObject {
public:
ScopedGObject() : object_(nullptr) {}
2021-06-12 20:53:23 +02:00
ScopedGObject(const ScopedGObject &other) : object_(nullptr) {
2018-02-27 18:06:05 +01:00
reset(other.object_);
}
~ScopedGObject() { reset(); }
2021-06-12 20:53:23 +02:00
ScopedGObject &operator=(const ScopedGObject &other) {
2018-02-27 18:06:05 +01:00
reset(other.object_);
return *this;
}
2021-06-12 20:53:23 +02:00
void reset(T *new_object = nullptr) {
2018-02-27 18:06:05 +01:00
if (new_object) g_object_ref(new_object);
reset_without_add(new_object);
}
2021-06-12 20:53:23 +02:00
void reset_without_add(T *new_object = nullptr) {
2018-02-27 18:06:05 +01:00
if (object_) g_object_unref(object_);
object_ = new_object;
}
2021-06-12 20:53:23 +02:00
T *get() const { return object_; }
2018-02-27 18:06:05 +01:00
operator T*() const { return get(); }
2021-06-12 20:53:23 +02:00
T *operator*() const { return get(); }
2018-02-27 18:06:05 +01:00
operator bool() const { return get(); }
2021-06-12 20:53:23 +02:00
bool operator==(const ScopedGObject &other) const {
2018-02-27 18:06:05 +01:00
return object_ == other.object_;
}
private:
2021-06-12 20:53:23 +02:00
T *object_;
2018-02-27 18:06:05 +01:00
};
#endif // SCOPEDGOBJECT_H