blob: 97020cb3b8644085442eda264cd8f742740a5a09 [file] [log] [blame]
Alex Deymo44666f92014-07-22 20:29:24 -07001// Copyright 2014 The Chromium OS Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#ifndef UPDATE_ENGINE_GLIB_UTILS_H_
6#define UPDATE_ENGINE_GLIB_UTILS_H_
7
8#include <string>
Alex Deymo8d2bbe32015-06-23 16:28:59 -07009#include <vector>
Alex Deymo44666f92014-07-22 20:29:24 -070010
11#include <glib.h>
12
13namespace chromeos_update_engine {
14namespace utils {
15
16// Returns the error message, if any, from a GError pointer. Frees the GError
Alex Vakulenko88b591f2014-08-28 16:48:57 -070017// object and resets error to null.
Alex Deymo44666f92014-07-22 20:29:24 -070018std::string GetAndFreeGError(GError** error);
19
Alex Deymo8d2bbe32015-06-23 16:28:59 -070020// Converts a vector of strings to a NUL-terminated array of
21// strings. The resulting array should be freed with g_strfreev()
22// when are you done with it.
23gchar** StringVectorToGStrv(const std::vector<std::string>& vec_str);
24
25// A base::FreeDeleter that frees memory using g_free(). Useful when
26// integrating with GLib since it can be used with std::unique_ptr to
27// automatically free memory when going out of scope.
28struct GLibFreeDeleter {
29 inline void operator()(void* ptr) const {
30 g_free(static_cast<gpointer>(ptr));
31 }
32};
33
34// A base::FreeDeleter that frees memory using g_strfreev(). Useful
35// when integrating with GLib since it can be used with std::unique_ptr to
36// automatically free memory when going out of scope.
37struct GLibStrvFreeDeleter {
38 inline void operator()(void* ptr) const {
39 g_strfreev(static_cast<gchar**>(ptr));
40 }
41};
42
Alex Deymo44666f92014-07-22 20:29:24 -070043} // namespace utils
44} // namespace chromeos_update_engine
45
46#endif // UPDATE_ENGINE_GLIB_UTILS_H_