base: move 'Buffered_output' class into public header

Fixes #3128
This commit is contained in:
Christian Prochaska 2019-01-22 16:46:52 +01:00 committed by Norman Feske
parent d3759811b6
commit 4b805ccde9
6 changed files with 72 additions and 53 deletions

View File

@ -14,6 +14,7 @@
/* Genode includes */
#include <base/log.h>
#include <base/buffered_output.h>
/* base-internal includes */
#include <base/internal/globals.h>

View File

@ -0,0 +1,68 @@
/*
* \brief Implementation of the output interface that buffers characters
* \author Norman Feske
* \date 2016-05-03
*/
/*
* Copyright (C) 2016-2019 Genode Labs GmbH
*
* This file is part of the Genode OS framework, which is distributed
* under the terms of the GNU Affero General Public License version 3.
*/
#ifndef _INCLUDE__BASE__BUFFERED_OUTPUT_H_
#define _INCLUDE__BASE__BUFFERED_OUTPUT_H_
#include <base/output.h>
namespace Genode { template <size_t, typename> class Buffered_output; }
/**
* Implementation of the output interface that buffers characters
*
* \param BUF_SIZE maximum number of characters to buffer before writing
* \param WRITE_FN functor called to writing the buffered characters to a
* backend.
*
* The 'WRITE_FN' functor is called with a null-terminated 'char const *'
* as argument.
*/
template <Genode::size_t BUF_SIZE, typename BACKEND_WRITE_FN>
class Genode::Buffered_output : public Output
{
private:
BACKEND_WRITE_FN _write_fn;
char _buf[BUF_SIZE];
unsigned _num_chars = 0;
void _flush()
{
/* null-terminate string */
_buf[_num_chars] = 0;
_write_fn(_buf);
/* restart with empty buffer */
_num_chars = 0;
}
public:
Buffered_output(BACKEND_WRITE_FN const &write_fn) : _write_fn(write_fn) { }
void out_char(char c) override
{
/* ensure enough buffer space for complete escape sequence */
if ((c == 27) && (_num_chars + 8 > BUF_SIZE)) _flush();
_buf[_num_chars++] = c;
/* flush immediately on line break */
if (c == '\n' || _num_chars >= sizeof(_buf) - 1)
_flush();
}
};
#endif /* _INCLUDE__BASE__BUFFERED_OUTPUT_H_ */

View File

@ -14,10 +14,10 @@
/* Genode includes */
#include <base/log.h>
#include <base/buffered_output.h>
/* base-internal includes */
#include <base/internal/globals.h>
#include <base/internal/output.h>
#include <base/internal/unmanaged_singleton.h>
/* core includes */

View File

@ -14,7 +14,6 @@
#ifndef _INCLUDE__BASE__INTERNAL__OUTPUT_H_
#define _INCLUDE__BASE__INTERNAL__OUTPUT_H_
#include <base/output.h>
@ -155,53 +154,4 @@ static inline void out_float(T value, unsigned base, unsigned length, OUT_CHAR_F
}
}
namespace Genode { template <size_t, typename> class Buffered_output; }
/**
* Implementation of the output interface that buffers characters
*
* \param BUF_SIZE maximum number of characters to buffer before writing
* \param WRITE_FN functor called to writing the buffered characters to a
* backend.
*
* The 'WRITE_FN' functor is called with a null-terminated 'char const *'
* as argument.
*/
template <Genode::size_t BUF_SIZE, typename BACKEND_WRITE_FN>
class Genode::Buffered_output : public Output
{
private:
BACKEND_WRITE_FN _write_fn;
char _buf[BUF_SIZE];
unsigned _num_chars = 0;
void _flush()
{
/* null-terminate string */
_buf[_num_chars] = 0;
_write_fn(_buf);
/* restart with empty buffer */
_num_chars = 0;
}
public:
Buffered_output(BACKEND_WRITE_FN const &write_fn) : _write_fn(write_fn) { }
void out_char(char c) override
{
/* ensure enough buffer space for complete escape sequence */
if ((c == 27) && (_num_chars + 8 > BUF_SIZE)) _flush();
_buf[_num_chars++] = c;
/* flush immediately on line break */
if (c == '\n' || _num_chars >= sizeof(_buf) - 1)
_flush();
}
};
#endif /* _INCLUDE__BASE__INTERNAL__OUTPUT_H_ */

View File

@ -14,10 +14,10 @@
/* Genode includes */
#include <base/log.h>
#include <log_session/log_session.h>
#include <base/buffered_output.h>
/* base-internal includes */
#include <base/internal/globals.h>
#include <base/internal/output.h>
#include <base/internal/unmanaged_singleton.h>
using namespace Genode;

View File

@ -13,10 +13,10 @@
/* Genode includes */
#include <base/log.h>
#include <base/buffered_output.h>
/* base-internal includes */
#include <base/internal/unmanaged_singleton.h>
#include <base/internal/output.h>
#include <base/internal/raw_write_string.h>