Skip to content

Commit

Permalink
iox-eclipse-iceoryx#418 replaced smart_c with posixCall in duration, …
Browse files Browse the repository at this point in the history
…convert; removed EINTR retry bool for semaphore from watch_dog and periodic task

Signed-off-by: Christian Eltzschig <me@elchris.org>
  • Loading branch information
elfenpiff committed May 14, 2021
1 parent 70a8446 commit 9226b39
Show file tree
Hide file tree
Showing 7 changed files with 107 additions and 89 deletions.
4 changes: 3 additions & 1 deletion iceoryx_utils/include/iceoryx_utils/cxx/convert.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,14 @@
#ifndef IOX_UTILS_CXX_CONVERT_HPP
#define IOX_UTILS_CXX_CONVERT_HPP

#include "iceoryx_utils/cxx/smart_c.hpp"
#include "iceoryx_utils/cxx/string.hpp"
#include "iceoryx_utils/posix_wrapper/posix_call.hpp"

#include <climits>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <limits>
#include <sstream>
#include <string>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ inline void PeriodicTask<T>::run() noexcept
IOX_DISCARD_RESULT(m_callable());

/// @todo use a refactored posix::Timer::wait method returning TIMER_TICK and TIMER_STOPPED once available
auto waitResult = m_stop.timedWait(m_interval, true);
auto waitResult = m_stop.timedWait(m_interval);
cxx::Expects(!waitResult.has_error());

waitState = waitResult.value();
Expand Down
145 changes: 66 additions & 79 deletions iceoryx_utils/include/iceoryx_utils/internal/cxx/convert.inl
Original file line number Diff line number Diff line change
Expand Up @@ -155,15 +155,11 @@ inline bool convert::fromString<float>(const char* v, float& dest)
return false;
}

auto call = makeSmartC(strtof, ReturnMode::PRE_DEFINED_ERROR_CODE, {HUGE_VALF, -HUGE_VALF}, {}, v, nullptr);

if (call.hasErrors())
{
return false;
}

dest = call.getReturnValue();
return true;
return !posix::posixCall(strtof)(v, nullptr)
.failureReturnValue(HUGE_VALF, -HUGE_VALF)
.evaluate()
.and_then([&](auto& r) { dest = r.value; })
.has_error();
}

template <>
Expand All @@ -174,15 +170,11 @@ inline bool convert::fromString<double>(const char* v, double& dest)
return false;
}

auto call = makeSmartC(strtod, ReturnMode::PRE_DEFINED_ERROR_CODE, {HUGE_VAL, -HUGE_VAL}, {}, v, nullptr);

if (call.hasErrors())
{
return false;
}

dest = call.getReturnValue();
return true;
return !posix::posixCall(strtod)(v, nullptr)
.failureReturnValue(HUGE_VAL, -HUGE_VAL)
.evaluate()
.and_then([&](auto& r) { dest = r.value; })
.has_error();
}

template <>
Expand All @@ -193,15 +185,11 @@ inline bool convert::fromString<long double>(const char* v, long double& dest)
return false;
}

auto call = makeSmartC(strtold, ReturnMode::PRE_DEFINED_ERROR_CODE, {HUGE_VALL, -HUGE_VALL}, {}, v, nullptr);

if (call.hasErrors())
{
return false;
}

dest = call.getReturnValue();
return true;
return !posix::posixCall(strtold)(v, nullptr)
.failureReturnValue(HUGE_VALL, -HUGE_VALL)
.evaluate()
.and_then([&](auto& r) { dest = r.value; })
.has_error();
}

template <>
Expand All @@ -212,19 +200,20 @@ inline bool convert::fromString<uint64_t>(const char* v, uint64_t& dest)
return false;
}

auto call = makeSmartC(strtoull, ReturnMode::PRE_DEFINED_ERROR_CODE, {ULLONG_MAX}, {}, v, nullptr, STRTOULL_BASE);
if (call.hasErrors())
auto call = posix::posixCall(strtoull)(v, nullptr, STRTOULL_BASE).failureReturnValue(ULLONG_MAX).evaluate();

