Boost logo

Boost :

Subject: Re: [boost] Interest in an Active Object CRTP "decorator"?
From: vicente.botet (vicente.botet_at_[hidden])
Date: 2008-11-08 12:06:59


----- Original Message -----
From: "Dean Michael Berris" <mikhailberis_at_[hidden]>
To: <boost_at_[hidden]>
Sent: Saturday, November 08, 2008 4:56 PM
Subject: [boost] Interest in an Active Object CRTP "decorator"?

>
> Any interest in something like this to be included in Boost?
>
> --->8--
> [NOTE: untested, licensed under the Boost Software License,
> Copyright 2008 Dean Michael Berris]
>
> template <class Derived>
> struct active {
> private:
> shared_ptr<io_service> queue;
> shared_ptr<io_service::work> sentinel;
> shared_ptr<thread> lifetime_thread;
>
> protected:
> active() :
> queue(new io_service()),
> sentinel(new io_service::work(*queue)),
> lifetime_thread(new thread(bind(&io_service::run, queue)))
> {
> static_cast<Derived*>->init();
> };
>
> ~active() {
> sentinel.reset();
> lifetime_thread->join();
> static_cast<Derived*>->destroy();
> };
>
> void post(function<void()> f) {
> queue->post(f);
> };
>
> };
>
> --->8--
>
> Example usage would be something like this:
>
> --->8--
>
> struct logger : active<logger> {
> void init() { }; // required
> void destroy() { }; // required
> void operator() (string const & message) {
> active<logger>::post(bind(&logger::write, this, string(message)));
> }
> void write(string message) {
> cout << message << endl;
> }
> };
>

Why the usual constructor is not enough -- init is called after active is
constructed?
IMO
      static_cast<Derived*>(this)->destroy();
is very dangerous. You are calling a function for an object that has already
been destructed, so any access to Derived data is undefined.

So ath the end whay do you need CRTP?
Why do you need the usual constructor is not enough -- init is called after
active is constructed?

Best,

Vicente


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk