[glibmm] Add Glib::c_str_or_null()



commit bf69428947398356c86c43e2be4f26dce03583dc
Author: Kjell Ahlstedt <kjell ahlstedt bredband net>
Date:   Mon Oct 19 18:38:08 2015 +0200

    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 files changed, 8 insertions(+), 0 deletions(-)
---
diff --git a/glib/glibmm/utility.h b/glib/glibmm/utility.h
index 3038dff..696a963 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);
 


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]