Skip to content

Commit

Permalink
cxx-qt-lib: use qint64 type to remove C++ methods
Browse files Browse the repository at this point in the history
  • Loading branch information
ahayzen-kdab committed Oct 11, 2024
1 parent 824aa44 commit 79cc8fe
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 213 deletions.
4 changes: 1 addition & 3 deletions crates/cxx-qt-lib/include/core/qdate.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,14 @@
namespace rust {
namespace cxxqtlib1 {

QDate
qdateAddDays(const QDate& date, ::std::int64_t ndays);
QDate
qdateCurrentDate();
QDate
qdateFromString(const QString& string, const QString& format);
QDate
qdateFromString(const QString& string, Qt::DateFormat format);
// In Qt 5 d is const-ref, in Qt 6 it is value
::std::int64_t
qint64
qdateDaysTo(const QDate& date, QDate d);
bool
qdateIsLeapYear(::std::int32_t year);
Expand Down
28 changes: 4 additions & 24 deletions crates/cxx-qt-lib/include/core/qdatetime.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,44 +28,24 @@ struct IsRelocatable<QDateTime> : ::std::true_type
namespace rust {
namespace cxxqtlib1 {

QDateTime
qdatetimeAddDays(const QDateTime& datetime, ::std::int64_t ndays);
QDateTime
qdatetimeAddMSecs(const QDateTime& datetime, ::std::int64_t msecs);
QDateTime
qdatetimeAddSecs(const QDateTime& datetime, ::std::int64_t secs);
QDateTime
qdatetimeCurrentDateTime();
QDateTime
qdatetimeCurrentDateTimeUtc();
::std::int64_t
qint64
qdatetimeCurrentMSecsSinceEpoch();
::std::int64_t
qint64
qdatetimeCurrentSecsSinceEpoch();
::std::int64_t
qdatetimeDaysTo(const QDateTime& datetime, const QDateTime& other);
QDateTime
qdatetimeFromMSecsSinceEpoch(::std::int64_t msecs, const QTimeZone& timeZone);
qdatetimeFromMSecsSinceEpoch(qint64 msecs, const QTimeZone& timeZone);
QDateTime
qdatetimeFromSecsSinceEpoch(::std::int64_t secs, const QTimeZone& timeZone);
::std::int64_t
qdatetimeMSecsTo(const QDateTime& datetime, const QDateTime& other);
::std::int64_t
qdatetimeSecsTo(const QDateTime& datetime, const QDateTime& other);
qdatetimeFromSecsSinceEpoch(qint64 secs, const QTimeZone& timeZone);
void
qdatetimeSetDate(QDateTime& datetime, QDate date);
void
qdatetimeSetMSecsSinceEpoch(QDateTime& datetime, ::std::int64_t msecs);
void
qdatetimeSetSecsSinceEpoch(QDateTime& datetime, ::std::int64_t secs);
void
qdatetimeSetTime(QDateTime& datetime, QTime time);
::std::unique_ptr<QTimeZone>
qdatetimeTimeZone(const QDateTime& datetime);
::std::int64_t
qdatetimeToMSecsSinceEpoch(const QDateTime& datetime);
::std::int64_t
qdatetimeToSecsSinceEpoch(const QDateTime& datetime);
void
qdatetimeSetTimeZone(QDateTime& datetime, const QTimeZone& timeZone);
}
Expand Down
10 changes: 2 additions & 8 deletions crates/cxx-qt-lib/src/core/qdate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,23 +21,17 @@ static_assert(::std::is_trivially_copyable<QDate>::value,
namespace rust {
namespace cxxqtlib1 {

QDate
qdateAddDays(const QDate& date, ::std::int64_t ndays)
{
return date.addDays(static_cast<qint64>(ndays));
}

QDate
qdateCurrentDate()
{
return QDate::currentDate();
}

::std::int64_t
qint64
qdateDaysTo(const QDate& date, QDate d)
{
// In Qt 5 d is const-ref, in Qt 6 it is value
return static_cast<::std::int64_t>(date.daysTo(d));
return date.daysTo(d);
}

QDate
Expand Down
33 changes: 17 additions & 16 deletions crates/cxx-qt-lib/src/core/qdate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,25 @@ mod ffi {
type DateFormat = crate::DateFormat;
}

unsafe extern "C++" {
include!("cxx-qt-lib/qtypes.h");
#[cxx_name = "qint64"]
type QInt64 = crate::QInt64;
}

unsafe extern "C++" {
include!("cxx-qt-lib/qstring.h");
type QString = crate::QString;

include!("cxx-qt-lib/qdate.h");
type QDate = super::QDate;

/// Returns a QDate object containing a date ndays later than the date of this object (or earlier if ndays is negative).
///
/// Returns a null date if the current date is invalid or the new date is out of range.
#[rust_name = "add_days"]
fn addDays(self: &QDate, ndays: QInt64) -> QDate;

/// Returns a QDate object containing a date nmonths later than the date of this object (or earlier if nmonths is negative).
#[rust_name = "add_months"]
fn addMonths(self: &QDate, nmonths: i32) -> QDate;
Expand Down Expand Up @@ -76,17 +88,13 @@ mod ffi {

#[namespace = "rust::cxxqtlib1"]
unsafe extern "C++" {
#[doc(hidden)]
#[rust_name = "qdate_add_days"]
fn qdateAddDays(date: &QDate, ndays: i64) -> QDate;

#[doc(hidden)]
#[rust_name = "qdate_current_date"]
fn qdateCurrentDate() -> QDate;

#[doc(hidden)]
#[rust_name = "qdate_days_to"]
fn qdateDaysTo(date: &QDate, d: QDate) -> i64;
fn qdateDaysTo(date: &QDate, d: QDate) -> QInt64;

#[doc(hidden)]
#[rust_name = "qdate_from_string"]
Expand Down Expand Up @@ -147,13 +155,6 @@ impl fmt::Debug for QDate {
}

impl QDate {
/// Returns a QDate object containing a date ndays later than the date of this object (or earlier if ndays is negative).
///
/// Returns a null date if the current date is invalid or the new date is out of range.
pub fn add_days(&self, ndays: i64) -> Self {
ffi::qdate_add_days(self, ndays)
}

// Returns the current date, as reported by the system clock.
pub fn current_date() -> Self {
ffi::qdate_current_date()
Expand All @@ -162,7 +163,7 @@ impl QDate {
/// Returns the number of days from this date to d (which is negative if d is earlier than this date).
///
/// Returns 0 if either date is invalid.
pub fn days_to(&self, date: Self) -> i64 {
pub fn days_to(&self, date: Self) -> ffi::QInt64 {
ffi::qdate_days_to(self, date)
}

Expand Down Expand Up @@ -271,15 +272,15 @@ mod test {
#[test]
fn qdate_current_date() {
let date_a = QDate::current_date();
let date_b = date_a.add_days(100);
assert_eq!(date_a.days_to(date_b), 100);
let date_b = date_a.add_days(100.into());
assert_eq!(i64::from(date_a.days_to(date_b)), 100);
}

#[test]
fn qdate_julian_day() {
let date_a = QDate::from_julian_day(1000);
let date_b = QDate::from_julian_day(1010);
assert_eq!(date_a.days_to(date_b), 10);
assert_eq!(i64::from(date_a.days_to(date_b)), 10);
}

#[cfg(feature = "chrono")]
Expand Down
74 changes: 6 additions & 68 deletions crates/cxx-qt-lib/src/core/qdatetime.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,24 +27,6 @@ static_assert(QTypeInfo<QDateTime>::isRelocatable);
namespace rust {
namespace cxxqtlib1 {

QDateTime
qdatetimeAddDays(const QDateTime& datetime, ::std::int64_t ndays)
{
return datetime.addDays(static_cast<qint64>(ndays));
}

QDateTime
qdatetimeAddMSecs(const QDateTime& datetime, ::std::int64_t msecs)
{
return datetime.addMSecs(static_cast<qint64>(msecs));
}

QDateTime
qdatetimeAddSecs(const QDateTime& datetime, ::std::int64_t secs)
{
return datetime.addSecs(static_cast<qint64>(secs));
}

QDateTime
qdatetimeCurrentDateTime()
{
Expand All @@ -57,48 +39,28 @@ qdatetimeCurrentDateTimeUtc()
return QDateTime::currentDateTimeUtc();
}

::std::int64_t
qint64
qdatetimeCurrentMSecsSinceEpoch()
{
return QDateTime::currentMSecsSinceEpoch();
}

::std::int64_t
qint64
qdatetimeCurrentSecsSinceEpoch()
{
return QDateTime::currentSecsSinceEpoch();
}

::std::int64_t
qdatetimeDaysTo(const QDateTime& datetime, const QDateTime& other)
{
return static_cast<::std::int64_t>(datetime.daysTo(other));
}

QDateTime
qdatetimeFromMSecsSinceEpoch(::std::int64_t msecs, const QTimeZone& timeZone)
qdatetimeFromMSecsSinceEpoch(qint64 msecs, const QTimeZone& timeZone)
{
return QDateTime::fromMSecsSinceEpoch(static_cast<qint64>(msecs), timeZone);
return QDateTime::fromMSecsSinceEpoch(msecs, timeZone);
}

QDateTime
qdatetimeFromSecsSinceEpoch(::std::int64_t secs, const QTimeZone& timeZone)
qdatetimeFromSecsSinceEpoch(qint64 secs, const QTimeZone& timeZone)
{
return QDateTime::fromSecsSinceEpoch(static_cast<qint64>(secs), timeZone);
}

::std::int64_t
qdatetimeMSecsTo(const QDateTime& datetime, const QDateTime& other)
{

return static_cast<::std::int64_t>(datetime.msecsTo(other));
}

::std::int64_t
qdatetimeSecsTo(const QDateTime& datetime, const QDateTime& other)
{

return static_cast<::std::int64_t>(datetime.secsTo(other));
return QDateTime::fromSecsSinceEpoch(secs, timeZone);
}

void
Expand All @@ -107,18 +69,6 @@ qdatetimeSetDate(QDateTime& datetime, QDate date)
datetime.setDate(date);
}

void
qdatetimeSetMSecsSinceEpoch(QDateTime& datetime, ::std::int64_t msecs)
{
datetime.setMSecsSinceEpoch(static_cast<qint64>(msecs));
}

void
qdatetimeSetSecsSinceEpoch(QDateTime& datetime, ::std::int64_t secs)
{
datetime.setSecsSinceEpoch(static_cast<qint64>(secs));
}

void
qdatetimeSetTime(QDateTime& datetime, QTime time)
{
Expand All @@ -131,18 +81,6 @@ qdatetimeTimeZone(const QDateTime& datetime)
return ::std::make_unique<QTimeZone>(datetime.timeZone());
}

::std::int64_t
qdatetimeToMSecsSinceEpoch(const QDateTime& datetime)
{
return static_cast<::std::int64_t>(datetime.toMSecsSinceEpoch());
}

::std::int64_t
qdatetimeToSecsSinceEpoch(const QDateTime& datetime)
{
return static_cast<::std::int64_t>(datetime.toSecsSinceEpoch());
}

void
qdatetimeSetTimeZone(QDateTime& datetime, const QTimeZone& timeZone)
{
Expand Down
Loading

0 comments on commit 79cc8fe

Please sign in to comment.