MCPcopy Create free account
hub / github.com/pybind/pybind11 / bind_vector

Function bind_vector

include/pybind11/stl_bind.h:491–570  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

489//
490template <typename Vector, typename holder_type = default_holder_type<Vector>, typename... Args>
491class_<Vector, holder_type> bind_vector(handle scope, std::string const &name, Args &&...args) {
492 using Class_ = class_<Vector, holder_type>;
493
494 // If the value_type is unregistered (e.g. a converting type) or is itself registered
495 // module-local then make the vector binding module-local as well:
496 using vtype = typename Vector::value_type;
497 auto *vtype_info = detail::get_type_info(typeid(vtype));
498 bool local = !vtype_info || vtype_info->module_local;
499
500 Class_ cl(scope, name.c_str(), pybind11::module_local(local), std::forward<Args>(args)...);
501
502 // Declare the buffer interface if a buffer_protocol() is passed in
503 detail::vector_buffer<Vector, Class_, Args...>(cl);
504
505 cl.def(init<>());
506
507 // Register copy constructor (if possible)
508 detail::vector_if_copy_constructible<Vector, Class_>(cl);
509
510 // Register comparison-related operators and functions (if possible)
511 detail::vector_if_equal_operator<Vector, Class_>(cl);
512
513 // Register stream insertion operator (if possible)
514 detail::vector_if_insertion_operator<Vector, Class_>(cl, name);
515
516 // Modifiers require copyable vector value type
517 detail::vector_modifiers<Vector, Class_>(cl);
518
519 // Accessor and iterator; return by value if copyable, otherwise we return by ref + keep-alive
520 detail::vector_accessor<Vector, Class_>(cl);
521
522 cl.def(
523 "__bool__",
524 [](const Vector &v) -> bool { return !v.empty(); },
525 "Check whether the list is nonempty");
526
527 cl.def("__len__", [](const Vector &vec) { return vec.size(); });
528
529#if 0
530 // C++ style functions deprecated, leaving it here as an example
531 cl.def(init<size_type>());
532
533 cl.def("resize",
534 (void (Vector::*) (size_type count)) & Vector::resize,
535 "changes the number of elements stored");
536
537 cl.def("erase",
538 [](Vector &v, SizeType i) {
539 if (i >= v.size())
540 throw index_error();
541 v.erase(v.begin() + i);
542 }, "erases element at index ``i``");
543
544 cl.def("empty", &Vector::empty, "checks whether the container is empty");
545 cl.def("size", &Vector::size, "returns the number of elements");
546 cl.def("push_back", (void (Vector::*)(const T&)) &Vector::push_back, "adds an element to the end");
547 cl.def("pop_back", &Vector::pop_back, "removes the last element");
548

Callers

nothing calls this directly

Calls 4

module_localClass · 0.85
emptyMethod · 0.45
sizeMethod · 0.45
beginMethod · 0.45

Tested by

no test coverage detected