add new sigc++2 directory
[ardour.git] / libs / glibmm2 / glib / glibmm / propertyproxy.h
1 // -*- c++ -*-
2 #ifndef _GLIBMM_PROPERTYPROXY_H
3 #define _GLIBMM_PROPERTYPROXY_H
4 /* $Id: propertyproxy.h 291 2006-05-12 08:08:45Z murrayc $ */
5
6 /* propertyproxy.h
7  *
8  * Copyright 2002 The gtkmm Development Team
9  *
10  * This library is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU Library General Public
12  * License as published by the Free Software Foundation; either
13  * version 2 of the License, or (at your option) any later version.
14  *
15  * This library is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18  * Library General Public License for more details.
19  *
20  * You should have received a copy of the GNU Library General Public
21  * License along with this library; if not, write to the Free
22  * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23  */
24
25 #include <glibmm/propertyproxy_base.h>
26
27 #ifdef GLIBMM_PROPERTIES_ENABLED
28
29 namespace Glib
30 {
31
32 /** A PropertyProxy can be used to get and set the value of an object's property.
33  * There are usually also get and set methods on the class itself, which you might find more convenient.
34  * With the PropertyProxy, you may use either get_value() and set_value(), or operator=() and
35  * operator PropertyType(), like so:
36  * @code
37  * int height = cellrenderer.property_height();
38  * cellrenderer.property_editable() = true;
39  * @endcode
40  *
41  * You may also receive notification when a property's value changes, by connecting to signal_changed().
42  */
43 template <class T>
44 class PropertyProxy : public PropertyProxy_Base
45 {
46 public:
47   typedef T PropertyType;
48
49   PropertyProxy(ObjectBase* obj, const char* name)
50     : PropertyProxy_Base(obj, name) {}
51
52   /** Set the value of this property.
53    * @param data The new value for the property.
54    */
55   void set_value(const PropertyType& data);
56
57   /** Get the value of this property.
58    * @result The current value of the property.
59    */
60   PropertyType get_value() const;
61
62   /** Set the value of this property back to its default value
63    */
64   void reset_value()
65     { reset_property_(); }
66
67   PropertyProxy<T>& operator=(const PropertyType& data)
68     { this->set_value(data); return *this; }
69
70   operator PropertyType() const
71     { return this->get_value(); }
72 };
73
74
75 /** See PropertyProxy().
76  * This property can be written, but not read, so there is no get_value() method.
77  */
78 template <class T>
79 class PropertyProxy_WriteOnly : public PropertyProxy_Base
80 {
81 public:
82   typedef T PropertyType;
83
84   PropertyProxy_WriteOnly(ObjectBase* obj, const char* name)
85     : PropertyProxy_Base(obj, name) {}
86
87   /** Set the value of this property.
88    * @param data The new value for the property.
89    */
90   void set_value(const PropertyType& data);
91
92   /** Set the value of this property back to its default value
93    */
94   void reset_value()
95     { reset_property_(); }
96
97   PropertyProxy_WriteOnly<T>& operator=(const PropertyType& data)
98     { this->set_value(data); return *this; }
99 };
100
101 /** See PropertyProxy().
102  * This property can be read, but not written, so there is no set_value() method.
103  */
104 template <class T>
105 class PropertyProxy_ReadOnly : public PropertyProxy_Base
106 {
107 public:
108   typedef T PropertyType;
109
110   //obj is const, because this should be returned by const accessors.
111   PropertyProxy_ReadOnly(const ObjectBase* obj, const char* name)
112     : PropertyProxy_Base(const_cast<ObjectBase*>(obj), name) {}
113
114   /** Get the value of this property.
115    * @result The current value of the property.
116    */
117   PropertyType get_value() const;
118
119   operator PropertyType() const
120     { return this->get_value(); }
121 };
122
123
124 /**** Template Implementation **********************************************/
125
126 #ifndef DOXYGEN_SHOULD_SKIP_THIS
127
128 template <class T>
129 void PropertyProxy<T>::set_value(const T& data)
130 {
131   Glib::Value<T> value;
132   value.init(Glib::Value<T>::value_type());
133
134   value.set(data);
135   set_property_(value);
136 }
137
138 template <class T>
139 T PropertyProxy<T>::get_value() const
140 {
141   Glib::Value<T> value;
142   value.init(Glib::Value<T>::value_type());
143
144   get_property_(value);
145   return value.get();
146 }
147
148 //We previously just static_cast<> PropertyProxy_WriteOnly<> to PropertyProxy<> to call its set_value(), 
149 //to avoid code duplication.
150 //But the AIX compiler does not like that hack.
151 template <class T>
152 void PropertyProxy_WriteOnly<T>::set_value(const T& data)
153 {
154   Glib::Value<T> value;
155   value.init(Glib::Value<T>::value_type());
156
157   value.set(data);
158   set_property_(value);
159 }
160
161 //We previously just static_cast<> PropertyProxy_WriteOnly<> to PropertyProxy<> to call its set_value(), 
162 //to avoid code duplication.
163 //But the AIX compiler does not like that hack.
164 template <class T>
165 T PropertyProxy_ReadOnly<T>::get_value() const
166 {
167   Glib::Value<T> value;
168   value.init(Glib::Value<T>::value_type());
169
170   get_property_(value);
171   return value.get();
172 }
173
174 #endif /* DOXYGEN_SHOULD_SKIP_THIS */
175
176 } // namespace Glib
177
178 #endif //GLIBMM_PROPERTIES_ENABLED
179
180 #endif /* _GLIBMM_PROPERTYPROXY_H */
181