Code review comment for lp:~pete-woods/unity-api/add-glib-assigner-class

Revision history for this message
Michi Henning (michihenning) wrote :

> Function overloading?

This works, but it means that each instance has two do_stuff() member functions, only one of which will ever be called, and the other one is dead code. (Admittedly, very little code.)

Instead, we can use SFINAE:

    ~GlibPtrAssigner()
    {
        release<SP>(smart_ptr_, ptr_);
    }

private:
    // Instantiate exactly one of the two release() methods below, depending
    // on whether we were instantiated with a shared_ptr or a unique_ptr.

    template<typename T>
    typename std::enable_if<
        std::is_same<T, std::shared_ptr<typename T::element_type>>::value,
        void
    >::type
    release(std::shared_ptr<ElementType>& smart, ElementType* p)
    {
        smart.reset(p, GlibDeleter<typename T::element_type>());
    }

    template<typename T>
    typename std::enable_if<
        std::is_same<T, std::unique_ptr<typename T::element_type, typename T::deleter_type>>::value,
        void
    >::type
    release(std::unique_ptr<ElementType, GlibDeleter<typename T::element_type>>& smart, ElementType* p)
    {
        smart.reset(p);
    }

Pete, I've pushed this change here:

https://code.launchpad.net/~unity-api-team/unity-api/add-glib-assigner-class

It's now possible to use the same glib_assign method, whether the argument is a shared_ptr or a unique_ptr:

EXPECT_FALSE(g_key_file_get_boolean(gkf.get(), "group", "key", glib_assign(error)));

« Back to merge proposal