<div dir="ltr">Hi Laurent,<br><br>On Wed, Jan 23, 2019 at 4:28 PM Laurent Pinchart <<a href="mailto:laurent.pinchart@ideasonboard.com">laurent.pinchart@ideasonboard.com</a>> wrote:<br>><br>> Implement additions and subtraction of struct timespec through<br>> non-member operators to simplify timespec handling.<br><br>IIUC, operator overloading on types of other libs is banned by the style guide :Q<br><a href="https://google.github.io/styleguide/cppguide.html#Operator_Overloading">https://google.github.io/styleguide/cppguide.html#Operator_Overloading</a><br><br>><br>> Signed-off-by: Laurent Pinchart <<a href="mailto:laurent.pinchart@ideasonboard.com">laurent.pinchart@ideasonboard.com</a>><br>> ---<br>><br>> I wrote this code while working on EINTR handling for the event<br>> dispatcher, and later refactored the implementation in a way that<br>> doesn't make use of these operators anymore. I thought they could still<br>> be useful as reference for later use. This patch is thus not meant to be<br>> merged now, but can be picked up later if anyone needs it.<br>><br>>  src/libcamera/include/utils.h |  6 +++++<br>>  src/libcamera/meson.build     |  1 +<br>>  src/libcamera/utils.cpp       | 46 +++++++++++++++++++++++++++++++++++<br>>  3 files changed, 53 insertions(+)<br>>  create mode 100644 src/libcamera/utils.cpp<br>><br>> diff --git a/src/libcamera/include/utils.h b/src/libcamera/include/utils.h<br>> index 73fa2e69b029..7df20976f36f 100644<br>> --- a/src/libcamera/include/utils.h<br>> +++ b/src/libcamera/include/utils.h<br>> @@ -8,6 +8,7 @@<br>>  #define __LIBCAMERA_UTILS_H__<br>><br>>  #include <memory><br>> +#include <time.h><br>><br>>  #define ARRAY_SIZE(a)  (sizeof(a) / sizeof(a[0]))<br>><br>> @@ -24,6 +25,11 @@ std::unique_ptr<T> make_unique(Args&&... args)<br>><br>>  } /* namespace utils */<br>><br>> +struct timespec &operator+=(struct timespec &lhs, const struct timespec &rhs);<br>> +struct timespec operator+(struct timespec lhs, const struct timespec &rhs);<br>> +struct timespec &operator-=(struct timespec &lhs, const struct timespec &rhs);<br>> +struct timespec operator-(struct timespec lhs, const struct timespec &rhs);<br>> +<br>>  } /* namespace libcamera */<br>><br>>  #endif /* __LIBCAMERA_UTILS_H__ */<br>> diff --git a/src/libcamera/meson.build b/src/libcamera/meson.build<br>> index f9f25c0ecf15..72d4a194e19a 100644<br>> --- a/src/libcamera/meson.build<br>> +++ b/src/libcamera/meson.build<br>> @@ -11,6 +11,7 @@ libcamera_sources = files([<br>>      'pipeline_handler.cpp',<br>>      'signal.cpp',<br>>      'timer.cpp',<br>> +    'utils.cpp',<br>>      'v4l2_device.cpp',<br>>  ])<br>><br>> diff --git a/src/libcamera/utils.cpp b/src/libcamera/utils.cpp<br>> new file mode 100644<br>> index 000000000000..5014b27d8c7d<br>> --- /dev/null<br>> +++ b/src/libcamera/utils.cpp<br>> @@ -0,0 +1,46 @@<br>> +/* SPDX-License-Identifier: LGPL-2.1-or-later */<br>> +/*<br>> + * Copyright (C) 2018, Google Inc.<br>> + *<br>> + * utils.cpp - Miscellaneous utility functions<br>> + */<br>> +<br>> +#include "utils.h"<br>> +<br>> +namespace libcamera {<br>> +<br>> +struct timespec &operator+=(struct timespec &lhs, const struct timespec &rhs)<br>> +{<br>> +       lhs.tv_sec += rhs.tv_sec;<br>> +       lhs.tv_nsec += rhs.tv_nsec;<br>> +       if (lhs.tv_nsec >= 1000000000) {<br>> +               lhs.tv_sec++;<br>> +               lhs.tv_nsec -= 1000000000;<br>> +       }<br>> +<br>> +       return lhs;<br>> +}<br>> +<br>> +struct timespec operator+(struct timespec lhs, const struct timespec &rhs)<br>> +{<br>> +       return lhs += rhs;<br>> +}<br>> +<br>> +struct timespec &operator-=(struct timespec &lhs, const struct timespec &rhs)<br>> +{<br>> +       lhs.tv_sec -= rhs.tv_sec;<br>> +       lhs.tv_nsec -= rhs.tv_nsec;<br>> +       if (lhs.tv_nsec < 0) {<br>> +               lhs.tv_sec--;<br>> +               lhs.tv_nsec += 1000000000;<br>> +       }<br>> +<br>> +       return lhs;<br>> +}<br>> +<br>> +struct timespec operator-(struct timespec lhs, const struct timespec &rhs)<br>> +{<br>> +       return lhs - rhs;<br>> +}<br>> +<br>> +} /* namespace libcamera */<br>> --<br>> Regards,<br>><br>> Laurent Pinchart<br><br>Sincerely,<div>Shik</div></div>