#ifndef GIOPTRS_H #define GIOPTRS_H // define smart pointers for GIO data types #include #include #include "gobjectptr.h" #include "cstrptr.h" namespace Fm { typedef GObjectPtr GFilePtr; typedef GObjectPtr GFileInfoPtr; typedef GObjectPtr GFileMonitorPtr; typedef GObjectPtr GCancellablePtr; typedef GObjectPtr GFileEnumeratorPtr; typedef GObjectPtr GInputStreamPtr; typedef GObjectPtr GFileInputStreamPtr; typedef GObjectPtr GOutputStreamPtr; typedef GObjectPtr GFileOutputStreamPtr; typedef GObjectPtr GIconPtr; typedef GObjectPtr GVolumeMonitorPtr; typedef GObjectPtr GVolumePtr; typedef GObjectPtr GMountPtr; typedef GObjectPtr GAppInfoPtr; class GErrorPtr { public: GErrorPtr(): err_{nullptr} { } GErrorPtr(GError*&& err) noexcept: err_{err} { err = nullptr; } GErrorPtr(const GErrorPtr& other) = delete; GErrorPtr(GErrorPtr&& other) noexcept: err_{other.err_} { other.err_ = nullptr; } GErrorPtr(std::uint32_t domain, unsigned int code, const char* msg): GErrorPtr{g_error_new_literal(domain, code, msg)} { } GErrorPtr(std::uint32_t domain, unsigned int code, const QString& msg): GErrorPtr{domain, code, msg.toUtf8().constData()} { } ~GErrorPtr() { reset(); } std::uint32_t domain() const { if(err_ != nullptr) { return err_->domain; } return 0; } unsigned int code() const { if(err_ != nullptr) { return err_->code; } return 0; } QString message() const { if(err_ != nullptr) { return err_->message; } return QString(); } void reset() { if(err_) { g_error_free(err_); } err_ = nullptr; } GError* get() const { return err_; } GErrorPtr& operator = (const GErrorPtr& other) = delete; GErrorPtr& operator = (GErrorPtr&& other) noexcept { reset(); err_ = other.err_; other.err_ = nullptr; return *this; } GErrorPtr& operator = (GError*&& err) { reset(); err_ = err; err_ = nullptr; return *this; } GError** operator&() { return &err_; } GError* operator->() { return err_; } bool operator == (const GErrorPtr& other) const { return err_ == other.err_; } bool operator == (GError* err) const { return err_ == err; } bool operator != (std::nullptr_t) const { return err_ != nullptr; } operator bool() const { return err_ != nullptr; } private: GError* err_; }; } //namespace Fm #endif // GIOPTRS_H