Line | Branch | Exec | Source |
---|---|---|---|
1 | /** | ||
2 | * @file sys.OutStreamChar.hpp | ||
3 | * @author Sergey Baigudin, sergey@baigudin.software | ||
4 | * @copyright 2022, Sergey Baigudin, Baigudin Software | ||
5 | */ | ||
6 | #ifndef SYS_OUTSTREAMCHAR_HPP_ | ||
7 | #define SYS_OUTSTREAMCHAR_HPP_ | ||
8 | |||
9 | #include "sys.NonCopyable.hpp" | ||
10 | #include "api.OutStream.hpp" | ||
11 | #include "lib.Memory.hpp" | ||
12 | |||
13 | namespace eoos | ||
14 | { | ||
15 | namespace sys | ||
16 | { | ||
17 | |||
18 | /** | ||
19 | * @class OutStreamChar. | ||
20 | * @brief OutStreamChar class. | ||
21 | */ | ||
22 | class OutStreamChar : public NonCopyable, public api::OutStream<char_t> | ||
23 | { | ||
24 | typedef NonCopyable Parent; | ||
25 | |||
26 | public: | ||
27 | |||
28 | /** | ||
29 | * @class Type. | ||
30 | * @brief Type output. | ||
31 | */ | ||
32 | enum Type | ||
33 | { | ||
34 | TYPE_COUT, ///< @brief COUT | ||
35 | TYPE_CERR ///< @brief CERR | ||
36 | }; | ||
37 | |||
38 | /** | ||
39 | * @brief Constructor. | ||
40 | * | ||
41 | * @param type Type output. | ||
42 | */ | ||
43 | 368 | OutStreamChar(Type type) | |
44 | 368 | : NonCopyable() | |
45 | , api::OutStream<char_t>() | ||
46 | 368 | , stream_( NULLPTR ) { | |
47 |
1/2✓ Branch 1 taken 368 times.
✗ Branch 2 not taken.
|
368 | bool_t const isConstructed( construct(type) ); |
48 | 368 | setConstructed( isConstructed ); | |
49 | 368 | } | |
50 | |||
51 | /** | ||
52 | * @brief Destructor. | ||
53 | */ | ||
54 | 736 | virtual ~OutStreamChar() | |
55 | { | ||
56 | } | ||
57 | |||
58 | /** | ||
59 | * @copydoc eoos::api::Object::isConstructed() | ||
60 | */ | ||
61 | 1116 | virtual bool_t isConstructed() const ///< SCA MISRA-C++:2008 Justified Rule 10-3-1 | |
62 | { | ||
63 | 1116 | return Parent::isConstructed(); | |
64 | } | ||
65 | |||
66 | /** | ||
67 | * @copydoc eoos::api::OutStream::operator<<() | ||
68 | */ | ||
69 | 8 | virtual OutStream<char_t>& operator<<(char_t const* source) | |
70 | { | ||
71 |
1/2✓ Branch 1 taken 8 times.
✗ Branch 2 not taken.
|
8 | if( isConstructed() ) |
72 | { | ||
73 | 8 | static_cast<void>( ::fputs(source, stream_) ); | |
74 | } | ||
75 | 8 | return *this; | |
76 | } | ||
77 | |||
78 | /** | ||
79 | * @copydoc eoos::api::OutStream::flush() | ||
80 | */ | ||
81 | 370 | virtual OutStream<char_t>& flush() | |
82 | { | ||
83 |
1/2✓ Branch 1 taken 370 times.
✗ Branch 2 not taken.
|
370 | if( isConstructed() ) |
84 | { | ||
85 | 370 | static_cast<void>( ::fflush(stream_) ); | |
86 | } | ||
87 | 370 | return *this; | |
88 | } | ||
89 | |||
90 | private: | ||
91 | |||
92 | /** | ||
93 | * @brief Constructor. | ||
94 | * | ||
95 | * @param type Type output. * | ||
96 | * @return True if object has been constructed successfully. | ||
97 | */ | ||
98 | 368 | bool_t construct(Type type) | |
99 | { | ||
100 | 368 | bool_t res( false ); | |
101 | while(true) | ||
102 | { | ||
103 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 368 times.
|
368 | if( !isConstructed() ) |
104 | { ///< UT Justified Branch: HW dependency | ||
105 | ✗ | break; | |
106 | } | ||
107 |
2/2✓ Branch 0 taken 184 times.
✓ Branch 1 taken 184 times.
|
368 | if(type == TYPE_COUT) |
108 | { | ||
109 | 184 | stream_ = ::stdout; | |
110 | } | ||
111 | else | ||
112 | { | ||
113 | 184 | stream_ = ::stderr; | |
114 | } | ||
115 | 368 | res = true; | |
116 | 368 | break; | |
117 | } | ||
118 | 368 | return res; | |
119 | } | ||
120 | |||
121 | /** | ||
122 | * @brief Output stream. | ||
123 | */ | ||
124 | ::FILE* stream_; | ||
125 | |||
126 | }; | ||
127 | |||
128 | } // namespace sys | ||
129 | } // namespace eoos | ||
130 | #endif // SYS_OUTSTREAMCHAR_HPP_ | ||
131 |