Skip to content

Commit

Permalink
Used C++20 default comparisons where possible
Browse files Browse the repository at this point in the history
  • Loading branch information
trueqbit committed Jan 29, 2025
1 parent 6feafc2 commit a162bfc
Show file tree
Hide file tree
Showing 14 changed files with 80 additions and 24 deletions.
2 changes: 2 additions & 0 deletions dev/arg_values.h
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,11 @@ namespace sqlite_orm {
return &other.container == &this->container && other.index == this->index;
}

#ifndef SQLITE_ORM_DEFAULT_COMPARISONS_SUPPORTED
bool operator!=(const iterator& other) const {
return !(*this == other);
}
#endif

private:
const arg_values& container;
Expand Down
2 changes: 2 additions & 0 deletions include/sqlite_orm/sqlite_orm.h
Original file line number Diff line number Diff line change
Expand Up @@ -17189,9 +17189,11 @@ namespace sqlite_orm {
return &other.container == &this->container && other.index == this->index;
}

#ifndef SQLITE_ORM_DEFAULT_COMPARISONS_SUPPORTED
bool operator!=(const iterator& other) const {
return !(*this == other);
}
#endif

private:
const arg_values& container;
Expand Down
12 changes: 8 additions & 4 deletions tests/backup_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,15 @@ namespace {
User() = default;
User(int id, std::string name) : id{id}, name{std::move(name)} {}
#endif
};

bool operator==(const User& lhs, const User& rhs) {
return lhs.id == rhs.id && lhs.name == rhs.name;
}
#ifdef SQLITE_ORM_DEFAULT_COMPARISONS_SUPPORTED
friend bool operator==(const User&, const User&) = default;
#else
friend bool operator==(const User& lhs, const User& rhs) {
return lhs.id == rhs.id && lhs.name == rhs.name;
}
#endif
};

