template<T>
class CSharpUtilities::ExtendedEnum< T >
Provides an extension of the typesafe enum idiom for complex
types.
- Template Parameters:
-
The standard .NET enum implementation allows the programmer to
create a set of named object instances around one of the primitive
data types such as int or byte.
ExtendedEnum provides much of enum's functionality around any
user-defined, preferably immutable, type that inherits from it.
This version of
ExtendedEnum uses the curiously recursive template pattern to
allow users to create an Enum whose underlying value is the class
itself.
To use
ExtendedEnum, create a derived class in which the set of
instances are defined as static constant members of the class. For
example, say we want a predefined set of Balls each defined by its
radius and bounciness, we could declare these as an
ExtendedEnum:
class Ball : ExtendedEnum<Ball>
{
public static readonly Ball Golf = new Ball(1, 10);
public static readonly Ball Billiard = new Ball(2, 1);
public static readonly Ball Baseball = new Ball(3, 3);
public static readonly Ball Soccer = new Ball(8, 4);
public static readonly Ball Basketball = new Ball(10, 5);
public int Radius { get { return mRadius;} }
public int Bounciness { get { return mBounciness;} }
private Ball(int radius, int bounciness)
{
mRadius = radius;
mBounciness = bounciness;
}
private int mRadius;
private int mBounciness;
}
Client code may now refer to members of the Ball enumeration as
Ball.Golf or Ball.Billiard, much as it would for any standard
enumeration. This enumeration is also closed at compile time since
the constructor is private.
When creating a class such as Ball which inherits from
ExtendedEnum, the generic parameter T must be the generic class
itself (i.e. Ball) or one of its base classes.
class Ball : ExtendedEnum<Ball>
{...}
class TennisRacket : ExtendedEnum <Ball>
{...}
Note: This will add values to ExtendedEnum<Ball>
class SpecializedBall : Ball
{...}
- Template Parameters:
-
T |
The enumerated type. |
U |
The underlying value type. |
The standard .NET enum implementation allows the programmer to
create a set of named object instances around one of the primitive
data types such as int or byte.
ExtendedEnum provides much of enum's functionality around any
user-defined, preferably immutable, type. This version of
ExtendedEnum allows users to create an Enum whose underlying
value is a user-defined or primitive type. It also uses the
curiously recursive template pattern to ensure that enums with the
same Underlying type, which are derived from different classes, do
not share their values.
To use
ExtendedEnum, create a derived class in which the set of
instances are defined as static constant members of the class. For
example, say we want a predefined set of Balls with their name
(String) as their UnderlyingValue, each defined by their name,
radius and bounciness, we could declare these as an
ExtendedEnum:
class Ball : ExtendedEnum<Ball, String>
{
public static readonly Ball Golf = new Ball("Golf", 1, 2);
public static readonly Ball Billiard = new Ball("Billiard", 3, 2);
public static readonly Ball Baseball = new Ball("Baseball", 4, 3);
public static readonly Ball Soccer = new Ball("Soccer", 10, 7);
public static readonly Ball Basketball = new Ball("Basketball", 12, 10);
public String Name {get { return mName;} }
public int Radius { get { return mRadius;} }
public int Bounciness { get { return mBounciness;} }
private Ball(String name, int radius, int bounciness) : base(name)
{
mName = name;
mRadius = radius;
mBounciness = bounciness;
}
private String name;
private int mRadius;
private int mBounciness;
}
Client code may now refer to members of the Ball enumeration as
Ball.Golf or Ball.Billiard, much as it would for any standard
enumeration. This enumeration is also closed at compile time since
the constructor is private.
When creating a class such as Ball which inherits from
ExtendedEnum, the generic parameter T must be the generic class
itself (i.e. Ball) or one of its base classes.
class Ball : ExtendedEnum<Ball, String>
{...}
class TennisRacket : ExtendedEnum <Ball, String>
{...}
class SpecializedBall : Ball
{...}
Public Member Functions
|
virtual
bool |
Equals (ExtendedEnum< T > other) |
|
Determine the equality of this
ExtendedEnum member to another
ExtendedEnum member.
|
virtual
bool |
Equals (T other) |
|
Determine the equality of this
ExtendedEnum member to another value.
|
override
bool |
Equals (object other) |
|
Determine the equality of this
ExtendedEnum member to another object.
|
override
int |
GetHashCode () |
|
Get a hash code for this
ExtendedEnum member.
|
virtual
int |
CompareTo (ExtendedEnum< T > other) |
|
Compare this to another member of this
ExtendedEnum, ordered according to their Ids.
|
virtual
bool |
Equals (ExtendedEnum< T, U > other) |
|
Determine the equality of this
ExtendedEnum member to another
ExtendedEnum member.
|
virtual
bool |
Equals (U other) |
|
Determine the equality of this
ExtendedEnum member to another value.
|
override
bool |
Equals (object other) |
|
Determine the equality of this
ExtendedEnum member to another object.
|
override
int |
GetHashCode () |
|
Get a hash code for this
ExtendedEnum member.
|
virtual
int |
CompareTo (ExtendedEnum< T, U > other) |
|
Compare this to another member of this
ExtendedEnum, ordered according to their Ids.
|
override
String |
ToString () |
|
Get the string representation for this
ExtendedEnum member.
|
Static Public Member
Functions
|
static
ExtendedEnum< T > |
GetExtendedEnum (T value) |
|
Finds the
ExtendedEnum member wrapping the given value.
|
static
bool |
IsDefined (ExtendedEnum< T > value) |
|
Determine if this
ExtendedEnum type contains the given value.
|
static
bool |
IsDefined (T value) |
|
Determine if this
ExtendedEnum type contains the given value.
|
static
ExtendedEnum< T, U > |
GetExtendedEnum (U value) |
|
Finds the
ExtendedEnum member wrapping the given value.
|
static
bool |
IsDefined (ExtendedEnum< T, U > value) |
|
Determine if this
ExtendedEnum type contains the given value.
|
static
bool |
IsDefined (U value) |
|
Determine if this
ExtendedEnum type contains the given value.
|
static |
operator U (ExtendedEnum< T, U > value) |
|
Converts from
ExtendedEnum<T, U> to U.
|
Protected Member Functions
|
|
ExtendedEnum () |
|
Constructor intended for the default case,
where the enumerated type is the derived type.
|
|
ExtendedEnum (U underlyingValue) |
|
Constructor intended for enumerations
designating a certain set of instances from another type.
|
Properties
|
static
Type |
UnderlyingType [get] |
|
The underlying type exposed as an Enum
through this class.
|
static
ExtendedEnum< T >[] |
Values [get] |
|
Produces an array of all the Enum's members.
|
T |
UnderlyingValue [get] |
|
The underlying T value wrapped in this
ExtendedEnum.
|
static
ExtendedEnum< T, U >[] |
Values [get] |
|
Produces an array of all the Enum's members.
|
U |
UnderlyingValue [get] |
|
The underlying T value wrapped in this
ExtendedEnum.
|