| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | // ====================================================================== | ||
| 2 | // \title Os/Console.hpp | ||
| 3 | // \brief common function definitions for Os::Console | ||
| 4 | // ====================================================================== | ||
| 5 | #ifndef Os_Console_hpp_ | ||
| 6 | #define Os_Console_hpp_ | ||
| 7 | |||
| 8 | #include <Fw/FPrimeBasicTypes.hpp> | ||
| 9 | #include <Fw/Logger/Logger.hpp> | ||
| 10 | #include <Os/Console.hpp> | ||
| 11 | #include <Os/Os.hpp> | ||
| 12 | |||
| 13 | namespace Os { | ||
| 14 | //! \brief Base class for storing implementation specific handle information | ||
| 15 | struct ConsoleHandle {}; | ||
| 16 | |||
| 17 | // \brief Interface defining the properties of the console | ||
| 18 | class ConsoleInterface { | ||
| 19 | public: | ||
| 20 | //! \brief Default constructor | ||
| 21 | ✗ | ConsoleInterface() = default; | |
| 22 | |||
| 23 | //! \brief Default destructor | ||
| 24 | ✗ | virtual ~ConsoleInterface() = default; | |
| 25 | |||
| 26 | //! \brief write message to console | ||
| 27 | //! | ||
| 28 | //! Write a message to the console with a bounded size. | ||
| 29 | //! | ||
| 30 | //! \param message: raw message to write | ||
| 31 | //! \param size: size of the message to write to the console | ||
| 32 | virtual void writeMessage(const CHAR* message, const FwSizeType size) = 0; | ||
| 33 | |||
| 34 | //! \brief returns the raw console handle | ||
| 35 | //! | ||
| 36 | //! Gets the raw console handle from the implementation. Note: users must include the implementation specific | ||
| 37 | //! header to make any real use of this handle. Otherwise it will be as an opaque type. | ||
| 38 | //! | ||
| 39 | //! \return raw console handle | ||
| 40 | //! | ||
| 41 | virtual ConsoleHandle* getHandle() = 0; | ||
| 42 | |||
| 43 | //! \brief provide a pointer to a console delegate object | ||
| 44 | //! | ||
| 45 | //! This function must return a pointer to a `ConsoleInterface` object that contains the real implementation of | ||
| 46 | //! the console functions as defined by the implementor. This function must do several things to be considered | ||
| 47 | //! correctly implemented: | ||
| 48 | //! | ||
| 49 | //! 1. Assert that their implementation fits within FW_HANDLE_MAX_SIZE. | ||
| 50 | //! e.g. `static_assert(sizeof(PosixFileImplementation) <= sizeof Os::File::m_handle_storage, | ||
| 51 | //! "FW_HANDLE_MAX_SIZE too small");` | ||
| 52 | //! 2. Assert that their implementation aligns within FW_HANDLE_ALIGNMENT. | ||
| 53 | //! e.g. `static_assert((FW_HANDLE_ALIGNMENT % alignof(PosixFileImplementation)) == 0, "Bad handle alignment");` | ||
| 54 | //! 3. If to_copy is null, placement new their implementation into `aligned_placement_new_memory` | ||
| 55 | //! e.g. `FileInterface* interface = new (aligned_placement_new_memory) PosixFileImplementation;` | ||
| 56 | //! 4. If to_copy is non-null, placement new using copy constructor their implementation into | ||
| 57 | //! `aligned_placement_new_memory` | ||
| 58 | //! e.g. `FileInterface* interface = new (aligned_placement_new_memory) PosixFileImplementation(*to_copy);` | ||
| 59 | //! 5. Return the result of the placement new | ||
| 60 | //! e.g. `return interface;` | ||
| 61 | //! | ||
| 62 | //! \return result of placement new, must be equivalent to `aligned_placement_new_memory` | ||
| 63 | //! | ||
| 64 | static ConsoleInterface* getDelegate(ConsoleHandleStorage& aligned_placement_new_memory, | ||
| 65 | const ConsoleInterface* to_copy = nullptr); | ||
| 66 | }; | ||
| 67 | |||
| 68 | class Console : public ConsoleInterface, public Fw::Logger { | ||
| 69 | public: | ||
| 70 | //! \brief Default constructor | ||
| 71 | Console(); | ||
| 72 | |||
| 73 | //! \brief Default destructor | ||
| 74 | ~Console(); | ||
| 75 | |||
| 76 | //! \brief copy constructor that copies the internal representation | ||
| 77 | Console(const Console& other); | ||
| 78 | |||
| 79 | //! \brief assignment operator that copies the internal representation | ||
| 80 | Console& operator=(const Console& other); | ||
| 81 | |||
| 82 | //! \brief write message to console | ||
| 83 | //! | ||
| 84 | //! Write a message to the console with a bounded size. This will delegate to the implementation defined write | ||
| 85 | //! method. | ||
| 86 | //! | ||
| 87 | //! \param message: raw message to write | ||
| 88 | //! \param size: size of the message to write to the console | ||
| 89 | void writeMessage(const CHAR* message, const FwSizeType size) override; | ||
| 90 | |||
| 91 | //! \brief write message to console | ||
| 92 | //! | ||
| 93 | //! Write a message to the console as stored as a ConstStringBase type | ||
| 94 | //! | ||
| 95 | //! \param message: raw message to write (ConstStringBase) | ||
| 96 | void writeMessage(const Fw::ConstStringBase& message) override; | ||
| 97 | |||
| 98 | //! \brief returns the raw console handle | ||
| 99 | //! | ||
| 100 | //! Gets the raw console handle from the implementation. Note: users must include the implementation specific | ||
| 101 | //! header to make any real use of this handle. Otherwise it will be as an opaque type. | ||
| 102 | //! | ||
| 103 | //! \return raw console handle | ||
| 104 | //! | ||
| 105 | ConsoleHandle* getHandle() override; | ||
| 106 | |||
| 107 | //! \brief write message to console | ||
| 108 | //! | ||
| 109 | //! Write a message to the console as stored as a ConstStringBase type | ||
| 110 | //! | ||
| 111 | //! \param message: raw message to write (ConstStringBase) | ||
| 112 | static void write(const Fw::ConstStringBase& message); | ||
| 113 | |||
| 114 | //! \brief write message to the global console | ||
| 115 | //! | ||
| 116 | //! Write a message to the console with a bounded size. This will delegate to the global singleton | ||
| 117 | //! implementation. | ||
| 118 | //! | ||
| 119 | //! \param message: raw message to write | ||
| 120 | //! \param size: size of the message to write to the console | ||
| 121 | static void write(const CHAR* message, const FwSizeType size); | ||
| 122 | |||
| 123 | //! \brief initialize singleton | ||
| 124 | static void init(); | ||
| 125 | |||
| 126 | //! \brief get a reference to singleton | ||
| 127 | //! \return reference to singleton | ||
| 128 | static Console& getSingleton(); | ||
| 129 | |||
| 130 | private: | ||
| 131 | // This section is used to store the implementation-defined console handle. To Os::Console and fprime, this type | ||
| 132 | // is opaque and thus normal allocation cannot be done. Instead, we allow the implementor to store then handle | ||
| 133 | // in the byte-array here and set `handle` to that address for storage. | ||
| 134 | alignas(FW_HANDLE_ALIGNMENT) ConsoleHandleStorage m_handle_storage; // Storage for the delegate | ||
| 135 | ConsoleInterface& m_delegate; //!< Delegate for the real implementation | ||
| 136 | }; | ||
| 137 | } // namespace Os | ||
| 138 | |||
| 139 | #endif | ||
| 140 |