diff options
author | Kjell Ahlstedt <kjell.ahlstedt@bredband.net> | 2015-10-19 18:38:08 +0200 |
---|---|---|
committer | Kjell Ahlstedt <kjell.ahlstedt@bredband.net> | 2015-10-19 18:38:08 +0200 |
commit | bf69428947398356c86c43e2be4f26dce03583dc (patch) | |
tree | bf66329a409355eccc6ef39819a3c16e3f873801 /glib/glibmm/utility.h | |
parent | ef77dd46cddfddbe7476b30ff53ac1ccab2aa27b (diff) | |
download | glibmm-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.
Diffstat (limited to 'glib/glibmm/utility.h')
-rw-r--r-- | glib/glibmm/utility.h | 8 |
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); |