MCPcopy Create free account
hub / github.com/davisking/dlib / vector_buffer_impl

Function vector_buffer_impl

dlib/external/pybind11/include/pybind11/stl_bind.h:430–475  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

428// Add the buffer interface to a vector
429template <typename Vector, typename Class_, typename... Args>
430void vector_buffer_impl(Class_ &cl, std::true_type) {
431 using T = typename Vector::value_type;
432
433 static_assert(vector_has_data_and_format<Vector>::value,
434 "There is not an appropriate format descriptor for this vector");
435
436 // numpy.h declares this for arbitrary types, but it may raise an exception and crash hard
437 // at runtime if PYBIND11_NUMPY_DTYPE hasn't been called, so check here
438 format_descriptor<T>::format();
439
440 cl.def_buffer([](Vector &v) -> buffer_info {
441 return buffer_info(v.data(),
442 static_cast<ssize_t>(sizeof(T)),
443 format_descriptor<T>::format(),
444 1,
445 {v.size()},
446 {sizeof(T)});
447 });
448
449 cl.def(init([](const buffer &buf) {
450 auto info = buf.request();
451 if (info.ndim != 1 || info.strides[0] % static_cast<ssize_t>(sizeof(T))) {
452 throw type_error("Only valid 1D buffers can be copied to a vector");
453 }
454 if (!detail::compare_buffer_info<T>::compare(info)
455 || (ssize_t) sizeof(T) != info.itemsize) {
456 throw type_error("Format mismatch (Python: " + info.format
457 + " C++: " + format_descriptor<T>::format() + ")");
458 }
459
460 T *p = static_cast<T *>(info.ptr);
461 ssize_t step = info.strides[0] / static_cast<ssize_t>(sizeof(T));
462 T *end = p + info.shape[0] * step;
463 if (step == 1) {
464 return Vector(p, end);
465 }
466 Vector vec;
467 vec.reserve((size_t) info.shape[0]);
468 for (; p != end; p += step) {
469 vec.push_back(*p);
470 }
471 return vec;
472 }));
473
474 return;
475}
476
477template <typename Vector, typename Class_, typename... Args>
478void vector_buffer_impl(Class_ &, std::false_type) {}

Callers

nothing calls this directly

Calls 7

buffer_infoClass · 0.85
initFunction · 0.85
requestMethod · 0.80
reserveMethod · 0.80
dataMethod · 0.45
sizeMethod · 0.45
push_backMethod · 0.45

Tested by

no test coverage detected