Loading...
Searching...
No Matches
GlibMemory.h
/*
* Copyright (C) 2017 Canonical Ltd.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License version 3 as
* published by the Free Software Foundation.
*
* This program 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 Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* Authored by: Pete Woods <pete.woods@canonical.com>
* Michi Henning <michi.henning@canonical.com>
* James Henstridge <james.henstridge@canonical.com>
*/
#ifndef UNITY_UTIL_GLIBMEMORY_H
#define UNITY_UTIL_GLIBMEMORY_H
#include <memory>
#include <glib.h>
#include <unity/util/ResourcePtr.h>
namespace unity
{
namespace util
{
namespace internal
{
template<typename T> struct GlibDeleter;
template<typename T> using GlibSPtr = std::shared_ptr<T>;
template<typename T> using GlibUPtr = std::unique_ptr<T, GlibDeleter<T>>;
template<typename SP>
class GlibAssigner
{
public:
typedef typename SP::element_type ElementType;
GlibAssigner(SP& smart_ptr) noexcept :
smart_ptr_(smart_ptr)
{
}
GlibAssigner(const GlibAssigner& other) = delete;
GlibAssigner(GlibAssigner&& other) noexcept:
ptr_(other.ptr_), smart_ptr_(other.smart_ptr_)
{
other.ptr_ = nullptr;
}
~GlibAssigner() noexcept
{
smart_ptr_ = SP(ptr_, GlibDeleter<ElementType>());
}
GlibAssigner& operator=(const GlibAssigner& other) = delete;
operator ElementType**() noexcept
{
return &ptr_;
}
private:
ElementType* ptr_ = nullptr;
SP& smart_ptr_;
};
struct GSourceUnsubscriber
{
void operator()(guint tag) noexcept
{
if (tag != 0)
{
g_source_remove(tag);
}
}
};
}
#define UNITY_UTIL_DEFINE_GLIB_SMART_POINTERS(TypeName, func) \
using TypeName##Deleter = internal::GlibDeleter<TypeName>; \
using TypeName##SPtr = internal::GlibSPtr<TypeName>; \
using TypeName##UPtr = internal::GlibUPtr<TypeName>; \
namespace internal \
{ \
template<> struct GlibDeleter<TypeName> \
{ \
void operator()(TypeName* ptr) noexcept \
{ \
if (ptr) \
{ \
::func(ptr); \
} \
} \
}; \
}
template<typename T>
inline internal::GlibSPtr<T> share_glib(T* ptr) noexcept
{
return internal::GlibSPtr<T>(ptr, internal::GlibDeleter<T>());
}
template<typename T>
inline internal::GlibUPtr<T> unique_glib(T* ptr) noexcept
{
return internal::GlibUPtr<T>(ptr, internal::GlibDeleter<T>());
}
template<typename SP>
inline internal::GlibAssigner<SP> assign_glib(SP& smart_ptr) noexcept
{
return internal::GlibAssigner<SP>(smart_ptr);
}
using GSourceManager = ResourcePtr<guint, internal::GSourceUnsubscriber>;
inline GSourceManager g_source_manager(guint id)
{
return GSourceManager(id, internal::GSourceUnsubscriber());
}
#pragma push_macro("G_DEFINE_AUTOPTR_CLEANUP_FUNC")
#undef G_DEFINE_AUTOPTR_CLEANUP_FUNC
#define G_DEFINE_AUTOPTR_CLEANUP_FUNC(TypeName, func) UNITY_UTIL_DEFINE_GLIB_SMART_POINTERS(TypeName, func)
#pragma push_macro("G_DEFINE_AUTO_CLEANUP_CLEAR_FUNC")
#undef G_DEFINE_AUTO_CLEANUP_CLEAR_FUNC
#define G_DEFINE_AUTO_CLEANUP_CLEAR_FUNC(TypeName, func)
#pragma push_macro("G_DEFINE_AUTO_CLEANUP_FREE_FUNC")
#undef G_DEFINE_AUTO_CLEANUP_FREE_FUNC
#define G_DEFINE_AUTO_CLEANUP_FREE_FUNC(TypeName, func, none)
#define __GLIB_H_INSIDE__
#include <glib/glib-autocleanups.h>
#undef __GLIB_H_INSIDE__
#pragma pop_macro("G_DEFINE_AUTOPTR_CLEANUP_FUNC")
#pragma pop_macro("G_DEFINE_AUTO_CLEANUP_CLEAR_FUNC")
#pragma pop_macro("G_DEFINE_AUTO_CLEANUP_FREE_FUNC")
#if GLIB_CHECK_VERSION(2, 57, 2)
typedef GRefStringSPtr gcharSPtr;
typedef GRefStringUPtr gcharUPtr;
#else
UNITY_UTIL_DEFINE_GLIB_SMART_POINTERS(gchar, g_free)
#endif
typedef gchar* gcharv;
UNITY_UTIL_DEFINE_GLIB_SMART_POINTERS(gcharv, g_strfreev)
} // namespace until
} // namespace unity
#endif
unity::util::ResourcePtr
Class to guarantee deallocation of arbitrary resources.
Definition ResourcePtr.h:121
unity
Top-level namespace for all things Unity-related.
Definition Version.h:38