Mercurial > archived > louis > epitech > zia-api
view examples/Buffer/Buffer.hpp @ 56:892f7a4277d0
[examples] added missing namespace specifications for some streamsize
author | Zoltan K. |
---|---|
date | Tue, 30 Mar 2010 00:07:44 +0000 |
parents | 1254fa6755a3 |
children |
line wrap: on
line source
#ifndef _BUFFER_HPP_ # define _BUFFER_HPP_ # include <streambuf> # include <vector> /** * @brief Example of a simple implementation of std::streambuf. * * It's purpose is only to show how streambuf works. * * The full documentation is available here: * http://cplusplus.com/reference/iostream/streambuf/ * * According to the official documentation, we will call @b put, all operations * that fill the buffer (such operations are performed by std::ostream) * and we will call @b get, all operations that consume the buffer's content * (operations performed by std::istream) * * The std::streambuf abstract class works with two kind of pointers. @b get * pointers and @b put pointers. * * For each kind of pointer, there are three different pointers: * @li A pointer to the beginning of the sequence; * @li A pointer to the current position; * @li A pointer to the end of the sequence. * * These pointers are sets by two methods: * @li setp (set @b put pointers); * @li setg (set @b get pointers). * * Anyway, these pointers can be moved using @ref seekoff and @ref seekpos. * Any operation that add or remove content from the buffer should move the * appropriate pointer. */ class StreamBufferImpl : public std::streambuf { public: StreamBufferImpl(void); virtual ~StreamBufferImpl(void) { } protected: /** * @brief Set a new buffer for the put/get operations. * * This method is called by pubsetbuf. * * @param [in] s A pointer to the array of char where operations will be * performed. * @param [in] n An integer giving the size of the array. * * In our example, we will store the pointer to the buffer and its size, * and then, set the @b put and @b get pointers using setp and setg. * * @return A pointer to the class itself. */ virtual StreamBufferImpl * setbuf(char *s, std::streamsize n); /** * @brief Set the @b get and/or @b put pointer to a relative * position. * * @param [in] off Offset value. * @param [in] way Where the offset takes effect. The possible values are: * @li @c std::ios_base::beg from the beginning of the stream buffer; * @li @c std::ios_base::cur from the current position in the stream * buffer; * @li @c std::ios_base::end from the end of the stream buffer. * @param [in] which Allow us to choose which pointer should be moved: * @li @c std::ios_base::in Move the @b get pointer; * @li @c std::ios_base::out Move the @b put pointer. * * @return The new position. Errors (like an invalid position) should be * signaled by a value of -1. */ virtual std::streampos seekoff(std::streamoff off, std::ios_base::seekdir way, std::ios_base::openmode which = std::ios_base::in | std::ios_base::out); /** * @brief Set the @b get and/or @b put pointer to an absolute position. * * @param [in] sp The pointer "new position". * @param [in] which Allow us to choose which pointer should be moved: * @li @c std::ios_base::in Move the @b get pointer; * @li @c std::ios_base::out Move the @b put pointer. * * @return The new position. Errors (like an invalid position) should be * signaled by a value of -1. */ virtual std::streampos seekpos(std::streampos sp, std::ios_base::openmode which = std::ios_base::in | std::ios_base::out); /** * @brief Called when there is a need to synchronize the buffer's content * with the ``real'' content it represents (a device or anything else). * * For example when std::endl is applied on a stream. * * In our example, this method do nothing. * * @return Zero in case of success otherwise -1. */ virtual int sync(void); /** * @brief Get the number of characters available once the buffer is empty. * * @return An estimation on the number of characters that could be * added to the buffer if an underflow is made. */ virtual std::streamsize showmanyc(void); /** * @brief Get characters from the buffer. * * @param [in] s Array where characters will be stored. * @param [in] n Number of characters to store. * * @return The number of characters stored. */ virtual std::streamsize xsgetn(char * s, std::streamsize n); /** * @brief Called when there is a need to read data from the buffer but * there no more data available in it. This method doesn't move the @b get * pointer. * * In our example, do nothing since we doesn't want to refill the buffer * when it's empty. * * @return The new character available at the get pointer position, if * any. Otherwise, @c EOF. */ virtual int underflow(void); /** * @brief Similar to underflow except that it moves the @b get * pointer. * * In our example, do nothing since we doesn't want to refill the buffer * when it's empty. * * @return The new character available at the get pointer position, if * any. Otherwise, @c EOF. * * @see underflow */ virtual int uflow(void); /** * @brief The stream class allows to putback characters in the buffer. * * The @ref pbackfail method is called when there is an error while putting * back a character in the buffer. * * @param [in] c The character to put back. * * @return The method returns @c EOF on failure. */ virtual int pbackfail(int c = EOF); /** * @brief Add content to the buffer. * * @param [in] s An array of char to add to the buffer. * @param [in] n The array's length * * @return The number of characters written to the buffer. */ virtual std::streamsize xsputn(const char * s, std::streamsize n); /** * @brief Called when there is no more available space on the buffer. This * method could be used to allocate a new buffer. * * @param [in] c The character to be written. * * @return Any value different than @c EOF in case of success. If the * function fails, it should return an @c EOF or throw an exception. */ virtual int overflow(int c = EOF); private: /** * @brief std::streambuf has a private copy constructor we do the same. */ StreamBufferImpl(const StreamBufferImpl & cpy); /** * @brief This is the buffer our class will fill or consume. */ char * _buffer; /** * @brief Size of _buffer. */ std::streamsize _size; }; #endif // ! _BUFFER_HPP_