From 5e1f2d6eb184da560be7fda4d31b07e62f80db0d Mon Sep 17 00:00:00 2001 From: kongr45gpen <electrovesta@gmail.com> Date: Sat, 10 Aug 2019 09:16:27 +0300 Subject: [PATCH] Remove unneeded templates from the appendString() functions This reduces the size of the code from 208608 to 208072 bytes, which is totally negligible --- inc/Message.hpp | 36 +++--------------------------------- src/Message.cpp | 29 +++++++++++++++++++++++++++++ src/Platform/x86/main.cpp | 2 +- test/Message.cpp | 2 +- 4 files changed, 34 insertions(+), 35 deletions(-) diff --git a/inc/Message.hpp b/inc/Message.hpp index 93821cbe..b59938dd 100644 --- a/inc/Message.hpp +++ b/inc/Message.hpp @@ -159,8 +159,7 @@ public: * * @param string The string to insert */ - template <const size_t SIZE> - void appendString(const String<SIZE>& string); + void appendString(const etl::istring& string); /** * Appends a number of bytes to the message @@ -175,8 +174,7 @@ public: * * @param string The string to insert */ - template <const size_t SIZE> - void appendFixedString(const String<SIZE>& string); + void appendFixedString(const etl::istring& string); /** * Reads the next \p numBits bits from the the message in a big-endian format @@ -358,14 +356,7 @@ public: * * PTC = 7, PFC = 0 */ - template <const size_t SIZE> - void appendOctetString(const String<SIZE>& string) { - // Make sure that the string is large enough to count - ASSERT_INTERNAL(string.size() <= (std::numeric_limits<uint16_t>::max)(), ErrorHandler::StringTooLarge); - - appendUint16(string.size()); - appendString(string); - } + void appendOctetString(const etl::istring& string); /** * Adds a nested TC or TM Message within the current Message @@ -573,25 +564,4 @@ public: } }; -template <const size_t SIZE> -inline void Message::appendString(const String<SIZE>& string) { - ASSERT_INTERNAL(dataSize + string.size() < ECSS_MAX_MESSAGE_SIZE, ErrorHandler::MessageTooLarge); - // TODO: Do we need to keep this check? How does etl::string handle it? - ASSERT_INTERNAL(string.size() < string.capacity(), ErrorHandler::StringTooLarge); - - memcpy(data + dataSize, string.data(), string.size()); - - dataSize += string.size(); -} - -template <const size_t SIZE> -inline void Message::appendFixedString(const String<SIZE>& string) { - ASSERT_INTERNAL((dataSize + SIZE) < ECSS_MAX_MESSAGE_SIZE, ErrorHandler::MessageTooLarge); - - memcpy(data + dataSize, string.data(), string.size()); // Append the bytes with content - (void) memset(data + dataSize + string.size(), 0, SIZE - string.size()); // The rest of the bytes is set to 0 - - dataSize += SIZE; -} - #endif // ECSS_SERVICES_PACKET_H diff --git a/src/Message.cpp b/src/Message.cpp index e96eacaa..b1dc6999 100644 --- a/src/Message.cpp +++ b/src/Message.cpp @@ -166,3 +166,32 @@ void Message::resetRead() { void Message::appendMessage(const Message& message, uint16_t size) { appendString(MessageParser::composeECSS(message, size)); } + +void Message::appendString(const etl::istring& string) { + ASSERT_INTERNAL(dataSize + string.size() < ECSS_MAX_MESSAGE_SIZE, ErrorHandler::MessageTooLarge); + // TODO: Do we need to keep this check? How does etl::string handle it? + ASSERT_INTERNAL(string.size() < string.capacity(), ErrorHandler::StringTooLarge); + + memcpy(data + dataSize, string.data(), string.size()); + + dataSize += string.size(); +} + +void Message::appendFixedString(const etl::istring& string) { + ASSERT_INTERNAL((dataSize + string.max_size()) < ECSS_MAX_MESSAGE_SIZE, ErrorHandler::MessageTooLarge); + + // Append the bytes with content + memcpy(data + dataSize, string.data(), string.size()); + // The rest of the bytes is set to 0 + (void) memset(data + dataSize + string.size(), 0, string.max_size() - string.size()); + + dataSize += string.max_size(); +} + +void Message::appendOctetString(const etl::istring& string) { + // Make sure that the string is large enough to count + ASSERT_INTERNAL(string.size() <= (std::numeric_limits<uint16_t>::max)(), ErrorHandler::StringTooLarge); + + appendUint16(string.size()); + appendString(string); +} diff --git a/src/Platform/x86/main.cpp b/src/Platform/x86/main.cpp index 54a997ba..391780a7 100644 --- a/src/Platform/x86/main.cpp +++ b/src/Platform/x86/main.cpp @@ -25,7 +25,7 @@ int main() { Message packet = Message(0, 0, Message::TC, 1); - packet.appendString<5>("hello"); + packet.appendString(String<5>("hello")); packet.appendBits(15, 0x28a8); packet.appendBits(1, 1); packet.appendFloat(5.7); diff --git a/test/Message.cpp b/test/Message.cpp index 2c92a13e..a13c29e2 100644 --- a/test/Message.cpp +++ b/test/Message.cpp @@ -156,7 +156,7 @@ TEST_CASE("Requirement 7.3.6 (Real)", "[message][ecss]") { TEST_CASE("Requirement 7.3.8 (Octet-string)", "[message][ecss]") { Message message(0, 0, Message::TC, 0); - message.appendString<4>("test"); + message.appendString(String<4>("test")); REQUIRE(message.dataSize == 4); -- GitLab