| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | // ====================================================================== | ||
| 2 | // @file ConstExternalString.hpp | ||
| 3 | // @brief A string backed by an immutable string literal | ||
| 4 | // ====================================================================== | ||
| 5 | |||
| 6 | #ifndef FW_CONST_EXTERNAL_STRING_HPP | ||
| 7 | #define FW_CONST_EXTERNAL_STRING_HPP | ||
| 8 | |||
| 9 | #include <Fw/FPrimeBasicTypes.hpp> | ||
| 10 | #include <Fw/Types/Assert.hpp> | ||
| 11 | #include <Fw/Types/StringBase.hpp> | ||
| 12 | |||
| 13 | namespace Fw { | ||
| 14 | |||
| 15 | //! A string backed by an immutable string literal | ||
| 16 | class ConstExternalString final : public ConstStringBase { | ||
| 17 | public: | ||
| 18 | // ---------------------------------------------------------------------- | ||
| 19 | // Construction and destruction | ||
| 20 | // ---------------------------------------------------------------------- | ||
| 21 | |||
| 22 | //! Constructor (uninitialized buffer) | ||
| 23 | 1 | ConstExternalString() : ConstStringBase(), m_bufferPtr(nullptr), m_bufferSize(0) {} | |
| 24 | |||
| 25 | //! Constructor (bufferPtr and bufferSize) | ||
| 26 | 3 | ConstExternalString(const char* bufferPtr, //!< The buffer pointer | |
| 27 | ConstStringBase::SizeType bufferSize //!< The buffer size | ||
| 28 | ) | ||
| 29 | 3 | : ConstStringBase(), m_bufferPtr(bufferPtr), m_bufferSize(bufferSize) { | |
| 30 | 3 | FW_ASSERT(bufferPtr != nullptr); | |
| 31 | 3 | FW_ASSERT(bufferSize > 0, static_cast<FwAssertArgType>(bufferSize)); | |
| 32 | // Assert that the string length plus the null terminator fills the buffer | ||
| 33 | 3 | FW_ASSERT(ConstStringBase::length() + 1 == bufferSize, | |
| 34 | static_cast<FwAssertArgType>(ConstStringBase::length() + 1), | ||
| 35 | static_cast<FwAssertArgType>(bufferSize)); | ||
| 36 | 3 | } | |
| 37 | |||
| 38 | //! Destructor | ||
| 39 | 8 | ~ConstExternalString() {} | |
| 40 | |||
| 41 | public: | ||
| 42 | // ---------------------------------------------------------------------- | ||
| 43 | // ConstStringBase interface | ||
| 44 | // ---------------------------------------------------------------------- | ||
| 45 | |||
| 46 | //! Get the length of the string | ||
| 47 | 10 | ConstStringBase::SizeType length() const override { | |
| 48 | // The length of the string is 1 less than its capacity (string + null terminator) | ||
| 49 |
2/2✓ Branch 4 taken 7 times.
✓ Branch 5 taken 3 times.
|
10 | return this->m_bufferSize == 0 ? 0 : this->m_bufferSize - 1; |
| 50 | } | ||
| 51 | |||
| 52 | //! Gets the char buffer | ||
| 53 | 13 | const char* toChar() const override { return this->m_bufferPtr; } | |
| 54 | |||
| 55 | //! Returns the buffer size | ||
| 56 | 13 | ConstStringBase::SizeType getCapacity() const override { return this->m_bufferSize; } | |
| 57 | |||
| 58 | private: | ||
| 59 | // ---------------------------------------------------------------------- | ||
| 60 | // Data members | ||
| 61 | // ---------------------------------------------------------------------- | ||
| 62 | |||
| 63 | //! Pointer to string buffer | ||
| 64 | const char* m_bufferPtr; | ||
| 65 | //! Size of string buffer | ||
| 66 | //! F Prime strings are null-terminated, so this is one more than | ||
| 67 | //! the length of the largest string that the buffer can hold | ||
| 68 | ConstStringBase::SizeType m_bufferSize; | ||
| 69 | }; | ||
| 70 | |||
| 71 | } // namespace Fw | ||
| 72 | |||
| 73 | #endif | ||
| 74 |