C++: use default operator==

Since we now use C++20, we can simplify our code a bit
This commit is contained in:
Olivier Goffart 2022-01-24 15:46:39 +01:00 committed by Olivier Goffart
parent 36e07b8801
commit 319704d3fb
8 changed files with 13 additions and 75 deletions

View file

@ -352,7 +352,12 @@ fn gen_corelib(
config.export.body.insert( config.export.body.insert(
"LayoutInfo".to_owned(), "LayoutInfo".to_owned(),
" inline LayoutInfo merge(const LayoutInfo &other) const; " inline LayoutInfo merge(const LayoutInfo &other) const;
friend inline LayoutInfo operator+(const LayoutInfo &a, const LayoutInfo &b) { return a.merge(b); }".into(), friend inline LayoutInfo operator+(const LayoutInfo &a, const LayoutInfo &b) { return a.merge(b); }
friend bool operator==(const LayoutInfo&, const LayoutInfo&) = default;".into(),
);
config.export.body.insert(
"StandardListViewItem".to_owned(),
"friend bool operator==(const StandardListViewItem&, const StandardListViewItem&) = default;".into(),
); );
config config
.export .export

View file

@ -54,6 +54,7 @@ using ItemTreeNode = cbindgen_private::ItemTreeNode<uint8_t>;
using cbindgen_private::KeyboardModifiers; using cbindgen_private::KeyboardModifiers;
using cbindgen_private::KeyEvent; using cbindgen_private::KeyEvent;
using cbindgen_private::PointerEvent; using cbindgen_private::PointerEvent;
using cbindgen_private::StandardListViewItem;
/// Internal function that checks that the API that must be called from the main /// Internal function that checks that the API that must be called from the main
/// thread is indeed called from the main thread, or abort the program otherwise /// thread is indeed called from the main thread, or abort the program otherwise
@ -393,18 +394,6 @@ inline LayoutInfo LayoutInfo::merge(const LayoutInfo &other) const
std::max(preferred, other.preferred), std::max(preferred, other.preferred),
std::min(stretch, other.stretch) }; std::min(stretch, other.stretch) };
} }
/// FIXME! this should be done by cbindgen
inline bool operator==(const LayoutInfo &a, const LayoutInfo &b)
{
return a.min == b.min && a.max == b.max && a.min_percent == b.min_percent
&& a.max_percent == b.max_percent && a.preferred == b.preferred
&& a.stretch == b.stretch;
}
inline bool operator!=(const LayoutInfo &a, const LayoutInfo &b)
{
return !(a == b);
}
} }
namespace private_api { namespace private_api {
@ -821,20 +810,6 @@ cbindgen_private::NativeStyleMetrics::~NativeStyleMetrics()
} }
#endif // !defined(DOXYGEN) #endif // !defined(DOXYGEN)
using cbindgen_private::StandardListViewItem;
namespace cbindgen_private {
inline bool operator==(const StandardListViewItem &a, const StandardListViewItem &b)
{
static_assert(sizeof(StandardListViewItem) == sizeof(std::tuple<SharedString>),
"must update to cover all fields");
return a.text == b.text;
}
inline bool operator!=(const StandardListViewItem &a, const StandardListViewItem &b)
{
return !(a == b);
}
}
namespace private_api { namespace private_api {
// Code generated by SixtyFPS <= 0.1.5 uses this enum with VersionCheckHelper // Code generated by SixtyFPS <= 0.1.5 uses this enum with VersionCheckHelper
enum class [[deprecated]] VersionCheck { Major = SIXTYFPS_VERSION_MAJOR, enum class [[deprecated]] VersionCheck { Major = SIXTYFPS_VERSION_MAJOR,

View file

@ -149,15 +149,7 @@ public:
/// Returns true if \a lhs has the same values for the individual color channels as \a rhs; /// Returns true if \a lhs has the same values for the individual color channels as \a rhs;
/// false otherwise. /// false otherwise.
friend bool operator==(const Color &lhs, const Color &rhs) friend bool operator==(const Color &lhs, const Color &rhs) = default;
{
return lhs.inner.red == rhs.inner.red && lhs.inner.green == rhs.inner.green
&& lhs.inner.blue == rhs.inner.blue && lhs.inner.alpha == rhs.inner.alpha;
}
/// Returns true if \a lhs has any different values for the individual color channels as \a rhs;
/// false otherwise.
friend bool operator!=(const Color &lhs, const Color &rhs) { return !(lhs == rhs); }
/// Writes the \a color to the specified \a stream and returns a reference to the /// Writes the \a color to the specified \a stream and returns a reference to the
/// stream. /// stream.

View file

@ -400,12 +400,6 @@ public:
{ {
return cbindgen_private::sixtyfps_interpreter_value_eq(&a.inner, &b.inner); return cbindgen_private::sixtyfps_interpreter_value_eq(&a.inner, &b.inner);
} }
/// Returns true if \a and \b hold values of the same type and the underlying vales are not
/// equal.
friend bool operator!=(const Value &a, const Value &b)
{
return !cbindgen_private::sixtyfps_interpreter_value_eq(&a.inner, &b.inner);
}
private: private:
inline Value(const void *) = delete; // Avoid that for example Value("foo") turns to Value(bool) inline Value(const void *) = delete; // Avoid that for example Value("foo") turns to Value(bool)

View file

@ -42,23 +42,7 @@ public:
{ {
} }
friend bool operator==(const PathData &a, const PathData &b) friend bool operator==(const PathData &a, const PathData &b) = default;
{
if (a.data.tag != b.data.tag)
return false;
switch (a.data.tag) {
case cbindgen_private::types::PathData::Tag::Elements:
return a.data.elements._0 == b.data.elements._0;
case cbindgen_private::types::PathData::Tag::Events:
return a.data.events._0 == b.data.events._0 && b.data.events._0 == b.data.events._0;
case cbindgen_private::types::PathData::Tag::Commands:
return a.data.commands._0 == b.data.commands._0;
case cbindgen_private::types::PathData::Tag::None:
return true;
}
return false; // unreachable
}
friend bool operator!=(const PathData &a, const PathData &b) { return !(a == b); }
private: private:
static SharedVector<PathElement> elements_from_array(const PathElement *firstElement, static SharedVector<PathElement> elements_from_array(const PathElement *firstElement,

View file

@ -415,7 +415,7 @@ PropertyAnimation := _ {
} }
export struct StandardListViewItem := { export struct StandardListViewItem := {
//-name:sixtyfps::StandardListViewItem //-name:sixtyfps::private_api::StandardListViewItem
text: string text: string
} }

View file

@ -565,11 +565,9 @@ pub fn generate(doc: &Document) -> impl std::fmt::Display {
} }
fn generate_struct(file: &mut File, name: &str, fields: &BTreeMap<String, Type>) { fn generate_struct(file: &mut File, name: &str, fields: &BTreeMap<String, Type>) {
let mut operator_eq = String::new();
let mut members = fields let mut members = fields
.iter() .iter()
.map(|(name, t)| { .map(|(name, t)| {
write!(operator_eq, " && a.{0} == b.{0}", ident(name)).unwrap();
( (
Access::Public, Access::Public,
Declaration::Var(Var { Declaration::Var(Var {
@ -588,22 +586,13 @@ fn generate_struct(file: &mut File, name: &str, fields: &BTreeMap<String, Type>)
Access::Public, Access::Public,
Declaration::Function(Function { Declaration::Function(Function {
name: "operator==".to_owned(), name: "operator==".to_owned(),
signature: format!("(const {0} &a, const {0} &b) -> bool", name), signature: format!("(const {0} &a, const {0} &b) -> bool = default", name),
is_friend: true, is_friend: true,
statements: Some(vec![format!("return true{};", operator_eq)]), statements: None,
..Function::default()
}),
));
members.push((
Access::Public,
Declaration::Function(Function {
name: "operator!=".to_owned(),
signature: format!("(const {0} &a, const {0} &b) -> bool", name),
is_friend: true,
statements: Some(vec!["return !(a == b);".into()]),
..Function::default() ..Function::default()
}), }),
)); ));
file.declarations.push(Declaration::Struct(Struct { file.declarations.push(Declaration::Struct(Struct {
name: name.into(), name: name.into(),
members, members,

View file

@ -19,7 +19,6 @@ pub enum Orientation {
type Coord = f32; type Coord = f32;
/// The constraint that applies to an item /// The constraint that applies to an item
// NOTE: when adding new fields, the C++ operator== also need updates
// Also, the field needs to be in alphabetical order because how the generated code sort fields for struct // Also, the field needs to be in alphabetical order because how the generated code sort fields for struct
#[repr(C)] #[repr(C)]
#[derive(Clone, Copy, Debug, PartialEq)] #[derive(Clone, Copy, Debug, PartialEq)]