Re: How to use Glib::Value?



Thank you for clarification.

This is what I did for my code. 

template<class T>
class Column {
        Glib::Value<T> data

public:
        Column(){
                data.init(data.get_value_type());
        }
        Column(const T &val):Column(){
                data.set(val);
        }

        void set_value(const T &val){
                data.set(val);
        }

        T get_value(void){
                return data.get();
        }

};

Usage is very convenient:
Column<Glib::ustring> mColumn;

mColumn.get(); // get value
mColumn.set("New string"); // Set value

This is just a general idea and some parts are missed. Do you think the
similar code can be incorporated to Glib::Value? For example. If we
rename the current class Glib::Value to Glib::Value_t and remove from
the public API, we can define a new class Glib::Value which will
include Glib::Value_t in the way I showed above. This will remove extra
step such as call Glib::Value<T>::init(...) method.

Just a suggestion. I can prepare MR if the idea is valuable.

Thanks.



On Tue, 2019-03-05 at 19:28 +0100, Kjell Ahlstedt wrote:
init() is a method in Glib::ValueBase, base class of all
Glib::Value<> specializations. The documentation says
    init() is not implemented as constructor, to avoid the necessity
    to implement a forward constructor in each derived class.
There are many subclasses of Glib::ValueBase. Most of these
subclasses (perhaps even all of them) has just compiler-generated
constructors. I suppose someone found it tedious to write
constructors with a call to init() in all those subclasses. It's not
obvious to me that it was a wise decision not to do that.
On 2019-03-05 14:37, Pavlo Solntsev via gtkmm-list wrote:
Thanks, that works. Out of curiosity, why not to call init() in the
constructor? 


On Tue, 2019-03-05 at 11:00 +0100, Kjell Ahlstedt wrote:
Replace
   data.init(G_TYPE_DATE_TIME);
by
   data.init(data.value_type());
or
   data.init(Glib::Value<Glib::DateTime>::value_type());
You don't store a GDateTime, you store a Glib::DateTime. That's
not
the same thing.
This is not well described in the documentation, but there is an
example at
https://gitlab.gnome.org/GNOME/glibmm/blob/master/tests/glibmm_value/main.cc
On 2019-03-04 14:05, Pavlo Solntsev via gtkmm-list wrote:
Hi,

Have this code 
```
Glib::Value<int> datai;
datai.init(G_TYPE_INT);
datai.set(8);
std::cout << "Value = " << datai.get() << std::endl;
```
I see in stdout:
Value = 8

Works good. Now I would like to use another type:
Glib::DateTime
```
Glib::Value<Glib::DateTime> data;

data.init(G_TYPE_DATE_TIME);
Glib::DateTime ctime = Glib::DateTime::create_now_utc();
data.set(ctime);
```

This compiles ok, but generates 
Program received signal SIGSEGV, Segmentation fault.
0x00007ffff7b2405c in Glib::DateTime::operator= (this=0x0,
src=...)
at
_______________________________________________ gtkmm-list
mailing list gtkmm-list gnome org 
https://mail.gnome.org/mailman/listinfo/gtkmm-list
/home/pavlo/jhbuild/checkout/glibmm-
2.4/glib/glibmm/datetime.cc:89
89        if(gobject_)

 



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