Commit b726b8b0 authored by carlosgc@webkit.org's avatar carlosgc@webkit.org

[GTK] Add GUniquePtr

https://bugs.webkit.org/show_bug.cgi?id=127170

Reviewed by Gustavo Noronha Silva.

Source/WTF:

The idea is to replace GOwnPtr with a template alias of
std:unique_ptr. Not everything can be replaced, though, because
GOwnPtr::outPtr() doesn't seem to be possible to implement with
unique_ptr. This smart pointer is more flexible than GOwnPtr and
allows to transfer the ownership of the pointer using
std::move().

* GNUmakefile.list.am: Add new file to compilation.
* wtf/gobject/GUniquePtr.h: Added.
(WTF::GPtrDeleter::operator()):

Tools:

Add unit tests for GUniquePtr.

* TestWebKitAPI/GNUmakefile.am: Add new file to compilation.
* TestWebKitAPI/Tests/WTF/gobject/GUniquePtr.cpp: Added.
(log):
(takeLogStr):
(TestWebKitAPI::TEST):

git-svn-id: http://svn.webkit.org/repository/webkit/trunk@162516 268f45cc-cd09-0410-ab3c-d52691b4dbfc
parent df985172
2014-01-22 Carlos Garcia Campos <cgarcia@igalia.com>
[GTK] Add GUniquePtr
https://bugs.webkit.org/show_bug.cgi?id=127170
Reviewed by Gustavo Noronha Silva.
The idea is to replace GOwnPtr with a template alias of
std:unique_ptr. Not everything can be replaced, though, because
GOwnPtr::outPtr() doesn't seem to be possible to implement with
unique_ptr. This smart pointer is more flexible than GOwnPtr and
allows to transfer the ownership of the pointer using
std::move().
* GNUmakefile.list.am: Add new file to compilation.
* wtf/gobject/GUniquePtr.h: Added.
(WTF::GPtrDeleter::operator()):
2014-01-20 Anders Carlsson <andersca@apple.com>
Remove an unused member variable
......
......@@ -214,6 +214,7 @@ wtf_sources += \
Source/WTF/wtf/gobject/GRefPtr.cpp \
Source/WTF/wtf/gobject/GRefPtr.h \
Source/WTF/wtf/gobject/GTypedefs.h \
Source/WTF/wtf/gobject/GUniquePtr.h \
Source/WTF/wtf/gobject/GlibUtilities.cpp \
Source/WTF/wtf/gobject/GlibUtilities.h \
Source/WTF/wtf/gtk/MainThreadGtk.cpp \
......
/*
* Copyright (C) 2014 Igalia S.L.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library 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
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public License
* along with this library; see the file COPYING.LIB. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*
*/
#ifndef GUniquePtr_h
#define GUniquePtr_h
#if USE(GLIB)
#include <gio/gio.h>
namespace WTF {
template<typename T>
struct GPtrDeleter {
void operator()(T* ptr) const { g_free(ptr); }
};
template<typename T>
using GUniquePtr = std::unique_ptr<T, GPtrDeleter<T>>;
#define FOR_EACH_GLIB_DELETER(macro) \
macro(GError, g_error_free) \
macro(GList, g_list_free) \
macro(GSList, g_slist_free) \
macro(GPatternSpec, g_pattern_spec_free) \
macro(GDir, g_dir_close) \
macro(GTimer, g_timer_destroy) \
macro(GKeyFile, g_key_file_free)
#define WTF_DEFINE_GPTR_DELETER(typeName, deleterFunc) \
template<> struct GPtrDeleter<typeName> \
{ \
void operator() (typeName* ptr) const \
{ \
if (ptr) \
deleterFunc(ptr); \
} \
};
FOR_EACH_GLIB_DELETER(WTF_DEFINE_GPTR_DELETER)
#undef FOR_EACH_GLIB_DELETER
} // namespace WTF
using WTF::GUniquePtr;
#endif // USE(GLIB)
#endif // GUniquePtr_h
2014-01-22 Carlos Garcia Campos <cgarcia@igalia.com>
[GTK] Add GUniquePtr
https://bugs.webkit.org/show_bug.cgi?id=127170
Reviewed by Gustavo Noronha Silva.
Add unit tests for GUniquePtr.
* TestWebKitAPI/GNUmakefile.am: Add new file to compilation.
* TestWebKitAPI/Tests/WTF/gobject/GUniquePtr.cpp: Added.
(log):
(takeLogStr):
(TestWebKitAPI::TEST):
2014-01-22 Jinwoo Song <jinwoo7.song@samsung.com>
Remove Nix files from Tools
......
......@@ -228,6 +228,7 @@ Programs_TestWebKitAPI_WTF_TestWTF_SOURCES = \
Tools/TestWebKitAPI/Tests/WTF/StringOperators.cpp \
Tools/TestWebKitAPI/Tests/WTF/TemporaryChange.cpp \
Tools/TestWebKitAPI/Tests/WTF/Vector.cpp \
Tools/TestWebKitAPI/Tests/WTF/gobject/GUniquePtr.cpp \
Tools/TestWebKitAPI/WTFStringUtilities.h
Programs_TestWebKitAPI_JavaScriptCore_TestJavaScriptCore_CPPFLAGS = \
......
/*
* Copyright (C) 2014 Igalia S.L.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
* THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "config.h"
#include <gio/gio.h>
inline std::ostringstream& log()
{
static std::ostringstream log;
return log;
}
inline std::string takeLogStr()
{
std::string string = log().str();
log().str("");
return string;
}
static void (* _g_free)(void*) = g_free;
#define g_free(x) \
log() << "g_free(" << ptr << ");"; \
_g_free(x);
static void (* _g_error_free)(GError*) = g_error_free;
#define g_error_free(x) \
log() << "g_error_free(" << ptr << ");"; \
_g_error_free(x);
static void (* _g_list_free)(GList*) = g_list_free;
#define g_list_free(x) \
log() << "g_list_free(" << ptr << ");"; \
_g_list_free(x);
static void (* _g_slist_free)(GSList*) = g_slist_free;
#define g_slist_free(x) \
log() << "g_slist_free(" << ptr << ");"; \
_g_slist_free(x);
static void (* _g_pattern_spec_free)(GPatternSpec*) = g_pattern_spec_free;
#define g_pattern_spec_free(x) \
log() << "g_pattern_spec_free(" << ptr << ");"; \
_g_pattern_spec_free(x);
static void (* _g_dir_close)(GDir*) = g_dir_close;
#define g_dir_close(x) \
log() << "g_dir_close(" << ptr << ");"; \
_g_dir_close(x);
static void (* _g_timer_destroy)(GTimer*) = g_timer_destroy;
#define g_timer_destroy(x) \
log() << "g_timer_destroy(" << ptr << ");"; \
_g_timer_destroy(x);
static void (* _g_key_file_free)(GKeyFile*) = g_key_file_free;
#define g_key_file_free(x) \
log() << "g_key_file_free(" << ptr << ");"; \
_g_key_file_free(x);
#include <wtf/gobject/GUniquePtr.h>
namespace TestWebKitAPI {
TEST(WTF_GUniquePtr, Basic)
{
std::ostringstream actual;
{
GUniquePtr<char> a(g_strdup("a"));
actual << "g_free(" << a.get() << ");";
}
ASSERT_STREQ(actual.str().c_str(), takeLogStr().c_str());
actual.str("");
{
GUniquePtr<GError> a(g_error_new_literal(G_IO_ERROR, G_IO_ERROR_NOT_FOUND, "a"));
actual << "g_error_free(" << a.get() << ");";
}
ASSERT_STREQ(actual.str().c_str(), takeLogStr().c_str());
actual.str("");
{
GUniquePtr<GList> a(g_list_prepend(nullptr, g_strdup("a")));
actual << "g_list_free(" << a.get() << ");";
}
ASSERT_STREQ(actual.str().c_str(), takeLogStr().c_str());
actual.str("");
{
GUniquePtr<GSList> a(g_slist_prepend(nullptr, g_strdup("a")));
actual << "g_slist_free(" << a.get() << ");";
}
ASSERT_STREQ(actual.str().c_str(), takeLogStr().c_str());
actual.str("");
{
GUniquePtr<GPatternSpec> a(g_pattern_spec_new("a"));
actual << "g_pattern_spec_free(" << a.get() << ");";
}
ASSERT_STREQ(actual.str().c_str(), takeLogStr().c_str());
actual.str("");
{
GUniquePtr<GDir> a(g_dir_open("/tmp", 0, nullptr));
actual << "g_dir_close(" << a.get() << ");";
}
ASSERT_STREQ(actual.str().c_str(), takeLogStr().c_str());
actual.str("");
{
GUniquePtr<GTimer> a(g_timer_new());
actual << "g_timer_destroy(" << a.get() << ");";
}
ASSERT_STREQ(actual.str().c_str(), takeLogStr().c_str());
actual.str("");
{
GUniquePtr<GKeyFile> a(g_key_file_new());
actual << "g_key_file_free(" << a.get() << ");";
}
ASSERT_STREQ(actual.str().c_str(), takeLogStr().c_str());
actual.str("");
}
} // namespace TestWebKitAPI
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment