2010-09-11 14:29:44 +02:00
|
|
|
/* This file is part of Clementine.
|
2014-11-02 19:36:21 +01:00
|
|
|
Copyright 2010, David Sansome <davidsansome@gmail.com>
|
2014-11-02 19:41:38 +01:00
|
|
|
Copyright 2014, Krzysztof Sobiecki <sobkas@gmail.com>
|
2014-11-02 19:36:21 +01:00
|
|
|
Copyright 2014, John Maguire <john.maguire@gmail.com>
|
2010-09-11 14:29:44 +02:00
|
|
|
|
|
|
|
Clementine 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.
|
|
|
|
|
|
|
|
Clementine 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 Clementine. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef SCOPEDGOBJECT_H
|
|
|
|
#define SCOPEDGOBJECT_H
|
|
|
|
|
|
|
|
#include <glib-object.h>
|
|
|
|
|
|
|
|
#include <QtDebug>
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
class ScopedGObject {
|
2014-02-07 16:34:20 +01:00
|
|
|
public:
|
2014-02-21 17:24:49 +01:00
|
|
|
ScopedGObject() : object_(nullptr) {}
|
2010-09-11 14:29:44 +02:00
|
|
|
|
2014-02-21 17:24:49 +01:00
|
|
|
explicit ScopedGObject(const ScopedGObject& other) : object_(nullptr) {
|
2010-09-11 14:29:44 +02:00
|
|
|
reset(other.object_);
|
|
|
|
}
|
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
~ScopedGObject() { reset(); }
|
2010-09-11 14:29:44 +02:00
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
ScopedGObject& operator=(const ScopedGObject& other) {
|
2010-09-11 14:29:44 +02:00
|
|
|
reset(other.object_);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2014-02-21 17:24:49 +01:00
|
|
|
void reset(T* new_object = nullptr) {
|
2014-02-07 16:34:20 +01:00
|
|
|
if (new_object) g_object_ref(new_object);
|
2010-09-11 14:29:44 +02:00
|
|
|
reset_without_add(new_object);
|
|
|
|
}
|
|
|
|
|
2014-02-21 17:24:49 +01:00
|
|
|
void reset_without_add(T* new_object = nullptr) {
|
2014-02-07 16:34:20 +01:00
|
|
|
if (object_) g_object_unref(object_);
|
2010-09-11 14:29:44 +02:00
|
|
|
|
|
|
|
object_ = new_object;
|
|
|
|
}
|
|
|
|
|
|
|
|
T* get() const { return object_; }
|
|
|
|
operator T*() const { return get(); }
|
2014-02-07 16:34:20 +01:00
|
|
|
T* operator*() const { return get(); }
|
2010-09-11 14:29:44 +02:00
|
|
|
operator bool() const { return get(); }
|
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
bool operator==(const ScopedGObject& other) const {
|
2010-09-11 14:29:44 +02:00
|
|
|
return object_ == other.object_;
|
|
|
|
}
|
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
private:
|
2010-09-11 14:29:44 +02:00
|
|
|
T* object_;
|
|
|
|
};
|
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
#endif // SCOPEDGOBJECT_H
|