fprime/Fw/Types/StringBase.hpp
Vince Woo 48e4720419
Created new SerialBufferBase as a parent of SerializeBufferBase (now renamed LinearBufferBase). (#4288)
* Created new SerialBufferBase as a parent of SerializeBufferBase. Renaming interface functions to be less confusing.

* Deprecating copyRawOffset. No direct use-cases in F' core.

* Make SerialBufferBase a true pure virtual interface.

* Changing Serializable to work with SerialBufferBase parent interface.

* Changing copyRaw and copyRawOffset to work with SerialBufferBase

* Updating documentation for SerialBufferBase usage

* Adding some documentation. Adding missing ASSERT in copyRaw. Fixing some bugs that new ASSERT uncovered.

* Renaming SerializeBufferBase to LinearBufferBase. Add a using declaration to maintain backwards compatability. Properly mark LinearBufferBase functions as override.

* Filling in the rest of the docstrings for the classes in Serializable

* Removing redundant virtual keyword on override function

* Applying clang formatting

* Incorporating PR comments

* Fix compile issues

* Bump version to alpha

* Format

* v

---------

Co-authored-by: M Starch <LeStarch@googlemail.com>
2025-11-06 16:23:20 -08:00

106 lines
4.1 KiB
C++

/**
* \file StringBase.hpp
* \author T. Canham
* \brief Declares F Prime string base class
*
* \copyright
* Copyright 2009-2016, by the California Institute of Technology.
* ALL RIGHTS RESERVED. United States Government Sponsorship
* acknowledged.
*
*/
#ifndef FW_STRING_BASE_HPP
#define FW_STRING_BASE_HPP
#include <Fw/FPrimeBasicTypes.hpp>
#include <Fw/Types/Serializable.hpp>
#include <Fw/Types/format.hpp>
#include <cstdarg>
#ifdef BUILD_UT
#include <iostream>
#endif
namespace Fw {
class StringBase : public Serializable {
public:
using SizeType = FwSizeType;
virtual const CHAR* toChar() const = 0; //<! Convert to a C-style char*
virtual SizeType getCapacity() const = 0; //!< return size of buffer
SizeType length() const; //!< Get length of string
//! Get the maximum length of a string that the buffer can hold (which is capacity - 1)
SizeType maxLength() const;
//! Get the static serialized size of a string
//! This is the max length of the string plus the size of the stored size
static constexpr SizeType STATIC_SERIALIZED_SIZE(SizeType maxLength //!< The maximum string length
) {
return static_cast<SizeType>(sizeof(FwSizeStoreType)) + maxLength;
}
//! Get the size of a null-terminated string buffer
static constexpr SizeType BUFFER_SIZE(SizeType maxLength //!< The maximum string length
) {
// Reserve one byte for each character plus one for the null terminator
return maxLength + 1;
}
//! Get the dynamic serialized size of a string
//! This is the length of the string plus the size of the stored size
SizeType serializedSize() const;
//! Get the serialized truncated size of a string
//! This is the minimum of the dynamic serialized size and the max length
SizeType serializedTruncatedSize(FwSizeType maxLength //!< The max string length
) const;
const CHAR* operator+=(const CHAR* src); //!< Concatenate a CHAR*
const StringBase& operator+=(const StringBase& src); //!< Concatenate a StringBase
bool operator==(const StringBase& other) const; //!< Check for equality with StringBase
bool operator==(const CHAR* other) const; //!< Check for equality with CHAR*
bool operator!=(const StringBase& other) const; //!< Inequality with StringBase
bool operator!=(const CHAR* other) const; //!< Inequality with CHAR*
StringBase& operator=(const CHAR* src); //!< Assign CHAR*
StringBase& operator=(const StringBase& src); //!< Assign another StringBase
FormatStatus format(const CHAR* formatString, ...); //!< write formatted string to buffer
FormatStatus vformat(const CHAR* formatString, va_list args); //!< write formatted string to buffer using va_list
SerializeStatus serializeTo(SerialBufferBase& buffer, Endianness mode = Endianness::BIG) const override;
virtual SerializeStatus serializeTo(SerialBufferBase& buffer,
SizeType maxLen,
Endianness mode = Endianness::BIG) const;
SerializeStatus deserializeFrom(SerialBufferBase& buffer, Endianness mode = Endianness::BIG) override;
DEPRECATED(SerializeStatus serialize(SerialBufferBase& buffer) const,
"Use serializeTo(SerializeBufferBase& buffer) instead") {
return this->serializeTo(buffer);
}
DEPRECATED(SerializeStatus serialize(SerialBufferBase& buffer, SizeType maxLen) const,
"Use serializeTo(SerializeBufferBase& buffer, SizeType maxLen) instead") {
return this->serializeTo(buffer, maxLen);
}
#ifdef BUILD_UT
// to support GoogleTest framework in unit tests
friend std::ostream& operator<<(std::ostream& os, const StringBase& str);
#endif
#if FW_SERIALIZABLE_TO_STRING || BUILD_UT
void toString(StringBase& text) const override; //!< write string with contents
#endif
protected:
StringBase();
virtual ~StringBase();
void appendBuff(const CHAR* buff, SizeType size);
private:
StringBase(const StringBase& src) = delete; //!< constructor with buffer as source
};
} // namespace Fw
#endif