Re: [protobuf] C++ modifying field without knowing field accessor at compile time

2014-10-27 Thread Peter Hubner
Yah, after looking back over my problem I refactored and removed the const 
ref. Just need to take my eyes of the problem for a bit.

Thanks for the help.

On Monday, October 27, 2014 11:15:32 AM UTC-6, Oliver wrote:
>
> On 24 October 2014 17:28, Peter Hubner > 
> wrote: 
> > All I am trying to achieve is to modify a message on the fly with a set 
> of 
> > fields that are supplied to the program at runtime. 
> > 
> > simple case: 
> > // message declared as - const google::protobuf::Message& message 
> > auto x = reflection->GetDouble(message, field); 
> > // do some modification to x 
> > reflection->SetDouble(message, field, x); 
> > 
> > my main question is how in the world do I get from a const ref (needed 
> for 
> > GetDouble() ) to a pointer (needed for SetDouble() )??? 
>
> const_cast(&message) 
>
> This obviously casts away constness which is something to be careful 
> about. 
> If you want to mutate the message, why are you starting with a const ref? 
>
> Oliver 
>

-- 
You received this message because you are subscribed to the Google Groups 
"Protocol Buffers" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to protobuf+unsubscr...@googlegroups.com.
To post to this group, send email to protobuf@googlegroups.com.
Visit this group at http://groups.google.com/group/protobuf.
For more options, visit https://groups.google.com/d/optout.


Re: [protobuf] C++ modifying field without knowing field accessor at compile time

2014-10-27 Thread Oliver Jowett
On 24 October 2014 17:28, Peter Hubner  wrote:
> All I am trying to achieve is to modify a message on the fly with a set of
> fields that are supplied to the program at runtime.
>
> simple case:
> // message declared as - const google::protobuf::Message& message
> auto x = reflection->GetDouble(message, field);
> // do some modification to x
> reflection->SetDouble(message, field, x);
>
> my main question is how in the world do I get from a const ref (needed for
> GetDouble() ) to a pointer (needed for SetDouble() )???

const_cast(&message)

This obviously casts away constness which is something to be careful about.
If you want to mutate the message, why are you starting with a const ref?

Oliver

-- 
You received this message because you are subscribed to the Google Groups 
"Protocol Buffers" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to protobuf+unsubscr...@googlegroups.com.
To post to this group, send email to protobuf@googlegroups.com.
Visit this group at http://groups.google.com/group/protobuf.
For more options, visit https://groups.google.com/d/optout.


[protobuf] C++ modifying field without knowing field accessor at compile time

2014-10-27 Thread Peter Hubner
All I am trying to achieve is to modify a message on the fly with a set of 
fields that are supplied to the program at runtime.

simple case:
// message declared as - const google::protobuf::Message& message
auto x = reflection->GetDouble(message, field);
// do some modification to x
reflection->SetDouble(message, field, x);

my main question is how in the world do I get from a const ref (needed for 
GetDouble() ) to a pointer (needed for SetDouble() )???

I know that if I knew the field name at compile time I would be able to use 
mutable_, but since I don't know that until runtime there does not 
seem to be an easy way to do this. Or I am just missing something super 
simple.

Thanks,

-- 
You received this message because you are subscribed to the Google Groups 
"Protocol Buffers" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to protobuf+unsubscr...@googlegroups.com.
To post to this group, send email to protobuf@googlegroups.com.
Visit this group at http://groups.google.com/group/protobuf.
For more options, visit https://groups.google.com/d/optout.