if (call.has_error())
{
return false;
}

if (call.getReturnValue() > std::numeric_limits<uint64_t>::max())
if (call->value > std::numeric_limits<uint64_t>::max())
{
std::cerr << call.getReturnValue() << " too large, uint64_t overflow" << std::endl;
std::cerr << call->value << " too large, uint64_t overflow" << std::endl;
return false;
}

dest = static_cast<uint64_t>(call.getReturnValue());
dest = static_cast<uint64_t>(call->value);
return true;
}

Expand All @@ -249,19 +238,20 @@ inline bool convert::fromString<uint32_t>(const char* v, uint32_t& dest)
return false;
}

auto call = makeSmartC(strtoull, ReturnMode::PRE_DEFINED_ERROR_CODE, {ULLONG_MAX}, {}, v, nullptr, STRTOULL_BASE);
if (call.hasErrors())
auto call = posix::posixCall(strtoull)(v, nullptr, STRTOULL_BASE).failureReturnValue(ULLONG_MAX).evaluate();

if (call.has_error())
{
return false;
}

if (call.getReturnValue() > std::numeric_limits<uint32_t>::max())
if (call->value > std::numeric_limits<uint32_t>::max())
{
std::cerr << call.getReturnValue() << " too large, uint32_t overflow" << std::endl;
std::cerr << call->value << " too large, uint32_t overflow" << std::endl;
return false;
}

dest = static_cast<uint32_t>(call.getReturnValue());
dest = static_cast<uint32_t>(call->value);
return true;
}

Expand All @@ -273,19 +263,20 @@ inline bool convert::fromString<uint16_t>(const char* v, uint16_t& dest)
return false;
}

auto call = makeSmartC(strtoul, ReturnMode::PRE_DEFINED_ERROR_CODE, {ULONG_MAX}, {}, v, nullptr, 10);
if (call.hasErrors())
auto call = posix::posixCall(strtoul)(v, nullptr, STRTOULL_BASE).failureReturnValue(ULONG_MAX).evaluate();

if (call.has_error())
{
return false;
}

if (call.getReturnValue() > std::numeric_limits<uint16_t>::max())
if (call->value > std::numeric_limits<uint16_t>::max())
{
std::cerr << call.getReturnValue() << " too large, uint16_t overflow" << std::endl;
std::cerr << call->value << " too large, uint16_t overflow" << std::endl;
return false;
}

dest = static_cast<uint16_t>(call.getReturnValue());
dest = static_cast<uint16_t>(call->value);
return true;
}

Expand All @@ -297,19 +288,20 @@ inline bool convert::fromString<uint8_t>(const char* v, uint8_t& dest)
return false;
}

auto call = makeSmartC(strtoul, ReturnMode::PRE_DEFINED_ERROR_CODE, {ULONG_MAX}, {}, v, nullptr, 10);
if (call.hasErrors())
auto call = posix::posixCall(strtoul)(v, nullptr, STRTOULL_BASE).failureReturnValue(ULONG_MAX).evaluate();

if (call.has_error())
{
return false;
}

if (call.getReturnValue() > std::numeric_limits<uint8_t>::max())
if (call->value > std::numeric_limits<uint8_t>::max())
{
std::cerr << call.getReturnValue() << " too large, uint8_t overflow" << std::endl;
std::cerr << call->value << " too large, uint8_t overflow" << std::endl;
return false;
}

dest = static_cast<uint8_t>(call.getReturnValue());
dest = static_cast<uint8_t>(call->value);
return true;
}

Expand All @@ -321,20 +313,20 @@ inline bool convert::fromString<int64_t>(const char* v, int64_t& dest)
return false;
}

auto call = makeSmartC(strtoll, ReturnMode::PRE_DEFINED_ERROR_CODE, {LLONG_MAX, LLONG_MIN}, {}, v, nullptr, 10);
if (call.hasErrors())
auto call =
posix::posixCall(strtoll)(v, nullptr, STRTOULL_BASE).failureReturnValue(LLONG_MAX, LLONG_MIN).evaluate();
if (call.has_error())
{
return false;
}

