fprime/Os/Console.cpp
Ian Brault 2b65cc83cf
Add new Fw::ConstStringBase type for strings backed by immutable string literals (#4269)
* Add new Fw::StringBase type StaticString for strings backed my immutable literals

* Spellcheck fix

* Add disclaimer comment about use of StaticString

* Refactor the StringBase interface into an immutable ConstStringBase abstract base class and the now mutable StringBase class

* Rename StaticString to ConstExternalString and inherit from ConstStringBase

* Fix typo

* Change references from StringBase to ConstStringBase where applicable

* Updates following review meeting: add missing deserialize function and add new error status, move length function implementation into ConstStringBase so it is not pure virtual

* Clang format fix

* Additional clang-format fixes

* Fix the copy-assignment operator for StringBase not being correctly evaluated

* Clang format fix

* Explicitly delete the Serializable assignment operator and provide a skeleton implementation for RawTimeInterface to appease the compiler

* Revert "Explicitly delete the Serializable assignment operator and provide a skeleton implementation for RawTimeInterface to appease the compiler"

This reverts commit 086d7bcd3ca9c4f6e553d7fc34d0d126a69a165b.

* Move ConstStringBase to separate hpp/cpp files, plus other pull request feedback

* Clang format fix

* Update length implementation for ConstStringBase and ConstExternalString

* Improved asserts in ConstExternalString constructor

Co-authored-by: Rob Bocchino <bocchino@icloud.com>

* Fixed ConstStringBase length implementation

Co-authored-by: Rob Bocchino <bocchino@icloud.com>

* Clang format fix

* Add some UTs for ConstExternalString, fix non-overridden interfaces, and fix ConstStringBase::maxLength asserting for zero capacity strings

* Spell-check fix for ConstExternalString UTs

* Revise length implementation in ConstStringBase

If the capacity is zero, return zero

* Format

---------

Co-authored-by: Ian Brault <ian.r.brault@jpl.nasa.gov>
Co-authored-by: Rob Bocchino <bocchino@icloud.com>
Co-authored-by: Rob Bocchino <bocchino@jpl.nasa.gov>
Co-authored-by: M Starch <LeStarch@googlemail.com>
2025-11-07 09:50:05 -08:00

67 lines
2.2 KiB
C++

// ======================================================================
// \title Os/Console.cpp
// \brief common function implementation for Os::Console
// ======================================================================
#include <Fw/Types/Assert.hpp>
#include <Os/Console.hpp>
namespace Os {
Console::Console()
: ConsoleInterface(),
Fw::Logger(),
m_handle_storage(),
m_delegate(*ConsoleInterface::getDelegate(m_handle_storage)) {}
Console::~Console() {
FW_ASSERT(&this->m_delegate == reinterpret_cast<ConsoleInterface*>(&this->m_handle_storage[0]));
m_delegate.~ConsoleInterface();
}
Console::Console(const Console& other)
: m_handle_storage(), m_delegate(*Console::getDelegate(m_handle_storage, &other.m_delegate)) {
FW_ASSERT(&this->m_delegate == reinterpret_cast<Console*>(&this->m_handle_storage[0]));
}
Console& Console::operator=(const Console& other) {
FW_ASSERT(&this->m_delegate == reinterpret_cast<Console*>(&this->m_handle_storage[0]));
if (this != &other) {
this->m_delegate = *ConsoleInterface::getDelegate(m_handle_storage, &other.m_delegate);
}
return *this;
}
void Console::writeMessage(const CHAR* message, const FwSizeType size) {
FW_ASSERT(&this->m_delegate == reinterpret_cast<ConsoleInterface*>(&this->m_handle_storage));
FW_ASSERT(message != nullptr || size == 0);
this->m_delegate.writeMessage(message, size);
}
void Console::writeMessage(const Fw::ConstStringBase& message) {
this->writeMessage(message.toChar(), message.length());
}
ConsoleHandle* Console::getHandle() {
FW_ASSERT(&this->m_delegate == reinterpret_cast<ConsoleInterface*>(&this->m_handle_storage));
return this->m_delegate.getHandle();
}
void Console::write(const CHAR* message, const FwSizeType size) {
Console::getSingleton().writeMessage(message, size);
}
void Console::write(const Fw::ConstStringBase& message) {
Console::getSingleton().writeMessage(message.toChar(), message.length());
}
void Console::init() {
// Force trigger on the fly singleton setup
(void)Console::getSingleton();
}
Console& Console::getSingleton() {
static Console s_singleton;
Fw::Logger::registerLogger(&s_singleton);
return s_singleton;
}
} // namespace Os