Alex Deymo | 44666f9 | 2014-07-22 20:29:24 -0700 | [diff] [blame] | 1 | // 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 Deymo | 8d2bbe3 | 2015-06-23 16:28:59 -0700 | [diff] [blame] | 9 | #include <vector> |
Alex Deymo | 44666f9 | 2014-07-22 20:29:24 -0700 | [diff] [blame] | 10 | |
| 11 | #include <glib.h> |
| 12 | |
| 13 | namespace chromeos_update_engine { |
| 14 | namespace utils { |
| 15 | |
| 16 | // Returns the error message, if any, from a GError pointer. Frees the GError |
Alex Vakulenko | 88b591f | 2014-08-28 16:48:57 -0700 | [diff] [blame] | 17 | // object and resets error to null. |
Alex Deymo | 44666f9 | 2014-07-22 20:29:24 -0700 | [diff] [blame] | 18 | std::string GetAndFreeGError(GError** error); |
| 19 | |
Alex Deymo | 8d2bbe3 | 2015-06-23 16:28:59 -0700 | [diff] [blame] | 20 | // 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. |
| 23 | gchar** 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. |
| 28 | struct 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. |
| 37 | struct GLibStrvFreeDeleter { |
| 38 | inline void operator()(void* ptr) const { |
| 39 | g_strfreev(static_cast<gchar**>(ptr)); |
| 40 | } |
| 41 | }; |
| 42 | |
Alex Deymo | 44666f9 | 2014-07-22 20:29:24 -0700 | [diff] [blame] | 43 | } // namespace utils |
| 44 | } // namespace chromeos_update_engine |
| 45 | |
| 46 | #endif // UPDATE_ENGINE_GLIB_UTILS_H_ |