if (call.getReturnValue() > std::numeric_limits<int64_t>::max()
|| call.getReturnValue() < std::numeric_limits<int64_t>::min())
if (call->value > std::numeric_limits<int64_t>::max() || call->value < std::numeric_limits<int64_t>::min())
{
std::cerr << call.getReturnValue() << " is out of range, int64_t overflow" << std::endl;
std::cerr << call->value << " is out of range, int64_t overflow" << std::endl;
return false;
}

dest = static_cast<int64_t>(call.getReturnValue());
dest = static_cast<int64_t>(call->value);
return true;
}

Expand All @@ -346,20 +338,20 @@ inline bool convert::fromString<int32_t>(const char* v, int32_t& dest)
return false;
}

auto call = makeSmartC(strtoll, ReturnMode::PRE_DEFINED_ERROR_CODE, {LLONG_MAX, LLONG_MIN}, {}, v, nullptr, 10);
if (call.hasErrors())
auto call =
posix::posixCall(strtoll)(v, nullptr, STRTOULL_BASE).failureReturnValue(LLONG_MAX, LLONG_MIN).evaluate();
if (call.has_error())
{
return false;
}

if (call.getReturnValue() > std::numeric_limits<int32_t>::max()
|| call.getReturnValue() < std::numeric_limits<int32_t>::min())
if (call->value > std::numeric_limits<int32_t>::max() || call->value < std::numeric_limits<int32_t>::min())
{
std::cerr << call.getReturnValue() << " is out of range, int32_t overflow" << std::endl;
std::cerr << call->value << " is out of range, int32_t overflow" << std::endl;
return false;
}

dest = static_cast<int32_t>(call.getReturnValue());
dest = static_cast<int32_t>(call->value);
return true;
}

Expand All @@ -371,20 +363,19 @@ inline bool convert::fromString<int16_t>(const char* v, int16_t& dest)
return false;
}

auto call = makeSmartC(strtol, ReturnMode::PRE_DEFINED_ERROR_CODE, {LONG_MAX, LONG_MIN}, {}, v, nullptr, 10);
if (call.hasErrors())
auto call = posix::posixCall(strtol)(v, nullptr, STRTOULL_BASE).failureReturnValue(LONG_MAX, LONG_MIN).evaluate();
if (call.has_error())
{
return false;
}

if (call.getReturnValue() > std::numeric_limits<int16_t>::max()
|| call.getReturnValue() < std::numeric_limits<int16_t>::min())
if (call->value > std::numeric_limits<int16_t>::max() || call->value < std::numeric_limits<int16_t>::min())
{
std::cerr << call.getReturnValue() << " is out of range, int16_t overflow" << std::endl;
std::cerr << call->value << " is out of range, int16_t overflow" << std::endl;
return false;
}

dest = static_cast<int16_t>(call.getReturnValue());
dest = static_cast<int16_t>(call->value);
return true;
}

Expand All @@ -396,20 +387,19 @@ inline bool convert::fromString<int8_t>(const char* v, int8_t& dest)
return false;
}

auto call = makeSmartC(strtol, ReturnMode::PRE_DEFINED_ERROR_CODE, {LONG_MAX, LONG_MIN}, {}, v, nullptr, 10);
if (call.hasErrors())
auto call = posix::posixCall(strtol)(v, nullptr, STRTOULL_BASE).failureReturnValue(LONG_MAX, LONG_MIN).evaluate();
if (call.has_error())
{
return false;
}

if (call.getReturnValue() > std::numeric_limits<int8_t>::max()
|| call.getReturnValue() < std::numeric_limits<int8_t>::min())
if (call->value > std::numeric_limits<int8_t>::max() || call->value < std::numeric_limits<int8_t>::min())
{
std::cerr << call.getReturnValue() << " is out of range, int8_t overflow" << std::endl;
std::cerr << call->value << " is out of range, int8_t overflow" << std::endl;
return false;
}

dest = static_cast<int8_t>(call.getReturnValue());
dest = static_cast<int8_t>(call->value);
return true;
}

