F´ Flight Software - C/C++ Documentation
A framework for building embedded system applications to NASA flight quality standards.
ConstStringBase.cpp
Go to the documentation of this file.
1 
12 #include <Fw/Types/Assert.hpp>
13 #include <Fw/Types/StringType.hpp>
14 #include <Fw/Types/StringUtils.hpp>
15 #include <cstdarg>
16 #include <cstring>
17 
18 namespace Fw {
19 
21 
23 
25  SizeType length = 0;
26  if (this->getCapacity() > 0) {
27  length = static_cast<SizeType>(StringUtils::string_length(this->toChar(), this->getCapacity()));
28  }
29  FW_ASSERT(length <= this->maxLength(), static_cast<FwAssertArgType>(length),
30  static_cast<FwAssertArgType>(this->maxLength()));
31  return length;
32 }
33 
35  const SizeType capacity = this->getCapacity();
36  return capacity == 0 ? 0 : capacity - 1;
37 }
38 
40  return static_cast<SizeType>(sizeof(FwSizeStoreType)) + this->length();
41 }
42 
44  return static_cast<SizeType>(sizeof(FwSizeStoreType)) + static_cast<SizeType>(FW_MIN(this->length(), maxLength));
45 }
46 
47 bool ConstStringBase::operator==(const ConstStringBase& other) const {
48  SizeType len = this->length();
49  if (len != other.length()) {
50  return false;
51  } else {
52  return this->operator==(other.toChar());
53  }
54 }
55 
56 bool ConstStringBase::operator==(const CHAR* other) const {
57  const CHAR* const us = this->toChar();
58  if ((us == nullptr) or (other == nullptr)) {
59  return false;
60  }
61 
62  const SizeType capacity = this->getCapacity();
63  const size_t result = static_cast<size_t>(strncmp(us, other, static_cast<size_t>(capacity)));
64  return (result == 0);
65 }
66 
67 bool ConstStringBase::operator!=(const ConstStringBase& other) const {
68  return !operator==(other);
69 }
70 
71 bool ConstStringBase::operator!=(const CHAR* other) const {
72  return !operator==(other);
73 }
74 
76  return buffer.serializeFrom(reinterpret_cast<const U8*>(this->toChar()), this->length());
77 }
78 
80  const FwSizeType len = FW_MIN(maxLength, this->length());
81  // Serialize length and then bytes
82  return buffer.serializeFrom(reinterpret_cast<const U8*>(this->toChar()), len, Serialization::INCLUDE_LENGTH);
83 }
84 
86  // Cannot deserialize into a read-only string
87  // This should be overridden by derived classes
89 }
90 
91 #ifdef BUILD_UT
92 std::ostream& operator<<(std::ostream& os, const ConstStringBase& str) {
93  os << str.toChar();
94  return os;
95 }
96 #endif
97 
98 #if FW_SERIALIZABLE_TO_STRING || BUILD_UT
99 void ConstStringBase::toString(StringBase& text) const {
100  text = *this;
101 }
102 #endif
103 
104 } // namespace Fw
PlatformSizeType FwSizeType
bool operator!=(const ConstStringBase &other) const
Inequality with ConstStringBase.
virtual const CHAR * toChar() const =0
Convert to a C-style char*.
char CHAR
Definition: BasicTypes.h:59
virtual SerializeStatus serializeFrom(U8 val, Endianness mode=Endianness::BIG)=0
Serialize an 8-bit unsigned integer value.
SerializeStatus
forward declaration for string
SizeType serializedSize() const
U16 FwSizeStoreType
The type used to serialize a size value.
bool operator==(const ConstStringBase &other) const
Check for equality with ConstStringBase.
SizeType serializedTruncatedSize(FwSizeType maxLength) const
#define FW_MIN(a, b)
MIN macro.
Definition: BasicTypes.h:92
virtual SizeType getCapacity() const =0
Return the size of the buffer.
SizeType maxLength() const
Get the maximum length of a string that the buffer can hold (which is capacity - 1) ...
A read-only abstract superclass for StringBase.
Include length as first token in serialization.
virtual SizeType length() const
Get the length of the string.
Implementation of malloc based allocator.
Endianness
Attempted to deserialize into an immutable buffer.
SerializeStatus deserializeFrom(SerialBufferBase &buffer, Endianness mode=Endianness::BIG) override
Deserialize the contents of this object from a buffer.
#define FW_ASSERT(...)
Definition: Assert.hpp:14
FwSizeType string_length(const CHAR *source, FwSizeType buffer_size)
get the length of the source string
Definition: StringUtils.cpp:24
SerializeStatus serializeTo(SerialBufferBase &buffer, Endianness mode=Endianness::BIG) const override
Serialize the contents of this object to a buffer.