Directory
Native enumerations and managed enumerations
A single class solution
Five class Solutions
Six class Solutions
After introducing the "from C + + to. NET" Theme in June 2006, I started writing a series of columns to delve into how to use the C++/cli language extensions in Visual c++®2005 to move a valid native C + + application to Microsoft®.net The Framework. The sample application that I moved was the text query language (Language, TQL), which was developed in 1996 for the third edition of the book C + + Primer that I wrote.
In June, I started a series of columns on how to wrap a native TQL application, and then in August 2006, I checked and tidied up the code carefully. Last month, I outlined how to use regular expressions in the. NET Framework. You can pass from C + + to. NET (June 2006) and from C + + to. NET (June 2006) to find these previous columns.
The TQL design is designed to support two types of operations:
For each unique word, the user-specified random text file is normalized to the mapping of the line number match.
The user query is processed according to the mapping to display the matching rows of the text.
The purpose of the design text normalization is to illustrate the important role of the collection class. In the native implementation, it uses the Standard Template Library (Standard Template Library, STL). In a subsequent column, I will look at this design to delve into the generic collection namespaces of the. NET base Class Library (BCL). The purpose of designing text query support is to illustrate an important but basic class design. And this aspect of C++/CLI programming is exactly what I'm going to focus on in this column.
By TQL, users can query text files using a series of logical tokens that represent relationships, including:&& representations "and"; means "or"; Indicates "non". Therefore, to find all occurrences of Holmes, simply type "Holmes" (excluding quotes). If you want to find all lines of text in which Holmes does not appear, simply append the "non" operator (!) to the Holmes front. Holmes). If your goal is to find all occurrences of Sherlock or Holmes, you need to use the OR operator to connect the two words together (Sherlock | | Holmes). Finally, to find all the matches that are Sherlock and immediately followed by Holmes, you need to use the "and" operator to connect the two words (Sherlock && Holmes).
Admittedly, in today's search engine world, this seems trivial. However, this method is really the right remedy for the study of various kinds of design strategies. Keep in mind that the history of this machine has been implemented for more than 10 years-I'm just using C++/CLI to overwrite it for the. NET Framework.
The candidate abstract concept of this design is the four types of queries that users may use. At first, we represented them as a series of enumerations, as follows:
enum EQueryType { // native enum
qWord = 1, qNot, qOr, qAnd
};The reason why I chose this method is two. First, you will need this enumeration to implement one of my design solutions, and secondly, I would like to provide a brief tutorial on using C++/CLI to implement enumeration support.
Native enumerations and managed enumerations
The first question asked by programmers unfamiliar with C++/CLI is the difference between the native enumeration I just described and the equivalent managed enumeration like the following:
class enum EQueryType : Byte { // managed enum
qWord = 1, qNot, qOr, qAnd
};
There are three major differences.
The first difference is the granularity of size management. In this case, I specify a byte of storage sufficient to represent each enumerator value (a total of four values). This feature has also been extended to native support for C + +, not just in Visual C + + (nonstandard extension).
The second difference is the scope of the enumerator. In a managed environment, an enumeration can maintain its own scope, so it can encapsulate its enumerator's visibility in the same way that the class encapsulates its member visibility. Of course, this is not true for native enumerations, because the enumerator is all dispersed to the enclosing scope. This can become a problem when you combine modules from multiple sites. To prevent the enumerator from polluting the global namespace, a design convention for native C + + is to nest enumerations in the class, as follows:
class ios {
public:
enum iostates { read, write, append };
// ...
}
To access any of these enumerators, you can precede the enumerator with the class scope operator, such as Ios::read. This is not a good design practice for managed enumerations because it causes two-tier encapsulation, such as Ios::iostates::read. Therefore, managed enumerations are typically defined within the shared scope of a class, and their purpose is to support native enumerations rather than providing support in that class.
The third difference is that a managed enumeration is more like an object than an integer. This allows you to parse overloaded functions correctly, as follows:
void f( int );
void f( Object^ );
EQueryType et;
f( et ); // which one?
If a managed enumeration is treated as an integral type-like a native enumeration-then this call must match the F (int). However, if a fallback enumeration class in the common type System (CTS) belongs to a value type derived from an object, its type must be explicitly bound to an object, not to an integer. Therefore, it can be concluded that a managed enumeration belongs to one object, not an integer.
I hope this is clear because I have no time to discuss this conclusion more forcefully. Although this is a reasonable conclusion, there are unfortunate side effects: there is no mechanism to implicitly convert a managed enumeration or one of its enumerators to integers, so it is cumbersome to have an explicit conversion every time you use an arithmetic value.
Therefore, the TQL application has four specific types that need to be handled: words (word), with (and), or (or), and not. But how many classes are needed? My design solution offers 1, 5, or 6 classes. Let's explore the design approach step-by-step and see what inspires you.