Skip to content

Commit

Permalink
iox-eclipse-iceoryx#831 Make GenericRAII a template to change storage…
Browse files Browse the repository at this point in the history
… value

Signed-off-by: Simon Hoinkis <simon.hoinkis@apex.ai>
  • Loading branch information
mossmaurice committed Jun 22, 2022
1 parent 33a09b0 commit bfee243
Show file tree
Hide file tree
Showing 6 changed files with 102 additions and 82 deletions.
1 change: 0 additions & 1 deletion iceoryx_hoofs/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ iox_add_library(
source/cxx/adaptive_wait.cpp
source/cxx/deadline_timer.cpp
source/cxx/filesystem.cpp
source/cxx/generic_raii.cpp
source/cxx/helplets.cpp
source/cxx/requires.cpp
source/cxx/unique_id.cpp
Expand Down
5 changes: 4 additions & 1 deletion iceoryx_hoofs/include/iceoryx_hoofs/cxx/function.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ namespace iox
{
namespace cxx
{
constexpr uint64_t DEFAULT_FUNCTION_CAPACITY{128U};

/// @brief A static memory replacement for std::function
/// Allows storing a callable with a given signature if its size does not exceed a limit.
/// This limit can be adjusted by changing the Bytes parameter.
Expand All @@ -42,7 +44,8 @@ namespace cxx
///
/// @note If the static storage is insufficient to store the callable we get a compile time error.
///
template <typename Signature, uint64_t Capacity = 128U>

template <typename Signature, uint64_t Capacity = DEFAULT_FUNCTION_CAPACITY>
using function = storable_function<static_storage<Capacity>, Signature>;


Expand Down
26 changes: 16 additions & 10 deletions iceoryx_hoofs/include/iceoryx_hoofs/cxx/generic_raii.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Copyright (c) 2019 by Robert Bosch GmbH. All rights reserved.
// Copyright (c) 2021 by Apex.AI Inc. All rights reserved.
// Copyright (c) 2021 - 2022 by Apex.AI Inc. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -41,39 +41,45 @@ namespace cxx
/// // destructor
/// }
/// @endcode
class GenericRAII
template <uint64_t CleanupCapacity = DEFAULT_FUNCTION_CAPACITY>
class GenericRAIIWithVariableCapacity
{
public:
/// @brief constructor which creates GenericRAII that calls only the cleanupFunction on destruction
/// @param[in] cleanupFunction callable which will be called in the destructor
explicit GenericRAII(const function<void()>& cleanupFunction) noexcept;
explicit GenericRAIIWithVariableCapacity(const function<void(), CleanupCapacity>& cleanupFunction) noexcept;

/// @brief constructor which calls initFunction and stores the cleanupFunction which will be
/// called in the destructor
/// @param[in] initFunction callable which will be called in the constructor
/// @param[in] cleanupFunction callable which will be called in the destructor
GenericRAII(const function_ref<void()>& initFunction, const function<void()>& cleanupFunction) noexcept;
GenericRAIIWithVariableCapacity(const function_ref<void()>& initFunction, const function<void()>& cleanupFunction) noexcept;

/// @brief calls m_cleanupFunction callable if it was set in the constructor
~GenericRAII() noexcept;
~GenericRAIIWithVariableCapacity() noexcept;

GenericRAII(const GenericRAII&) = delete;
GenericRAII& operator=(const GenericRAII&) = delete;
GenericRAIIWithVariableCapacity(const GenericRAIIWithVariableCapacity&) = delete;
GenericRAIIWithVariableCapacity& operator=(const GenericRAIIWithVariableCapacity&) = delete;

/// @brief move constructor which moves a generic raii object without calling the cleanupFunction
GenericRAII(GenericRAII&& rhs) noexcept;
GenericRAIIWithVariableCapacity(GenericRAIIWithVariableCapacity&& rhs) noexcept;

/// @brief move assignment which moves a generic raii object without calling the cleanupFunction
GenericRAII& operator=(GenericRAII&& rhs) noexcept;
GenericRAIIWithVariableCapacity& operator=(GenericRAIIWithVariableCapacity&& rhs) noexcept;

private:
void destroy() noexcept;

private:
function<void()> m_cleanupFunction;
function<void(), CleanupCapacity> m_cleanupFunction;
};

// This alias can be removed with C++17 and class template argument deduction
using GenericRAII = GenericRAIIWithVariableCapacity<>;

} // namespace cxx
} // namespace iox

#include "iceoryx_hoofs/internal/cxx/generic_raii.inl"

#endif // IOX_HOOFS_CXX_GENERIC_RAII_HPP
81 changes: 81 additions & 0 deletions iceoryx_hoofs/include/iceoryx_hoofs/internal/cxx/generic_raii.inl
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
// Copyright (c) 2019 by Robert Bosch GmbH. All rights reserved.
// Copyright (c) 2021 - 2022 by Apex.AI Inc. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// SPDX-License-Identifier: Apache-2.0

#ifndef IOX_HOOFS_CXX_GENERIC_RAII_INL
#define IOX_HOOFS_CXX_GENERIC_RAII_INL

#include "iceoryx_hoofs/cxx/generic_raii.hpp"

namespace iox
{
namespace cxx
{
template <uint64_t Capacity>
inline GenericRAIIWithVariableCapacity<Capacity>::GenericRAIIWithVariableCapacity(const cxx::function<void(), Capacity>& cleanupFunction) noexcept
: GenericRAIIWithVariableCapacity(function_ref<void()>(), cleanupFunction)
{
}

template <uint64_t Capacity>
inline GenericRAIIWithVariableCapacity<Capacity>::GenericRAIIWithVariableCapacity(const function_ref<void()>& initFunction,
const function<void()>& cleanupFunction) noexcept
: m_cleanupFunction(cleanupFunction)
{
if (initFunction)
{
initFunction();
}
}

template <uint64_t Capacity>
inline GenericRAIIWithVariableCapacity<Capacity>::~GenericRAIIWithVariableCapacity() noexcept
{
destroy();
}

template <uint64_t Capacity>
inline GenericRAIIWithVariableCapacity<Capacity>::GenericRAIIWithVariableCapacity(GenericRAIIWithVariableCapacity&& rhs) noexcept
{
*this = std::move(rhs);
}

template <uint64_t Capacity>
inline GenericRAIIWithVariableCapacity<Capacity>& GenericRAIIWithVariableCapacity<Capacity>::operator=(GenericRAIIWithVariableCapacity<Capacity>&& rhs) noexcept
{
if (this != &rhs)
{
destroy();
m_cleanupFunction = rhs.m_cleanupFunction;
rhs.m_cleanupFunction = function<void()>();
}
return *this;
}

template <uint64_t Capacity>
inline void GenericRAIIWithVariableCapacity<Capacity>::destroy() noexcept
{
if (m_cleanupFunction)
{
m_cleanupFunction();
m_cleanupFunction = function<void()>();
}
}

} // namespace cxx
} // namespace iox

#endif // IOX_HOOFS_CXX_GENERIC_RAII_INL
69 changes: 0 additions & 69 deletions iceoryx_hoofs/source/cxx/generic_raii.cpp

This file was deleted.

2 changes: 1 addition & 1 deletion iceoryx_hoofs/test/moduletests/test_cxx_generic_raii.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ TEST_F(GenericRAII_test, InitFunctionIsCalledInCtorWhenSet)
{
::testing::Test::RecordProperty("TEST_ID", "9314e17c-5f02-4e5b-8d46-e324aa2cb88f");
int hasCalledInit = 0;
GenericRAII sut([&] { ++hasCalledInit; }, std::function<void()>());
GenericRAII sut([&] { ++hasCalledInit; }, function<void()>());
EXPECT_THAT(hasCalledInit, Eq(1));
}

Expand Down

0 comments on commit bfee243

Please sign in to comment.