Skip to content

Commit

Permalink
iox-eclipse-iceoryx#1969 Port iceoryx to 'ok()' and 'err()'
Browse files Browse the repository at this point in the history
  • Loading branch information
elBoberido committed May 5, 2023
1 parent 270e16a commit fa48377
Show file tree
Hide file tree
Showing 79 changed files with 606 additions and 642 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ For more information on how it is used for error handling see
Assume we have `E` as an error type, then we can create a value

```cpp
iox::expected<int, E> result(iox::success<int>(73));
iox::expected<int, E> result(iox::ok(73));
```
and use the value or handle a potential error
Expand All @@ -106,7 +106,7 @@ else
If we need an error value, we set

```cpp
result = iox::error<E>(errorCode);
result = iox::err(errorCode);
```

which assumes that `E` can be constructed from an `errorCode`.
Expand Down
10 changes: 5 additions & 5 deletions iceoryx_dust/include/iceoryx_dust/internal/cli/arguments.inl
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ inline expected<T, Arguments::Error> Arguments::convertFromString(const Argument
if (!cxx::convert::fromString(stringValue.c_str(), value))
{
std::cout << "\"" << stringValue.c_str() << "\" could not be converted to the requested type" << std::endl;
return error<Error>(Error::UNABLE_TO_CONVERT_VALUE);
return err(Error::UNABLE_TO_CONVERT_VALUE);
}
return success<T>(value);
return ok(value);
}

template <>
Expand All @@ -42,10 +42,10 @@ inline expected<bool, Arguments::Error> Arguments::convertFromString(const Argum
if (stringValue != "true" && stringValue != "false")
{
std::cout << "\"" << stringValue.c_str() << "\" could not be converted to the requested type" << std::endl;
return error<Error>(Error::UNABLE_TO_CONVERT_VALUE);
return err(Error::UNABLE_TO_CONVERT_VALUE);
}

return success<bool>(stringValue == "true");
return ok(stringValue == "true");
}

template <typename T>
Expand All @@ -59,7 +59,7 @@ inline expected<T, Arguments::Error> Arguments::get(const OptionName_t& optionNa
}
}

return error<Error>(Error::NO_SUCH_VALUE);
return err(Error::NO_SUCH_VALUE);
}
} // namespace internal
} // namespace cli
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,10 @@ Creation<DerivedClass, ErrorType>::verify(DerivedClass&& newObject) noexcept
{
if (!newObject.m_isInitialized)
{
return iox::error<ErrorType>(newObject.m_errorValue);
return iox::err(newObject.m_errorValue);
}

return iox::success<DerivedClass>(std::move(newObject));
return iox::ok(std::move(newObject));
}

template <typename DerivedClass, typename ErrorType>
Expand All @@ -74,10 +74,10 @@ inline iox::expected<void, ErrorType> Creation<DerivedClass, ErrorType>::placeme
{
ErrorType errorValue = newClass->m_errorValue;
newClass->~DerivedClass();
return iox::error<ErrorType>(errorValue);
return iox::err(errorValue);
}

return iox::success<>();
return iox::ok();
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,8 @@ class MessageQueue : public DesignPattern::Creation<MessageQueue, IpcChannelErro

expected<void, IpcChannelError> close() noexcept;
expected<void, IpcChannelError> unlink() noexcept;
error<IpcChannelError> createErrorFromErrnum(const int32_t errnum) const noexcept;
static error<IpcChannelError> createErrorFromErrnum(const IpcChannelName_t& name, const int32_t errnum) noexcept;
IpcChannelError errnoToEnum(const int32_t errnum) const noexcept;
static IpcChannelError errnoToEnum(const IpcChannelName_t& name, const int32_t errnum) noexcept;
static expected<IpcChannelName_t, IpcChannelError> sanitizeIpcChannelName(const IpcChannelName_t& name) noexcept;
expected<void, IpcChannelError> destroy() noexcept;

Expand Down
78 changes: 39 additions & 39 deletions iceoryx_dust/source/posix_wrapper/message_queue.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ expected<bool, IpcChannelError> MessageQueue::unlinkIfExists(const IpcChannelNam
auto sanitizedIpcChannelName = sanitizeIpcChannelName(name);
if (sanitizedIpcChannelName.has_error())
{
return error<IpcChannelError>(IpcChannelError::INVALID_CHANNEL_NAME);
return err(IpcChannelError::INVALID_CHANNEL_NAME);
}


Expand All @@ -149,9 +149,9 @@ expected<bool, IpcChannelError> MessageQueue::unlinkIfExists(const IpcChannelNam

if (mqCall.has_error())
{
return createErrorFromErrnum(*sanitizedIpcChannelName, mqCall.get_error().errnum);
return err(errnoToEnum(*sanitizedIpcChannelName, mqCall.get_error().errnum));
}
return success<bool>(mqCall->errnum != ENOENT);
return ok(mqCall->errnum != ENOENT);
}

expected<void, IpcChannelError> MessageQueue::destroy() noexcept
Expand All @@ -174,26 +174,26 @@ expected<void, IpcChannelError> MessageQueue::destroy() noexcept

m_mqDescriptor = INVALID_DESCRIPTOR;
m_isInitialized = false;
return success<void>();
return ok();
}

