F´ Flight Software - C/C++ Documentation
A framework for building embedded system applications to NASA flight quality standards.
ConstExternalString.hpp
Go to the documentation of this file.
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 
10 #include <Fw/Types/Assert.hpp>
11 #include <Fw/Types/StringBase.hpp>
12 
13 namespace Fw {
14 
16 class ConstExternalString final : public ConstStringBase {
17  public:
18  // ----------------------------------------------------------------------
19  // Construction and destruction
20  // ----------------------------------------------------------------------
21 
23  ConstExternalString() : ConstStringBase(), m_bufferPtr(nullptr), m_bufferSize(0) {}
24 
26  ConstExternalString(const char* bufferPtr,
27  ConstStringBase::SizeType bufferSize
28  )
29  : ConstStringBase(), m_bufferPtr(bufferPtr), m_bufferSize(bufferSize) {
30  FW_ASSERT(bufferPtr != nullptr);
31  FW_ASSERT(bufferSize > 0, static_cast<FwAssertArgType>(bufferSize));
32  // Assert that the string length plus the null terminator fills the buffer
33  FW_ASSERT(ConstStringBase::length() + 1 == bufferSize,
34  static_cast<FwAssertArgType>(ConstStringBase::length() + 1),
35  static_cast<FwAssertArgType>(bufferSize));
36  }
37 
40 
41  public:
42  // ----------------------------------------------------------------------
43  // ConstStringBase interface
44  // ----------------------------------------------------------------------
45 
47  ConstStringBase::SizeType length() const override {
48  // The length of the string is 1 less than its capacity (string + null terminator)
49  return this->m_bufferSize == 0 ? 0 : this->m_bufferSize - 1;
50  }
51 
53  const char* toChar() const override { return this->m_bufferPtr; }
54 
56  ConstStringBase::SizeType getCapacity() const override { return this->m_bufferSize; }
57 
58  private:
59  // ----------------------------------------------------------------------
60  // Data members
61  // ----------------------------------------------------------------------
62 
64  const char* m_bufferPtr;
68  ConstStringBase::SizeType m_bufferSize;
69 };
70 
71 } // namespace Fw
72 
73 #endif
A string backed by an immutable string literal.
ConstStringBase::SizeType getCapacity() const override
Returns the buffer size.
ConstStringBase::SizeType length() const override
Get the length of the string.
A read-only abstract superclass for StringBase.
virtual SizeType length() const
Get the length of the string.
ConstExternalString()
Constructor (uninitialized buffer)
Declares F Prime string base class.
Implementation of malloc based allocator.
#define FW_ASSERT(...)
Definition: Assert.hpp:14
ConstExternalString(const char *bufferPtr, ConstStringBase::SizeType bufferSize)
Constructor (bufferPtr and bufferSize)
const char * toChar() const override
Gets the char buffer.