rollback to 3428, before the mysterious removal of libs/* at 3431/3432
[ardour.git] / libs / glibmm2 / glib / glibmm / property.h
1 // -*- c++ -*-
2 #ifndef _GLIBMM_PROPERTY_H
3 #define _GLIBMM_PROPERTY_H
4 /* $Id: property.h 291 2006-05-12 08:08:45Z 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 <glibmm/propertyproxy.h>
24
25 #ifdef GLIBMM_PROPERTIES_ENABLED
26
27 #include <glibmm/value.h>
28
29 namespace Glib
30 {
31
32 #ifndef DOXYGEN_SHOULD_SKIP_THIS
33
34 #ifdef GLIBMM_CXX_CAN_USE_NAMESPACES_INSIDE_EXTERNC
35 //For the AIX xlC compiler, I can not find a way to do this without putting the functions in the global namespace. murrayc
36 extern "C"
37 {
38 #endif //GLIBMM_CXX_CAN_USE_NAMESPACES_INSIDE_EXTERNC
39
40 void custom_get_property_callback(GObject* object, unsigned int property_id,
41                                   GValue* value, GParamSpec* param_spec);
42
43 void custom_set_property_callback(GObject* object, unsigned int property_id,
44                                   const GValue* value, GParamSpec* param_spec);
45
46 #ifdef GLIBMM_CXX_CAN_USE_NAMESPACES_INSIDE_EXTERNC
47 } //extern "C"
48 #endif //GLIBMM_CXX_CAN_USE_NAMESPACES_INSIDE_EXTERNC
49
50 #endif /* DOXYGEN_SHOULD_SKIP_THIS */
51
52
53 class PropertyBase
54 {
55 public:
56   Glib::ustring get_name() const;
57   void notify();
58
59 protected:
60   Glib::Object*   object_;
61   Glib::ValueBase value_;
62   GParamSpec*     param_spec_;
63
64   PropertyBase(Glib::Object& object, GType value_type);
65   ~PropertyBase();
66
67   bool lookup_property(const Glib::ustring& name);
68   void install_property(GParamSpec* param_spec);
69
70   const char* get_name_internal() const;
71
72 private:
73   // noncopyable
74   PropertyBase(const PropertyBase&);
75   PropertyBase& operator=(const PropertyBase&);
76
77 #ifndef DOXYGEN_SHOULD_SKIP_THIS
78
79   friend void Glib::custom_get_property_callback(GObject* object, unsigned int property_id,
80                                                  GValue* value, GParamSpec* param_spec);
81
82   friend void Glib::custom_set_property_callback(GObject* object, unsigned int property_id,
83                                                  const GValue* value, GParamSpec* param_spec);
84
85 #endif /* DOXYGEN_SHOULD_SKIP_THIS */
86 };
87
88
89 template <class T>
90 class Property : public PropertyBase
91 {
92 public:
93   typedef T PropertyType;
94   typedef Glib::Value<T> ValueType;
95
96   Property(Glib::Object& object, const Glib::ustring& name);
97   Property(Glib::Object& object, const Glib::ustring& name, const PropertyType& default_value);
98
99   inline void set_value(const PropertyType& data);
100   inline PropertyType get_value() const;
101
102   inline Property<T>& operator=(const PropertyType& data);
103   inline operator PropertyType() const;
104
105   inline Glib::PropertyProxy<T> get_proxy();
106 };
107
108
109 #ifndef DOXYGEN_SHOULD_SKIP_THIS
110
111 /**** Glib::Property<T> ****************************************************/
112
113 template <class T>
114 Property<T>::Property(Glib::Object& object, const Glib::ustring& name)
115 :
116   PropertyBase(object, ValueType::value_type())
117 {
118   if(!lookup_property(name))
119     install_property(static_cast<ValueType&>(value_).create_param_spec(name));
120 }
121
122 template <class T>
123 Property<T>::Property(Glib::Object& object, const Glib::ustring& name,
124                       const typename Property<T>::PropertyType& default_value)
125 :
126   PropertyBase(object, ValueType::value_type())
127 {
128   static_cast<ValueType&>(value_).set(default_value);
129
130   if(!lookup_property(name))
131     install_property(static_cast<ValueType&>(value_).create_param_spec(name));
132 }
133
134 template <class T> inline
135 void Property<T>::set_value(const typename Property<T>::PropertyType& data)
136 {
137   static_cast<ValueType&>(value_).set(data);
138   this->notify();
139 }
140
141 template <class T> inline
142 typename Property<T>::PropertyType Property<T>::get_value() const
143 {
144   return static_cast<const ValueType&>(value_).get();
145 }
146
147 template <class T> inline
148 Property<T>& Property<T>::operator=(const typename Property<T>::PropertyType& data)
149 {
150   static_cast<ValueType&>(value_).set(data);
151   this->notify();
152   return *this;
153 }
154
155 template <class T> inline
156 Property<T>::operator T() const
157 {
158   return static_cast<const ValueType&>(value_).get();
159 }
160
161 template <class T> inline
162 Glib::PropertyProxy<T> Property<T>::get_proxy()
163 {
164   return Glib::PropertyProxy<T>(object_, get_name_internal());
165 }
166
167 #endif /* DOXYGEN_SHOULD_SKIP_THIS */
168
169 } // namespace Glib
170
171 #endif //GLIBMM_PROPERTIES_ENABLED
172
173 #endif /* _GLIBMM_PROPERTY_H */
174