| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | // ====================================================================== | ||
| 2 | // @file ExternalString.hpp | ||
| 3 | // @author Robert Bocchino | ||
| 4 | // @brief A string backed by an external buffer | ||
| 5 | // ====================================================================== | ||
| 6 | |||
| 7 | #ifndef FW_EXTERNAL_STRING_HPP | ||
| 8 | #define FW_EXTERNAL_STRING_HPP | ||
| 9 | |||
| 10 | #include <Fw/FPrimeBasicTypes.hpp> | ||
| 11 | |||
| 12 | #include "Fw/Types/StringBase.hpp" | ||
| 13 | |||
| 14 | namespace Fw { | ||
| 15 | |||
| 16 | //! A string backed by an external buffer | ||
| 17 | class ExternalString final : public Fw::StringBase { | ||
| 18 | public: | ||
| 19 | // ---------------------------------------------------------------------- | ||
| 20 | // Construction and destruction | ||
| 21 | // ---------------------------------------------------------------------- | ||
| 22 | |||
| 23 | //! Deleted copy constructor | ||
| 24 | ExternalString(const ExternalString&) = delete; | ||
| 25 | |||
| 26 | //! Constructor (uninitialized buffer) | ||
| 27 | ExternalString() : StringBase(), m_bufferPtr(nullptr), m_bufferSize(0) {} | ||
| 28 | |||
| 29 | //! Constructor (bufferPtr and bufferSize) | ||
| 30 | ✗ | ExternalString(char* bufferPtr, //!< The buffer pointer | |
| 31 | StringBase::SizeType bufferSize //!< The buffer size | ||
| 32 | ) | ||
| 33 | ✗ | : StringBase(), m_bufferPtr(bufferPtr), m_bufferSize(bufferSize) { | |
| 34 | ✗ | *this = ""; | |
| 35 | ✗ | } | |
| 36 | |||
| 37 | //! Constructor (bufferPtr, bufferSize, and StringBase) | ||
| 38 | ExternalString(char* bufferPtr, //!< The buffer pointer | ||
| 39 | StringBase::SizeType bufferSize, //!< The buffer size | ||
| 40 | const StringBase& sb //!< The source string | ||
| 41 | ) | ||
| 42 | : StringBase(), m_bufferPtr(bufferPtr), m_bufferSize(bufferSize) { | ||
| 43 | *this = sb; | ||
| 44 | } | ||
| 45 | |||
| 46 | //! Constructor (bufferPtr, bufferSize, and const char*) | ||
| 47 | ExternalString(char* bufferPtr, //!< The buffer pointer | ||
| 48 | StringBase::SizeType bufferSize, //!< The buffer size | ||
| 49 | const char* str //!< The source string | ||
| 50 | ) | ||
| 51 | : StringBase(), m_bufferPtr(bufferPtr), m_bufferSize(bufferSize) { | ||
| 52 | *this = str; | ||
| 53 | } | ||
| 54 | |||
| 55 | //! Destructor | ||
| 56 | ✗ | ~ExternalString() {} | |
| 57 | |||
| 58 | public: | ||
| 59 | // ---------------------------------------------------------------------- | ||
| 60 | // StringBase interface | ||
| 61 | // ---------------------------------------------------------------------- | ||
| 62 | |||
| 63 | //! Gets the char buffer | ||
| 64 | //! @return The char buffer | ||
| 65 | ✗ | const char* toChar() const { return this->m_bufferPtr; } | |
| 66 | |||
| 67 | //! Returns the buffer size | ||
| 68 | //! @return The buffer size | ||
| 69 | ✗ | StringBase::SizeType getCapacity() const { return this->m_bufferSize; } | |
| 70 | |||
| 71 | public: | ||
| 72 | // ---------------------------------------------------------------------- | ||
| 73 | // Public interface | ||
| 74 | // ---------------------------------------------------------------------- | ||
| 75 | |||
| 76 | //! Set the buffer and initialize it to the empty string | ||
| 77 | void setBuffer(char* bufferPtr, //!< The buffer pointer | ||
| 78 | StringBase::SizeType bufferSize //!< The buffer size | ||
| 79 | ) { | ||
| 80 | this->m_bufferPtr = bufferPtr; | ||
| 81 | this->m_bufferSize = bufferSize; | ||
| 82 | *this = ""; | ||
| 83 | } | ||
| 84 | |||
| 85 | public: | ||
| 86 | // ---------------------------------------------------------------------- | ||
| 87 | // Operators | ||
| 88 | // ---------------------------------------------------------------------- | ||
| 89 | |||
| 90 | // Operator= (const ExternalString&) | ||
| 91 | ExternalString& operator=(const ExternalString& src) { | ||
| 92 | (void)StringBase::operator=(src); | ||
| 93 | return *this; | ||
| 94 | } | ||
| 95 | |||
| 96 | // Operator= (const ConstStringBase&) | ||
| 97 | ExternalString& operator=(const ConstStringBase& src) { | ||
| 98 | (void)StringBase::operator=(src); | ||
| 99 | return *this; | ||
| 100 | } | ||
| 101 | |||
| 102 | // const char* assignment operator | ||
| 103 | ✗ | ExternalString& operator=(const char* src) { | |
| 104 | ✗ | (void)StringBase::operator=(src); | |
| 105 | ✗ | return *this; | |
| 106 | } | ||
| 107 | |||
| 108 | private: | ||
| 109 | // ---------------------------------------------------------------------- | ||
| 110 | // Data members | ||
| 111 | // ---------------------------------------------------------------------- | ||
| 112 | |||
| 113 | //! Pointer to string buffer | ||
| 114 | char* m_bufferPtr; | ||
| 115 | |||
| 116 | //! Size of string buffer | ||
| 117 | //! F Prime strings are null-terminated, so this is one more than | ||
| 118 | //! the length of the largest string that the buffer can hold | ||
| 119 | StringBase::SizeType m_bufferSize; | ||
| 120 | }; | ||
| 121 | } // namespace Fw | ||
| 122 | |||
| 123 | #endif | ||
| 124 |