expected<void, IpcChannelError> MessageQueue::send(const std::string& msg) const noexcept
{
const uint64_t messageSize = msg.size() + NULL_TERMINATOR_SIZE;
if (messageSize > static_cast<uint64_t>(m_attributes.mq_msgsize))
{
return error<IpcChannelError>(IpcChannelError::MESSAGE_TOO_LONG);
return err(IpcChannelError::MESSAGE_TOO_LONG);
}

auto mqCall =
posixCall(mq_send)(m_mqDescriptor, msg.c_str(), messageSize, 1U).failureReturnValue(ERROR_CODE).evaluate();

if (mqCall.has_error())
{
return createErrorFromErrnum(mqCall.get_error().errnum);
return err(errnoToEnum(mqCall.get_error().errnum));
}

return success<void>();
return ok();
}

expected<std::string, IpcChannelError> MessageQueue::receive() const noexcept
Expand All @@ -208,10 +208,10 @@ expected<std::string, IpcChannelError> MessageQueue::receive() const noexcept

if (mqCall.has_error())
{
return createErrorFromErrnum(mqCall.get_error().errnum);
return err(errnoToEnum(mqCall.get_error().errnum));
}

return success<std::string>(std::string(&(message[0])));
return ok(std::string(&(message[0])));
}

expected<mqd_t, IpcChannelError> MessageQueue::open(const IpcChannelName_t& name,
Expand All @@ -220,7 +220,7 @@ expected<mqd_t, IpcChannelError> MessageQueue::open(const IpcChannelName_t& name
auto sanitizedIpcChannelName = sanitizeIpcChannelName(name);
if (sanitizedIpcChannelName.has_error())
{
return error<IpcChannelError>(IpcChannelError::INVALID_CHANNEL_NAME);
return err(IpcChannelError::INVALID_CHANNEL_NAME);
}

int32_t openFlags = O_RDWR;
Expand All @@ -242,10 +242,10 @@ expected<mqd_t, IpcChannelError> MessageQueue::open(const IpcChannelName_t& name

if (mqCall.has_error())
{
return createErrorFromErrnum(mqCall.get_error().errnum);
return err(errnoToEnum(mqCall.get_error().errnum));
}

return success<mqd_t>(mqCall->value);
return ok<mqd_t>(mqCall->value);
}

expected<void, IpcChannelError> MessageQueue::close() noexcept
Expand All @@ -254,26 +254,26 @@ expected<void, IpcChannelError> MessageQueue::close() noexcept

if (mqCall.has_error())
{
return createErrorFromErrnum(mqCall.get_error().errnum);
return err(errnoToEnum(mqCall.get_error().errnum));
}

return success<void>();
return ok();
}

expected<void, IpcChannelError> MessageQueue::unlink() noexcept
{
if (m_channelSide == IpcChannelSide::CLIENT)
{
return success<void>();
return ok();
}

auto mqCall = posixCall(mq_unlink)(m_name.c_str()).failureReturnValue(ERROR_CODE).evaluate();
if (mqCall.has_error())
{
return createErrorFromErrnum(mqCall.get_error().errnum);
return err(errnoToEnum(mqCall.get_error().errnum));
}

return success<void>();
return ok();
}

expected<std::string, IpcChannelError> MessageQueue::timedReceive(const units::Duration& timeout) const noexcept
Expand All @@ -291,15 +291,15 @@ expected<std::string, IpcChannelError> MessageQueue::timedReceive(const units::D

if (mqCall.has_error())
{
return createErrorFromErrnum(mqCall.get_error().errnum);
return err(errnoToEnum(mqCall.get_error().errnum));
}

if (mqCall->errnum == TIMEOUT_ERRNO)
{
return createErrorFromErrnum(ETIMEDOUT);
return err(errnoToEnum(ETIMEDOUT));
}

return success<std::string>(std::string(&(message[0])));
return ok(std::string(&(message[0])));
}

expected<void, IpcChannelError> MessageQueue::timedSend(const std::string& msg,
Expand All @@ -310,7 +310,7 @@ expected<void, IpcChannelError> MessageQueue::timedSend(const std::string& msg,
{
std::cerr << "the message \"" << msg << "\" which should be sent to the message queue \"" << m_name
<< "\" is too long" << std::endl;
return error<IpcChannelError>(IpcChannelError::MESSAGE_TOO_LONG);
return err(IpcChannelError::MESSAGE_TOO_LONG);
}

timespec timeOut = timeout.timespec(units::TimeSpecReference::Epoch);
Expand All @@ -323,70 +323,70 @@ expected<void, IpcChannelError> MessageQueue::timedSend(const std::string& msg,

if (mqCall.has_error())
{
return createErrorFromErrnum(mqCall.get_error().errnum);
return err(errnoToEnum(mqCall.get_error().errnum));
}

if (mqCall->errnum == TIMEOUT_ERRNO)
{
return createErrorFromErrnum(ETIMEDOUT);
return err(errnoToEnum(ETIMEDOUT));
}

return success<void>();
return ok();
}

expected<bool, IpcChannelError> MessageQueue::isOutdated() noexcept
{
return success<bool>(false);
return ok(false);
}

error<IpcChannelError> MessageQueue::createErrorFromErrnum(const int32_t errnum) const noexcept
IpcChannelError MessageQueue::errnoToEnum(const int32_t errnum) const noexcept
{
return createErrorFromErrnum(m_name, errnum);
return errnoToEnum(m_name, errnum);
}

error<IpcChannelError> MessageQueue::createErrorFromErrnum(const IpcChannelName_t& name, const int32_t errnum) noexcept
IpcChannelError MessageQueue::errnoToEnum(const IpcChannelName_t& name, const int32_t errnum) noexcept
{
switch (errnum)
{
case EACCES:
{
std::cerr << "access denied to message queue \"" << name << "\"" << std::endl;
return error<IpcChannelError>(IpcChannelError::ACCESS_DENIED);
return IpcChannelError::ACCESS_DENIED;
}
case EAGAIN:
{
std::cerr << "the message queue \"" << name << "\" is full" << std::endl;
return error<IpcChannelError>(IpcChannelError::CHANNEL_FULL);
return IpcChannelError::CHANNEL_FULL;
}
case ETIMEDOUT:
{
// no error message needed since this is a normal use case
return error<IpcChannelError>(IpcChannelError::TIMEOUT);
return IpcChannelError::TIMEOUT;
}
case EEXIST:
{
std::cerr << "message queue \"" << name << "\" already exists" << std::endl;
return error<IpcChannelError>(IpcChannelError::CHANNEL_ALREADY_EXISTS);
return IpcChannelError::CHANNEL_ALREADY_EXISTS;
}
case EINVAL:
{
std::cerr << "provided invalid arguments for message queue \"" << name << "\"" << std::endl;
return error<IpcChannelError>(IpcChannelError::INVALID_ARGUMENTS);
return IpcChannelError::INVALID_ARGUMENTS;
}
case ENOENT:
{
// no error message needed since this is a normal use case
return error<IpcChannelError>(IpcChannelError::NO_SUCH_CHANNEL);
return IpcChannelError::NO_SUCH_CHANNEL;
}
case ENAMETOOLONG:
{
std::cerr << "message queue name \"" << name << "\" is too long" << std::endl;
return error<IpcChannelError>(IpcChannelError::INVALID_CHANNEL_NAME);
return IpcChannelError::INVALID_CHANNEL_NAME;
}
default:
{
std::cerr << "internal logic error in message queue \"" << name << "\" occurred" << std::endl;
return error<IpcChannelError>(IpcChannelError::INTERNAL_LOGIC_ERROR);
return IpcChannelError::INTERNAL_LOGIC_ERROR;
}
}
}
Expand All @@ -399,16 +399,16 @@ expected<IpcChannelName_t, IpcChannelError> MessageQueue::sanitizeIpcChannelName
/// See: https://pubs.opengroup.org/onlinepubs/9699919799/functions/mq_open.html
if (name.empty() || name.size() < SHORTEST_VALID_QUEUE_NAME)
{
return error<IpcChannelError>(IpcChannelError::INVALID_CHANNEL_NAME);
return err(IpcChannelError::INVALID_CHANNEL_NAME);
}
// name is checked for emptiness, so it's ok to get a first member
// NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic)
if (name.c_str()[0] != '/')
{
return success<IpcChannelName_t>(IpcChannelName_t("/").append(iox::TruncateToCapacity, name));
return ok(IpcChannelName_t("/").append(iox::TruncateToCapacity, name));
}

return success<IpcChannelName_t>(name);
return ok(name);
}

} // namespace posix
Expand Down
Loading

0 comments on commit fa48377

Please sign in to comment.