mirror of
https://github.com/pybind/pybind11.git
synced 2024-11-25 06:35:12 +00:00
Fix Clazy warnings (#4988)
* stl.h: Use C++11 range-loops with const reference This saves copy-ctor and dtor for non-trivial types by value Found by clazy (range-loop-reference) * test_smart_ptr.cpp cleanup Introduce `pointer_set<T>` https://github.com/boostorg/unordered/issues/139 > Based on the standard, the first move should leave a in a "valid but unspecified state"; --------- Co-authored-by: Ralf W. Grosse-Kunstleve <rwgk@google.com>
This commit is contained in:
parent
eeac2f4572
commit
976fea05a3
@ -172,7 +172,7 @@ struct list_caster {
|
|||||||
auto s = reinterpret_borrow<sequence>(src);
|
auto s = reinterpret_borrow<sequence>(src);
|
||||||
value.clear();
|
value.clear();
|
||||||
reserve_maybe(s, &value);
|
reserve_maybe(s, &value);
|
||||||
for (auto it : s) {
|
for (const auto &it : s) {
|
||||||
value_conv conv;
|
value_conv conv;
|
||||||
if (!conv.load(it, convert)) {
|
if (!conv.load(it, convert)) {
|
||||||
return false;
|
return false;
|
||||||
@ -247,7 +247,7 @@ public:
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
size_t ctr = 0;
|
size_t ctr = 0;
|
||||||
for (auto it : l) {
|
for (const auto &it : l) {
|
||||||
value_conv conv;
|
value_conv conv;
|
||||||
if (!conv.load(it, convert)) {
|
if (!conv.load(it, convert)) {
|
||||||
return false;
|
return false;
|
||||||
|
@ -103,21 +103,26 @@ private:
|
|||||||
int value;
|
int value;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
std::unordered_set<T *> &pointer_set() {
|
||||||
|
// https://google.github.io/styleguide/cppguide.html#Static_and_Global_Variables
|
||||||
|
static auto singleton = new std::unordered_set<T *>();
|
||||||
|
return *singleton;
|
||||||
|
}
|
||||||
|
|
||||||
// test_unique_nodelete
|
// test_unique_nodelete
|
||||||
// Object with a private destructor
|
// Object with a private destructor
|
||||||
class MyObject4;
|
|
||||||
std::unordered_set<MyObject4 *> myobject4_instances;
|
|
||||||
class MyObject4 {
|
class MyObject4 {
|
||||||
public:
|
public:
|
||||||
explicit MyObject4(int value) : value{value} {
|
explicit MyObject4(int value) : value{value} {
|
||||||
print_created(this);
|
print_created(this);
|
||||||
myobject4_instances.insert(this);
|
pointer_set<MyObject4>().insert(this);
|
||||||
}
|
}
|
||||||
int value;
|
int value;
|
||||||
|
|
||||||
static void cleanupAllInstances() {
|
static void cleanupAllInstances() {
|
||||||
auto tmp = std::move(myobject4_instances);
|
auto tmp = std::move(pointer_set<MyObject4>());
|
||||||
myobject4_instances.clear();
|
pointer_set<MyObject4>().clear();
|
||||||
for (auto *o : tmp) {
|
for (auto *o : tmp) {
|
||||||
delete o;
|
delete o;
|
||||||
}
|
}
|
||||||
@ -125,7 +130,7 @@ public:
|
|||||||
|
|
||||||
private:
|
private:
|
||||||
~MyObject4() {
|
~MyObject4() {
|
||||||
myobject4_instances.erase(this);
|
pointer_set<MyObject4>().erase(this);
|
||||||
print_destroyed(this);
|
print_destroyed(this);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
@ -133,19 +138,17 @@ private:
|
|||||||
// test_unique_deleter
|
// test_unique_deleter
|
||||||
// Object with std::unique_ptr<T, D> where D is not matching the base class
|
// Object with std::unique_ptr<T, D> where D is not matching the base class
|
||||||
// Object with a protected destructor
|
// Object with a protected destructor
|
||||||
class MyObject4a;
|
|
||||||
std::unordered_set<MyObject4a *> myobject4a_instances;
|
|
||||||
class MyObject4a {
|
class MyObject4a {
|
||||||
public:
|
public:
|
||||||
explicit MyObject4a(int i) : value{i} {
|
explicit MyObject4a(int i) : value{i} {
|
||||||
print_created(this);
|
print_created(this);
|
||||||
myobject4a_instances.insert(this);
|
pointer_set<MyObject4a>().insert(this);
|
||||||
};
|
};
|
||||||
int value;
|
int value;
|
||||||
|
|
||||||
static void cleanupAllInstances() {
|
static void cleanupAllInstances() {
|
||||||
auto tmp = std::move(myobject4a_instances);
|
auto tmp = std::move(pointer_set<MyObject4a>());
|
||||||
myobject4a_instances.clear();
|
pointer_set<MyObject4a>().clear();
|
||||||
for (auto *o : tmp) {
|
for (auto *o : tmp) {
|
||||||
delete o;
|
delete o;
|
||||||
}
|
}
|
||||||
@ -153,7 +156,7 @@ public:
|
|||||||
|
|
||||||
protected:
|
protected:
|
||||||
virtual ~MyObject4a() {
|
virtual ~MyObject4a() {
|
||||||
myobject4a_instances.erase(this);
|
pointer_set<MyObject4a>().erase(this);
|
||||||
print_destroyed(this);
|
print_destroyed(this);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
Loading…
Reference in New Issue
Block a user