Using the Adapter Classes for Input
The following example implements the previous example’s corresponding input operation. It creates a RWStreambufFromCharInputStream object that connects to an instance of class RWCharFromStreambufInputStreamImp, which in turn uses an iostreams filebuf as a source of narrow characters. The instance of the adapter class RWStreambufFromCharInputStream is used to construct an iostreams object of type istream, which is used as the source of data in the rest of the example.
Figure 18 is a representation of the chain of streaming elements used in this example.
Figure 18 – Streaming with adapter classes—input
The complete example is located in directory ...\examples\stream in the file adapterRead.cpp. Only part of the code is presented below.
 
filebuf fbuf; // 1
fbuf.open("adaptorWrite.dat", ios::in);
 
RWCharInputStream charInputStream =
RWCharFromStreambufInputStreamImp::make(fbuf); // 2
 
RWStreambufFromCharInputStream streambufAdaptor(charInputStream);// 3
 
istream is(&streambufAdaptor); // 4
 
try {
double f;
int i;
char text[40];
 
// extract a bunch of things
is >> f >> i >> text; // 5
 
cout << '\n' << f << '\n' << i << '\n' << text << '\n' << endl;
}
catch(const RWIncompleteStreamOperation& e) { // 6
cout << e.why() << endl;
cout << e.elementsProcessed() << endl;
}
catch(const RWExternalStreamException& e) {
cout << e.why() << endl;
}
//1 Creates an iostreams file buffer, and opens it in input mode. If you built the Advanced Tools Module with the Standard iostreams library, you need to qualify filebuf and other iostreams elements with std::, or you need to include using declarations. The complete example makes use of macros defined by the Essential Tools Module in order to support both the classic and Standard iostreams.
//2 Creates an instance of class RWCharFromStreambufInputStreamImp. The class is created by calling its static member function make(), which creates an instance of self and returns it as a narrow character input stream handle. In this example the make() function takes a reference to an iostreams streambuf object that is used as the source of narrow characters.
//3 Creates the adapter class that implement the iostreams streambuf interface and forwards a request to an Advanced Tools Module narrow character input stream.
//4 Creates an iostreams istream object that uses the previously created adapter class as the source of narrow characters.
//5 Uses the istream object.
//6 Catches exceptions potentially thrown by the stream. The Streams package defines two exception classes: RWExternalStreamException and RWIncompleteStreamOperation. Class RWExternalStreamException returns an error message and an error code. Class RWIncompleteStreamOperation inherits from class RWExternalStreamException and is thrown when an operation partially succeeds. An instance of class RWIncompleteStreamOperation can be queried for the number of elements successfully read. For more information on exceptions, see Error Handling.