Main Page | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Class Members | File Members | Related Pages

Stdout.d

Go to the documentation of this file.
00001 /*******************************************************************************
00002 
00003         @file Stdout.d
00004         
00005         Copyright (c) 2004 Kris Bell
00006         
00007         This software is provided 'as-is', without any express or implied
00008         warranty. In no event will the authors be held liable for damages
00009         of any kind arising from the use of this software.
00010         
00011         Permission is hereby granted to anyone to use this software for any 
00012         purpose, including commercial applications, and to alter it and/or 
00013         redistribute it freely, subject to the following restrictions:
00014         
00015         1. The origin of this software must not be misrepresented; you must 
00016            not claim that you wrote the original software. If you use this 
00017            software in a product, an acknowledgment within documentation of 
00018            said product would be appreciated but is not required.
00019 
00020         2. Altered source versions must be plainly marked as such, and must 
00021            not be misrepresented as being the original software.
00022 
00023         3. This notice may not be removed or altered from any distribution
00024            of the source.
00025 
00026         4. Derivative works are permitted, but they must carry this notice
00027            in full and credit the original source.
00028 
00029 
00030                         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
00031 
00032        
00033         @version        Initial version, Feb 2005      
00034         @author         Kris
00035 
00036 
00037 *******************************************************************************/
00038 
00039 module mango.io.Stdout;
00040 
00041 private import  mango.io.Console,
00042                 mango.io.FlushWriter,
00043                 mango.io.DeviceConduit;
00044 
00045                 // for CR
00046 public import   mango.io.Writer;        
00047 
00048 
00049 /*******************************************************************************
00050 
00051         The ubiquitous console IO support. These are standard Conduit 
00052         instances, with Reader/Writer wrappers applied appropriately. Note 
00053         that the outputs use FlushBuffer to automatically flush data as it 
00054         is added to the buffer. The basic usage of this module is illustrated 
00055         below:
00056 
00057         @code
00058         char[] msg = "on the console";
00059 
00060         Stdout ("print ") (1) (' ') ("message ") (msg) (CR);
00061         @endcode
00062 
00063         An alternative is to use put() notation like so:
00064 
00065         @code
00066         char[] msg = "on the console";
00067 
00068         Stdout.put ("print ")
00069               .put (1)
00070               .put (' ')
00071               .put ("message ")
00072               .put (msg)
00073               .put (CR);
00074         @endcode
00075 
00076         Another alternative is to use the C++ iostream operators like so:
00077 
00078         @code
00079         char[] msg = "on the console";
00080 
00081         Stdout << "print "
00082                << 1
00083                << ' '
00084                << "message "
00085                << msg
00086                << CR;
00087         @endcode
00088 
00089         Since console idioms are based upon Conduit, you can use them 
00090         as direct targets for stream-oriented operations. For example, 
00091         the code:
00092 
00093         @code
00094           FileConduit from = new FileConduit ("myfile.txt");
00095           Stdout.conduit.copy (from);
00096         @endcode
00097 
00098         copies a text file directly to the console. Likewise, you can 
00099         copy console input directly to a FileConduit or a SocketConduit.
00100         Input via Stdin is similar in nature, but uses the Token classes
00101         to isolate and parse each token on an input line:
00102 
00103         @code
00104         Stdout ("please input a number: ") ();
00105         int x;
00106         Stdin (x);          
00107         @endcode
00108 
00109         @code
00110         Stdout ("please enter your name: ") ();
00111         char[] you;
00112         Stdin (you);           
00113         Stdout ("Hello ") (you) (CR);
00114         @endcode
00115 
00116         Stdout automatically flushes the output when it sees a CR, so you 
00117         may need to flush the output manually where a CR is not desired.
00118         This is the case in the above example, so we use the empty () to
00119         request a flush (which is actually an alias for the flush method).
00120 
00121         Note that Stdin awaits a carriage-return before parsing the input
00122         into the targets. Note also that the Stdout and Stderr are not written 
00123         to be thread-safe. As such you may find that output from two threads 
00124         intersect across each other. If this is a problem you should wrap a 
00125         synchronized block around the offending entity, like so:
00126 
00127         @code
00128         synchronized (Stdout)
00129                       Stdout ("this is ") ("'atomic' ") (" output") (CR);
00130         @endcode
00131         
00132         Alternatively, please consider using the mango.log (Logger) package 
00133         to provide detailed runtime diagnostics from your application. The 
00134         functionality exposed there is likely sufficient for most application 
00135         needs.
00136 
00137         Redirecting the standard IO handles (via a shell) operates as one 
00138         would expect.
00139 
00140 *******************************************************************************/
00141 
00142 class ConsoleWriter : FlushWriter
00143 {
00144         /***********************************************************************
00145         
00146                 Standard output conduits. 
00147 
00148         ***********************************************************************/
00149 
00150         ConsoleConduit conduit;
00151 
00152         /***********************************************************************
00153         
00154                 Prohibit instantiation of this class 
00155 
00156         ***********************************************************************/
00157 
00158         private this (ConsoleConduit conduit)
00159         {
00160                 super (this.conduit = conduit);
00161         }
00162 }
00163 
00164 /*******************************************************************************
00165 
00166         Standard IO writers. These are exposed at global scope
00167 
00168 *******************************************************************************/
00169 
00170 static ConsoleWriter Stdout,
00171                      Stderr;
00172 
00173 static this ()
00174 {
00175         Stdout = new ConsoleWriter (Cout.conduit);
00176         Stderr = new ConsoleWriter (Cerr.conduit);
00177 }

Generated on Mon Nov 14 10:59:41 2005 for Mango by  doxygen 1.4.0