Advertisement
Guest User

BlittableBool

a guest
Apr 3rd, 2019
170
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.85 KB | None | 0 0
  1. using System;
  2.  
  3. namespace ECS.Utils
  4. {
  5.     /// <summary>
  6.     /// Boolean backed by a byte, to allow for blitting (mem-copy)
  7.     /// </summary>
  8.     [Serializable]
  9.     public struct BlittableBool
  10.     {
  11.         #region Variables
  12.         /// <summary>
  13.         /// Value of Bool
  14.         /// </summary>
  15.         public byte Value;
  16.         #endregion
  17.  
  18.         #region Constructors
  19.         /// <summary>
  20.         /// Constructs a BlittableBool from a Byte.
  21.         /// <para>
  22.         /// Throws <see cref="ArgumentOutOfRangeException"/> if <paramref name="value"/> is neither 0 nor 1
  23.         /// </para>
  24.         /// </summary>
  25.         /// <param name="value">Value to set. Must be 0 or 1</param>
  26.         public BlittableBool(byte value)
  27.         {
  28.             if (value != (byte)0 && value != (byte)1)
  29.                 throw new ArgumentOutOfRangeException("value", value, "Invalid value for BlittableBool. Value must be 0 or 1");
  30.             Value = value;
  31.         }
  32.         /// <summary>
  33.         /// Constructs a BlittableBool from a Boolean
  34.         /// </summary>
  35.         /// <param name="value">Value to set</param>
  36.         public BlittableBool(bool value)
  37.         {
  38.             Value = value ? (byte)1 : (byte)0;
  39.         }
  40.         #endregion
  41.  
  42.         #region Operators
  43.         /// <summary>
  44.         /// Implicit cast from BlittableBool to Boolean
  45.         /// </summary>
  46.         /// <param name="bb">BlittableBool to cast</param>
  47.         public static implicit operator bool(BlittableBool bb)
  48.         {
  49.             return bb.Value == 1;
  50.         }
  51.         /// <summary>
  52.         /// Implicit cast from Boolean to BlittableBool
  53.         /// </summary>
  54.         /// <param name="b">Boolean to cast</param>
  55.         public static implicit operator BlittableBool(bool b)
  56.         {
  57.             return new BlittableBool(b);
  58.         }
  59.         #endregion
  60.     }
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement