import ctypes


def WADFlags(flags):
    """
    This is a helper function to generate flags which can be accessed either
    individually or as an entire 16-bit field.

    The flags argument is a list of (name, size) tuples, where size is in bits.
    See omg.mapedit for usage examples.
    """
    class FlagsUnion(ctypes.Union):
        class Flags(ctypes.LittleEndianStructure):
            _fields_ = [(name, ctypes.c_uint16, size) for (name, size) in flags]

        _fields_ = [("flags", ctypes.c_uint16), ("_flags", Flags)]
        _anonymous_ = ("_flags",)

    return FlagsUnion