summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKjell Ahlstedt <kjell.ahlstedt@bredband.net>2015-10-19 18:38:08 +0200
committerKjell Ahlstedt <kjell.ahlstedt@bredband.net>2015-10-19 18:38:08 +0200
commitbf69428947398356c86c43e2be4f26dce03583dc (patch)
treebf66329a409355eccc6ef39819a3c16e3f873801
parentef77dd46cddfddbe7476b30ff53ac1ccab2aa27b (diff)
downloadglibmm-bf69428947398356c86c43e2be4f26dce03583dc.tar.gz
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.
-rw-r--r--glib/glibmm/utility.h8
1 files changed, 8 insertions, 0 deletions
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 <typename T>
+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);