add new sigc++2 directory
[ardour.git] / libs / glibmm2 / glib / glibmm / utility.h
1 // -*- c++ -*-
2 #ifndef _GLIBMM_UTILITY_H
3 #define _GLIBMM_UTILITY_H
4 /* $Id: utility.h 386 2007-03-23 17:31:16Z murrayc $ */
5
6 /* Copyright 2002 The gtkmm Development Team
7  *
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.
12  *
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.
17  *
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.
21  */
22
23 #include <glib/gmacros.h>
24 #include <glib/gmem.h>
25 #include <glibmm/ustring.h>
26
27
28 #ifndef DOXYGEN_SHOULD_SKIP_THIS
29
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.
34  */
35 #if ((__GNUC__ >= 3) || (__GNUC__ == 2 && __GNUC_MINOR__ >= 96)) && !defined(__STRICT_ANSI__)
36
37 #define GLIBMM_INITIALIZE_STRUCT(Var, Type) __builtin_memset(&(Var), 0, sizeof(Type))
38
39 #else
40
41 #define GLIBMM_INITIALIZE_STRUCT(Var, Type) \
42     G_STMT_START{ \
43         Type const temp_initializer__ = { 0, }; \
44         (Var) = temp_initializer__; \
45     }G_STMT_END
46
47 #endif /* ((__GNUC__ >= 3) || (__GNUC__ == 2 && __GNUC_MINOR__ >= 96)) && !defined(__STRICT_ANSI__) */
48
49
50 namespace Glib
51 {
52
53 // These are used by gtkmmproc-generated type conversions:
54
55 // Helper to deal with memory allocated
56 // by GLib functions in an exception-safe manner.
57 template <typename T>
58 class ScopedPtr
59 {
60 private:
61   T* ptr_;
62   ScopedPtr(const ScopedPtr<T>&);
63   ScopedPtr<T>& operator=(const ScopedPtr<T>&);
64
65 public:
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_; }
71 };
72
73 // Removes the const nature of a ptr
74 template <class T>
75 inline T* unconst(const T* t)
76   { return const_cast<T*>(t); }
77
78 // Convert const gchar* to ustring, while treating NULL as empty string.
79 inline
80 Glib::ustring convert_const_gchar_ptr_to_ustring(const char* str)
81 {
82   return (str) ? Glib::ustring(str) : Glib::ustring();
83 }
84
85 // Convert const gchar* to std::string, while treating NULL as empty string.
86 inline
87 std::string convert_const_gchar_ptr_to_stdstring(const char* str)
88 {
89   return (str) ? std::string(str) : std::string();
90 }
91
92 // Convert a non-const gchar* return value to ustring, freeing it too.
93 inline
94 Glib::ustring convert_return_gchar_ptr_to_ustring(char* str)
95 {
96   return (str) ? Glib::ustring(Glib::ScopedPtr<char>(str).get())
97                : Glib::ustring();
98 }
99
100 // Convert a non-const gchar* return value to std::string, freeing it too.
101 inline
102 std::string convert_return_gchar_ptr_to_stdstring(char* str)
103 {
104   return (str) ? std::string(Glib::ScopedPtr<char>(str).get())
105                : std::string();
106 }
107
108 // Append type_name to dest, while replacing special characters with '+'.
109 void append_canonical_typename(std::string& dest, const char* type_name);
110
111 } // namespace Glib
112
113 #endif /* DOXYGEN_SHOULD_SKIP_THIS */
114
115
116 #endif /* _GLIBMM_UTILITY_H */
117