Skip to content

Commit

Permalink
Put definition of inline functions in separate .ipp file (#168)
Browse files Browse the repository at this point in the history
  • Loading branch information
PatrickKa authored Nov 15, 2023
2 parents 0a907d6 + 7714496 commit d245497
Show file tree
Hide file tree
Showing 14 changed files with 194 additions and 121 deletions.
1 change: 1 addition & 0 deletions .clang-tidy
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ Checks: "*,\
-google-objc-*,\
-google-readability-todo,\
-google-readability-namespace-comments,\
-google-runtime-int,\
-hicpp-vararg,\
-linuxkernel-must-use-errs,\
-llvm-header-guard,\
Expand Down
14 changes: 2 additions & 12 deletions Sts1CobcSw/FileSystem/FileSystem.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ extern lfs_t lfs;
extern lfs_file_t lfsFile;
extern const lfs_config lfsConfig;


// Must be called once in a thread's init() function
auto Initialize() -> void;
auto Format() -> int;
Expand All @@ -33,18 +34,7 @@ auto Remove(char const * path) -> int;
auto Ls(char const * path) -> int;
// TODO: Implement cat
// TODO: Implement simple hexdump


template<typename T>
inline auto ReadFromFile(T * t) -> int
{
return lfs_file_read(&lfs, &lfsFile, t, sizeof(T));
}


template<typename T>
inline auto WriteToFile(T const & t) -> int
{
return lfs_file_write(&lfs, &lfsFile, &t, sizeof(T));
}
}
#include <Sts1CobcSw/FileSystem/FileSystem.ipp>
21 changes: 21 additions & 0 deletions Sts1CobcSw/FileSystem/FileSystem.ipp
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#pragma once


#include <Sts1CobcSw/FileSystem/FileSystem.hpp>


namespace sts1cobcsw::fs
{
template<typename T>
inline auto ReadFromFile(T * t) -> int
{
return lfs_file_read(&lfs, &lfsFile, t, sizeof(T));
}


template<typename T>
inline auto WriteToFile(T const & t) -> int
{
return lfs_file_write(&lfs, &lfsFile, &t, sizeof(T));
}
}
71 changes: 11 additions & 60 deletions Sts1CobcSw/Hal/Communication.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,75 +18,26 @@ namespace sts1cobcsw::hal


template<typename T, std::size_t size>
inline auto WriteTo(auto * communicationInterface, std::span<T, size> data)
{
std::size_t nSentBytes = 0U;
auto bytes = std::as_bytes(data);

while(nSentBytes < bytes.size())
{
nSentBytes +=
communicationInterface->write(bytes.data() + nSentBytes, bytes.size() - nSentBytes);
}
}


inline auto WriteTo(auto * communicationInterface, std::string_view message)
{
std::size_t nSentBytes = 0U;
while(nSentBytes < message.size())
{
nSentBytes +=
communicationInterface->write(message.data() + nSentBytes, message.size() - nSentBytes);
}
}
auto WriteTo(auto * communicationInterface, std::span<T, size> data);

auto WriteTo(auto * communicationInterface, std::string_view message);

template<std::size_t size>
inline auto ReadFrom(auto * communicationInterface, std::span<std::byte, size> readBuffer)
{
std::size_t nReadBytes = 0U;
while(nReadBytes < size)
{
nReadBytes +=
communicationInterface->read(data(readBuffer) + nReadBytes, size - nReadBytes);
}
}

auto ReadFrom(auto * communicationInterface, std::span<std::byte, size> readBuffer);

template<std::size_t size>
inline auto ReadFrom(auto * communicationInterface, std::span<char, size> readBuffer)
{
std::size_t nReadBytes = 0U;
while(nReadBytes < size)
{
nReadBytes +=
communicationInterface->read(data(readBuffer) + nReadBytes, size - nReadBytes);
}
}

auto ReadFrom(auto * communicationInterface, std::span<char, size> readBuffer);

template<std::size_t size>
inline auto WriteToReadFrom(auto * communicationInterface,
std::string_view message,
etl::string<size> * answer)
{
answer->initialize_free_space();
auto nReceivedBytes = communicationInterface->writeRead(
message.data(), message.size(), answer->data(), answer->capacity());
answer->trim_to_terminator();

return nReceivedBytes;
}