Expand All @@ -421,14 +411,11 @@ inline bool convert::fromString<bool>(const char* v, bool& dest)
return false;
}

auto call = makeSmartC(strtoul, ReturnMode::PRE_DEFINED_ERROR_CODE, {ULONG_MAX}, {}, v, nullptr, 10);
if (call.hasErrors())
{
return false;
}

dest = static_cast<bool>(call.getReturnValue());
return true;
return !posix::posixCall(strtoul)(v, nullptr, STRTOULL_BASE)
.failureReturnValue(ULONG_MAX)
.evaluate()
.and_then([&](auto& r) { dest = static_cast<bool>(r.value); })
.has_error();
}

} // namespace cxx
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,28 @@ PosixCallVerificator<ReturnType>::successReturnValue(const ReturnType value) &&
return PosixCallEvaluator<ReturnType>(m_details);
}

template <typename ReturnType>
template <typename... SuccessReturnValues>
inline PosixCallEvaluator<ReturnType>
PosixCallVerificator<ReturnType>::successReturnValue(const ReturnType value,
const SuccessReturnValues... remainingValues) && noexcept
{
m_details.hasSuccess = (m_details.result.value == value);
// we require an rvalue of our object
return std::move(*this).successReturnValue(remainingValues...);
}

template <typename ReturnType>
template <typename... FailureReturnValues>
inline PosixCallEvaluator<ReturnType>
PosixCallVerificator<ReturnType>::failureReturnValue(const ReturnType value,
const FailureReturnValues... remainingValues) && noexcept
{
m_details.hasSuccess = (m_details.result.value != value);
// we require an rvalue of our object
return std::move(*this).failureReturnValue(remainingValues...);
}

template <typename ReturnType>
inline PosixCallEvaluator<ReturnType>
PosixCallVerificator<ReturnType>::failureReturnValue(const ReturnType value) && noexcept
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,11 +142,17 @@ class IOX_NO_DISCARD PosixCallVerificator
/// @brief the posix function call defines success through a single value
/// @param[in] value the value which defines success
/// @return the PosixCallEvaluator which evaluates the errno values
template <typename... SuccessReturnValues>
PosixCallEvaluator<ReturnType> successReturnValue(const ReturnType value,
const SuccessReturnValues... remainingValues) && noexcept;
PosixCallEvaluator<ReturnType> successReturnValue(const ReturnType value) && noexcept;

/// @brief the posix function call defines failure through a single value
/// @param[in] value the value which defines failure
/// @return the PosixCallEvaluator which evaluates the errno values
template <typename... FailureReturnValues>
PosixCallEvaluator<ReturnType> failureReturnValue(const ReturnType value,
const FailureReturnValues... remainingValues) && noexcept;
PosixCallEvaluator<ReturnType> failureReturnValue(const ReturnType value) && noexcept;

private:
Expand Down
15 changes: 8 additions & 7 deletions iceoryx_utils/source/units/duration.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
#include "iceoryx_utils/internal/units/duration.hpp"
#include "iceoryx_utils/platform/platform_correction.hpp"

#include "iceoryx_utils/posix_wrapper/posix_call.hpp"

#include <utility>

namespace iox
Expand Down Expand Up @@ -45,13 +47,12 @@ struct timespec Duration::timespec(const TimeSpecReference& reference) const noe
else
{
struct timespec referenceTime;
auto result = cxx::makeSmartC(clock_gettime,
cxx::ReturnMode::PRE_DEFINED_ERROR_CODE,
{-1},
{},
(reference == TimeSpecReference::Epoch) ? CLOCK_REALTIME : CLOCK_MONOTONIC,
&referenceTime);
if (result.hasErrors())

if (posix::posixCall(clock_gettime)((reference == TimeSpecReference::Epoch) ? CLOCK_REALTIME : CLOCK_MONOTONIC,
&referenceTime)
.failureReturnValue(-1)
.evaluate()
.has_error())
{
return {0, 0};
}
Expand Down
Loading

0 comments on commit 9226b39

Please sign in to comment.