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