auto WriteToReadFrom(auto * communicationInterface,
std::string_view message,
etl::string<size> * answer);

// TODO: Try const correctness for span again
template<std::size_t nBytes>
auto WriteToReadFrom(auto * communicationInterface, std::span<Byte, nBytes> data)
-> std::array<Byte, nBytes>
{
auto readData = std::array<Byte, nBytes>{};
communicationInterface->writeRead(std::data(data), nBytes, std::data(readData), nBytes);
return readData;
}
-> std::array<Byte, nBytes>;
}


#include <Sts1CobcSw/Hal/Communication.ipp>
79 changes: 79 additions & 0 deletions Sts1CobcSw/Hal/Communication.ipp
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
#pragma once


#include <Sts1CobcSw/Hal/Communication.hpp>


namespace sts1cobcsw::hal
{
template<typename T, std::size_t size>
inline auto WriteTo(auto * communicationInterface, std::span<T, size> data)
{
std::size_t nSentBytes = 0U;
auto bytes = std::as_bytes(data);

while(nSentBytes < bytes.size())
{
nSentBytes +=
communicationInterface->write(bytes.data() + nSentBytes, bytes.size() - nSentBytes);
}
}


inline auto WriteTo(auto * communicationInterface, std::string_view message)
{
std::size_t nSentBytes = 0U;
while(nSentBytes < message.size())
{
nSentBytes +=
communicationInterface->write(message.data() + nSentBytes, message.size() - nSentBytes);
}
}


template<std::size_t size>
inline auto ReadFrom(auto * communicationInterface, std::span<std::byte, size> readBuffer)
{
std::size_t nReadBytes = 0U;
while(nReadBytes < size)
{
nReadBytes +=
communicationInterface->read(data(readBuffer) + nReadBytes, size - nReadBytes);
}
}


template<std::size_t size>
inline auto ReadFrom(auto * communicationInterface, std::span<char, size> readBuffer)
{
std::size_t nReadBytes = 0U;
while(nReadBytes < size)
{
nReadBytes +=
communicationInterface->read(data(readBuffer) + nReadBytes, size - nReadBytes);
}
}


template<std::size_t size>
inline auto WriteToReadFrom(auto * communicationInterface,
std::string_view message,
etl::string<size> * answer)
{
answer->initialize_free_space();
auto nReceivedBytes = communicationInterface->writeRead(
message.data(), message.size(), answer->data(), answer->capacity());
answer->trim_to_terminator();

return nReceivedBytes;
}

template<std::size_t nBytes>
auto WriteToReadFrom(auto * communicationInterface, std::span<Byte, nBytes> data)
-> std::array<Byte, nBytes>
{
auto readData = std::array<Byte, nBytes>{};
communicationInterface->writeRead(std::data(data), nBytes, std::data(readData), nBytes);
return readData;
}
}
29 changes: 1 addition & 28 deletions Sts1CobcSw/Hal/GpioPin.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,34 +36,7 @@ class GpioPin
private:
mutable RODOS::HAL_GPIO pin_;
};


inline GpioPin::GpioPin(RODOS::GPIO_PIN pinIndex) : pin_(pinIndex)
{
}


inline auto GpioPin::Direction(PinDirection pinDirection) -> void
{
pin_.reset();
pin_.init(pinDirection == PinDirection::out, 1, 0);
}


inline auto GpioPin::Set() -> void
{
pin_.setPins(1U);
}


inline auto GpioPin::Reset() -> void
{
pin_.setPins(0U);
}


inline auto GpioPin::Read() const -> PinState
{
return pin_.readPins() == 0U ? PinState::reset : PinState::set;
}
}
#include <Sts1CobcSw/Hal/GpioPin.ipp>
37 changes: 37 additions & 0 deletions Sts1CobcSw/Hal/GpioPin.ipp
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#pragma once


#include <Sts1CobcSw/Hal/GpioPin.hpp>


