mirror of
https://github.com/pybind/pybind11.git
synced 2026-04-20 06:49:25 +00:00
Fully-automatic clang-format with include reordering (#3713)
* chore: add clang-format
* Removing check-style (Classic check-style)
Ported from @henryiii's 53056b1b0e
* Automatic clang-format changes (NO manual changes).
Co-authored-by: Henry Schreiner <henryschreineriii@gmail.com>
This commit is contained in:
committed by
GitHub
parent
e96221beff
commit
ec24786eab
@@ -8,44 +8,52 @@
|
||||
BSD-style license that can be found in the LICENSE file.
|
||||
*/
|
||||
|
||||
#include "pybind11_tests.h"
|
||||
#include "constructor_stats.h"
|
||||
#include <pybind11/operators.h>
|
||||
#include <pybind11/stl.h>
|
||||
|
||||
#include "constructor_stats.h"
|
||||
#include "pybind11_tests.h"
|
||||
|
||||
#include <algorithm>
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
|
||||
#ifdef PYBIND11_HAS_OPTIONAL
|
||||
#include <optional>
|
||||
#endif // PYBIND11_HAS_OPTIONAL
|
||||
# include <optional>
|
||||
#endif // PYBIND11_HAS_OPTIONAL
|
||||
|
||||
|
||||
template<typename T>
|
||||
template <typename T>
|
||||
class NonZeroIterator {
|
||||
const T* ptr_;
|
||||
const T *ptr_;
|
||||
|
||||
public:
|
||||
explicit NonZeroIterator(const T *ptr) : ptr_(ptr) {}
|
||||
const T& operator*() const { return *ptr_; }
|
||||
NonZeroIterator& operator++() { ++ptr_; return *this; }
|
||||
const T &operator*() const { return *ptr_; }
|
||||
NonZeroIterator &operator++() {
|
||||
++ptr_;
|
||||
return *this;
|
||||
}
|
||||
};
|
||||
|
||||
class NonZeroSentinel {};
|
||||
|
||||
template<typename A, typename B>
|
||||
bool operator==(const NonZeroIterator<std::pair<A, B>>& it, const NonZeroSentinel&) {
|
||||
template <typename A, typename B>
|
||||
bool operator==(const NonZeroIterator<std::pair<A, B>> &it, const NonZeroSentinel &) {
|
||||
return !(*it).first || !(*it).second;
|
||||
}
|
||||
|
||||
/* Iterator where dereferencing returns prvalues instead of references. */
|
||||
template<typename T>
|
||||
template <typename T>
|
||||
class NonRefIterator {
|
||||
const T* ptr_;
|
||||
const T *ptr_;
|
||||
|
||||
public:
|
||||
explicit NonRefIterator(const T *ptr) : ptr_(ptr) {}
|
||||
T operator*() const { return T(*ptr_); }
|
||||
NonRefIterator& operator++() { ++ptr_; return *this; }
|
||||
NonRefIterator &operator++() {
|
||||
++ptr_;
|
||||
return *this;
|
||||
}
|
||||
bool operator==(const NonRefIterator &other) const { return ptr_ == other.ptr_; }
|
||||
};
|
||||
|
||||
@@ -54,17 +62,18 @@ public:
|
||||
explicit NonCopyableInt(int value) : value_(value) {}
|
||||
NonCopyableInt(const NonCopyableInt &) = delete;
|
||||
NonCopyableInt(NonCopyableInt &&other) noexcept : value_(other.value_) {
|
||||
other.value_ = -1; // detect when an unwanted move occurs
|
||||
other.value_ = -1; // detect when an unwanted move occurs
|
||||
}
|
||||
NonCopyableInt &operator=(const NonCopyableInt &) = delete;
|
||||
NonCopyableInt &operator=(NonCopyableInt &&other) noexcept {
|
||||
value_ = other.value_;
|
||||
other.value_ = -1; // detect when an unwanted move occurs
|
||||
other.value_ = -1; // detect when an unwanted move occurs
|
||||
return *this;
|
||||
}
|
||||
int get() const { return value_; }
|
||||
void set(int value) { value_ = value; }
|
||||
~NonCopyableInt() = default;
|
||||
|
||||
private:
|
||||
int value_;
|
||||
};
|
||||
@@ -81,7 +90,9 @@ py::list test_random_access_iterator(PythonType x) {
|
||||
auto checks = py::list();
|
||||
auto assert_equal = [&checks](py::handle a, py::handle b) {
|
||||
auto result = PyObject_RichCompareBool(a.ptr(), b.ptr(), Py_EQ);
|
||||
if (result == -1) { throw py::error_already_set(); }
|
||||
if (result == -1) {
|
||||
throw py::error_already_set();
|
||||
}
|
||||
checks.append(result != 0);
|
||||
};
|
||||
|
||||
@@ -116,7 +127,7 @@ py::list test_random_access_iterator(PythonType x) {
|
||||
|
||||
TEST_SUBMODULE(sequences_and_iterators, m) {
|
||||
// test_sliceable
|
||||
class Sliceable{
|
||||
class Sliceable {
|
||||
public:
|
||||
explicit Sliceable(int n) : size(n) {}
|
||||
int start, stop, step;
|
||||
@@ -130,18 +141,20 @@ TEST_SUBMODULE(sequences_and_iterators, m) {
|
||||
throw py::error_already_set();
|
||||
}
|
||||
int istart = static_cast<int>(start);
|
||||
int istop = static_cast<int>(stop);
|
||||
int istep = static_cast<int>(step);
|
||||
int istop = static_cast<int>(stop);
|
||||
int istep = static_cast<int>(step);
|
||||
return std::make_tuple(istart, istop, istep);
|
||||
});
|
||||
|
||||
m.def("make_forward_slice_size_t", []() { return py::slice(0, -1, 1); });
|
||||
m.def("make_reversed_slice_object", []() { return py::slice(py::none(), py::none(), py::int_(-1)); });
|
||||
m.def("make_reversed_slice_object",
|
||||
[]() { return py::slice(py::none(), py::none(), py::int_(-1)); });
|
||||
#ifdef PYBIND11_HAS_OPTIONAL
|
||||
m.attr("has_optional") = true;
|
||||
m.def("make_reversed_slice_size_t_optional_verbose", []() { return py::slice(std::nullopt, std::nullopt, -1); });
|
||||
// Warning: The following spelling may still compile if optional<> is not present and give wrong answers.
|
||||
// Please use with caution.
|
||||
m.def("make_reversed_slice_size_t_optional_verbose",
|
||||
[]() { return py::slice(std::nullopt, std::nullopt, -1); });
|
||||
// Warning: The following spelling may still compile if optional<> is not present and give
|
||||
// wrong answers. Please use with caution.
|
||||
m.def("make_reversed_slice_size_t_optional", []() { return py::slice({}, {}, -1); });
|
||||
#else
|
||||
m.attr("has_optional") = false;
|
||||
@@ -166,7 +179,7 @@ TEST_SUBMODULE(sequences_and_iterators, m) {
|
||||
print_copy_created(this);
|
||||
// NOLINTNEXTLINE(cppcoreguidelines-prefer-member-initializer)
|
||||
m_data = new float[m_size];
|
||||
memcpy(m_data, s.m_data, sizeof(float)*m_size);
|
||||
memcpy(m_data, s.m_data, sizeof(float) * m_size);
|
||||
}
|
||||
Sequence(Sequence &&s) noexcept : m_size(s.m_size), m_data(s.m_data) {
|
||||
print_move_created(this);
|
||||
@@ -174,14 +187,17 @@ TEST_SUBMODULE(sequences_and_iterators, m) {
|
||||
s.m_data = nullptr;
|
||||
}
|
||||
|
||||
~Sequence() { print_destroyed(this); delete[] m_data; }
|
||||
~Sequence() {
|
||||
print_destroyed(this);
|
||||
delete[] m_data;
|
||||
}
|
||||
|
||||
Sequence &operator=(const Sequence &s) {
|
||||
if (&s != this) {
|
||||
delete[] m_data;
|
||||
m_size = s.m_size;
|
||||
m_data = new float[m_size];
|
||||
memcpy(m_data, s.m_data, sizeof(float)*m_size);
|
||||
memcpy(m_data, s.m_data, sizeof(float) * m_size);
|
||||
}
|
||||
print_copy_assigned(this);
|
||||
return *this;
|
||||
@@ -235,7 +251,7 @@ TEST_SUBMODULE(sequences_and_iterators, m) {
|
||||
size_t size() const { return m_size; }
|
||||
|
||||
const float *begin() const { return m_data; }
|
||||
const float *end() const { return m_data+m_size; }
|
||||
const float *end() const { return m_data + m_size; }
|
||||
|
||||
private:
|
||||
size_t m_size;
|
||||
@@ -303,8 +319,8 @@ TEST_SUBMODULE(sequences_and_iterators, m) {
|
||||
;
|
||||
|
||||
// test_map_iterator
|
||||
// Interface of a map-like object that isn't (directly) an unordered_map, but provides some basic
|
||||
// map-like functionality.
|
||||
// Interface of a map-like object that isn't (directly) an unordered_map, but provides some
|
||||
// basic map-like functionality.
|
||||
class StringMap {
|
||||
public:
|
||||
StringMap() = default;
|
||||
@@ -314,8 +330,10 @@ TEST_SUBMODULE(sequences_and_iterators, m) {
|
||||
void set(const std::string &key, std::string val) { map[key] = std::move(val); }
|
||||
std::string get(const std::string &key) const { return map.at(key); }
|
||||
size_t size() const { return map.size(); }
|
||||
|
||||
private:
|
||||
std::unordered_map<std::string, std::string> map;
|
||||
|
||||
public:
|
||||
decltype(map.cbegin()) begin() const { return map.cbegin(); }
|
||||
decltype(map.cend()) end() const { return map.cend(); }
|
||||
@@ -350,90 +368,115 @@ TEST_SUBMODULE(sequences_and_iterators, m) {
|
||||
class IntPairs {
|
||||
public:
|
||||
explicit IntPairs(std::vector<std::pair<int, int>> data) : data_(std::move(data)) {}
|
||||
const std::pair<int, int>* begin() const { return data_.data(); }
|
||||
const std::pair<int, int> *begin() const { return data_.data(); }
|
||||
// .end() only required for py::make_iterator(self) overload
|
||||
const std::pair<int, int>* end() const { return data_.data() + data_.size(); }
|
||||
const std::pair<int, int> *end() const { return data_.data() + data_.size(); }
|
||||
|
||||
private:
|
||||
std::vector<std::pair<int, int>> data_;
|
||||
};
|
||||
py::class_<IntPairs>(m, "IntPairs")
|
||||
.def(py::init<std::vector<std::pair<int, int>>>())
|
||||
.def("nonzero", [](const IntPairs& s) {
|
||||
return py::make_iterator(NonZeroIterator<std::pair<int, int>>(s.begin()), NonZeroSentinel());
|
||||
}, py::keep_alive<0, 1>())
|
||||
.def("nonzero_keys", [](const IntPairs& s) {
|
||||
return py::make_key_iterator(NonZeroIterator<std::pair<int, int>>(s.begin()), NonZeroSentinel());
|
||||
}, py::keep_alive<0, 1>())
|
||||
.def("nonzero_values", [](const IntPairs& s) {
|
||||
return py::make_value_iterator(NonZeroIterator<std::pair<int, int>>(s.begin()), NonZeroSentinel());
|
||||
}, py::keep_alive<0, 1>())
|
||||
.def(
|
||||
"nonzero",
|
||||
[](const IntPairs &s) {
|
||||
return py::make_iterator(NonZeroIterator<std::pair<int, int>>(s.begin()),
|
||||
NonZeroSentinel());
|
||||
},
|
||||
py::keep_alive<0, 1>())
|
||||
.def(
|
||||
"nonzero_keys",
|
||||
[](const IntPairs &s) {
|
||||
return py::make_key_iterator(NonZeroIterator<std::pair<int, int>>(s.begin()),
|
||||
NonZeroSentinel());
|
||||
},
|
||||
py::keep_alive<0, 1>())
|
||||
.def(
|
||||
"nonzero_values",
|
||||
[](const IntPairs &s) {
|
||||
return py::make_value_iterator(NonZeroIterator<std::pair<int, int>>(s.begin()),
|
||||
NonZeroSentinel());
|
||||
},
|
||||
py::keep_alive<0, 1>())
|
||||
|
||||
// test iterator that returns values instead of references
|
||||
.def("nonref", [](const IntPairs& s) {
|
||||
return py::make_iterator(NonRefIterator<std::pair<int, int>>(s.begin()),
|
||||
NonRefIterator<std::pair<int, int>>(s.end()));
|
||||
}, py::keep_alive<0, 1>())
|
||||
.def("nonref_keys", [](const IntPairs& s) {
|
||||
return py::make_key_iterator(NonRefIterator<std::pair<int, int>>(s.begin()),
|
||||
NonRefIterator<std::pair<int, int>>(s.end()));
|
||||
}, py::keep_alive<0, 1>())
|
||||
.def("nonref_values", [](const IntPairs& s) {
|
||||
return py::make_value_iterator(NonRefIterator<std::pair<int, int>>(s.begin()),
|
||||
NonRefIterator<std::pair<int, int>>(s.end()));
|
||||
}, py::keep_alive<0, 1>())
|
||||
.def(
|
||||
"nonref",
|
||||
[](const IntPairs &s) {
|
||||
return py::make_iterator(NonRefIterator<std::pair<int, int>>(s.begin()),
|
||||
NonRefIterator<std::pair<int, int>>(s.end()));
|
||||
},
|
||||
py::keep_alive<0, 1>())
|
||||
.def(
|
||||
"nonref_keys",
|
||||
[](const IntPairs &s) {
|
||||
return py::make_key_iterator(NonRefIterator<std::pair<int, int>>(s.begin()),
|
||||
NonRefIterator<std::pair<int, int>>(s.end()));
|
||||
},
|
||||
py::keep_alive<0, 1>())
|
||||
.def(
|
||||
"nonref_values",
|
||||
[](const IntPairs &s) {
|
||||
return py::make_value_iterator(NonRefIterator<std::pair<int, int>>(s.begin()),
|
||||
NonRefIterator<std::pair<int, int>>(s.end()));
|
||||
},
|
||||
py::keep_alive<0, 1>())
|
||||
|
||||
// test single-argument make_iterator
|
||||
.def("simple_iterator", [](IntPairs& self) {
|
||||
return py::make_iterator(self);
|
||||
}, py::keep_alive<0, 1>())
|
||||
.def("simple_keys", [](IntPairs& self) {
|
||||
return py::make_key_iterator(self);
|
||||
}, py::keep_alive<0, 1>())
|
||||
.def("simple_values", [](IntPairs& self) {
|
||||
return py::make_value_iterator(self);
|
||||
}, py::keep_alive<0, 1>())
|
||||
.def(
|
||||
"simple_iterator",
|
||||
[](IntPairs &self) { return py::make_iterator(self); },
|
||||
py::keep_alive<0, 1>())
|
||||
.def(
|
||||
"simple_keys",
|
||||
[](IntPairs &self) { return py::make_key_iterator(self); },
|
||||
py::keep_alive<0, 1>())
|
||||
.def(
|
||||
"simple_values",
|
||||
[](IntPairs &self) { return py::make_value_iterator(self); },
|
||||
py::keep_alive<0, 1>())
|
||||
|
||||
// Test iterator with an Extra (doesn't do anything useful, so not used
|
||||
// at runtime, but tests need to be able to compile with the correct
|
||||
// overload. See PR #3293.
|
||||
.def("_make_iterator_extras", [](IntPairs& self) {
|
||||
return py::make_iterator(self, py::call_guard<int>());
|
||||
}, py::keep_alive<0, 1>())
|
||||
.def("_make_key_extras", [](IntPairs& self) {
|
||||
return py::make_key_iterator(self, py::call_guard<int>());
|
||||
}, py::keep_alive<0, 1>())
|
||||
.def("_make_value_extras", [](IntPairs& self) {
|
||||
return py::make_value_iterator(self, py::call_guard<int>());
|
||||
}, py::keep_alive<0, 1>())
|
||||
;
|
||||
.def(
|
||||
"_make_iterator_extras",
|
||||
[](IntPairs &self) { return py::make_iterator(self, py::call_guard<int>()); },
|
||||
py::keep_alive<0, 1>())
|
||||
.def(
|
||||
"_make_key_extras",
|
||||
[](IntPairs &self) { return py::make_key_iterator(self, py::call_guard<int>()); },
|
||||
py::keep_alive<0, 1>())
|
||||
.def(
|
||||
"_make_value_extras",
|
||||
[](IntPairs &self) { return py::make_value_iterator(self, py::call_guard<int>()); },
|
||||
py::keep_alive<0, 1>());
|
||||
|
||||
// test_iterater_referencing
|
||||
py::class_<NonCopyableInt>(m, "NonCopyableInt")
|
||||
.def(py::init<int>())
|
||||
.def("set", &NonCopyableInt::set)
|
||||
.def("__int__", &NonCopyableInt::get)
|
||||
;
|
||||
.def("__int__", &NonCopyableInt::get);
|
||||
py::class_<std::vector<NonCopyableInt>>(m, "VectorNonCopyableInt")
|
||||
.def(py::init<>())
|
||||
.def("append", [](std::vector<NonCopyableInt> &vec, int value) {
|
||||
vec.emplace_back(value);
|
||||
})
|
||||
.def("append",
|
||||
[](std::vector<NonCopyableInt> &vec, int value) { vec.emplace_back(value); })
|
||||
.def("__iter__", [](std::vector<NonCopyableInt> &vec) {
|
||||
return py::make_iterator(vec.begin(), vec.end());
|
||||
})
|
||||
;
|
||||
});
|
||||
py::class_<std::vector<NonCopyableIntPair>>(m, "VectorNonCopyableIntPair")
|
||||
.def(py::init<>())
|
||||
.def("append", [](std::vector<NonCopyableIntPair> &vec, const std::pair<int, int> &value) {
|
||||
vec.emplace_back(NonCopyableInt(value.first), NonCopyableInt(value.second));
|
||||
})
|
||||
.def("keys", [](std::vector<NonCopyableIntPair> &vec) {
|
||||
return py::make_key_iterator(vec.begin(), vec.end());
|
||||
})
|
||||
.def("append",
|
||||
[](std::vector<NonCopyableIntPair> &vec, const std::pair<int, int> &value) {
|
||||
vec.emplace_back(NonCopyableInt(value.first), NonCopyableInt(value.second));
|
||||
})
|
||||
.def("keys",
|
||||
[](std::vector<NonCopyableIntPair> &vec) {
|
||||
return py::make_key_iterator(vec.begin(), vec.end());
|
||||
})
|
||||
.def("values", [](std::vector<NonCopyableIntPair> &vec) {
|
||||
return py::make_value_iterator(vec.begin(), vec.end());
|
||||
})
|
||||
;
|
||||
});
|
||||
|
||||
#if 0
|
||||
// Obsolete: special data structure for exposing custom iterator types to python
|
||||
@@ -511,7 +554,9 @@ TEST_SUBMODULE(sequences_and_iterators, m) {
|
||||
|
||||
// test_iterator_rvp
|
||||
// #388: Can't make iterators via make_iterator() with different r/v policies
|
||||
static std::vector<int> list = { 1, 2, 3 };
|
||||
m.def("make_iterator_1", []() { return py::make_iterator<py::return_value_policy::copy>(list); });
|
||||
m.def("make_iterator_2", []() { return py::make_iterator<py::return_value_policy::automatic>(list); });
|
||||
static std::vector<int> list = {1, 2, 3};
|
||||
m.def("make_iterator_1",
|
||||
[]() { return py::make_iterator<py::return_value_policy::copy>(list); });
|
||||
m.def("make_iterator_2",
|
||||
[]() { return py::make_iterator<py::return_value_policy::automatic>(list); });
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user