This is a nice one I discovered this morning, by setting the Flag Property of an enumeration, enumeration bitwise values and an editor box are supplied by default.
Consider we have an property, for the sake of this example I'll use Font, that uses an enumeration to define its state, with possible values of Bold, Underlined, Italic etc.
Where an owning object of such a property, like Pen, may have be in multiple states concurrerntly (eg, a Bold, Underlined state), we could define each property as a boolean, to say that property is in affect or not, or we could use bitwise shifting of an enumeration. To do this we would define our enumeration as having set, bitshifted, values:
public enum FontProperty
{
Bold = 1 << 0, // 1 bit shifted 0 position to the left, therefore 0001 = 1
Underlined = 1 << 1, // 1 bit shifted 1 position to the left, therefore 0010 = 2
Italic = 1 << 2, // 1 bit shifted 2 positions to the left, therefore 0100 = 4
Strikethrough = 1 << 3, // 1 bit shifted 3 positions to the left, therefore 0100 = 8
}
We could then set our object state as follows:
Pen pen = new Pen();
pen.Font = FontProperty.Bold | Underlined
This would effectively set the Font property (confusingly, sorry, of type FontProperty) to be 3, or 0011, the amalgamation of 0001 and 0010, which is basic binary addition, a nice way of describing multiple states. This can be translated back as shown below:
//Test for Bold
if ((FontProperty & FontProperty.Bold) != 0)
{
//Font is bold
}
We can do a similar thing in a DSL definition:
|
Define a Domain Enumeration (essentially just an enumeration like any other in .NET that is created in context of a DSL Definition) from the DSL explorer
|
|
Set the Enumeration Name property, and the flag property to True
|
|
Create the enumeration values
|
|
Note, the bitwise integer value of the enumeration has been automatically set
|
|
Add a Domain Property to your owning Domain Class with your enumeration as its type
|
|
Now, when the property is used in the generated DSL, a checked box editor window has been automatically provided, and the correct bitwise value set
|
|