0

I'm now looking at some codes from the CARLA simulator (http://carla.org/).
It exposes many C++ classes and member functions to python using boost python. But I cannot understand a syntax from lines below..

void export_blueprint() {
  using namespace boost::python;
  namespace cc = carla::client;
  namespace crpc = carla::rpc;
...
  class_<cc::ActorBlueprint>("ActorBlueprint", no_init)
    .add_property("id", +[](const cc::ActorBlueprint &self) -> std::string {
      return self.GetId();
    })
    .add_property("tags", &cc::ActorBlueprint::GetTags)
    .def("contains_tag", &cc::ActorBlueprint::ContainsTag)
    .def("match_tags", &cc::ActorBlueprint::MatchTags)
    .def("contains_attribute", &cc::ActorBlueprint::ContainsAttribute)
    .def("get_attribute", +[](const cc::ActorBlueprint &self, const std::string &id) -> cc::ActorAttribute {  
      return self.GetAttribute(id);
    })  // <=== THESE LINES
    .def("set_attribute", &cc::ActorBlueprint::SetAttribute)
    .def("__len__", &cc::ActorBlueprint::size)
    .def("__iter__", range(&cc::ActorBlueprint::begin, &cc::ActorBlueprint::end))
    .def(self_ns::str(self_ns::self))
  ;
}

What does the code below

.def("get_attribute", +[](const cc::ActorBlueprint &self, 
     const std::string &id) -> cc::ActorAttribute { 
      return self.GetAttribute(id);
    })  

mean? It looks like the python class ActorBlueprint 's function get_attribute function is being newly(overriding) defined with new arguments passed from the python interface. I'm almost sure but is there any document about this syntax? I couldn't find one in https://www.boost.org/doc/libs/1_63_0/libs/python/doc/html/tutorial/index.html.

Mike Kinghan
  • 55,740
  • 12
  • 153
  • 182
Chan Kim
  • 5,177
  • 12
  • 57
  • 112
  • 1
    What specifically troubles you here? If it is the "+" sign before the lambda, it is explaned in https://stackoverflow.com/questions/17822131/resolving-ambiguous-overload-on-function-pointer-and-stdfunction-for-a-lambda and https://stackoverflow.com/questions/18889028/a-positive-lambda-what-sorcery-is-this – aparpara Apr 05 '19 at 06:57
  • Ah, I was not aware of that lambda expression in C++ also. thanks! – Chan Kim Apr 05 '19 at 09:04

1 Answers1

0

I`ll explain it element by element.

.def("get_attribute", ...) //method call with arguments

+[](const cc::ActorBlueprint &self, const std::string &id) -> cc::ActorAttribute {...}
// C++ lambda, passed to above method as second argument

return self.GetAttribute(id); // this is the lambda body

You can read about lambda syntax here. Also that + in front of lambda might look strange for you. It is used to trigger a conversion to a plain old function pointer. Read here.

That arrow in lambda -> cc::ActorAttribute specifies return type. It can be used for ordinary functions and methods as well.

This is native c++ syntax, not something boost specific.

As a result of this code, python class ActorBlueprint's method get_attribute will be defined. It will have one string argument and will do what lambda body does (return attribute by id in this case).

Oliort UA
  • 1,568
  • 1
  • 14
  • 31
  • ok I learned about C++ lambda expression today :) about the + sign, I can vaguelly understand but will look again, because I have to go out now. :) – Chan Kim Apr 05 '19 at 09:08