Sources exist for different types of objects (as shown above). A StringSources requires no additional information to originate data. Other objects, such as FileSources require additional information such as a filename. Still others, such as a RandomNumberSource require a RandomNumberGenerator and byte count.
The following example demonstrates creation of a FileSource.
FileSource file( filename );
The following example demonstrates reading a file, and placing the contents of the file in a string. This is known as pipelining.
string s; FileSource file( filename, new StringSink( s ) ); cout << s << endl;
The following example performs the same operation as above, but without the variable file.
string s; FileSource( filename, true, new StringSink( s ) ); cout << s << endl;
string s; FileSource( filename, new HexDecoder( new StringSink( s ) ) ); cout << s << endl;
Note that the HexDecoder and StringSink created with new do not require explicit destruction - the FileSource<tt> will call <tt>delete on the HexDecoder, which in turns calls delete on the StringSink when it (the FileSource) is destroyed.
Finally, the example below places 4 random bytes of data into a StringSink after hex encoding using a random number source. As the chaining gets longer, nesting the chaining structure as with if statements offers readability.
AutoSeededRandomPool rng; RandomNumberSource( rng, 4, true, new HexEncoder( new ArraySink( s ) ) // HexEncoder ); // RandomNumberSource
With the type formatting in place, data flow through through the construct is readily apparent.