Re: Getting GValue* from Glib::Value



The description of Glib::ValueBase says

 * Glib::Value<> is specialized for almost any type used within the glibmm and gtkmm libraries.
 *
 * - Basic types like <tt>int</tt>, <tt>char</tt>, <tt>bool</tt>, etc., also <tt>void*</tt>.
 * - Glib::ustring and std::string.
 * - Pointers to classes derived from Glib::Object.
 * - Glib::RefPtr<> pointer types, which are assumed to be Glib::Object pointers.
 * - All flags and enum types used within the gtkmm libraries.
 *
 * If a type doesn't fit into any of these categories, then a generic
 * implementation for custom types will be used.

"Almost any type" does not include Glib::DateTime, unfortunately. And "All flags and enum types" is not quite right. There are some enum types, especailly in glibmm, without a Glib::Value specialization.

I think this Glib::Value<Glib::DateTime> specialization will work:

namespace Glib
{
template <>
class Value<Glib::DateTime> : public ValueBase_Boxed
{
public:
  using CppType = Glib::DateTime;
  using CType = GDateTime*;

  static GType value_type() { return G_TYPE_DATE_TIME; }

  void set(const CppType& data) { set_boxed(data.gobj()); }
  CppType get() const { return CppType(static_cast<CType>(get_boxed()), true); }
};
} // namespace Glib


On 2019-04-23 15:01, p sun fun gmail com wrote:
Thanks, Kjell. I missed that gobj() returns a pointer to gobject_.
However, the returned type is not a pure C gtype. Let me illustrate
this by example:

```
  Glib::Value<Glib::DateTime> data;

  data.init(data.value_type());

  const GValue *val = data.gobj();
  std::cout << "val type is " << G_VALUE_TYPE_NAME(val) << std::endl;

  GValue *val2 = g_new0(GValue, 1);
  g_value_init(val2, G_TYPE_DATE_TIME);
  std::cout << "val2 type is " << G_VALUE_TYPE_NAME(val2) << std::endl;

```
In stdout I see

```
val type is glibmm__CustomBoxed_N4Glib8DateTimeE
val2 type is GDateTime
```

I know that GDateTime is a boxed type and Glib::Value registers it own
type. If I use built-in type, e.g. int, I see the same type for C++ and
C parts. Basically, my question should be refined and tailed to the
conversion of C++ gtype (boxed equivalent) to the C-like gtype. 

Thanks.


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]