Cross-Platform C++

ot::io
class Console

#include "ot/io/Console.h"

Class module providing access to the standard console i/o streams: In (stdin), Out (stdout) and Err (stderr). Instances of this class cannot be instantiated - all methods are static.

Unicode I/O and the Console

The streams provided by the Console class are Unicode character streams. This means that bytes read from the console are first decoded before being made available to the application. Likewise, when the application writes Unicode character strings to a Console output stream, the characters are encoded into a sequence of bytes before being written to the terminal device.

The streams provided by the Console class are created with a default encoding. If this does not match the encoding of the console terminal, the application can configure the Console's encoding via the SetEncoding() method.

Multi-threaded considerations:
All methods can safely be called from multiple concurrent threads.



Method Summary
static PrintWriter& cerr()
         Returns a raw C++ reference to the PrintWriter which is connected to the stderr file handle.
static InputStreamReader& cin()
         Returns a raw C++ reference to the InputStreamReader which is connected to the stdin file handle.
static PrintWriter& cout()
         Returns a raw C++ reference to the PrintWriter which is connected to the stdout file handle.
static RefPtr< PrintWriter > Err()
         Returns a PrintWriter connected to the stderr file handle.
static String GetEncoding()
         Returns the name of the encoding used to encode and decode Unicode characters when written to or read from the console.
static RefPtr< InputStreamReader > In()
         Returns an InputStreamReader connected to the stdin file handle.
static RefPtr< PrintWriter > Out()
         Returns a PrintWriter connected to the stdout file handle.
static void SetEncoding(const String& encoding)
         Configures the console to encode and decode Unicode characters using a specified encoding name.

Method Detail

cerr

static PrintWritercerr()
Returns a raw C++ reference to the PrintWriter which is connected to the stderr file handle. This function has been provided to make it more convenient to use the C++ IO operators on the standard output streams.

   try
   {
       ...
   }
   catch(Exception& e)
   {
       Console::cerr() << e.toString() << endl;
   }

It is unusual for a OpenTop function to return a raw C++ reference to a ManagedObject. However, in this case, OpenTop will ensure that the PrintWriter object exists for the lifetime of the application, thereby removing the need for application code to maintain the reference count.

This function is also remarkable because it deviates from the OpenTop convention of starting static member functions with upper case. This is to be consistent with the standard C++ ostream called cerr.


cin

static InputStreamReadercin()
Returns a raw C++ reference to the InputStreamReader which is connected to the stdin file handle. It is unusual for a OpenTop function to return a raw C++ reference to a ManagedObject. However, in this case, OpenTop will ensure that the InputStreamReader object exists for the lifetime of the application, thereby removing the need for application code to maintain the reference count.

This function is also remarkable because it deviates from the OpenTop convention of starting static member functions with upper case. This is to be consistent with the standard C++ ostream called cin.


cout

static PrintWritercout()
Returns a raw C++ reference to the PrintWriter which is connected to the stdout file handle. This function has been provided to make it more convenient to use the C++ IO operators on the standard output streams.

   int age = 5;
   Console::cout() << OT_T("I am ") << age
                   << OT_T(" years old") << endl;

It is unusual for a OpenTop function to return a raw C++ reference to a ManagedObject. However, in this case, OpenTop will ensure that the PrintWriter object exists for the lifetime of the application, thereby removing the need for application code to maintain the reference count.

This function is also remarkable because it deviates from the OpenTop convention of starting static member functions with upper case. This is to be consistent with the standard C++ ostream called cout.


Err

static RefPtr< PrintWriterErr()
Returns a PrintWriter connected to the stderr file handle.

Multi-threaded considerations:
Can safely be called from multiple concurrent threads.

GetEncoding

static String GetEncoding()
Returns the name of the encoding used to encode and decode Unicode characters when written to or read from the console.

Returns:
the encoding name if set; otherwise the name of OpenTop's default encoding.
Since:
OpenTop 1.5

In

static RefPtr< InputStreamReaderIn()
Returns an InputStreamReader connected to the stdin file handle.

Multi-threaded considerations:
Can safely be called from multiple concurrent threads.

Out

static RefPtr< PrintWriterOut()
Returns a PrintWriter connected to the stdout file handle.

Multi-threaded considerations:
Can safely be called from multiple concurrent threads.

SetEncoding

static void SetEncoding(const String& encoding)
Configures the console to encode and decode Unicode characters using a specified encoding name. The specified encoding name is used when creating the the standard console i/o streams. If any of the streams have already been created (due to an earlier request), the original streams continue to be valid, but they are not altered to use the new encoding. However, subsequent calls to In(), Out() or Err() will receive a new stream which has been configured to use the specified encoding.

Parameters:
encoding - the name of the encoding or an empty String to use a default encoding.
Exceptions:
UnsupportedEncodingException - if the registered CodeConverterFactory is unable to create a CodeConverter for the specified encoding.
Since:
OpenTop 1.5


Cross-Platform C++

Found a bug or missing feature? Please email us at support@elcel.com

Copyright © 2000-2005 ElCel Technology   Trademark Acknowledgements