CSharpUtilities::ExtendedEnum< T > Class Template Reference


Detailed Description

template<T>
class CSharpUtilities::ExtendedEnum< T >

Provides an extension of the typesafe enum idiom for complex types.

Template Parameters:
T The enumerated 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 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&lt;Ball&gt;
    {
       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);
       // etc

       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&lt;Ball&gt; // Valid
    {...}

    class TennisRacket : ExtendedEnum &lt;Ball&gt; // Invalid
    {...}

    Note: This will add values to ExtendedEnum&lt;Ball&gt;
    class SpecializedBall : Ball // Valid. 
    {...}
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&lt;Ball, String&gt;
    {
       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);
       // etc

       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&lt;Ball, String&gt; // Valid
    {...}

    class TennisRacket : ExtendedEnum &lt;Ball, String&gt; // Invalid
    {...}

    class SpecializedBall : Ball // Valid
    {...}
Type Constraints
T  : ExtendedEnum<T>  
Inheritance diagram for CSharpUtilities::ExtendedEnum< T >:
Inheritance graph
[legend]

List of all members.

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.
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.
UnderlyingValue [get]
  The underlying T value wrapped in this ExtendedEnum.

Member Function Documentation

template<T >
static ExtendedEnum<T> CSharpUtilities::ExtendedEnum< T >::GetExtendedEnum ( value ) [inline, static]

Finds the ExtendedEnum member wrapping the given value.

Parameters:
value The value to find in this Enum.
Returns:
Either the found Enum member or null if no member is found.
template<T >
static bool CSharpUtilities::ExtendedEnum< T >::IsDefined ( ExtendedEnum< T >  value ) [inline, static]

Determine if this ExtendedEnum type contains the given value.

Parameters:
value The value to try to find.
Returns:
true if value is found as a member of this Enum, false if not.
template<T >
static bool CSharpUtilities::ExtendedEnum< T >::IsDefined ( value ) [inline, static]

Determine if this ExtendedEnum type contains the given value.

Parameters:
value The value to try to find.
Returns:
true if value is found as a member of this Enum, false if not.
template<T >
virtual bool CSharpUtilities::ExtendedEnum< T >::Equals ( ExtendedEnum< T >  other ) [inline, virtual]

Determine the equality of this ExtendedEnum member to another ExtendedEnum member.

The equivalence is established based on the rank of the extended enum elements.

Parameters:
other The other ExtendedEnum for comparison.
Returns:
true if this.Id == other.Id, false if not.
template<T >
virtual bool CSharpUtilities::ExtendedEnum< T >::Equals ( other ) [inline, virtual]

Determine the equality of this ExtendedEnum member to another value.

The equivalence is established based on the rank of the extended enum elements.

Parameters:
other T value against which to compare this ExtendedEnum member.
Returns:
true if this.Id == other.Id.
template<T >
override bool CSharpUtilities::ExtendedEnum< T >::Equals ( object  other ) [inline]

Determine the equality of this ExtendedEnum member to another object.

Parameters:
other object against which to compare this ExtendedEnum member.
Returns:
  • Returns false if other is null.
  • Returns true if other is referentially equal to this.
  • Returns true if other is a T and this.Equals( (T)other ).
  • Returns true if other is an ExtendedEnum<T> and
  • this.Equals( (ExtendedEnum<T>)other ).
  • Returns false otherwise.
template<T >
override int CSharpUtilities::ExtendedEnum< T >::GetHashCode ( ) [inline]

Get a hash code for this ExtendedEnum member.

Returns:
A hash code dependent on this enum type and on this enum member's Id.
template<T >
virtual int CSharpUtilities::ExtendedEnum< T >::CompareTo ( ExtendedEnum< T >  other ) [inline, virtual]

Compare this to another member of this ExtendedEnum, ordered according to their Ids.

Parameters:
other Other member against which to compare.
Returns:
<0 if this member is ranked first, 0 if they are the same member, and >0 if other should be ranked first.
template<T >
CSharpUtilities::ExtendedEnum< T >::ExtendedEnum ( ) [inline, protected]

Constructor intended for the default case, where the enumerated type is the derived type.

Also checks to see if the curiously recursive template pattern requirements are satisfied.

template<T >
static ExtendedEnum<T, U> CSharpUtilities::ExtendedEnum< T >::GetExtendedEnum ( value ) [inline, static]

Finds the ExtendedEnum member wrapping the given value.

