2 #ifndef _GLIBMM_UTILITY_H
3 #define _GLIBMM_UTILITY_H
6 /* Copyright 2002 The gtkmm Development Team
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Library General Public
10 * License as published by the Free Software Foundation; either
11 * version 2 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Library General Public License for more details.
18 * You should have received a copy of the GNU Library General Public
19 * License along with this library; if not, write to the Free
20 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 #include <glib/gmacros.h>
24 #include <glibmm/ustring.h>
27 #ifndef DOXYGEN_SHOULD_SKIP_THIS
29 extern "C" void g_free(void*);
32 /* Occasionally, a struct variable has to be initialized after its definition,
33 * i.e. when using structs as class member data. For convenience, the macro
34 * GLIBMM_INITIALIZE_STRUCT(Var, Type) is provided. It even avoids creating
35 * a temporary if the compiler is GCC.
37 #if ((__GNUC__ >= 3) || (__GNUC__ == 2 && __GNUC_MINOR__ >= 96)) && !defined(__STRICT_ANSI__)
39 #define GLIBMM_INITIALIZE_STRUCT(Var, Type) __builtin_memset(&(Var), 0, sizeof(Type))
43 #define GLIBMM_INITIALIZE_STRUCT(Var, Type) \
45 Type const temp_initializer__ = { 0, }; \
46 (Var) = temp_initializer__; \
49 #endif /* ((__GNUC__ >= 3) || (__GNUC__ == 2 && __GNUC_MINOR__ >= 96)) && !defined(__STRICT_ANSI__) */
55 // These are used by gtkmmproc-generated type conversions:
57 // Helper to deal with memory allocated
58 // by GLib functions in an exception-safe manner.
64 ScopedPtr(const ScopedPtr<T>&);
65 ScopedPtr<T>& operator=(const ScopedPtr<T>&);
68 ScopedPtr() : ptr_ (0) {}
69 explicit ScopedPtr(T* ptr) : ptr_ (ptr) {}
70 ~ScopedPtr() { g_free(ptr_); }
71 T* get() const { return ptr_; }
72 T** addr() { return &ptr_; }
75 // Removes the const nature of a ptr
77 inline T* unconst(const T* t)
78 { return const_cast<T*>(t); }
80 // Convert const gchar* to ustring, while treating NULL as empty string.
82 Glib::ustring convert_const_gchar_ptr_to_ustring(const char* str)
84 return (str) ? Glib::ustring(str) : Glib::ustring();
87 // Convert const gchar* to std::string, while treating NULL as empty string.
89 std::string convert_const_gchar_ptr_to_stdstring(const char* str)
91 return (str) ? std::string(str) : std::string();
94 // Convert a non-const gchar* return value to ustring, freeing it too.
96 Glib::ustring convert_return_gchar_ptr_to_ustring(char* str)
98 return (str) ? Glib::ustring(Glib::ScopedPtr<char>(str).get())
102 // Convert a non-const gchar* return value to std::string, freeing it too.
104 std::string convert_return_gchar_ptr_to_stdstring(char* str)
106 return (str) ? std::string(Glib::ScopedPtr<char>(str).get())
110 // Append type_name to dest, while replacing special characters with '+'.
111 void append_canonical_typename(std::string& dest, const char* type_name);
115 #endif /* DOXYGEN_SHOULD_SKIP_THIS */
118 #endif /* _GLIBMM_UTILITY_H */