[glibmm] Add Glib::c_str_or_nullptr()
- From: Kjell Ahlstedt <kjellahl src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [glibmm] Add Glib::c_str_or_nullptr()
- Date: Thu, 21 Jan 2016 17:22:00 +0000 (UTC)
commit d00ee0394d76251113e00a86fa40fc7071e0c1e1
Author: Kjell Ahlstedt <kjell ahlstedt bredband net>
Date: Thu Jan 21 18:11:18 2016 +0100
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.
glib/glibmm/utility.h | 9 +++++++++
1 files changed, 9 insertions(+), 0 deletions(-)
---
diff --git a/glib/glibmm/utility.h b/glib/glibmm/utility.h
index 4b773d7..cae4452 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);
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]