summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKjell Ahlstedt <kjell.ahlstedt@bredband.net>2016-01-21 18:11:18 +0100
committerKjell Ahlstedt <kjell.ahlstedt@bredband.net>2016-01-21 18:11:18 +0100
commitd00ee0394d76251113e00a86fa40fc7071e0c1e1 (patch)
treeec3ad167b1e25efa0e1232ab51798b968d3dbc79
parent16fabd982d85dc84c68217816aef357ccd931c4e (diff)
downloadglibmm-d00ee0394d76251113e00a86fa40fc7071e0c1e1.tar.gz
Add Glib::c_str_or_nullptr()
* glib/glibmm/utility.h: Add Glib::c_str_or_nullptr(). Code such as "s.empty() ? nullptr : s.c_str()" can be replaced by Glib::c_str_or_nullptr(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.
-rw-r--r--glib/glibmm/utility.h9
1 files changed, 9 insertions, 0 deletions
diff --git a/glib/glibmm/utility.h b/glib/glibmm/utility.h
index 4b773d73..cae44526 100644
--- a/glib/glibmm/utility.h
+++ b/glib/glibmm/utility.h
@@ -130,6 +130,15 @@ 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.
+ * If the string is empty, a nullptr is returned.
+ */
+template <typename T>
+inline const char* c_str_or_nullptr(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);