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  if (this->getCapacity() > 0) {
28  length = static_cast<SizeType>(StringUtils::string_length(this->toChar(), this->getCapacity()));
29  }
30  FW_ASSERT(length <= this->maxLength(), static_cast<FwAssertArgType>(length),
31  static_cast<FwAssertArgType>(this->maxLength()));
32  return length;
33 }
34 
36  const SizeType capacity = this->getCapacity();
37  return capacity == 0 ? 0 : capacity - 1;
38 }
39 
41  return static_cast<SizeType>(sizeof(FwSizeStoreType)) + this->length();
42 }
43 
45  return static_cast<SizeType>(sizeof(FwSizeStoreType)) + static_cast<SizeType>(std::min(this->length(), maxLength));
46 }
47 
48 bool ConstStringBase::operator==(const ConstStringBase& other) const {
49  SizeType len = this->length();
50  if (len != other.length()) {
51  return false;
52  } else {
53  return this->operator==(other.toChar());
54  }
55 }
56 
57 bool ConstStringBase::operator==(const CHAR* other) const {
58  const CHAR* const us = this->toChar();
59  if ((us == nullptr) or (other == nullptr)) {
60  return false;
61  }
62 
63  const SizeType capacity = this->getCapacity();
64  const size_t result = static_cast<size_t>(strncmp(us, other, static_cast<size_t>(capacity)));
65  return (result == 0);
66 }
67 
68 bool ConstStringBase::operator!=(const ConstStringBase& other) const {
69  return !operator==(other);
70 }
71 
72 bool ConstStringBase::operator!=(const CHAR* other) const {
73  return !operator==(other);
74 }
75 
77  return buffer.serializeFrom(reinterpret_cast<const U8*>(this->toChar()), this->length());
78 }
79 
81  const FwSizeType len = std::min(maxLength, this->length());
82  // Serialize length and then bytes
83  return buffer.serializeFrom(reinterpret_cast<const U8*>(this->toChar()), len, Serialization::INCLUDE_LENGTH);
84 }
85 
87  // Cannot deserialize into a read-only string
88  // This should be overridden by derived classes
90 }
91 
92 #ifdef BUILD_UT
93 std::ostream& operator<<(std::ostream& os, const ConstStringBase& str) {
94  os << str.toChar();
95  return os;
96 }
97 #endif
98 
99 #if FW_SERIALIZABLE_TO_STRING || BUILD_UT
100 void ConstStringBase::toString(StringBase& text) const {
101  text = *this;
102 }
103 #endif
104 
105 } // 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
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:24
SerializeStatus serializeTo(SerialBufferBase &buffer, Endianness mode=Endianness::BIG) const override
Serialize the contents of this object to a buffer.