GithubHelp home page GithubHelp logo

cjmfastenumconversion's Introduction

CjmFastEnumConversion (Copyright © 2021, CJM Screws, LLC)

Summary

This very simple utility is used for fast conversions between enums and their underlying types and is useful in the generic context. Safety checks are made once per process per distinct pair of enum type and Underlying type. Once the safety check passes for a given enum and type Underlying type, no further safety checks will be performed for that combination. This utility can only be used to convert values back and forth between their enum form and the form of their exact underlying type.

Compatible Platforms

  • Net Framework 4.8
  • Net Standard 2.0
  • Net Standard 2.1
  • Net 5.0
  • Net 6.0

Usage Examples

Correct Usage Example

This example comes from the example code portion of this repository's unit tests.

public static void DemonstrateSuccessfulConversion()
{
    //OK -- UShortBacked is backed by ushort
    UShortBacked enumValue = UShortBacked.MaxValueMinusOne;
    ushort convertedToBacker = EnumConverterUtil.ConvertEnumToUnderlier<UShortBacked, ushort>(enumValue);
    UShortBacked andBack =
        EnumConverterUtil.ConvertUnderlierToEnumValue<UShortBacked, ushort>(convertedToBacker);
    Assert.True(enumValue == andBack && convertedToBacker == (ushort)enumValue);

    //Note that no checking is done as to whether a (correctly typed) backer is actually a defined value of the type.
    ushort bla = 0xbabe; //random ushort
    UShortBacked enumVal = EnumConverterUtil.ConvertUnderlierToEnumValue<UShortBacked, ushort>(bla);
    ushort roundTripped = EnumConverterUtil.ConvertEnumToUnderlier<UShortBacked, ushort>(enumVal);
    Assert.True(bla == roundTripped); //works just fine even though val not defined
}

The above amply demonstrates correct usage.

Incorrect Usage Example

This utility is only usable to convert enum values to their exact underlying type and back. Attempts to convert an enum to a type other than their exact backer or vice versa will result in a TypeInitializationException being thrown. The inner exception of the TypeInitializationException will be of type EnumAndUnderlierMismatchException<TEnum, TIncorrectBacker>. The following snippet demonstrates incorrect usage that will throw exceptions:

public static void DemonstrateExceptionOnMismatch()
{
    UShortBacked enumValue = UShortBacked.DefaultPlusOne;
    short incorrectBacker = 1;
    //This will throw a type initialization exception because UShortBacked is backed by ushort, not short
    try
    {
        EnumConverterUtil.ConvertUnderlierToEnumValue<UShortBacked, short>(incorrectBacker);
    }
    catch (TypeInitializationException ex) when
        (ex.InnerException is EnumAndUnderlierMismatchException<UShortBacked, short>)
    {
        //CORRECT BEHAVIOR
    }
    catch (DidntThrowException)
    {
        //INCORRECT -- should have thrown
    }
    catch
    {
        Assert.False(true, "It threw the wrong thing!");
    }

    //And in reverse
    try
    {
        EnumConverterUtil.ConvertEnumToUnderlier<UShortBacked, short>(enumValue);
    }
    catch (TypeInitializationException ex) when
        (ex.InnerException is EnumAndUnderlierMismatchException<UShortBacked, short>)
    {
        //CORRECT BEHAVIOR
    }
    catch (DidntThrowException)
    {
        //INCORRECT -- should have thrown
    }
    catch
    {
        Assert.False(true, "It threw the wrong thing!");
    }
}

The foregoing should be sufficient to demonstrate the usage of this (very) limited-purpose library.

cjmfastenumconversion's People

Contributors

cpsusie avatar

Watchers

 avatar

cjmfastenumconversion's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.