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 <algorithm>
16 #include <cstdarg>
17 #include <cstring>
18 
19 namespace Fw {
20 
22 
24 
26  SizeType length = 0;
27  SizeType capacity = this->getCapacity();
28  SizeType maxLength = this->maxLength(capacity);
29  if (capacity > 0) {
30  length = static_cast<SizeType>(StringUtils::string_length(this->toChar(), capacity));
31  }
32  FW_ASSERT(length <= maxLength, static_cast<FwAssertArgType>(length), static_cast<FwAssertArgType>(maxLength));
33  return length;
34 }
35 
37  const SizeType capacity = this->getCapacity();
38  return maxLength(capacity);
39 }
40 
42  return capacity == 0 ? 0 : capacity - 1;
43 }
44 
46  return static_cast<SizeType>(sizeof(FwSizeStoreType)) + this->length();
47 }
48 
50  return static_cast<SizeType>(sizeof(FwSizeStoreType)) + static_cast<SizeType>(std::min(this->length(), maxLength));
51 }
52 
53 bool ConstStringBase::operator==(const ConstStringBase& other) const {
54  SizeType len = this->length();
55  if (len != other.length()) {
56  return false;
57  } else {
58  return this->operator==(other.toChar());
59  }
60 }
61 
62 bool ConstStringBase::operator==(const CHAR* other) const {
63  const CHAR* const us = this->toChar();
64  if ((us == nullptr) or (other == nullptr)) {
65  return false;
66  }
67 
68  const SizeType capacity = this->getCapacity();
69  const size_t result = static_cast<size_t>(strncmp(us, other, static_cast<size_t>(capacity)));
70  return (result == 0);
71 }
72 
73 bool ConstStringBase::operator!=(const ConstStringBase& other) const {
74  return !operator==(other);
75 }
76 
77 bool ConstStringBase::operator!=(const CHAR* other) const {
78  return !operator==(other);
79 }
80 
82  return buffer.serializeFrom(reinterpret_cast<const U8*>(this->toChar()), this->length());
83 }
84 
86  const FwSizeType len = std::min(maxLength, this->length());
87  // Serialize length and then bytes
88  return buffer.serializeFrom(reinterpret_cast<const U8*>(this->toChar()), len, Serialization::INCLUDE_LENGTH);
89 }
90 
92  // Cannot deserialize into a read-only string
93  // This should be overridden by derived classes
95 }
96 
97 #ifdef BUILD_UT
98 std::ostream& operator<<(std::ostream& os, const ConstStringBase& str) {
99  os << str.toChar();
100  return os;
101 }
102 #endif
103 
104 #if FW_SERIALIZABLE_TO_STRING || BUILD_UT
105 void ConstStringBase::toString(StringBase& text) const {
106  text = *this;
107 }
108 #endif
109 
110 } // 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:60
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
virtual SizeType getCapacity() const =0
Return the size of the buffer.
static U32 min(const U32 a, const U32 b)
Definition: Checksum.cpp:16
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:32
SerializeStatus serializeTo(SerialBufferBase &buffer, Endianness mode=Endianness::BIG) const override
Serialize the contents of this object to a buffer.