rlmm::basic_streambuf< Lock > Struct Template Reference

#include <rlmm/streambuf.hh>

Inheritance diagram for rlmm::basic_streambuf< Lock >:

Inheritance graph
[legend]
Collaboration diagram for rlmm::basic_streambuf< Lock >:

Collaboration graph
[legend]
List of all members.

Detailed Description

template<typename Lock = single_thread>
struct rlmm::basic_streambuf< Lock >

Stream buffer reading from readline.

Using this stream buffer layer, you can use a standard std::istream to read lines from the via readline.

Internally, the buffer uses the global instance of rlmm::readline, as it really makes the code simpler.

Note:
This stream buffer always returns the newline from readline. If it didn't, you'd never know when one line start and one ends. This requries a bit more bookkeeping but is worth the effort.
Example
Below is shown an example of using this class template.
namespace examples 
{
  /** Class tha shows the stream buffer layer. */
  class streamer 
  {
  private:
    /// Readline stream to read from.
    std::istream _in;
  public:
    /** CTOR. 
        Initialise our readline stream with a rlmm::streambuf
        object. */ 
    streamer() : _in(new rlmm::streambuf("streamer> ")) {}
    /// Do the character extraction example
    void do_char() { 
      char c; while((_in >> noskipws >> c)) cout << c; 
      cout << flush;
    }
    /// Do the string extraction example
    void do_string() { 
      string s; while((_in >> s)) cout << s << flush; }
    /// Do the single character get example
    void do_get() { 
      char g; 
      while((g = _in.get()) != EOF) cout << g;


Public Types

typedef basic_readline< Lock > readline_type

Public Member Functions

 basic_streambuf (const std::string &prompt=std::string(), bool use_history=true, char *buf=0, std::streamsize n=0)
virtual std::string & prompt (const std::string &p)
virtual const std::string & prompt () const

Protected Member Functions

virtual bool internal_read ()
virtual int_type pbackfail (int_type c=EOF)
virtual int_type showmanyc () const
virtual int_type uflow ()
virtual int_type underflow ()
virtual std::streamsize xsgetn (char_type *buf, std::streamsize n)

Protected Attributes

bool _use_history
std::string _prompt
std::string _line
std::string::iterator _current


Member Typedef Documentation

template<typename Lock = single_thread>
typedef basic_readline<Lock> rlmm::basic_streambuf< Lock >::readline_type
 


Constructor & Destructor Documentation

template<typename Lock>
rlmm::basic_streambuf< Lock >::basic_streambuf const std::string &  prompt = std::string(),
bool  use_history = true,
char *  buf = 0,
std::streamsize  n = 0
[inline]
 

Constructor.

Allocates a new stream buffer reading via readline.

Parameters:
prompt The prompt to display.
use_history Whether to use history
buf externally allocated buffer for buffered reads. If this is zero, the reading will not be buffered.
n Size of the externally allocated buffer.


Member Function Documentation

template<typename Lock>
bool rlmm::basic_streambuf< Lock >::internal_read  )  [inline, protected, virtual]
 

Read a line via readline and store it.

This member uses the C library readline function to read a line from the input.

Returns:
true if a non-empty line was read, false otherwise.

template<typename Lock>
basic_streambuf< Lock >::int_type rlmm::basic_streambuf< Lock >::pbackfail int_type  c = EOF  )  [inline, protected, virtual]
 

Put back a character in the buffer.

If we're at the begining of line read, expand the buffer at start and insert c there; otherwise, simply back up one character in the read line.

Parameters:
c the character to put back.
Returns:
EOF in case of errors. c otherwise.

template<typename Lock = single_thread>
virtual const std::string& rlmm::basic_streambuf< Lock >::prompt  )  const [inline, virtual]
 

Get the prompt.

template<typename Lock = single_thread>
virtual std::string& rlmm::basic_streambuf< Lock >::prompt const std::string &  p  )  [inline, virtual]
 

Set the prompt.

Parameters:
p The prompt to use.

template<typename Lock = single_thread>
virtual int_type rlmm::basic_streambuf< Lock >::showmanyc  )  const [inline, protected, virtual]
 

Return the number of characters avaliable in internal buffer.

Returns:
# of characters avaliable in input sequence.

template<typename Lock>
basic_streambuf< Lock >::int_type rlmm::basic_streambuf< Lock >::uflow  )  [inline, protected, virtual]
 

Return next character from input sequence.

Returns next character from pending sequence and pushes it back to on read sequence.

Returns:
pending character.

template<typename Lock>
basic_streambuf< Lock >::int_type rlmm::basic_streambuf< Lock >::underflow  )  [inline, protected, virtual]
 

Return next pending character.

If the sequence is empty, read a new line from terminal. If this again results in an empty line, repeat.

Returns:
pending character in sequence.

template<typename Lock>
std::streamsize rlmm::basic_streambuf< Lock >::xsgetn char_type *  buf,
std::streamsize  n
[inline, protected, virtual]
 

Get a number of characters from sequence.

Get a block of characters from the input sequence, and put them in the passed buffer buf which is of size n. The maximum of n or # of pending characters are read into the passed buffer. The iterators are updated accordingly.

Parameters:
buf the buffer to read into.
n the size of the buffer buf.
Returns:
# of copied characters.


Member Data Documentation

template<typename Lock = single_thread>
std::string::iterator rlmm::basic_streambuf< Lock >::_current [protected]
 

Current pending point in buffer Pointer into sequence, marking the start of avaliable characters.

template<typename Lock = single_thread>
std::string rlmm::basic_streambuf< Lock >::_line [protected]
 

The line read from readline (the sequence).

template<typename Lock = single_thread>
std::string rlmm::basic_streambuf< Lock >::_prompt [protected]
 

The prompt to use.

template<typename Lock = single_thread>
bool rlmm::basic_streambuf< Lock >::_use_history [protected]
 

Whether to use history.


The documentation for this struct was generated from the following file:
Top of page
Last update Fri Aug 26 15:19:53 2005
Christian Holm
Created by DoxyGen 1.4.4