Looking at the Glib::ValueBase classes
- From: José Alburquerque <jaalburquerque cox net>
- To: gtkmm-list gnome org
- Subject: Looking at the Glib::ValueBase classes
- Date: Sun, 13 Jan 2008 23:30:13 -0500
Working with the gstreamermm Structure class, I've been looking a little
at how it can be modified to use the Glib::ValueBase classes to get/set
its fields. Looking at the ValueBase classes (for example, the
Glib::Value<int> class) I have a general question on them: Is there a
reason why they don't have equal/cast operators for setting them and
using them in expressions (e.g. "int i = 5 + value", where value would
be a Glib::Value<int>)? Is it done purposefully? Also is there a
reason their value types are not initialized in some constructor when
they are declared (so it doesn't have to be done in applications before
using)? I'm thinking of writing a small patch that does these things
(I've done some of it already and so far it makes their use nearly
"seamless" in expressions). Working with these, for example, could look
something like:
...
Glib::Value<int> int_val1;
Glib::Value<int> int_val2 = 10;
val1 = 5;
std::cout << "int_val1 + int_val2 =" << int_val1 + int_val2 << std::endl;
...
(I'm including what I've done so far to provide a sense of the
modifications.) Would something like this be useful? If so, I would
complete it and submit to enhance Glibmm. If not, it's no big deal.
Thanks.
-Jose
Index: glib/glibmm/value.h
===================================================================
--- glib/glibmm/value.h (revision 508)
+++ glib/glibmm/value.h (working copy)
@@ -213,8 +213,12 @@
static GType value_type() { return T::get_type(); }
+ Value_Boxed<T>() { init(value_type()); }
+ Value_Boxed<T>(const CppType& data) { init(value_type()); set(data); }
void set(const CppType& data) { set_boxed(data.gobj()); }
+ Value_Boxed<T>& operator=(const CppType& data) { set_boxed(data.gobj()); return *this; }
CppType get() const { return CppType(static_cast<CType>(get_boxed())); }
+ operator CppType() const { return CppType(static_cast<CType>(get_boxed())); }
};
//More spec-compliant compilers (such as Tru64) need this to be near Glib::Object instead.
@@ -232,8 +236,12 @@
static GType value_type() { return T::get_base_type(); }
+ Value< Glib::RefPtr<T> >() { init(value_type()); }
+ Value< Glib::RefPtr<T> >(const CppType& data) { init(value_type()); set(data); }
void set(const CppType& data) { set_object(data.operator->()); }
+ Value< Glib::RefPtr<T> >& operator=(const CppType& data) { set_object(data.operator->()); return *this; }
CppType get() const { return Glib::RefPtr<T>::cast_dynamic(get_object_copy()); }
+ operator CppType() const { return Glib::RefPtr<T>::cast_dynamic(get_object_copy()); }
};
//The SUN Forte Compiler has a problem with this:
@@ -251,8 +259,12 @@
static GType value_type() { return T::get_base_type(); }
+ Value< Glib::RefPtr<const T> >() { init(value_type()); }
+ Value< Glib::RefPtr<const T> >(const CppType& data) { init(value_type()); set(data); }
void set(const CppType& data) { set_object(const_cast<T*>(data.operator->())); }
+ Value< Glib::RefPtr<const T> >& operator=(const CppType& data) { set_object(const_cast<T*>(data.operator->())); return *this; }
CppType get() const { return Glib::RefPtr<T>::cast_dynamic(get_object_copy()); }
+ operator CppType() const { return Glib::RefPtr<T>::cast_dynamic(get_object_copy()); }
};
#endif //GLIBMM_HAVE_DISAMBIGUOUS_CONST_TEMPLATE_SPECIALIZATIONS
@@ -280,8 +292,14 @@
public:
typedef std::string CppType;
+ Value<std::string>() { init(value_type()); }
+ Value<std::string>(const std::string& data) { init(value_type()); set(data); }
+ Value<std::string>(const char* data) { init(value_type()); set(std::string(data)); }
void set(const std::string& data);
+ Value<std::string>& operator=(const std::string& data);
+ Value<std::string>& operator=(const char* data);
std::string get() const { return get_cstring(); }
+ operator std::string() const { return get_cstring(); }
};
/** Specialization for UTF-8 strings.
@@ -293,8 +311,14 @@
public:
typedef Glib::ustring CppType;
+ Value<Glib::ustring>() { init(value_type()); }
+ Value<Glib::ustring>(const Glib::ustring& data) { init(value_type()); set(data); }
+ Value<Glib::ustring>(const char* data) { init(value_type()); set(Glib::ustring(data)); }
void set(const Glib::ustring& data);
+ Value<Glib::ustring>& operator=(const Glib::ustring& data);
+ Value<Glib::ustring>& operator=(const char* data);
Glib::ustring get() const { return get_cstring(); }
+ operator Glib::ustring() const { return get_cstring(); }
};
@@ -307,8 +331,12 @@
public:
typedef T CppType;
+ Value_Enum<T>() { init(value_type()); }
+ Value_Enum<T>(CppType data) { init(value_type()); set(data); }
void set(CppType data) { set_enum(data); }
+ Value_Enum<T>& operator=(CppType data) { set(data); return *this; }
CppType get() const { return CppType(get_enum()); }
+ operator CppType() const { return CppType(get_enum()); }
};
/** Base class of Glib::Value<T> specializations for flags types.
@@ -320,8 +348,12 @@
public:
typedef T CppType;
+ Value_Flags<T>() { init(value_type()); }
+ Value_Flags<T>(CppType data) { init(value_type()); set(data); }
void set(CppType data) { set_flags(data); }
+ Value_Flags<T>& operator=(CppType data) { set_flags(data); return *this; }
CppType get() const { return CppType(get_flags()); }
+ operator CppType() const { return CppType(get_flags()); }
};
} // namespace Glib
Index: glib/glibmm/value.cc
===================================================================
--- glib/glibmm/value.cc (revision 508)
+++ glib/glibmm/value.cc (working copy)
@@ -238,7 +238,19 @@
g_value_set_string(&gobject_, data.c_str());
}
+Value<std::string>& Value<std::string>::operator=(const std::string& data)
+{
+ g_value_set_string(&gobject_, data.c_str());
+ return *this;
+}
+Value<std::string>& Value<std::string>::operator=(const char* data)
+{
+ g_value_set_string(&gobject_, data);
+ return *this;
+}
+
+
/**** Glib::Value<Glib::ustring> *******************************************/
void Value<Glib::ustring>::set(const Glib::ustring& data)
@@ -246,5 +258,17 @@
g_value_set_string(&gobject_, data.c_str());
}
+Value<Glib::ustring>& Value<Glib::ustring>::operator=(const Glib::ustring& data)
+{
+ g_value_set_string(&gobject_, data.c_str());
+ return *this;
+}
+
+Value<Glib::ustring>& Value<Glib::ustring>::operator=(const char* data)
+{
+ g_value_set_string(&gobject_, data);
+ return *this;
+}
+
} // namespace Glib
Index: glib/src/value_basictypes.cc.m4
===================================================================
--- glib/src/value_basictypes.cc.m4 (revision 508)
+++ glib/src/value_basictypes.cc.m4 (working copy)
@@ -37,6 +37,17 @@
return G_TYPE_[]UPPER($2);
}
+Value<$1>::Value()
+{
+ init(value_type());
+}
+
+Value<$1>::Value($1 data)
+{
+ init(value_type());
+ set(data);
+}
+
void Value<$1>::set($1 data)
{
g_value_set_$2(&gobject_, data);
@@ -47,6 +58,17 @@
return g_value_get_$2(&gobject_);
}
+Value<$1>::operator $1() const
+{
+ return get();
+}
+
+Value<$1>& Value<$1>::operator=($1 data)
+{
+ set(data);
+ return *this;
+}
+
GParamSpec* Value<$1>::create_param_spec(const Glib::ustring& name) const
{
return g_param_spec_$2(
Index: glib/src/value_basictypes.h.m4
===================================================================
--- glib/src/value_basictypes.h.m4 (revision 508)
+++ glib/src/value_basictypes.h.m4 (working copy)
@@ -40,8 +40,12 @@
static GType value_type() G_GNUC_CONST;
+ Value<$1>();
+ Value<$1>($1 data);
void set($1 data);
$1 get() const;
+ operator $1() const;
+ Value<$1>& operator=($1 data);
#ifndef DOXYGEN_SHOULD_SKIP_THIS
GParamSpec* create_param_spec(const Glib::ustring& name) const;
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]