Tracing system on a const object

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Tracing system on a const object

Natale Patriciello
Hi all,

this afternoon I ran into a problem that in theory was easy to solve,
but it reality it was not (and I'm still not convinced of the solution).
Basically I needed to do this:

Ptr<const Object> o;
o->TraceConnectWithoutContext (&MyFunct);

This is not allowed by our current API, because Trace* methods are not
const inside the ObjectBase class. I wondered why, since tracing is not
modifying the class state. Then I found the root: inside each Trace*
method, there is a call to Connect* methods from
trace-source-accessor.h. They are const, but as parameter they want a
'ObjectBase*' , not a 'const ObjectBase*'. Just for reference, I
copy/paste one declaration:

1  virtual bool
2  ConnectWithoutContext (ObjectBase *obj, const CallbackBase &cb) const
3  {
4
5   T *p = dynamic_cast<T*> (obj);
6   if (p == 0)
7     {
8       return false;
9     }
10  (p->*m_source).ConnectWithoutContext (cb);
11  return true;
12 }

Changing the declaration to 'const ObjectBase *obj' and adding const as
well at line 5 is not working, because of some obscure error that I
can't decodify at line 10.
For now, I have solved by the old way of const_casting the parameter:

  ObjectBase *obj_copy = const_cast<ObjectBase*> (obj);

but I would like to avoid that by using the mutable keyword somewhere in
the code, and using const as much as possible all around in these
functions.

So, after this long mail, I'm asking if someone can help me in decodify
the error and how to solve it. I'm attaching two patch: 0001_ is the
patch that solves this problem using const_cast , 0002_ is the patch
that (if applied on top of 0001_) shows the obscure error when using
const pointers.

Thank you!
Natale




0001_const_tracing.patch (7K) Download Attachment
0002_trace_sources.patch (646 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Tracing system on a const object

Barnes, Peter D.
Could you please circulate the error?

Thanks,
Peter

On Dec 8, 2016, at 1:02 PM, Natale Patriciello <[hidden email]<mailto:[hidden email]>> wrote:

Hi all,

this afternoon I ran into a problem that in theory was easy to solve,
but it reality it was not (and I'm still not convinced of the solution).
Basically I needed to do this:

Ptr<const Object> o;
o->TraceConnectWithoutContext (&MyFunct);

This is not allowed by our current API, because Trace* methods are not
const inside the ObjectBase class. I wondered why, since tracing is not
modifying the class state. Then I found the root: inside each Trace*
method, there is a call to Connect* methods from
trace-source-accessor.h. They are const, but as parameter they want a
'ObjectBase*' , not a 'const ObjectBase*'. Just for reference, I
copy/paste one declaration:

1  virtual bool
2  ConnectWithoutContext (ObjectBase *obj, const CallbackBase &cb) const
3  {
4
5   T *p = dynamic_cast<T*> (obj);
6   if (p == 0)
7     {
8       return false;
9     }
10  (p->*m_source).ConnectWithoutContext (cb);
11  return true;
12 }

Changing the declaration to 'const ObjectBase *obj' and adding const as
well at line 5 is not working, because of some obscure error that I
can't decodify at line 10.
For now, I have solved by the old way of const_casting the parameter:

 ObjectBase *obj_copy = const_cast<ObjectBase*> (obj);

but I would like to avoid that by using the mutable keyword somewhere in
the code, and using const as much as possible all around in these
functions.

So, after this long mail, I'm asking if someone can help me in decodify
the error and how to solve it. I'm attaching two patch: 0001_ is the
patch that solves this problem using const_cast , 0002_ is the patch
that (if applied on top of 0001_) shows the obscure error when using
const pointers.

Thank you!
Natale



<0001_const_tracing.patch><0002_trace_sources.patch>

_____________________________________________________________
Dr. Peter D. Barnes, Jr. NACS Division
Lawrence Livermore National Laboratory Physical and Life Sciences
7000 East Avenue, L-50 email:  [hidden email]<mailto:[hidden email]>
P. O. Box 808 Voice:  (925) 422-3384
Livermore, California 94550 Fax:    (925) 423-3371





Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Tracing system on a const object

Matt
In reply to this post by Natale Patriciello
I haven't looked into it but my guess would be that many templates in
the core are manually specialized, hence don't cover everything
(references or const for instance). Converting those to benefit from
C++11 might help in that regards.

2016-12-08 22:02 GMT+01:00 Natale Patriciello <[hidden email]>:

> Hi all,
>
> this afternoon I ran into a problem that in theory was easy to solve,
> but it reality it was not (and I'm still not convinced of the solution).
> Basically I needed to do this:
>
> Ptr<const Object> o;
> o->TraceConnectWithoutContext (&MyFunct);
>
> This is not allowed by our current API, because Trace* methods are not
> const inside the ObjectBase class. I wondered why, since tracing is not
> modifying the class state. Then I found the root: inside each Trace*
> method, there is a call to Connect* methods from
> trace-source-accessor.h. They are const, but as parameter they want a
> 'ObjectBase*' , not a 'const ObjectBase*'. Just for reference, I
> copy/paste one declaration:
>
> 1  virtual bool
> 2  ConnectWithoutContext (ObjectBase *obj, const CallbackBase &cb) const
> 3  {
> 4
> 5   T *p = dynamic_cast<T*> (obj);
> 6   if (p == 0)
> 7     {
> 8       return false;
> 9     }
> 10  (p->*m_source).ConnectWithoutContext (cb);
> 11  return true;
> 12 }
>
> Changing the declaration to 'const ObjectBase *obj' and adding const as
> well at line 5 is not working, because of some obscure error that I
> can't decodify at line 10.
> For now, I have solved by the old way of const_casting the parameter:
>
>   ObjectBase *obj_copy = const_cast<ObjectBase*> (obj);
>
> but I would like to avoid that by using the mutable keyword somewhere in
> the code, and using const as much as possible all around in these
> functions.
>
> So, after this long mail, I'm asking if someone can help me in decodify
> the error and how to solve it. I'm attaching two patch: 0001_ is the
> patch that solves this problem using const_cast , 0002_ is the patch
> that (if applied on top of 0001_) shows the obscure error when using
> const pointers.
>
> Thank you!
> Natale
>
>
>
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Tracing system on a const object

Natale Patriciello
In reply to this post by Barnes, Peter D.
On 08/12/16 at 10:10pm, Barnes, Peter D. wrote:
> Could you please circulate the error?
>
> Thanks,
> Peter

It's a bit long, but here is the interesting part:

In file included from ./ns3/type-id.h:25:0,
                 from ./ns3/object-base.h:23,
                 from ./ns3/object.h:29,
                 from ./ns3/scheduler.h:25,
                 from ./ns3/calendar-scheduler.h:24,
                 from ./ns3/core-module.h:19,
                 from ../examples/stats/wifi-example-apps.cc:30:
./ns3/trace-source-accessor.h: In instantiation of ‘bool ns3::DoMakeTraceSourceAccessor(SOURCE T::*)::Accessor::ConnectWithoutContext(const ns3::ObjectBase*, const ns3::CallbackBase&) const [with T = Sender; SOURCE = ns3::TracedCallback<ns3::Ptr<const ns3::Packet> >]’:
./ns3/trace-source-accessor.h:195:3:   required from ‘ns3::Ptr<const ns3::TraceSourceAccessor> ns3::DoMakeTraceSourceAccessor(SOURCE T::*) [with T = Sender; SOURCE = ns3::TracedCallback<ns3::Ptr<const ns3::Packet> >]’
./ns3/trace-source-accessor.h:203:36:   required from ‘ns3::Ptr<const ns3::TraceSourceAccessor> ns3::MakeTraceSourceAccessor(T) [with T = ns3::TracedCallback<ns3::Ptr<const ns3::Packet> > Sender::*]’
../examples/stats/wifi-example-apps.cc:69:65:   required from here
./ns3/trace-source-accessor.h:161:7: error: passing ‘const ns3::TracedCallback<ns3::Ptr<const ns3::Packet> >’ as ‘this’ argument discards qualifiers [-fpermissive]
       (p->*m_source).ConnectWithoutContext (cb);
       ^
In file included from ./ns3/core-module.h:87:0,
                 from ../examples/stats/wifi-example-apps.cc:30:
./ns3/traced-callback.h:264:1: note:   in call to ‘void ns3::TracedCallback<T1, T2, T3, T4, T5, T6, T7, T8>::ConnectWithoutContext(const ns3::CallbackBase&) [with T1 = ns3::Ptr<const ns3::Packet>; T2 = ns3::empty; T3 = ns3::empty; T4 = ns3::empty; T5 = ns3::empty; T6 = ns3::empty; T7 = ns3::empty; T8 = ns3::empty]’
 TracedCallback<T1,T2,T3,T4,T5,T6,T7,T8>::ConnectWithoutContext (const CallbackBase & callback)
 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


I have edited the function in this way:

155     virtual bool ConnectWithoutContext (const ObjectBase *obj, const CallbackBase &cb) const {
156       const T *p = dynamic_cast<const T*> (obj);
157       if (p == 0)
158         {
159           return false;
160         }
161       (p->*m_source).ConnectWithoutContext (cb);
162       return true;
163     }

(removing the const_cast and adding const at line 156).
I think there is some problem (p->*m_source), but I can't understand
where it's failing.

Nat
Loading...