Skip to content

Commit d198bda

Browse files
committed
[feature][QuoteBuffer]: Remove redundant buffer initialization and implement empty/clear methods
Removed unnecessary m_buffer.clear() from QuoteBuffer constructor, as std::string is default-initialized to empty. Added thread-safe empty() and clear() methods to QuoteBuffer for checking and resetting buffer content. Updated documentation for new methods Signed-off-by: Goran Mišković <[email protected]>
1 parent 95cba1c commit d198bda

File tree

2 files changed

+51
-8
lines changed

2 files changed

+51
-8
lines changed

include/QuoteBuffer.hpp

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ namespace e5 {
3737
*/
3838
class QuoteBuffer final : public SyncBridge {
3939

40-
std::string m_buffer{}; ///< The internal string buffer
40+
std::string m_buffer; ///< The internal string buffer
4141

4242
/**
4343
* @struct BufferPayload
@@ -117,8 +117,15 @@ namespace e5 {
117117
*/
118118
void append(std::string data);
119119

120-
// Shared end-of-quote marker for QOTD protocol
121-
static constexpr const char* END_OF_QUOTE_MARKER = "\n--- End of Quote ---\n";
120+
/**
121+
* @brief Returns true if the buffer is empty, false otherwise.
122+
*/
123+
bool empty();
124+
125+
/**
126+
* @brief Clears the buffer content.
127+
*/
128+
void clear();
122129
};
123130

124131
} // namespace e5

src/QuoteBuffer.cpp

Lines changed: 41 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,7 @@ namespace e5 {
2121
*
2222
* @param ctx Shared context manager for synchronized execution
2323
*/
24-
QuoteBuffer::QuoteBuffer(const AsyncCtx &ctx) : SyncBridge(ctx) {
25-
// Initialize with an empty string to ensure it's properly
26-
// null-terminated
27-
m_buffer.clear();
28-
}
24+
QuoteBuffer::QuoteBuffer(const AsyncCtx &ctx) : SyncBridge(ctx) {}
2925

3026
/**
3127
* @brief Executes buffer operations in a thread-safe manner
@@ -126,4 +122,44 @@ namespace e5 {
126122
}
127123
}
128124

125+
/**
126+
* @brief Checks if the buffer is empty
127+
*
128+
* This method checks if the current buffer content is empty.
129+
* Thread-safe through SyncBridge integration, can be called from any core
130+
* or interrupt context.
131+
*
132+
* @return true if the buffer is empty, false otherwise
133+
*/
134+
bool QuoteBuffer::empty() {
135+
std::string result_string;
136+
auto payload = std::make_unique<BufferPayload>();
137+
payload->op = BufferPayload::GET;
138+
payload->result_ptr = &result_string;
139+
if (const auto result = execute(std::move(payload));
140+
result != PICO_OK) {
141+
DEBUGV("[c%d][%llu][ERROR] QuoteBuffer::empty() returned error %d.\n",
142+
rp2040.cpuid(), time_us_64(), result);
143+
}
144+
return result_string.empty();
145+
}
146+
147+
/**
148+
* @brief Clears the buffer content
149+
*
150+
* This method removes the current buffer content, making the buffer empty.
151+
* Thread-safe through SyncBridge integration, can be called from any core
152+
* or interrupt context.
153+
*/
154+
void QuoteBuffer::clear() {
155+
auto payload = std::make_unique<BufferPayload>();
156+
payload->op = BufferPayload::SET;
157+
payload->data = "";
158+
if (const auto result = execute(std::move(payload));
159+
result != PICO_OK) {
160+
DEBUGV("[c%d][%llu][ERROR] QuoteBuffer::clear() returned error %d.\n",
161+
rp2040.cpuid(), time_us_64(), result);
162+
}
163+
}
164+
129165
} // namespace e5

0 commit comments

Comments
 (0)