From bf69428947398356c86c43e2be4f26dce03583dc Mon Sep 17 00:00:00 2001 From: Kjell Ahlstedt Date: Mon, 19 Oct 2015 18:38:08 +0200 Subject: Add Glib::c_str_or_null() * glib/glibmm/utility.h: Add Glib::c_str_or_null(). Code such as "s.empty() ? nullptr : s.c_str()" can be replaced by Glib::c_str_or_null(s) when C functions are called, where s is a std::string or a Glib::ustring. A very small part of a fix of bug #755245. --- glib/glibmm/utility.h | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/glib/glibmm/utility.h b/glib/glibmm/utility.h index 3038dffc..696a963f 100644 --- a/glib/glibmm/utility.h +++ b/glib/glibmm/utility.h @@ -109,6 +109,14 @@ std::string convert_return_gchar_ptr_to_stdstring(char* str) : std::string(); } +// Get a pointer to the C style string in a std::string or Glib::ustring, +// returning nullptr, if the string is empty. +template +inline const char* c_str_or_null(const T& str) +{ + return str.empty() ? nullptr : str.c_str(); +} + // Append type_name to dest, while replacing special characters with '+'. void append_canonical_typename(std::string& dest, const char* type_name); -- cgit v1.2.1