On Modifying Bro tables in C++

Hi,

The following problem is probably easy, but my knowledge
of C++ is pretty limited.

I have a table defined in Bro, and I want to modify it
(add/delete terms) in C++. Actually, I want to modify the
table *only* in C++, but this is another subject.

I use internal_val() to get a Val* from the table's Bro name.
When I want to read the table, I access to the table
underneath using my_table->AsTable() or, if I want to
do further casting, using my_table->AsTable()->AsTableVal().
Everything works fine.

Problem is when I want to write. AsTable() is defined in
Val.h as a const accessor, but not as a non-const one. This
means that my_table->AsTable() returns actually a "const
TableEntryValPDict" object. Therefore, any access to Insert()
or Remove() causes a "discards qualifiers" compiler error.

While I can do brute-force cast and turn my "const
TableEntryValPDict" into a simple TableEntryValPDict, I've
read in Val.h that "Accessors for mutable values are called
AsNonConst* and are protected to avoid external state
changes." This seems to state that there is a valid reason
to avoid doing what I'm doing. "Avoid external state
changes" is not descriptive enough for me.

Can somebody provide advice on this?

TIA.
-Chema

Problem is when I want to write. AsTable() is defined in
Val.h as a const accessor, but not as a non-const one.

Hi, Chema,

How about using TableVal::Assign()?

Ruoming

The reason for the accessors being protected is the new
&synchronized keyword (and other related issues). For synchronizing
values between remote instances of Bro, we need to track all
accesses to the table which change its state in some way. The code
of TableVal (and derived classes) takes care of this. If you modify
the values content from the outside you have to do it yourself (grep
for LogAccess to see how that works).

So, I'd also suggest to use TableVal::Assign().

Robin

Robin,

Thanks for the answer. Ruoming convinced me first (it's
easier), but you answer my real doubt (why such a
centralization of access).

Regards.
-Chema