Parameters:
value The value to find in this Enum.
Returns:
Either the found Enum member or null if no member is found.
template<T >
static bool CSharpUtilities::ExtendedEnum< T >::IsDefined ( ExtendedEnum< T, U >  value ) [inline, static]

Determine if this ExtendedEnum type contains the given value.

Parameters:
value The value to try to find.
Returns:
true if value is found as a member of this Enum, false if not.
template<T >
static bool CSharpUtilities::ExtendedEnum< T >::IsDefined ( value ) [inline, static]

Determine if this ExtendedEnum type contains the given value.

Parameters:
value The value to try to find.
Returns:
true if value is found as a member of this Enum, false if not.
template<T >
static CSharpUtilities::ExtendedEnum< T >::operator U ( ExtendedEnum< T, U >  value ) [inline, explicit, static]

Converts from ExtendedEnum<T, U> to U.

This is equivalent to calling UnderlyingValue.

Parameters:
value The ExtendedEnum member to convert.
Returns:
value's underlying value.
template<T >
virtual bool CSharpUtilities::ExtendedEnum< T >::Equals ( ExtendedEnum< T, U >  other ) [inline, virtual]

Determine the equality of this ExtendedEnum member to another ExtendedEnum member.

This is equivalent to calling UnderlyingValue.Equals(other.UnderlyingValue).

Parameters:
other The other value for comparison.
Returns:
true if this.UnderlyingValue.Equals(other.UnderlyingValue), false if not.
template<T >
virtual bool CSharpUtilities::ExtendedEnum< T >::Equals ( other ) [inline, virtual]

Determine the equality of this ExtendedEnum member to another value.

This is equivalent to calling UnderlyingValue.Equals(other).

Parameters:
other T value against which to compare this ExtendedEnum member.
Returns:
true if this.UnderlyingValue.Equals(other).
template<T >
override bool CSharpUtilities::ExtendedEnum< T >::Equals ( object  other ) [inline]

Determine the equality of this ExtendedEnum member to another object.

Parameters:
other object against which to compare this ExtendedEnum member.
Returns:
  • Returns false if other is null.
  • Returns true if other is referentially equal to this.
  • Returns true if other is a U and this.Equals( (U)other ).
  • Returns true if other is an ExtendedEnum<T, U> and
  • this.Equals( (ExtendedEnum<T, U>)other ).
  • Returns false otherwise.
template<T >
override int CSharpUtilities::ExtendedEnum< T >::GetHashCode ( ) [inline]

Get a hash code for this ExtendedEnum member.

Returns:
A hash code dependent on this enum type and on this enum member's Id.
template<T >
virtual int CSharpUtilities::ExtendedEnum< T >::CompareTo ( ExtendedEnum< T, U >  other ) [inline, virtual]

Compare this to another member of this ExtendedEnum, ordered according to their Ids.

Parameters:
other Other member against which to compare.
Returns:
<0 if this member is ranked first, 0 if they are the same member, and >0 if other should be ranked first.
template<T >
override String CSharpUtilities::ExtendedEnum< T >::ToString ( ) [inline]

Get the string representation for this ExtendedEnum member.

Equivalent to calling UnderlyingValue.ToString().

Returns:
The string representation for this ExtendedEnum member.
template<T >
CSharpUtilities::ExtendedEnum< T >::ExtendedEnum ( underlyingValue ) [inline, protected]

Constructor intended for enumerations designating a certain set of instances from another type.

This means that instances of the generic parameter U type can be constructed without labeling them as members of the enumeration. Also checks to see if the curiously recursive template pattern requirements are satisfied.

Parameters:
underlyingValue U value to wrap as an ExtendedEnum member.

Property Documentation

template<T >
Type CSharpUtilities::ExtendedEnum< T >::UnderlyingType [static, get]

The underlying type exposed as an Enum through this class.

template<T >
ExtendedEnum<T> [] CSharpUtilities::ExtendedEnum< T >::Values [static, get]

Produces an array of all the Enum's members.

Changing the returned array will not affect this ExtendedEnum type.

template<T >
T CSharpUtilities::ExtendedEnum< T >::UnderlyingValue [get]

The underlying T value wrapped in this ExtendedEnum.

template<T >
ExtendedEnum<T, U> [] CSharpUtilities::ExtendedEnum< T >::Values [static, get]

Produces an array of all the Enum's members.

Changing the returned array will not affect this ExtendedEnum type.

template<T >
U CSharpUtilities::ExtendedEnum< T >::UnderlyingValue [get]

The underlying T value wrapped in this ExtendedEnum.