struct MarvelHero {
int id;
Expand Down
4 changes: 4 additions & 0 deletions tests/iterate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,13 @@ TEST_CASE("Iterate mapped") {
int64_t id;
std::vector<char> key;

#ifdef SQLITE_ORM_DEFAULT_COMPARISONS_SUPPORTED
bool operator==(const Test&) const = default;
#else
bool operator==(const Test& rhs) const {
return this->id == rhs.id && this->key == rhs.key;
}
#endif
};

auto db =
Expand Down
4 changes: 4 additions & 0 deletions tests/operators/in.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,13 @@ TEST_CASE("In") {
User(int id) : id{id} {}
#endif

#ifdef SQLITE_ORM_DEFAULT_COMPARISONS_SUPPORTED
bool operator==(const User& other) const = default;
#else
bool operator==(const User& other) const {
return this->id == other.id;
}
#endif

std::ostream& operator<<(std::ostream& os) const {
return os << this->id;
Expand Down
20 changes: 12 additions & 8 deletions tests/prepared_statement_tests/prepared_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,18 @@ namespace PreparedStatementTests {
User() = default;
User(int id, std::string name) : id{id}, name{std::move(name)} {}
#endif

#ifdef SQLITE_ORM_DEFAULT_COMPARISONS_SUPPORTED
friend bool operator==(const User&, const User&) = default;
#else
friend bool operator==(const User& lhs, const User& rhs) {
return lhs.id == rhs.id && lhs.name == rhs.name;
}

friend bool operator!=(const User& lhs, const User& rhs) {
return !(lhs == rhs);
}
#endif
};

struct Visit {
Expand All @@ -40,14 +52,6 @@ namespace PreparedStatementTests {
#endif
};

inline bool operator==(const User& lhs, const User& rhs) {
return lhs.id == rhs.id && lhs.name == rhs.name;
}

inline bool operator!=(const User& lhs, const User& rhs) {
return !(lhs == rhs);
}

inline void testSerializing(const sqlite_orm::internal::prepared_statement_base& statement) {
auto sql = statement.sql();
std::ignore = sql;
Expand Down
6 changes: 6 additions & 0 deletions tests/prepared_statement_tests/select.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -373,11 +373,17 @@ TEST_CASE("Prepared select") {
decltype(User::id) id = 0;
decltype(User::name) name;

#ifndef SQLITE_ORM_AGGREGATE_PAREN_INIT_SUPPORTED
Z(decltype(id) id, decltype(name) name) : id{id}, name{std::move(name)} {}
#endif

#ifdef SQLITE_ORM_DEFAULT_COMPARISONS_SUPPORTED
bool operator==(const Z&) const = default;
#else
bool operator==(const Z& z) const {
return this->id == z.id && this->name == z.name;
}
#endif
};
constexpr auto z_struct = struct_<Z>(&User::id, &User::name);
auto statement = storage.prepare(select(z_struct));
Expand Down
4 changes: 4 additions & 0 deletions tests/schema/virtual_table.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,13 @@ TEST_CASE("virtual table") {
std::string title;
std::string body;

#ifdef SQLITE_ORM_DEFAULT_COMPARISONS_SUPPORTED
bool operator==(const Post&) const = default;
#else
bool operator==(const Post& other) const {
return this->title == other.title && this->body == other.body;
}
#endif
};

/// CREATE VIRTUAL TABLE posts
Expand Down
8 changes: 8 additions & 0 deletions tests/select_constraints_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,14 @@ namespace {
std::string address; // optional
double salary; // optional

#ifdef SQLITE_ORM_DEFAULT_COMPARISONS_SUPPORTED
bool operator==(const Employee&) const = default;
#else
bool operator==(const Employee& other) const {
return this->id == other.id && this->name == other.name && this->age == other.age &&
this->address == other.address && this->salary == other.salary;
}
#endif
};
}
TEST_CASE("select constraints") {
Expand Down Expand Up @@ -428,9 +432,13 @@ namespace {
User4(int id, std::string firstName) : id{id}, firstName{std::move(firstName)} {}
#endif

#ifdef SQLITE_ORM_DEFAULT_COMPARISONS_SUPPORTED
bool operator==(const User4&) const = default;
#else
bool operator==(const User4& user) const {
return this->id == user.id && this->firstName == user.firstName;
}
#endif
};
}
TEST_CASE("collate") {
Expand Down
4 changes: 4 additions & 0 deletions tests/storage_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -460,10 +460,14 @@ TEST_CASE("insert with generated column") {
name{std::move(name)}, price{price}, discount{discount}, tax{tax}, netPrice{netPrice} {}
#endif

#ifdef SQLITE_ORM_DEFAULT_COMPARISONS_SUPPORTED
bool operator==(const Product&) const = default;
#else
bool operator==(const Product& other) const {
return this->name == other.name && this->price == other.price && this->discount == other.discount &&
this->tax == other.tax && this->netPrice == other.netPrice;
}
#endif
};
auto storage =
make_storage({},
Expand Down
8 changes: 6 additions & 2 deletions tests/sync_schema_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ TEST_CASE("Sync schema") {
int id;
std::string name;

bool operator==(const UserBefore& userBefore) const {
return this->id == userBefore.id && this->name == userBefore.name;
bool operator==(const UserBefore& before) const {
return this->id == before.id && this->name == before.name;
}
};

Expand Down Expand Up @@ -497,9 +497,13 @@ TEST_CASE("sync_schema with generated columns") {
User(int id, int hash = 0) : id{id}, hash{hash} {}
#endif

#ifdef SQLITE_ORM_DEFAULT_COMPARISONS_SUPPORTED
bool operator==(const User& other) const = default;
#else
bool operator==(const User& other) const {
return this->id == other.id && this->hash == other.hash;
}
#endif
};
auto storagePath = "sync_schema_with_generated.sqlite";
std::remove(storagePath);
Expand Down
4 changes: 4 additions & 0 deletions tests/transaction_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,13 @@ namespace {
Object(int id, std::string name) : id{id}, name{std::move(name)} {}
#endif

#ifdef SQLITE_ORM_DEFAULT_COMPARISONS_SUPPORTED
bool operator==(const Object&) const = default;
#else
bool operator==(const Object& other) const {
return this->id == other.id && this->name == other.name;
}
#endif
};
}

Expand Down
12 changes: 8 additions & 4 deletions tests/unique_cases/get_all_with_two_tables.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,15 @@ namespace {
Item() = default;
Item(int id, std::string attributes) : id{id}, attributes{std::move(attributes)} {}
#endif
};

inline bool operator==(const Item& lhs, const Item& rhs) {
return lhs.id == rhs.id && lhs.attributes == rhs.attributes;
}
#ifdef SQLITE_ORM_DEFAULT_COMPARISONS_SUPPORTED
friend bool operator==(const Item&, const Item&) = default;
#else
friend bool operator==(const Item& lhs, const Item& rhs) {
return lhs.id == rhs.id && lhs.attributes == rhs.attributes;
}
#endif
};
}

TEST_CASE("get_all with two tables") {
Expand Down
14 changes: 8 additions & 6 deletions tests/unique_cases/issue663.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,16 +65,18 @@ namespace {

namespace {
struct User1 {
bool operator==(const User1& rhs) const {
return std::tie(id, name, age, email) == std::tie(rhs.id, rhs.name, rhs.age, rhs.email);
}
bool operator!=(const User1& rhs) const {
return !(rhs == *this);
}
int id;
std::string name;
int age;
std::string email;

#ifdef SQLITE_ORM_DEFAULT_COMPARISONS_SUPPORTED
bool operator==(const User1&) const = default;
#else
bool operator==(const User1& rhs) const {
return std::tie(id, name, age, email) == std::tie(rhs.id, rhs.name, rhs.age, rhs.email);
}
#endif
};
}

Expand Down

0 comments on commit a162bfc

Please sign in to comment.