2 #ifndef _GLIBMM_UTILITY_H
3 #define _GLIBMM_UTILITY_H
4 /* $Id: utility.h 386 2007-03-23 17:31:16Z murrayc $ */
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 <glib/gmem.h>
25 #include <glibmm/ustring.h>
28 #ifndef DOXYGEN_SHOULD_SKIP_THIS
30 /* Occasionally, a struct variable has to be initialized after its definition,
31 * i.e. when using structs as class member data. For convenience, the macro
32 * GLIBMM_INITIALIZE_STRUCT(Var, Type) is provided. It even avoids creating
33 * a temporary if the compiler is GCC.
35 #if ((__GNUC__ >= 3) || (__GNUC__ == 2 && __GNUC_MINOR__ >= 96)) && !defined(__STRICT_ANSI__)
37 #define GLIBMM_INITIALIZE_STRUCT(Var, Type) __builtin_memset(&(Var), 0, sizeof(Type))
41 #define GLIBMM_INITIALIZE_STRUCT(Var, Type) \
43 Type const temp_initializer__ = { 0, }; \
44 (Var) = temp_initializer__; \
47 #endif /* ((__GNUC__ >= 3) || (__GNUC__ == 2 && __GNUC_MINOR__ >= 96)) && !defined(__STRICT_ANSI__) */
53 // These are used by gtkmmproc-generated type conversions:
55 // Helper to deal with memory allocated
56 // by GLib functions in an exception-safe manner.
62 ScopedPtr(const ScopedPtr<T>&);
63 ScopedPtr<T>& operator=(const ScopedPtr<T>&);
66 ScopedPtr() : ptr_ (0) {}
67 explicit ScopedPtr(T* ptr) : ptr_ (ptr) {}
68 ~ScopedPtr() { g_free(ptr_); }
69 T* get() const { return ptr_; }
70 T** addr() { return &ptr_; }
73 // Removes the const nature of a ptr
75 inline T* unconst(const T* t)
76 { return const_cast<T*>(t); }
78 // Convert const gchar* to ustring, while treating NULL as empty string.
80 Glib::ustring convert_const_gchar_ptr_to_ustring(const char* str)
82 return (str) ? Glib::ustring(str) : Glib::ustring();
85 // Convert const gchar* to std::string, while treating NULL as empty string.
87 std::string convert_const_gchar_ptr_to_stdstring(const char* str)
89 return (str) ? std::string(str) : std::string();
92 // Convert a non-const gchar* return value to ustring, freeing it too.
94 Glib::ustring convert_return_gchar_ptr_to_ustring(char* str)
96 return (str) ? Glib::ustring(Glib::ScopedPtr<char>(str).get())
100 // Convert a non-const gchar* return value to std::string, freeing it too.
102 std::string convert_return_gchar_ptr_to_stdstring(char* str)
104 return (str) ? std::string(Glib::ScopedPtr<char>(str).get())
108 // Append type_name to dest, while replacing special characters with '+'.
109 void append_canonical_typename(std::string& dest, const char* type_name);
113 #endif /* DOXYGEN_SHOULD_SKIP_THIS */
116 #endif /* _GLIBMM_UTILITY_H */