*
* @brief @c GLib traits.
*
+ * This header contains traits specific to GLib types.
+ *
* @author Ossama Othman @<ossama.othman@@intel.com@>
*
* @copyright @par
{
template<typename T> struct traits;
+ // GVariant traits specialization.
template<>
struct traits<GVariant>
{
- static void
- release(GVariant * p)
+ struct delete_functor
{
- if (p != nullptr)
- g_variant_unref(p);
- }
+ void
+ operator()(GVariant * p) const
+ {
+ if (p != nullptr)
+ g_variant_unref(p);
+ }
+ };
};
+ // GVariantIter traits specialization.
template<>
struct traits<GVariantIter>
{
- static void release(GVariantIter * p) { g_variant_iter_free(p); }
+ // Functor to be used in std::unique_ptr<> specialization.
+ struct delete_functor
+ {
+ void
+ operator()(GVariantIter * p) const
+ {
+ g_variant_iter_free(p);
+ }
+ };
};
+ // gchar traits specialization.
template<>
struct traits<gchar>
{
- static void release(gchar * p) { g_free(p); }
+ struct delete_functor
+ {
+ void
+ operator()(gchar * p) const
+ {
+ g_free(p);
+ }
+ };
};
+ // GError traits specialization.
template<>
struct traits<GError>
{
- static void release(GError * p)
+ struct delete_functor
{
- if (p != nullptr)
- g_error_free(p);
- }
+ void
+ operator()(GError * p) const
+ {
+ if (p != nullptr)
+ g_error_free(p);
+ }
+ };
};
+ // GMainLoop traits specialization.
template<>
struct traits<GMainLoop>
{
- static void release(GMainLoop * p)
+ struct delete_functor
{
- if (p != nullptr)
- g_main_loop_unref(p);
- }
+ void
+ operator()(GMainLoop * p) const
+ {
+ if (p != nullptr)
+ g_main_loop_unref(p);
+ }
+ };
};
}
}
+
#endif /* IVI_SETTINGS_GLIB_TRAITS_HPP */
*
* @brief @c JSON-GLib traits.
*
+ * This header contains traits specific to JSON-GLib types.
+ *
* @author Ossama Othman @<ossama.othman@@intel.com@>
*
* @copyright @par
{
namespace settings
{
+ /**
+ * @struct gobject_delete_functor
+ *
+ * @brief Release @c GObject subclasses.
+ *
+ * This functor is used to release @c GObject classes. It may be
+ * used anywhere a C++ functor with a function call operator of
+ * the form:
+ * @c void operator()(T * p) const
+ * is required, such as for the @c std::unique_ptr<> "@c deleter"
+ * template or constructor parameter.
+ */
+ template<typename T>
+ struct gobject_delete_functor
+ {
+ void
+ operator()(T * p) const
+ {
+ if (p != nullptr)
+ g_object_unref(G_OBJECT(p));
+ }
+ };
+
template<typename T> struct traits;
+ // JsonNode traits specialization.
template<>
struct traits<JsonNode>
{
- static void release(JsonNode * p) { json_node_free(p); }
+ struct delete_functor
+ {
+ void
+ operator()(JsonNode * p) const
+ {
+ json_node_free(p);
+ }
+ };
};
+ // JsonParser traits specialization.
template<>
struct traits<JsonParser>
{
- static void
- release(JsonParser * p)
- {
- if (p != nullptr)
- g_object_unref(p);
- }
+ typedef gobject_delete_functor<JsonParser> delete_functor;
};
+ // JsonReader traits specialization.
template<>
struct traits<JsonReader>
{
- static void
- release(JsonReader * p)
- {
- if (p != nullptr)
- g_object_unref(p);
- }
+ typedef gobject_delete_functor<JsonReader> delete_functor;
};
+ // JsonBuilder traits specialization.
template<>
struct traits<JsonBuilder>
{
- static void
- release(JsonBuilder * p)
- {
- if (p != nullptr)
- g_object_unref(p);
- }
+ typedef gobject_delete_functor<JsonBuilder> delete_functor;
};
+ // JsonGenerator traits specialization.
template<>
struct traits<JsonGenerator>
{
- static void
- release(JsonGenerator * p)
- {
- if (p != nullptr)
- g_object_unref(p);
- }
+ typedef gobject_delete_functor<JsonGenerator> delete_functor;
};
}
#ifndef IVI_SETTINGS_SMART_PTR_HPP
#define IVI_SETTINGS_SMART_PTR_HPP
-#include <cstddef>
-#include <utility>
-#include <functional>
+#include <memory>
namespace ivi
namespace settings
{
/**
- * @class smart_ptr
+ * @typedef smart_ptr
*
* @brief C++11 style smart pointer for use by settings plug-ins.
*
- * This class exposes a C++11-style smart pointer interface to
- * allow RAII based handling of resources provided by third party
- * libraries, such as Glib. Core functionality is implemented in
- * traits that this class template leverages.
+ * This type alias exposes a C++11-style smart pointer interface
+ * to allow RAII based handling of resources provided by third
+ * party libraries, such as Glib. Core functionality is
+ * implemented in traits that this type alias leverages.
*/
- template<typename T>
- class smart_ptr
- {
- public:
- typedef T * pointer;
- typedef T element_type;
- typedef traits<T> traits_type;
-
- constexpr smart_ptr() : p_(nullptr) {}
-
- constexpr smart_ptr(std::nullptr_t) : p_(nullptr) {}
-
- explicit smart_ptr(pointer p) : p_(p) {}
-
- smart_ptr(smart_ptr&& u) : p_(u.p_) { u.p_ = nullptr; }
-
- ~smart_ptr() { traits_type::release(p_); }
-
- smart_ptr &
- operator=(smart_ptr && r)
- {
- if (this != &r) {
- smart_ptr<T> tmp(r);
- swap(tmp);
- }
-
- return *this;
- }
-
- smart_ptr &
- operator=(std::nullptr_t)
- {
- reset(nullptr);
-
- return *this;
- }
-
- pointer
- release()
- {
- pointer const old = p_;
- p_ = nullptr;
-
- return old;
- }
-
- void
- reset(pointer p = pointer())
- {
- smart_ptr<T> tmp(p);
- swap(tmp);
- }
-
- void swap(smart_ptr& other) { std::swap(p_, other.p_); }
-
- pointer get() const { return p_; }
-
- explicit operator bool() const { return p_ != nullptr; }
-
- typename std::add_lvalue_reference<T>::type
- operator*() const
- {
- /**
- * @todo Throw exception if @c p_ @c == @c nullptr.
- */
- return *p_;
- }
-
- pointer operator->() const { return p_; }
-
- private:
-
- /**
- * @name Prevent copying
- */
- //@{
- smart_ptr(smart_ptr const &) = delete;
- smart_ptr & operator=(smart_ptr const &) = delete;
- //@}
-
- private:
-
- /// Pointer to the object being managed.
- T * p_;
-
- };
-
- template<typename T1, typename T2>
- bool
- operator==(smart_ptr<T1> const & x, smart_ptr<T2> const & y)
- {
- return x.get() == y.get();
- }
-
- template<typename T1, typename T2>
- bool
- operator!=(smart_ptr<T1> const & x, smart_ptr<T2> const & y)
- {
- return x.get() != y.get();
- }
-
- template<typename T1, typename T2>
- bool
- operator<(smart_ptr<T1> const & x, smart_ptr<T2> const & y)
- {
- typedef typename
- std::common_type<typename smart_ptr<T1>::pointer,
- typename smart_ptr<T2>::pointer>::type type;
-
- return std::less<type>()(x.get(), y.get());
- }
-
-
- template<typename T1, typename T2>
- bool operator<=(smart_ptr<T1> const & x, smart_ptr<T2> const & y)
- {
- return !(y < x);
- }
-
- template<typename T1, typename T2>
- bool
- operator>(smart_ptr<T1> const & x, smart_ptr<T2> const & y)
- {
- return (y < x);
- }
-
- template<typename T1, typename T2>
- bool
- operator>=(smart_ptr<T1> const & x, smart_ptr<T2> const & y)
- {
- return !(x < y);
- }
-
- template<typename T>
- bool
- operator==(smart_ptr<T> const & x, std::nullptr_t) noexcept
- {
- return !x;
- }
-
- template<typename T>
- bool
- operator==(std::nullptr_t, smart_ptr<T> const & x) noexcept
- {
- return !x;
- }
-
- template<typename T>
- bool
- operator!=(smart_ptr<T> const & x, std::nullptr_t) noexcept
- {
- return static_cast<bool>(x);
- }
-
- template<typename T>
- bool
- operator!=(std::nullptr_t, smart_ptr<T> const & x) noexcept
- {
- return static_cast<bool>(x);
- }
-
- template<typename T>
- bool
- operator<(smart_ptr<T> const & x, std::nullptr_t)
- {
- return std::less<typename smart_ptr<T>::pointer>()(x.get(), nullptr);
- }
-
- template<typename T>
- bool
- operator<(std::nullptr_t, smart_ptr<T> const & y)
- {
- return std::less<typename smart_ptr<T>::pointer>()(nullptr, y.get());
- }
-
- template<typename T>
- bool
- operator<=(smart_ptr<T> const & x, std::nullptr_t)
- {
- return nullptr < x;
- }
-
- template<typename T>
- bool
- operator<=(std::nullptr_t, smart_ptr<T> const & y)
- {
- return y < nullptr;
- }
-
- template<typename T>
- bool
- operator>(smart_ptr<T> const & x, std::nullptr_t)
- {
- return !(nullptr < x);
- }
-
- template<typename T>
- bool
- operator>(std::nullptr_t, smart_ptr<T> const & y)
- {
- return !(y < nullptr);
- }
-
- template<typename T>
- bool
- operator>=(smart_ptr<T> const & x, std::nullptr_t)
- {
- return !(x < nullptr);
- }
-
- template<typename T>
- bool
- operator>=(std::nullptr_t, smart_ptr<T> const & y)
- {
- return !(nullptr < y);
- }
-
+ template<typename T> using smart_ptr =
+ ::std::unique_ptr<T,
+ typename traits<T>::delete_functor>;
}
}
-namespace std
-{
- template<typename T>
- void
- swap(ivi::settings::smart_ptr<T> & lhs,
- ivi::settings::smart_ptr<T> & rhs)
- {
- lhs.swap(rhs);
- }
-
- template<typename T>
- struct hash<ivi::settings::smart_ptr<T>>
- {
- typedef ivi::settings::smart_ptr<T> argument_type;
- typedef std::size_t result_type;
-
- hash();
-
- result_type
- operator()(argument_type const & p) const
- {
- typedef typename argument_type::pointer key_type;
-
- return hash<key_type>(p.get());
- }
- };
-}
-
#endif /* IVI_SETTINGS_SMART_PTR_HPP */