- Libraries Shared Across Languages
- Namespaces in C++
- The System Namespace
- Other Useful Namespaces
- In Brief
There are plenty of other useful classes contained in sub-namespaces of the System namespace. Some are covered elsewhere in this book. Four are covered here because almost every .NET developer is likely to use them: System::IO, System::Text, System::Collections, and System::Threading.
The System::IO Namespace
Getting information from users and providing it to them is the sort of task that can be incredibly simple (like reading a string and echoing it back to the users) or far more complex. The most basic operations are in the Console class in the System namespace. More complicated tasks are in the System::IO namespace. This namespace includes 27 classes, as well as some structures and other related utilities. They handle tasks such as
Reading and writing to a file
Binary reads and writes (bytes or blocks of bytes)
Creating, deleting, renaming, or moving files
Working with directories
This snippet uses the FileInfo class to determine whether a file exists, and then deletes it if it does:
System::IO::FileInfo* fi = new System::IO::FileInfo("c:\\test.txt"); if (fi->Exists) fi->Delete();
This snippet writes a string to a file:
System::IO::StreamWriter* streamW = new System::IO::StreamWriter("c:\\test.txt"); streamW->Write("Hi there" ); streamW->Close();
Be sure to close all files, readers, and writers when you have finished with them. The garbage collector might not finalize the streamW instance for a long time, and the file stays open until you explicitly close it or until the instance that opened it is finalized.
When Typing Strings with Backslashes
The backslash character (\) in the filename must be "escaped" by placing another backslash before it. Otherwise the combination \t will be read as a tab character. This is standard C++ behavior when typing strings with backslashes.
Check the documentation to learn more about IO classes that you can use in console applications, Windows applications, and class libraries. Keep in mind also that many classes can persist themselves to and from a file, or to and from a stream of XML.
The System::Text Namespace
Just as Console offers simple input and output abilities, the simplest string work can be tackled with just the String class from the System namespace. More complicated work involves the System::Text namespace. You've already seen System::Text::StringBuilder. Other classes in this namespace handle conversions between different types of text, such as Unicode and ASCII.
The System::Text::RegularExpressions namespace lets you use regular expressions in string manipulations and elsewhere. Here is a function that determines whether a string passed to it is a valid US ZIP code:
using namespace System; using namespace System::Text::RegularExpressions; // . . . String* Check(String* code) { String* error = S"OK"; Match* m; switch (code->get_Length()) { case 5: Regex* fivenums; fivenums = new Regex("\\d\\d\\d\\d\\d"); m = fivenums->Match(code); if (!m->Success) error = S"Non numeric characters in 5 digit code"; break; case 10: Regex* fivedashfour; fivedashfour = new Regex("\\d\\d\\d\\d\\d-\\d\\d\\d\\d"); m = fivedashfour->Match(code); if (!m->Success) error = S"Not a valid zip+4 code"; break; default: error = S"invalid length"; } return error; }
The Regex class represents a pattern, such as "five numbers" or "three letters." The Match class represents a possible match between a particular string and a particular pattern. This code checks the string against two patterns representing the two sets of rules for ZIP codes.
The syntax for regular expressions in the .NET class libraries will be familiar to developers who have used regular expression as MFC programmers, or even as UNIX users. In addition to using regular expressions with classes from the System::Text namespace, you can use them in the Find and Replace dialog boxes of the Visual Studio editor, and with ASP.NET validation controls. It's worth learning how they work.
Regular Expression Syntax
A regular expression is some text combined with special characters that represent things that can't be typed, such as "the end of a string" or "any number" or "three capital letters."
When regular expressions are being used, some characters give up their usual meaning and instead stand in for one or more other characters. Regular expressions in Visual C++ are built from ordinary characters mixed in with these special entries, shown in Table 3.2.
Here are some examples of regular expressions:
^test$ matches only test alone in a string.
doc[1234] matches doc1, doc2, doc3, or doc4 but not doc5.
doc[1-4] matches the same strings as doc[1234] but requires less typing.
doc[^56] matches doca, doc1, and anything else that starts with doc, except doc5 and doc6.n -H\~ello matches Hillo and Hxllo (and lots more) but not Hello. H[^e]llo has the same effect.
[xy]z matches xz and yz.
-
New *York matches New York, NewYork, and New York (with several spaces between the words).
New +York matches New York and New York, but not NewYork.
New.*k matches Newk, Newark, and New York, plus lots more.
-
World$ matches World at the end of a string, but World\$ matches only World$ anywhere in a string.
Table 3.2 Regular Expression Entries
Entry |
Matches |
^ |
Start of the string. |
$ |
End of the string. |
. |
Any single character. |
[] |
Any one of the characters within the brackets (use for a range, ^ for "except"). |
\~ |
Anything except the character that follows. |
* |
Zero or more of the next character. |
+ |
One or more of the next character. |
\w |
-A single letter or number, or an underscore. (These are called word characters and are the only characters that can be used in a variable name.) |
\s |
Whitespace (tabs or spaces). |
\d |
A single numerical digit. |
\ |
Removes the special meaning from the character that follows. |
The System::Collections Namespace
Another incredibly common programming task is holding on to a collection of objects. If you have just a few, you can use an array to read three integers in one line of input. In fact, arrays in .NET are actually objects, instances of the System::Array class, which have some useful member functions of their own, such as Copy(). There are times when you want specific types of collections, though, and the System::Collections namespace has plenty of them. The provided collections include
Stack. A collection that stores objects in order. The object stored most recently is the first taken out.
Queue. A collection that stores objects in order. The first stored is the first taken out.
Hashtable. A collection that can be searched far more quickly than other types of collections, but takes up more space.
ArrayList. An array that grows as elements are added to it.
SortedList. A collection of two-part (key and value) items that can be accessed by key or in numerical order.
BitArray. A compact way to store an array of true/false flags.
One rather striking omission here is a linked list. You have to code your own if you need a linked or double-linked list.
The System::Threading Namespace
Threading has been a difficult part of Windows programming from the very beginning. It's quite a bit simpler in .NET. The vital classes for threading are in the System::Threading namespace. These include classes such as Mutex, Thread, and ThreadPool, which developers with experience in threaded applications will recognize instantly.
A thread is a path of execution through a program. In a multithreaded program, each thread has its own stack and operates independently of other threads running within the same program.
How Many Threads?
Any application always has at least one thread, which is the program's primary or main thread. You can start and stop as many additional threads as you need, but the main thread keeps running as long as the application is active.
A thread is the smallest unit of execution, much smaller than a process. Generally each running application on your system is a process. If you start the same application twice (for example, Notepad), there are two processes: one for each instance. It is possible for several instances of an application to share a single process: For example, if you choose File, New Window in Internet Explorer, two applications appear on your taskbar, and they share a process. The unfortunate consequence is that if one instance crashes, they all do.
Writing a multithreaded application is simple with classes from the System::Thread namespace. First, you need to think of some work for a new thread to do: some slow calculation that you want to run without slowing the responsiveness of your application. This work will go into a function, and the function will be executed on a new thread.
The Thread Proc
For a long time, Windows C++ programmers have called the function the thread proc (short for procedure).
Your thread proc must be a member function of a garbage-collected class. For example
public __gc class Counter { public: void Countdown() { String* threadName = Thread::CurrentThread->Name; Console::Write(S"This is the current thread: "); Console::WriteLine(threadName); for (int counter = maxCount; counter >= 1; counter--) { Console::WriteLine(S"{0} is currently on {1}.",threadName, __box(counter)); } Console::WriteLine(S"{0} has finished counting down from {1}.", threadName, __box(maxCount)); } };
The heart of this function is the for loop that counts down from maxCount to zero. It uses the Name property of the thread that is executing the function, because the sample that uses this class calls the function on two different threads. Normally, the function that does the asynchronous work would not communicate with the user; it would be quietly working in the background, doing some long slow work. It might, however, update a progress bar, or an icon that indicates a background task is in progress.
This code calls the thread proc on a new thread and also on the application's main thread:
Console::Write("Enter a number to count down from: "); maxCount = Int16::Parse(Console::ReadLine()); Thread::CurrentThread->Name = "Main Thread"; Counter* counter = new Counter(); ThreadStart* SecondStart = new ThreadStart(counter,Counter::Countdown); Thread* secondThread = new Thread(SecondStart); secondThread->Name = "Secondary Thread"; secondThread->Start(); counter->Countdown();
This code keeps the number entered by the user in a global variable called maxCount. Because ReadLine returns a pointer to a System::String instance, the static Parse() method of Int16 is used to convert a string to an integer so that it can be stored in maxCount.
To execute a particular function on a new thread, you create a Thread object and call its Start() method. To create a Thread object, you need a ThreadStart object, and the constructor for ThreadStart needs a reference to an instance of a garbage-collected class (counter in this example), and a function pointer (just the name of the function without the parentheses) to a member function of that garbage-collected class.
Because this code calls Countdown on a new thread and also on the main thread, the output shows the two threads taking turns, as in Figure 3.1.
Figure 3.1 A multithreaded application demonstrates how threads take turns doing their work.