namespace sts1cobcsw::hal
{
inline GpioPin::GpioPin(RODOS::GPIO_PIN pinIndex) : pin_(pinIndex)
{
}


inline auto GpioPin::Direction(PinDirection pinDirection) -> void
{
pin_.reset();
pin_.init(pinDirection == PinDirection::out, 1, 0);
}


inline auto GpioPin::Set() -> void
{
pin_.setPins(1U);
}


inline auto GpioPin::Reset() -> void
{
pin_.setPins(0U);
}


inline auto GpioPin::Read() const -> PinState
{
return pin_.readPins() == 0U ? PinState::reset : PinState::set;
}
}
2 changes: 1 addition & 1 deletion Sts1CobcSw/Periphery/Fram.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,4 @@ auto WriteTo(Address address, T const & t) -> void;
}


#include <Sts1CobcSw/Periphery/Fram.tpp>
#include <Sts1CobcSw/Periphery/Fram.ipp>
File renamed without changes.
9 changes: 4 additions & 5 deletions Sts1CobcSw/Serial/Byte.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,8 @@ namespace sts1cobcsw
using Byte = std::byte;


inline constexpr auto operator"" _b(unsigned long long number) // NOLINT(google-runtime-int)
-> Byte
{
return static_cast<Byte>(number);
}
constexpr auto operator"" _b(unsigned long long number) -> Byte;
}


#include <Sts1CobcSw/Serial/Byte.ipp>
13 changes: 13 additions & 0 deletions Sts1CobcSw/Serial/Byte.ipp
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#pragma once


#include <Sts1CobcSw/Serial/Byte.hpp>


namespace sts1cobcsw
{
inline constexpr auto operator"" _b(unsigned long long number) -> Byte
{
return static_cast<Byte>(number);
}
}
1 change: 0 additions & 1 deletion Sts1CobcSw/Serial/Serial.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@ template<std::default_initializable T>


// --- Function template definitions ---

template<TriviallySerializable T>
inline auto SerializeTo(void * destination, T const & data) -> void *
{
Expand Down
17 changes: 3 additions & 14 deletions Sts1CobcSw/Utility/Time.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,9 @@ constexpr auto rodosUnixOffset = 946'684'800 * RODOS::SECONDS;
//! @brief Print UTC system time in human readable format.
auto PrintFormattedSystemUtc() -> void;

[[nodiscard]] inline auto UnixToRodosTime(std::int32_t unixTimeSeconds) -> std::int64_t;
[[nodiscard]] inline auto GetUnixUtc() -> std::int32_t;


//! @brief Given a time in seconds since 01.01.1970, return a time in nanoseconds since 01.01.2000.
inline auto UnixToRodosTime(std::int32_t unixTimeSeconds) -> std::int64_t
{
return static_cast<std::int64_t>(unixTimeSeconds) * RODOS::SECONDS - rodosUnixOffset;
[[nodiscard]] auto UnixToRodosTime(std::int32_t unixTimeSeconds) -> std::int64_t;
[[nodiscard]] auto GetUnixUtc() -> std::int32_t;
}


inline auto GetUnixUtc() -> std::int32_t
{
auto unixUtc = (RODOS::sysTime.getUTC() + rodosUnixOffset) / RODOS::SECONDS;
return static_cast<std::int32_t>(unixUtc);
}
}
#include <Sts1CobcSw/Utility/Time.ipp>
21 changes: 21 additions & 0 deletions Sts1CobcSw/Utility/Time.ipp
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#pragma once


#include <Sts1CobcSw/Utility/Time.hpp>


namespace sts1cobcsw::utility
{
//! @brief Given a time in seconds since 01.01.1970, return a time in nanoseconds since 01.01.2000.
inline auto UnixToRodosTime(std::int32_t unixTimeSeconds) -> std::int64_t
{
return static_cast<std::int64_t>(unixTimeSeconds) * RODOS::SECONDS - rodosUnixOffset;
}


inline auto GetUnixUtc() -> std::int32_t
{
auto unixUtc = (RODOS::sysTime.getUTC() + rodosUnixOffset) / RODOS::SECONDS;
return static_cast<std::int32_t>(unixUtc);
}
}

0 comments on commit d245497

Please sign in to comment.