Does Chai has "for a in map" style loops?


#1

Hi!

Does Chai has “for a in map” style loops?

Thanks


#2

Not directly, it’s one of the current feature requests for “more modern loops”

You can use a built in for_each functional-style algorithm, like:

map.for_each(fun(elem) {
  // do something with elem.first
  // do something with elem.second
});

#3

Here’s the related feature request:


#4

Thank you for fast reply.

Is it possible to write loop something like iterator style?
for (std::map<std::string, int>::iterator it = map.begin();it!=map.end();It++)

And we experience issues with maps with pointer data type. We can’t access the data. Like std::map<std::string, UserClass *>

We register it as Map type, and can access it, but not the “second” elements. I will provide sample as it will be possible


#5

I try voodoo and black magic. It crashes and I have no idea what is wrong

typedef std::map<std::string, int*> map_type_pointer;
chai.add(chaiscript::bootstrap::standard_library::map_type<map_type_pointer>("map_type_pointer"));


int local_i;
map_type_pointer tmp = { { "ss", &local_i } };
chai.add(chaiscript::var(&tmp), "tmp");


chai.eval(R"_(
var j = tmp["ss"]
)_");

We have tons of maps string->object pointer :frowning:


#6

What kind of crash do you get? Memory error? Uncaught exception? If it’s an exception, what’s the type/description? If it’s a memory error, what’s the stack trace look like?

-Jason


#7

I will try to revert all my changes now, to look what it was. But what I have found. If this map is map of shared_ptr< int> - it works, if it is map of int* - it raise exception.

Will report details soon


#8


Does it helps?


#9

Not 100% helpful, knowing the text of that exception would be best. However, I have a hunch from the info you gave that if you try:

var j := tmp["ss"]; // reference assignment vs copy assignment.

It will work, as that will prevent the system from attempting a clone() operation