text:
'''
Python bindings for bgfx.
'''

__author__ = "Jason Nadro"
__copyright__ = "Copyright 2016, Jason Nadro"
__credits__ = ["Jason Nadro"]
__license__ = "BSD 2-clause"
__version__ = "0.0.1"
__maintainer__ = "Jason Nadro"
__email__ = ""
__status__ = "Development"

import ctypes
from ctypes import Structure, POINTER, cast, byref, CFUNCTYPE
from ctypes import c_bool, c_int, c_int8, c_int16, c_int32, c_int64, c_uint8, c_uint16, c_uint32, c_uint64, c_float, c_char_p, c_void_p, c_size_t, c_char
import os

bgfx_dll_path = os.path.dirname(__file__) + "\\bgfx-shared-libRelease"
_bgfx = ctypes.CDLL(bgfx_dll_path)

enum_type = c_int

# bgfx_renderer_type
bgfx_renderer_type = enum_type
(
    BGFX_RENDERER_TYPE_NOOP,
    BGFX_RENDERER_TYPE_DIRECT3D9,
    BGFX_RENDERER_TYPE_DIRECT3D11,
    BGFX_RENDERER_TYPE_DIRECT3D12,
    BGFX_RENDERER_TYPE_GNM,
    BGFX_RENDERER_TYPE_METAL,
    BGFX_RENDERER_TYPE_OPENGLES,
    BGFX_RENDERER_TYPE_OPENGL,
    BGFX_RENDERER_TYPE_VULKAN,

    BGFX_RENDERER_TYPE_COUNT
) = map(bgfx_renderer_type,range(10))

# bgfx_access
bgfx_access = enum_type
(
    BGFX_ACCESS_READ,
    BGFX_ACCESS_WRITE,
    BGFX_ACCESS_READWRITE,

    BGFX_ACCESS_COUNT
) = map(bgfx_access, range(4))

# bgfx_attrib
bgfx_attrib = enum_type
(
    BGFX_ATTRIB_POSITION,
    BGFX_ATTRIB_NORMAL,
    BGFX_ATTRIB_TANGENT,
    BGFX_ATTRIB_BITANGENT,
    BGFX_ATTRIB_COLOR0,
    BGFX_ATTRIB_COLOR1,
    BGFX_ATTRIB_COLOR2,
    BGFX_ATTRIB_COLOR3,
    BGFX_ATTRIB_INDICES,
    BGFX_ATTRIB_WEIGHT,
    BGFX_ATTRIB_TEXCOORD0,
    BGFX_ATTRIB_TEXCOORD1,
    BGFX_ATTRIB_TEXCOORD2,
    BGFX_ATTRIB_TEXCOORD3,
    BGFX_ATTRIB_TEXCOORD4,
    BGFX_ATTRIB_TEXCOORD5,
    BGFX_ATTRIB_TEXCOORD6,
    BGFX_ATTRIB_TEXCOORD7,

    BGFX_ATTRIB_COUNT
) = map(bgfx_attrib, range(19))

# bgfx_attrib_type
bgfx_attrib_type = enum_type
(
    BGFX_ATTRIB_TYPE_UINT8,
    BGFX_ATTRIB_TYPE_UINT10,
    BGFX_ATTRIB_TYPE_INT16,
    BGFX_ATTRIB_TYPE_HALF,
    BGFX_ATTRIB_TYPE_FLOAT,

    BGFX_ATTRIB_TYPE_COUNT
) = map(bgfx_attrib_type, range(6))

# bgfx_texture_format
bgfx_texture_format = enum_type
(
    BGFX_TEXTURE_FORMAT_BC1,
    BGFX_TEXTURE_FORMAT_BC2,
    BGFX_TEXTURE_FORMAT_BC3,
    BGFX_TEXTURE_FORMAT_BC4,
    BGFX_TEXTURE_FORMAT_BC5,
    BGFX_TEXTURE_FORMAT_BC6H,
    BGFX_TEXTURE_FORMAT_BC7,
    BGFX_TEXTURE_FORMAT_ETC1,
    BGFX_TEXTURE_FORMAT_ETC2,
    BGFX_TEXTURE_FORMAT_ETC2A,
    BGFX_TEXTURE_FORMAT_ETC2A1,
    BGFX_TEXTURE_FORMAT_PTC12,
    BGFX_TEXTURE_FORMAT_PTC14,
    BGFX_TEXTURE_FORMAT_PTC12A,
    BGFX_TEXTURE_FORMAT_PTC14A,
    BGFX_TEXTURE_FORMAT_PTC22,
    BGFX_TEXTURE_FORMAT_PTC24,
    BGFX_TEXTURE_FORMAT_ATC,
    BGFX_TEXTURE_FORMAT_ATCE,
    BGFX_TEXTURE_FORMAT_ATCI,
    BGFX_TEXTURE_FORMAT_ASTC4x4,
    BGFX_TEXTURE_FORMAT_ASTC5x5,
    BGFX_TEXTURE_FORMAT_ASTC6x6,
    BGFX_TEXTURE_FORMAT_ASTC8x5,
    BGFX_TEXTURE_FORMAT_ASTC8x6,
    BGFX_TEXTURE_FORMAT_ASTC10x5,

    BGFX_TEXTURE_FORMAT_UNKNOWN,

    BGFX_TEXTURE_FORMAT_R1,
    BGFX_TEXTURE_FORMAT_A8,
    BGFX_TEXTURE_FORMAT_R8,
    BGFX_TEXTURE_FORMAT_R8I,
    BGFX_TEXTURE_FORMAT_R8U,
    BGFX_TEXTURE_FORMAT_R8S,
    BGFX_TEXTURE_FORMAT_R16,
    BGFX_TEXTURE_FORMAT_R16I,
    BGFX_TEXTURE_FORMAT_R16U,
    BGFX_TEXTURE_FORMAT_R16F,
    BGFX_TEXTURE_FORMAT_R16S,
    BGFX_TEXTURE_FORMAT_R32I,
    BGFX_TEXTURE_FORMAT_R32U,
    BGFX_TEXTURE_FORMAT_R32F,
    BGFX_TEXTURE_FORMAT_RG8,
    BGFX_TEXTURE_FORMAT_RG8I,
    BGFX_TEXTURE_FORMAT_RG8U,
    BGFX_TEXTURE_FORMAT_RG8S,
    BGFX_TEXTURE_FORMAT_RG16,
    BGFX_TEXTURE_FORMAT_RG16I,
    BGFX_TEXTURE_FORMAT_RG16U,
    BGFX_TEXTURE_FORMAT_RG16F,
    BGFX_TEXTURE_FORMAT_RG16S,
    BGFX_TEXTURE_FORMAT_RG32I,
    BGFX_TEXTURE_FORMAT_RG32U,
    BGFX_TEXTURE_FORMAT_RG32F,
    BGFX_TEXTURE_FORMAT_RGB8,
    BGFX_TEXTURE_FORMAT_RGB8I,
    BGFX_TEXTURE_FORMAT_RGB8U,
    BGFX_TEXTURE_FORMAT_RGB8S,
    BGFX_TEXTURE_FORMAT_RGB9E5F,
    BGFX_TEXTURE_FORMAT_BGRA8,
    BGFX_TEXTURE_FORMAT_RGBA8,
    BGFX_TEXTURE_FORMAT_RGBA8I,
    BGFX_TEXTURE_FORMAT_RGBA8U,
    BGFX_TEXTURE_FORMAT_RGBA8S,
    BGFX_TEXTURE_FORMAT_RGBA16,
    BGFX_TEXTURE_FORMAT_RGBA16I,
    BGFX_TEXTURE_FORMAT_RGBA16U,
    BGFX_TEXTURE_FORMAT_RGBA16F,
    BGFX_TEXTURE_FORMAT_RGBA16S,
    BGFX_TEXTURE_FORMAT_RGBA32I,
    BGFX_TEXTURE_FORMAT_RGBA32U,
    BGFX_TEXTURE_FORMAT_RGBA32F,
    BGFX_TEXTURE_FORMAT_R5G6B5,
    BGFX_TEXTURE_FORMAT_RGBA4,
    BGFX_TEXTURE_FORMAT_RGB5A1,
    BGFX_TEXTURE_FORMAT_RGB10A2,
    BGFX_TEXTURE_FORMAT_RG11B10F,

    BGFX_TEXTURE_FORMAT_UNKNOWN_DEPTH,

    BGFX_TEXTURE_FORMAT_D16,
    BGFX_TEXTURE_FORMAT_D24,
    BGFX_TEXTURE_FORMAT_D24S8,
    BGFX_TEXTURE_FORMAT_D32,
    BGFX_TEXTURE_FORMAT_D16F,
    BGFX_TEXTURE_FORMAT_D24F,
    BGFX_TEXTURE_FORMAT_D32F,
    BGFX_TEXTURE_FORMAT_D0S8,

    BGFX_TEXTURE_FORMAT_COUNT
) = map(bgfx_texture_format, range(86))

# bgfx_uniform_type
bgfx_uniform_type = enum_type
(
    BGFX_UNIFORM_TYPE_SAMPLER,
    BGFX_UNIFORM_TYPE_END,

    BGFX_UNIFORM_TYPE_VEC4,
    BGFX_UNIFORM_TYPE_MAT3,
    BGFX_UNIFORM_TYPE_MAT4,

    BGFX_UNIFORM_TYPE_COUNT
) = map(bgfx_uniform_type, range(6))

# backbuffer_ratio
backbuffer_ratio = enum_type
(
    BGFX_BACKBUFFER_RATIO_EQUAL,
    BGFX_BACKBUFFER_RATIO_HALF,
    BGFX_BACKBUFFER_RATIO_QUARTER,
    BGFX_BACKBUFFER_RATIO_EIGHTH,
    BGFX_BACKBUFFER_RATIO_SIXTEENTH,
    BGFX_BACKBUFFER_RATIO_DOUBLE,

    BGFX_BACKBUFFER_RATIO_COUNT
) = map(backbuffer_ratio, range(7))

# occlusion_query_result
occlusion_query_result = enum_type
(
    BGFX_OCCLUSION_QUERY_RESULT_INVISIBLE,
    BGFX_OCCLUSION_QUERY_RESULT_VISIBLE,
    BGFX_OCCLUSION_QUERY_RESULT_NORESULT,

    BGFX_OCCLUSION_QUERY_RESULT_COUNT
) = map(occlusion_query_result, range(4))

# topology
topology = enum_type
(
    BGFX_TOPOLOGY_TRI_LIST,
    BGFX_TOPOLOGY_TRI_STRIP,
    BGFX_TOPOLOGY_LINE_LIST,
    BGFX_TOPOLOGY_LINE_STRIP,
    BGFX_TOPOLOGY_POINT_LIST,

    BGFX_TOPOLOGY_COUNT
) = map(topology, range(6))

# topology_convert
topology_convert = enum_type
(
    BGFX_TOPOLOGY_CONVERT_TRI_LIST_FLIP_WINDING,
    BGFX_TOPOLOGY_CONVERT_TRI_STRIP_FLIP_WINDING,
    BGFX_TOPOLOGY_CONVERT_TRI_LIST_TO_LINE_LIST,
    BGFX_TOPOLOGY_CONVERT_TRI_STRIP_TO_TRI_LIST,
    BGFX_TOPOLOGY_CONVERT_LINE_STRIP_TO_LINE_LIST,

    BGFX_TOPOLOGY_CONVERT_COUNT
) = map(topology_convert, range(6))

# topology_sort
topology_sort = enum_type
(
    BGFX_TOPOLOGY_SORT_DIRECTION_FRONT_TO_BACK_MIN,
    BGFX_TOPOLOGY_SORT_DIRECTION_FRONT_TO_BACK_AVG,
    BGFX_TOPOLOGY_SORT_DIRECTION_FRONT_TO_BACK_MAX,
    BGFX_TOPOLOGY_SORT_DIRECTION_BACK_TO_FRONT_MIN,
    BGFX_TOPOLOGY_SORT_DIRECTION_BACK_TO_FRONT_AVG,
    BGFX_TOPOLOGY_SORT_DIRECTION_BACK_TO_FRONT_MAX,
    BGFX_TOPOLOGY_SORT_DISTANCE_FRONT_TO_BACK_MIN,
    BGFX_TOPOLOGY_SORT_DISTANCE_FRONT_TO_BACK_AVG,
    BGFX_TOPOLOGY_SORT_DISTANCE_FRONT_TO_BACK_MAX,
    BGFX_TOPOLOGY_SORT_DISTANCE_BACK_TO_FRONT_MIN,
    BGFX_TOPOLOGY_SORT_DISTANCE_BACK_TO_FRONT_AVG,
    BGFX_TOPOLOGY_SORT_DISTANCE_BACK_TO_FRONT_MAX,

    BGFX_TOPOLOGY_SORT_COUNT
) = map(topology_sort, range(13))

# view_mode
view_mode = enum_type
(
    BGFX_VIEW_MODE_DEFAULT,
    BGFX_VIEW_MODE_SEQUENTIAL,
    BGFX_VIEW_MODE_DEPTH_ASCENDING,
    BGFX_VIEW_MODE_DEPTH_DESCENDING,

    BGFX_VIEW_MODE_CCOUNT
) = map(view_mode, range(5))

BGFX_PCI_ID_NONE = 0x0000
BGFX_PCI_ID_SOFTWARE_RASTERIZER = 0x0001
BGFX_PCI_ID_AMD = 0x1002
BGFX_PCI_ID_INTEL = 0x8086
BGFX_PCI_ID_NVIDIA = 0x10de

BGFX_RESET_NONE = 0x00000000  # //!< No reset flags.
BGFX_RESET_FULLSCREEN = 0x00000001  # //!< Not supported yet.
BGFX_RESET_FULLSCREEN_SHIFT = 0          # //!< Fullscreen bit shift.
BGFX_RESET_FULLSCREEN_MASK = 0x00000001  # //!< Fullscreen bit mask.
BGFX_RESET_MSAA_X2 = 0x00000010  # //!< Enable 2x MSAA.
BGFX_RESET_MSAA_X4 = 0x00000020  # //!< Enable 4x MSAA.
BGFX_RESET_MSAA_X8 = 0x00000030  # //!< Enable 8x MSAA.
BGFX_RESET_MSAA_X16 = 0x00000040  # //!< Enable 16x MSAA.
BGFX_RESET_MSAA_SHIFT = 4          # //!< MSAA mode bit shift.
BGFX_RESET_MSAA_MASK = 0x00000070  # //!< MSAA mode bit mask.
BGFX_RESET_VSYNC = 0x00000080  # //!< Enable V-Sync.
BGFX_RESET_MAXANISOTROPY = 0x00000100  # //!< Turn on/off max anisotropy.
BGFX_RESET_CAPTURE = 0x00000200  # //!< Begin screen capture.
BGFX_RESET_HMD = 0x00000400  # //!< HMD stereo rendering.
BGFX_RESET_HMD_DEBUG = 0x00000800  # //!< HMD stereo rendering debug mode.
BGFX_RESET_HMD_RECENTER = 0x00001000  # //!< HMD calibration.
# //!< Flush rendering after submitting to GPU.
BGFX_RESET_FLUSH_AFTER_RENDER = 0x00002000
# //!< This flag  specifies where flip occurs. Default behavior is that flip occurs before rendering new frame. This flag only has effect when `BGFX_CONFIG_MULTITHREADED=0`.
BGFX_RESET_FLIP_AFTER_RENDER = 0x00004000
BGFX_RESET_SRGB_BACKBUFFER = 0x00008000  # //!< Enable sRGB backbuffer.
BGFX_RESET_HIDPI = 0x00010000  # //!< Enable HiDPI rendering.
BGFX_RESET_DEPTH_CLAMP = 0x00020000  # //!< Enable depth clamp.
BGFX_RESET_RESERVED_SHIFT = 31         # //!< Internal bits shift.
BGFX_RESET_RESERVED_MASK = 0x80000000  # //!< Internal bits mask.

BGFX_CLEAR_NONE = 0x0000
BGFX_CLEAR_COLOR = 0x0001
BGFX_CLEAR_DEPTH = 0x0002
BGFX_CLEAR_STENCIL = 0x0004
BGFX_CLEAR_DISCARD_COLOR_0 = 0x0008
BGFX_CLEAR_DISCARD_COLOR_1 = 0x0010
BGFX_CLEAR_DISCARD_COLOR_2 = 0x0020
BGFX_CLEAR_DISCARD_COLOR_3 = 0x0040
BGFX_CLEAR_DISCARD_COLOR_4 = 0x0080
BGFX_CLEAR_DISCARD_COLOR_5 = 0x0100
BGFX_CLEAR_DISCARD_COLOR_6 = 0x0200
BGFX_CLEAR_DISCARD_COLOR_7 = 0x0400
BGFX_CLEAR_DISCARD_DEPTH = 0x0800
BGFX_CLEAR_DISCARD_STENCIL = 0x1000

BGFX_CLEAR_DISCARD_COLOR_MASK = 0 | BGFX_CLEAR_DISCARD_COLOR_0 | BGFX_CLEAR_DISCARD_COLOR_1 | BGFX_CLEAR_DISCARD_COLOR_2 | BGFX_CLEAR_DISCARD_COLOR_3 | BGFX_CLEAR_DISCARD_COLOR_4 | BGFX_CLEAR_DISCARD_COLOR_5 | BGFX_CLEAR_DISCARD_COLOR_6 | BGFX_CLEAR_DISCARD_COLOR_7
BGFX_CLEAR_DISCARD_MASK = 0 | BGFX_CLEAR_DISCARD_COLOR_MASK | BGFX_CLEAR_DISCARD_DEPTH | BGFX_CLEAR_DISCARD_STENCIL

BGFX_DEBUG_NONE = 0x00000000  # //!< No debug.
BGFX_DEBUG_WIREFRAME = 0x00000001  # //!< Enable wireframe for all primitives.
BGFX_DEBUG_IFH = 0x00000002
BGFX_DEBUG_STATS = 0x00000004  # //!< Enable statistics display.
BGFX_DEBUG_TEXT = 0x00000008  # //!< Enable debug text display.

BGFX_BUFFER_NONE = 0x0000

BGFX_INVALID_HANDLE = 0xFFFF

class bgfx_dynamic_index_buffer_handle(Structure):
    _fields_ = [("idx", c_uint16)]

class bgfx_dynamic_vertex_buffer_handle(Structure):
    _fields_ = [("idx", c_uint16)]

class bgfx_frame_buffer_handle(Structure):
    _fields_ = [("idx", c_uint16)]

class bgfx_index_buffer_handle(Structure):
    _fields_ = [("idx", c_uint16)]

class bgfx_indirect_buffer_handle(Structure):
    _fields_ = [("idx", c_uint16)]

class bgfx_occlusion_query_handle(Structure):
    _fields_ = [("idx", c_uint16)]

class bgfx_program_handle(Structure):
    _fields_ = [("idx", c_uint16)]

class bgfx_shader_handle(Structure):
    _fields_ = [("idx", c_uint16)]

class bgfx_texture_handle(Structure):
    _fields_ = [("idx", c_uint16)]

class bgfx_uniform_handle(Structure):
    _fields_ = [("idx", c_uint16)]

class bgfx_vertex_buffer_handle(Structure):
    _fields_ = [("idx", c_uint16)]

class bgfx_vertex_decl_handle(Structure):
    _fields_ = [("idx", c_uint16)]

RELEASEFUNC = CFUNCTYPE(None, c_void_p, c_void_p)

def bgfx_release_fn(ptr, user_data):
    return

class bgfx_memory(Structure):
    _fields_ = [("data", POINTER(c_uint8)),
                ("size", c_uint32)]

class bgfx_transform(Structure):
    _fields_ = [("data", POINTER(c_float)),
                ("num", c_uint16)]

bgfx_view_id = c_uint16

class bgfx_view_stats(Structure):
    _fields_ = [("name", c_char * 256),
                ("view", bgfx_view_id),
                ("cpuTimeElapsed", c_int64),
                ("gpuTimeElapsed", c_int64)]

class bgfx_encoder_stats(Structure):
    _fields_ = [("cpuTimeBegin", c_int64),
                ("cpuTimeEnd", c_int64)]

class bgfx_stats(Structure):
    _fields_ = [
    ("cpuTimeFrame", c_int64),
    ("cpuTimeBegin", c_int64),
    ("cpuTimeEnd", c_int64),
    ("cpuTimerFreq", c_int64),

    ("gpuTimeBegin", c_int64),
    ("gpuTimeEnd", c_int64),
    ("gpuTimerFreq", c_int64),

    ("waitRender", c_int64),
    ("waitSubmit", c_int64),

    ("numDraw", c_uint32),
    ("numCompute", c_uint32),
    ("numBlit", c_uint32),
    ("maxGpuLatency", c_uint32),

    ("numDynamicIndexBuffers", c_uint16),
    ("numDynamicVertexBuffers", c_uint16),
    ("numFrameBuffers", c_uint16),
    ("numIndexBuffers", c_uint16),
    ("numOcclusionQueries", c_uint16),
    ("numPrograms", c_uint16),
    ("numShaders", c_uint16),
    ("numTextures", c_uint16),
    ("numUniforms", c_uint16),
    ("numVertexBuffers", c_uint16),
    ("numVertexDecls", c_uint16),

    ("textureMemoryUsed", c_int64),
    ("rtMemoryUsed", c_int64),
    ("transientVbUsed", c_uint32),
    ("transientIbUsed", c_uint32),

    ("numPrims", c_uint32 * BGFX_TOPOLOGY_COUNT.value),

    ("gpuMemoryMax", c_int64),
    ("gpuMemoryUsed", c_int64),

    ("width", c_uint16),
    ("height", c_uint16),
    ("textWidth", c_uint16),
    ("textHeight", c_uint16),

    ("numViews", c_uint16),
    ("viewStats", POINTER(bgfx_view_stats)),

    ("numEncoders", c_uint8),
    ("encoderStats", POINTER(bgfx_encoder_stats))]

class vertex_decl(Structure):
    _fields_ = [("hash", c_uint32),
                ("stride", c_uint16),
                ("offset", c_uint16 * BGFX_ATTRIB_COUNT.value),
                ("attributes", c_uint16 * BGFX_ATTRIB_COUNT.value)]

class bgfx_transient_index_buffer(Structure):
    _fields_ = [("data", POINTER(c_uint8)),
                ("size", c_uint32),
                ("handle", bgfx_index_buffer_handle),
                ("startIndex", c_uint32)]

class transient_vertex_buffer(Structure):
    _fields_ = [("data", POINTER(c_uint8)),
                ("size", c_uint32),
                ("startVertex", c_uint32),
                ("stride", c_uint16),
                ("handle", bgfx_vertex_buffer_handle),
                ("decl", bgfx_vertex_decl_handle)]

class bgfx_instance_data_buffer(Structure):
    _fields_ = [
        ("data", c_uint8),
        ("size", c_uint32),
        ("offset", c_uint32),
        ("num", c_uint32),
        ("stride", c_uint16),
        ("handle", bgfx_vertex_buffer_handle)
    ]

class texture_info(Structure):
    _fields_ = [
        ("format", bgfx_texture_format),
        ("storageSize", c_uint32),
        ("width", c_uint16),
        ("height", c_uint16),
        ("depth", c_uint16),
        ("numLayers", c_uint16),
        ("numMips", c_uint8),
        ("bitsPerPixel", c_uint8),
        ("cubeMap", c_bool)
    ]

class uniform_info(Structure):
    _fields_ = [
        ("name", c_char * 256),
        ("type", bgfx_uniform_type),
        ("num", c_uint16)
    ]

class attachment(Structure):
    _fields_ = [
        ("access", bgfx_access),
        ("handle", bgfx_texture_handle),
        ("mip", c_uint16),
        ("layer", c_uint16),
        ("resolve", c_uint8)
    ]

class caps_gpu(Structure):
    _fields_ = [
        ("vendorId", c_uint16),
        ("deviceId", c_uint16)
    ]

class cap_limits(Structure):
    _fields_ = [
        ("maxDrawCalls", c_uint32),
        ("maxBlits", c_uint32),
        ("maxTextureSize", c_uint32),
        ("maxTextureLayers", c_uint32),
        ("maxViews", c_uint32),
        ("maxFrameBuffers", c_uint32),
        ("maxFBAttachments", c_uint32),
        ("maxPrograms", c_uint32),
        ("maxShaders", c_uint32),
        ("maxTextures", c_uint32),
        ("maxTextureSamplers", c_uint32),
        ("maxComputeBindings", c_uint32),
        ("maxVertexDecls", c_uint32),
        ("maxVertexStreams", c_uint32),
        ("maxIndexBuffers", c_uint32),
        ("maxVertexBuffers", c_uint32),
        ("maxDynamicIndexBuffers", c_uint32),
        ("maxDynamicVertexBuffers", c_uint32),
        ("maxUniforms", c_uint32),
        ("maxOcclusionQueries", c_uint32),
        ("maxEncoders", c_uint32),
        ("transientVbSize", c_uint32),
        ("transientIbSize", c_uint32)
    ]

class caps(Structure):
    _fields_ = [
        ("rendererType", bgfx_renderer_type),
        ("supported", c_uint64),
        ("vendorId", c_uint16),
        ("deviceId", c_uint16),
        ("homogeneousDepth", c_bool),
        ("originBottomLeft", c_bool),
        ("numGPUs", c_uint8),
        ("gpu", caps_gpu * 4),
        ("limits", cap_limits),
        ("formats", c_uint16 * BGFX_TEXTURE_FORMAT_COUNT.value)
    ]

# bgfx_fatal
bgfx_fatal = enum_type
(
    BGFX_FATAL_DEBUG_CHECK,
    BGFX_FATAL_INVALID_SHADER,
    BGFX_FATAL_UNABLE_TO_INITIALIZE,
    BGFX_FATAL_UNABLE_TO_CREATE_TEXTURE,
    BGFX_FATAL_DEVICE_LOST,

    BGFX_FATAL_COUNT
) = map(bgfx_fatal, range(6))

#fatal = CFUNCTYPE(None, POINTER(bgfx_callback_interface_s), c_char_p, c_uint16, c_int, c_char_p)
#trace_vargs = CFUNCTYPE(None, POINTER(bgfx_callback_interface_s), c_char_p, c_uint16, c_char_p) # BUG va_list _argList
#profiler_begin = CFUNCTYPE(None, POINTER(bgfx_callback_interface_s), c_char_p, c_uint32, c_char_p, c_uint16)
#profiler_begin_literal = CFUNCTYPE(None, POINTER(bgfx_callback_interface_s), c_char_p, c_uint32, c_char_p, c_uint16)
#profiler_end = CFUNCTYPE(None, POINTER(bgfx_callback_interface_s))
#cache_read_size = CFUNCTYPE(c_uint32, POINTER(bgfx_callback_interface_s), c_uint64)
#cache_read = CFUNCTYPE(c_bool, POINTER(bgfx_callback_interface_s), c_uint64, c_void_p, c_uint32)
#cache_write = CFUNCTYPE(None, POINTER(bgfx_callback_interface_s), c_uint64, c_void_p, c_uint32)
#screen_shot = CFUNCTYPE(None, POINTER(bgfx_callback_interface_s), c_char_p, c_uint32, c_uint32, c_uint32, c_void_p, c_uint32, c_bool)
#capture_begin = CFUNCTYPE(None, POINTER(bgfx_callback_interface_s), c_uint32, c_uint32, c_uint32, c_int, c_bool)
#capture_end = CFUNCTYPE(None, POINTER(bgfx_callback_interface_s))
#capture_frame = CFUNCTYPE(None, POINTER(bgfx_callback_interface_s), c_void_p, c_uint32)

#class bgfx_callback_vtbl_s(Structure):
#    _fields_ = [
#        ("fatal", fatal)
#        ("trace_vargs", trace_vargs)
#        ("profiler_begin", profiler_begin)
#        ("profiler_begin_literal", profiler_begin_literal)
#        ("profiler_end", profiler_end)
#        ("cache_read_size", cache_read_size)
#        ("cache_read", cache_read)
#        ("cache_write", cache_write)
#        ("screen_shot", screen_shot)
#        ("capture_begin", capture_begin)
#        ("capture_end", capture_end)
#        ("capture_frame", capture_frame)
#    ]

#class bgfx_callback_interface_s(Structure):
#    _fields_ = [
#        ("vtbl", POINTER(bgfx_callback_vtbl_s))
#    ]

class bgfx_callback_interface_t(Structure):
    _fields_ = []

#realloc = CFUNCTYPE(c_void_p, POINTER(bgfx_allocator_interface_s), c_void_p, c_size_t, c_size_t, c_char_p, c_uint32)
#
#class bgfx_allocator_interface_s(Structure):
#    _fields_ = [
#        ("realloc", realloc)
#    ]

class bgfx_allocator_interface_t(Structure):
    _fields_ = []

class bgfx_platform_data(Structure):
    _fields_ = [
        ("ndt", c_void_p),
        ("nwh", c_void_p),
        ("context", c_void_p),
        ("backBuffer", c_void_p),
        ("backBufferDS", c_void_p)
    ]

class bgfx_resolution_s(Structure):
    _fields_ = [
        ("format", c_int),
        ("width", c_uint32),
        ("height", c_uint32),
        ("reset", c_uint32),
        ("numBackBuffers", c_uint8),
        ("maxFrameLatency", c_uint8)
    ]

class bgfx_init_limits_s(Structure):
    _fields_ = [
        ("maxEncoders", c_uint16),
        ("transientVbSize", c_uint32),
        ("transientIbSize", c_uint32)
    ]

# https://bkaradzic.github.io/bgfx/bgfx.html#_CPPv2N4bgfx4InitE
class bgfx_init_t(Structure):
    _fields_ = [
        ("type", c_int),
        ("vendorId", c_uint16),
        ("deviceId", c_uint16),
        ("debug", c_bool),
        ("profile", c_bool),

        ("platformData", bgfx_platform_data),
        ("resolution", bgfx_resolution_s),
        ("limits", bgfx_init_limits_s),
        ("callback", POINTER(bgfx_callback_interface_t)),
        ("allocator", POINTER(bgfx_allocator_interface_t))
    ]

def _bind(funcname, args=None, returns=None):
    func = getattr(_bgfx, funcname)
    func.argtypes = args
    func.restype = returns
    return func

vertex_decl_begin = _bind("bgfx_vertex_decl_begin",
    args=[POINTER(vertex_decl), bgfx_renderer_type],
    returns=None)

vertex_decl_add = _bind("bgfx_vertex_decl_add",
    args=[POINTER(vertex_decl), bgfx_attrib, c_uint8, bgfx_attrib_type, c_bool, c_bool],
    returns=None)

vertex_decl_decode = _bind("bgfx_vertex_decl_decode",
    args=[POINTER(vertex_decl), bgfx_attrib, POINTER(c_uint8), POINTER(bgfx_attrib_type), POINTER(c_bool), POINTER(c_bool)],
    returns=None)

vertex_decl_has = _bind("bgfx_vertex_decl_has",
    args=[POINTER(vertex_decl), c_uint8],
    returns=c_bool)

vertex_decl_skip = _bind("bgfx_vertex_decl_skip",
    args=[POINTER(vertex_decl), c_uint8],
    returns=None)

vertex_decl_end = _bind("bgfx_vertex_decl_end",
    args=[POINTER(vertex_decl)],
    returns=None)

vertex_pack = _bind("bgfx_vertex_pack",
    args=[POINTER(c_float), c_bool, bgfx_attrib, POINTER(vertex_decl), c_void_p, c_uint32],
    returns=None)

vertex_unpack = _bind("bgfx_vertex_unpack",
    args=[POINTER(c_float), bgfx_attrib, POINTER(vertex_decl), c_void_p, c_uint32],
    returns=None)

vertex_convert = _bind("bgfx_vertex_convert",
    args=[POINTER(vertex_decl), c_void_p, POINTER(vertex_decl), c_void_p, c_uint32],
    returns=None)

weld_vertices = _bind("bgfx_weld_vertices",
    args=[POINTER(c_uint16), POINTER(vertex_decl), c_void_p, c_uint16, c_float],
    returns=c_uint16)

topology_convert = _bind("bgfx_topology_convert",
    args=[topology_convert, c_void_p, c_uint32, c_void_p, c_uint32, c_bool],
    returns=None)

topology_sort_tri_list = _bind("bgfx_topology_sort_tri_list",
    args=[topology_sort, c_void_p, c_uint32, POINTER(c_float), POINTER(c_float), c_void_p, c_uint32, c_void_p, c_uint32, c_bool],
    returns=None)

get_supported_renderers = _bind("bgfx_get_supported_renderers",
    args=[c_uint8, POINTER(bgfx_renderer_type)],
    returns=c_uint8)

get_renderer_name = _bind("bgfx_get_renderer_name",
    args=[bgfx_renderer_type],
    returns=c_char_p)

init_ctor = _bind("bgfx_init_ctor",
             args=[POINTER(bgfx_init_t)],
             returns=None)

# bgfx_init
# https://bkaradzic.github.io/bgfx/bgfx.html#_CPPv2N4bgfx4initERK4Init
init = _bind("bgfx_init",
             args=[POINTER(bgfx_init_t)],
             returns=c_bool)

shutdown = _bind("bgfx_shutdown")

reset = _bind("bgfx_reset",
              args=[c_uint32, c_uint32, c_uint32, bgfx_texture_format])

class bgfx_encoder(Structure):
    _fields_ = []

#begin = _bind("bgfx_begin",
#    args=[],
#    returns=POINTER(bgfx_encoder))

#end = _bind("bgfx_end",
#    args=[POINTER(bgfx_encoder)],
#    returns=None)

frame = _bind("bgfx_frame",
              args=[c_bool],
              returns=c_uint32)

get_renderer_type = _bind("bgfx_get_renderer_type",
    args=[],
    returns=bgfx_renderer_type)

get_caps = _bind("bgfx_get_caps",
    args=[],
    returns=POINTER(caps))

get_stats = _bind("bgfx_get_stats",
    args=[],
    returns=POINTER(bgfx_stats))

alloc = _bind("bgfx_alloc",
    args=[c_uint32],
    returns=POINTER(bgfx_memory))

copy = _bind("bgfx_copy",
    args=[c_void_p, c_uint32],
    returns=POINTER(bgfx_memory))

make_ref = _bind("bgfx_make_ref",
    args=[c_void_p, c_uint32],
    returns=POINTER(bgfx_memory))

make_ref_release = _bind("bgfx_make_ref_release",
    args=[c_void_p, c_uint32, RELEASEFUNC, c_void_p],
    returns=POINTER(bgfx_memory))

set_debug = _bind("bgfx_set_debug",
    args=[c_uint32],
    returns=None)

dbg_text_clear = _bind("bgfx_dbg_text_clear",
    args=[c_uint8, c_bool])

dbg_text_printf = _bind("bgfx_dbg_text_printf",
    args=[c_uint16, c_uint16, c_uint8, c_char_p])

dbg_text_image = _bind("bgfx_dbg_text_image",
    args=[c_uint16, c_uint16, c_uint16, c_uint16, c_void_p, c_uint16])

dbg_text_vprintf = _bind("bgfx_dbg_text_vprintf",
    args=[c_uint16, c_uint16, c_uint8, c_char_p],
    returns=None)

dbg_text_image = _bind("bgfx_dbg_text_image",
    args=[c_uint16, c_uint16, c_uint16, c_uint16, c_void_p, c_uint16],
    returns=None)

create_index_buffer = _bind("bgfx_create_index_buffer",
    args=[POINTER(bgfx_memory), c_uint16],
    returns=bgfx_index_buffer_handle)

set_index_buffer_name = _bind("bgfx_set_index_buffer_name",
    args=[bgfx_index_buffer_handle, c_char_p, c_int32],
    returns=None)

destroy_index_buffer = _bind("bgfx_destroy_index_buffer",
    args=[bgfx_index_buffer_handle])

create_vertex_buffer = _bind("bgfx_create_vertex_buffer",
    args=[POINTER(bgfx_memory), POINTER(vertex_decl), c_uint16],
    returns=bgfx_vertex_buffer_handle)

set_vertex_buffer_name = _bind("bgfx_set_vertex_buffer_name",
    args=[bgfx_vertex_buffer_handle, c_char_p, c_int32],
    returns=None)

destroy_vertex_buffer = _bind("bgfx_destroy_vertex_buffer",
    args=[bgfx_vertex_buffer_handle])

create_dynamic_index_buffer = _bind("bgfx_create_dynamic_index_buffer",
    args=[c_uint32, c_uint16],
    returns=bgfx_dynamic_index_buffer_handle)

create_dynamic_index_buffer_mem = _bind("bgfx_create_dynamic_index_buffer_mem",
    args=[POINTER(bgfx_memory), c_uint16],
    returns=bgfx_dynamic_index_buffer_handle)

update_dynamic_index_buffer = _bind("bgfx_update_dynamic_index_buffer",
    args=[bgfx_dynamic_index_buffer_handle, c_uint32, POINTER(bgfx_memory)],
    returns=None)

destroy_dynamic_index_buffer = _bind("bgfx_destroy_dynamic_index_buffer",
    args=[bgfx_dynamic_index_buffer_handle],
    returns=None)

create_dynamic_vertex_buffer = _bind("bgfx_create_dynamic_vertex_buffer",
    args=[c_uint32, POINTER(vertex_decl), c_uint16],
    returns=bgfx_dynamic_vertex_buffer_handle)

create_dynamic_vertex_buffer_mem = _bind("bgfx_create_dynamic_vertex_buffer_mem",
    args=[POINTER(bgfx_memory), POINTER(vertex_decl), c_uint16],
    returns=bgfx_dynamic_vertex_buffer_handle)

update_dynamic_vertex_buffer = _bind("bgfx_update_dynamic_vertex_buffer",
    args=[bgfx_dynamic_vertex_buffer_handle, c_uint32, POINTER(bgfx_memory)],
    returns=None)

destroy_dynamic_vertex_buffer = _bind("bgfx_destroy_dynamic_vertex_buffer",
    args=[bgfx_dynamic_vertex_buffer_handle],
    returns=None)

get_avail_transient_index_buffer = _bind("bgfx_get_avail_transient_index_buffer",
    args=[c_uint32],
    returns=c_uint32)

get_avail_transient_vertex_buffer = _bind("bgfx_get_avail_transient_vertex_buffer",
    args=[c_uint32, POINTER(vertex_decl)],
    returns=c_uint32)

get_avail_instance_data_buffer = _bind("bgfx_get_avail_instance_data_buffer",
    args=[c_uint32, c_uint16],
    returns=c_uint32)

alloc_transient_index_buffer = _bind("bgfx_alloc_transient_index_buffer",
    args=[POINTER(bgfx_transient_index_buffer), c_uint32],
    returns=None)

alloc_transient_vertex_buffer = _bind("bgfx_alloc_transient_vertex_buffer",
    args=[POINTER(transient_vertex_buffer), c_uint32, POINTER(vertex_decl)],
    returns=None)

alloc_transient_buffers = _bind("bgfx_alloc_transient_buffers",
    args=[POINTER(transient_vertex_buffer), POINTER(vertex_decl), c_uint32, POINTER(bgfx_transient_index_buffer), c_uint32],
    returns=c_bool)

alloc_instance_data_buffer = _bind("bgfx_alloc_instance_data_buffer",
    args=[POINTER(bgfx_instance_data_buffer), c_uint32, c_uint16],
    returns=None)

create_indirect_buffer = _bind("bgfx_create_indirect_buffer",
    args=[c_uint32],
    returns=bgfx_indirect_buffer_handle)

destroy_indirect_buffer = _bind("bgfx_destroy_indirect_buffer",
    args=[bgfx_indirect_buffer_handle],
    returns=None)

create_shader = _bind("bgfx_create_shader",
    args=[POINTER(bgfx_memory)],
    returns=bgfx_shader_handle)

get_shader_uniforms = _bind("bgfx_get_shader_uniforms",
    args=[bgfx_shader_handle, POINTER(bgfx_uniform_handle), c_uint16],
    returns=c_uint16)

get_uniform_info = _bind("bgfx_get_uniform_info",
    args=[bgfx_uniform_handle, POINTER(uniform_info)],
    returns=None)

set_shader_name = _bind("bgfx_set_shader_name",
    args=[bgfx_shader_handle, c_char_p, c_int32],
    returns=None)

destroy_shader = _bind("bgfx_destroy_shader",
    args=[bgfx_shader_handle],
    returns=None)

create_program = _bind("bgfx_create_program",
    args=[bgfx_shader_handle, bgfx_shader_handle, c_bool],
    returns=bgfx_program_handle)

create_compute_program = _bind("bgfx_create_compute_program",
    args=[bgfx_shader_handle, c_bool],
    returns=bgfx_program_handle)

destroy_program = _bind("bgfx_destroy_program",
    args=[bgfx_program_handle],
    returns=None)

is_texture_valid = _bind("bgfx_is_texture_valid",
    args=[c_uint16, c_bool, c_uint16, bgfx_texture_format, c_uint64],
    returns=c_bool)

calc_texture_size = _bind("bgfx_calc_texture_size",
    args=[POINTER(texture_info), c_uint16, c_uint16, c_uint16, c_bool, c_bool, c_uint16, bgfx_texture_format],
    returns=None)

create_texture = _bind("bgfx_create_texture",
    args=[POINTER(bgfx_memory), c_uint64, c_uint8, POINTER(texture_info)],
    returns=bgfx_texture_handle)

create_texture_2d = _bind("bgfx_create_texture_2d",
    args=[c_uint16, c_uint16, c_bool, c_uint16, bgfx_texture_format, c_uint64, POINTER(bgfx_memory)],
    returns=bgfx_texture_handle)

create_texture_2d_scaled = _bind("bgfx_create_texture_2d_scaled",
    args=[backbuffer_ratio, c_bool, c_uint16, bgfx_texture_format, c_uint64],
    returns=bgfx_texture_handle)

create_texture_3d = _bind("bgfx_create_texture_3d",
    args=[c_uint16, c_uint16, c_uint16, c_bool, bgfx_texture_format, c_uint64, POINTER(bgfx_memory)],
    returns=bgfx_texture_handle)

create_texture_cube = _bind("bgfx_create_texture_cube",
    args=[c_uint16, c_bool, c_uint16, bgfx_texture_format, c_uint64, POINTER(bgfx_memory)],
    returns=bgfx_texture_handle)

update_texture_2d = _bind("bgfx_update_texture_2d",
    args=[bgfx_texture_handle, c_uint16, c_uint8, c_uint16, c_uint16, c_uint16, c_uint16, POINTER(bgfx_memory), c_uint16],
    returns=None)

update_texture_3d = _bind("bgfx_update_texture_3d",
    args=[bgfx_texture_handle, c_uint8, c_uint16, c_uint16, c_uint16, c_uint16, c_uint16, c_uint16, POINTER(bgfx_memory)],
    returns=None)

update_texture_cube = _bind("bgfx_update_texture_cube",
    args=[bgfx_texture_handle, c_uint16, c_uint8, c_uint8, c_uint16, c_uint16, c_uint16, c_uint16, POINTER(bgfx_memory), c_uint16],
    returns=None)

read_texture = _bind("bgfx_read_texture",
    args=[bgfx_texture_handle, c_void_p, c_uint8],
    returns=c_uint32)

set_texture_name = _bind("bgfx_set_texture_name",
    args=[bgfx_texture_handle],
    returns=None)

destroy_texture = _bind("bgfx_destroy_texture",
    args=[bgfx_texture_handle],
    returns=None)

create_frame_buffer = _bind("bgfx_create_frame_buffer",
    args=[c_uint16, c_uint16, bgfx_texture_format, c_uint64],
    returns=bgfx_frame_buffer_handle)

set_frame_buffer_name = _bind("bgfx_set_frame_buffer_name",
    args=[bgfx_frame_buffer_handle, c_char_p, c_int32],
    returns=None)

create_frame_buffer_scaled = _bind("bgfx_create_frame_buffer_scaled",
    args=[backbuffer_ratio, bgfx_texture_format, c_uint64],
    returns=bgfx_frame_buffer_handle)

create_frame_buffer_from_handles = _bind("bgfx_create_frame_buffer_from_handles",
    args=[c_uint8, POINTER(bgfx_texture_handle), c_bool],
    returns=bgfx_frame_buffer_handle)

create_frame_buffer_from_attachment = _bind("bgfx_create_frame_buffer_from_attachment",
    args=[c_uint8, POINTER(attachment), c_bool],
    returns=bgfx_frame_buffer_handle)

create_frame_buffer_from_nwh = _bind("bgfx_create_frame_buffer_from_nwh",
    args=[c_void_p, c_uint16, c_uint16, bgfx_texture_format, bgfx_texture_format],
    returns=bgfx_frame_buffer_handle)

get_texture = _bind("bgfx_get_texture",
    args=[bgfx_frame_buffer_handle, c_uint8],
    returns=bgfx_texture_handle)

destroy_frame_buffer = _bind("bgfx_destroy_frame_buffer",
    args=[bgfx_frame_buffer_handle],
    returns=None)

create_uniform = _bind("bgfx_create_uniform",
    args=[c_char_p, bgfx_uniform_type, c_uint16],
    returns=bgfx_uniform_handle)

destroy_uniform = _bind("bgfx_destroy_uniform",
    args=[bgfx_uniform_handle],
    returns=None)

create_occlusion_query = _bind("bgfx_create_occlusion_query",
    args=[],
    returns=bgfx_occlusion_query_handle)

get_result = _bind("bgfx_get_result",
    args=[bgfx_occlusion_query_handle, POINTER(c_int32)],
    returns=occlusion_query_result)

destroy_occlusion_query = _bind("bgfx_destroy_occlusion_query",
    args=[bgfx_occlusion_query_handle],
    returns=None)

set_palette_color = _bind("bgfx_set_palette_color",
    args=[c_uint8, POINTER(c_float)],
    returns=None)

set_view_name = _bind("bgfx_set_view_name",
    args=[bgfx_view_id, c_char_p],
    returns=None)

set_view_rect = _bind("bgfx_set_view_rect",
    args=[bgfx_view_id, c_uint16, c_uint16, c_uint16, c_uint16],
    returns=None)

set_view_rect_auto = _bind("bgfx_set_view_rect_auto",
    args=[bgfx_view_id, c_uint16, c_uint16, backbuffer_ratio],
    returns=None)

set_view_scissor = _bind("bgfx_set_view_scissor",
    args=[bgfx_view_id, c_uint16, c_uint32, c_float, c_uint8],
    returns=None)

set_view_clear = _bind("bgfx_set_view_clear",
    args=[bgfx_view_id, c_uint16, c_uint32, c_float, c_uint8],
    returns=None)

set_view_clear_mrt = _bind("bgfx_set_view_clear_mrt",
    args=[bgfx_view_id, c_uint16, c_float, c_uint8, c_uint8, c_uint8, c_uint8, c_uint8, c_uint8, c_uint8, c_uint8, c_uint8],
    returns=None)

set_view_mode = _bind("bgfx_set_view_mode",
    args=[bgfx_view_id, view_mode],
    returns=None)

set_view_frame_buffer = _bind("bgfx_set_view_frame_buffer",
    args=[bgfx_view_id, bgfx_frame_buffer_handle],
    returns=None)

set_view_transform = _bind("bgfx_set_view_transform",
    args=[bgfx_view_id, c_void_p, c_void_p],
    returns=None)

set_view_order = _bind("bgfx_set_view_order",
    args=[bgfx_view_id, c_uint16, POINTER(bgfx_view_id)],
    returns=None)

reset_view = _bind("bgfx_reset_view",
    args=[bgfx_view_id],
    returns=None)

set_marker = _bind("bgfx_set_marker",
    args=[c_char_p],
    returns=None)

set_state = _bind("bgfx_set_state",
    args=[c_uint64, c_uint32],
    returns=None)

set_condition = _bind("bgfx_set_condition",
    args=[c_uint64, c_uint32],
    returns=None)

set_stencil = _bind("bgfx_set_stencil",
    args=[c_uint32, c_uint32],
    returns=None)

set_scissor = _bind("bgfx_set_scissor",
    args=[c_uint16, c_uint16, c_uint16, c_uint16],
    returns=c_uint16)

set_scissor_cache = _bind("bgfx_set_scissor_cached",
    args=[c_uint16],
    returns=None)

_set_transform = _bind("bgfx_set_transform",
    args=[c_void_p, c_uint16],
    returns=c_uint32)

def set_transform(mtx, count):
    return _set_transform(mtx.ctypes.data_as(POINTER(c_void_p)), 1)

alloc_transform = _bind("bgfx_alloc_transform",
    args=[POINTER(bgfx_transform), c_uint16],
    returns=c_uint32)

set_transform_cached = _bind("bgfx_set_transform_cached",
    args=[c_uint32, c_uint16],
    returns=None)

set_uniform = _bind("bgfx_set_uniform",
    args=[bgfx_uniform_handle, c_void_p, c_uint16],
    returns=None)

set_index_buffer = _bind("bgfx_set_index_buffer",
    args=[bgfx_index_buffer_handle, c_uint32, c_uint32],
    returns=None)

set_dynamic_index_buffer = _bind("bgfx_set_dynamic_index_buffer",
    args=[bgfx_dynamic_index_buffer_handle, c_uint32, c_uint32],
    returns=None)

set_transient_index_buffer = _bind("bgfx_set_transient_index_buffer",
    args=[POINTER(bgfx_transient_index_buffer), c_uint32, c_uint32],
    returns=None)

set_vertex_buffer = _bind("bgfx_set_vertex_buffer",
    args=[c_uint8, bgfx_vertex_buffer_handle, c_uint32, c_uint32],
    returns=None)

set_dynamic_vertex_buffer = _bind("bgfx_set_dynamic_vertex_buffer",
    args=[c_uint8, POINTER(bgfx_dynamic_vertex_buffer_handle), c_uint32, c_uint32],
    returns=None)

set_transient_vertex_buffer = _bind("bgfx_set_transient_vertex_buffer",
    args=[c_uint8, POINTER(transient_vertex_buffer), c_uint32, c_uint32],
    returns=None)

set_vertex_count = _bind("bgfx_set_vertex_count",
    args=[c_uint32],
    returns=None)

set_instance_data_buffer = _bind("bgfx_set_instance_data_buffer",
    args=[POINTER(bgfx_instance_data_buffer), c_uint32, c_uint32],
    returns=None)

set_instance_data_from_vertex_buffer = _bind("bgfx_set_instance_data_from_vertex_buffer",
    args=[bgfx_vertex_buffer_handle, c_uint32, c_uint32],
    returns=None)

set_instance_data_from_dynamic_vertex_buffer = _bind("bgfx_set_instance_data_from_dynamic_vertex_buffer",
	args=[bgfx_dynamic_vertex_buffer_handle, c_uint32, c_uint32],
	returns=None)

set_instance_count = _bind("bgfx_set_instance_count",
	args=[c_uint32],
	returns=None)

set_texture = _bind("bgfx_set_texture",
	args=[c_uint8, bgfx_uniform_handle, bgfx_texture_handle, c_uint32],
	returns=None)

touch = _bind("bgfx_touch",
	args=[bgfx_view_id],
	returns=None)

submit = _bind("bgfx_submit",
	args=[bgfx_view_id, bgfx_program_handle, c_uint32, c_bool],
	returns=None)

submit_occlusion_query = _bind("bgfx_submit_occlusion_query",
	args=[bgfx_view_id, bgfx_program_handle, bgfx_occlusion_query_handle, c_uint32, c_bool],
	returns=None)

submit_indirect = _bind("bgfx_submit_indirect",
	args=[bgfx_view_id, bgfx_program_handle, bgfx_indirect_buffer_handle, c_uint16, c_uint16, c_uint32, c_bool],
	returns=None)

set_image = _bind("bgfx_set_image",
	args=[c_uint8, bgfx_texture_handle, c_uint8, bgfx_access, bgfx_texture_format],
	returns=None)

set_compute_index_buffer = _bind("bgfx_set_compute_index_buffer",
	args=[c_uint8, bgfx_index_buffer_handle, bgfx_access],
	returns=None)

set_compute_vertex_buffer = _bind("bgfx_set_compute_vertex_buffer",
	args=[c_uint8, bgfx_vertex_buffer_handle, bgfx_access],
	returns=None)

set_compute_dynamic_index_buffer = _bind("bgfx_set_compute_dynamic_index_buffer",
	args=[c_uint8, bgfx_dynamic_index_buffer_handle, bgfx_access],
	returns=None)

set_compute_dynamic_vertex_buffer = _bind("bgfx_set_compute_dynamic_vertex_buffer",
	args=[c_uint8, bgfx_dynamic_vertex_buffer_handle, bgfx_access],
	returns=None)

set_compute_indirect_buffer = _bind("bgfx_set_compute_indirect_buffer",
	args=[c_uint8, bgfx_indirect_buffer_handle, bgfx_access],
	returns=None)

dispatch = _bind("bgfx_dispatch",
	args=[bgfx_view_id, bgfx_program_handle, c_uint32, c_uint32, c_uint32],
	returns=None)

dispatch_indirect = _bind("bgfx_dispatch_indirect",
	args=[bgfx_view_id, bgfx_program_handle, bgfx_indirect_buffer_handle, c_uint16, c_uint16],
	returns=None)

discard = _bind("bgfx_discard",
	args=[c_void_p],
	returns=None)

blit = _bind("bgfx_blit",
	args=[bgfx_view_id, bgfx_texture_handle, c_uint8, c_uint16, c_uint16, c_uint16, bgfx_texture_handle, c_uint8, c_uint16, c_uint16, c_uint16, c_uint16, c_uint16, c_uint16],
	returns=None)

encoder_set_marker = _bind("bgfx_encoder_set_marker",
	args=[POINTER(bgfx_encoder), POINTER(c_char)],
	returns=None)

encoder_set_state = _bind("bgfx_encoder_set_state",
	args=[POINTER(bgfx_encoder), c_uint64, c_uint32],
	returns=None)

encoder_set_condition = _bind("bgfx_encoder_set_condition",
	args=[POINTER(bgfx_encoder), bgfx_occlusion_query_handle, c_bool],
	returns=None)

encoder_set_stencil = _bind("bgfx_encoder_set_stencil",
	args=[POINTER(bgfx_encoder), c_uint32, c_uint32],
	returns=None)

encoder_set_scissor = _bind("bgfx_encoder_set_scissor",
	args=[POINTER(bgfx_encoder), c_uint16, c_uint16, c_uint16, c_uint16],
	returns=c_uint16)

encoder_set_scissor_cached = _bind("bgfx_encoder_set_scissor_cached",
	args=[POINTER(bgfx_encoder), c_uint16],
	returns=None)

encoder_set_transform = _bind("bgfx_encoder_set_transform",
	args=[POINTER(bgfx_encoder), POINTER(c_void_p), c_uint16],
	returns=c_uint32)

encoder_alloc_transform = _bind("bgfx_encoder_alloc_transform",
	args=[POINTER(bgfx_encoder), POINTER(bgfx_transform), c_uint16],
	returns=c_uint32)

encoder_set_transform_cached = _bind("bgfx_encoder_set_transform_cached",
	args=[POINTER(bgfx_encoder), c_uint32, c_uint16],
	returns=None)

encoder_set_uniform = _bind("bgfx_encoder_set_uniform",
	args=[POINTER(bgfx_encoder), bgfx_uniform_handle, POINTER(c_void_p), c_uint16],
	returns=None)

encoder_set_index_buffer = _bind("bgfx_encoder_set_index_buffer",
	args=[POINTER(bgfx_encoder), bgfx_index_buffer_handle, c_uint32, c_uint32],
	returns=None)

encoder_set_dynamic_index_buffer = _bind("bgfx_encoder_set_dynamic_index_buffer",
	args=[POINTER(bgfx_encoder), bgfx_dynamic_index_buffer_handle, c_uint32, c_uint32],
	returns=None)

encoder_set_transient_index_buffer = _bind("bgfx_encoder_set_transient_index_buffer",
	args=[POINTER(bgfx_encoder), POINTER(bgfx_transient_index_buffer), c_uint32, c_uint32],
	returns=None)

encoder_set_vertex_buffer = _bind("bgfx_encoder_set_vertex_buffer",
	args=[POINTER(bgfx_encoder), c_uint8, bgfx_vertex_buffer_handle, c_uint32, c_uint32],
	returns=None)

encoder_set_dynamic_vertex_buffer = _bind("bgfx_encoder_set_dynamic_vertex_buffer",
	args=[POINTER(bgfx_encoder), c_uint8, bgfx_dynamic_vertex_buffer_handle, c_uint32, c_uint32],
	returns=None)

encoder_set_transient_vertex_buffer = _bind("bgfx_encoder_set_transient_vertex_buffer",
	args=[POINTER(bgfx_encoder), c_uint8, POINTER(bgfx_transient_index_buffer), c_uint32, c_uint32],
	returns=None)

encoder_set_vertex_count = _bind("bgfx_encoder_set_vertex_count",
	args=[POINTER(bgfx_encoder), c_uint32],
	returns=None)

encoder_set_instance_data_buffer = _bind("bgfx_encoder_set_instance_data_buffer",
	args=[POINTER(bgfx_encoder), POINTER(bgfx_instance_data_buffer), c_uint32, c_uint32],
	returns=None)

encoder_set_instance_data_from_vertex_buffer = _bind("bgfx_encoder_set_instance_data_from_vertex_buffer",
	args=[POINTER(bgfx_encoder), bgfx_vertex_buffer_handle, c_uint32, c_uint32],
	returns=None)

encoder_set_instance_data_from_dynamic_vertex_buffer = _bind("bgfx_encoder_set_instance_data_from_dynamic_vertex_buffer",
	args=[POINTER(bgfx_encoder), bgfx_dynamic_vertex_buffer_handle, c_uint32, c_uint32],
	returns=None)

encoder_set_texture = _bind("bgfx_encoder_set_texture",
	args=[POINTER(bgfx_encoder), c_uint8, bgfx_uniform_handle, bgfx_texture_handle, c_uint32],
	returns=None)

encoder_touch = _bind("bgfx_encoder_touch",
	args=[POINTER(bgfx_encoder), bgfx_view_id],
	returns=None)

encoder_submit = _bind("bgfx_encoder_submit",
	args=[POINTER(bgfx_encoder), bgfx_view_id, bgfx_program_handle, c_uint32, c_bool],
	returns=None)

encoder_submit_occlusion_query = _bind("bgfx_encoder_submit_occlusion_query",
	args=[POINTER(bgfx_encoder), bgfx_view_id, bgfx_program_handle, bgfx_occlusion_query_handle, c_uint32, c_bool],
	returns=None)

encoder_submit_indirect = _bind("bgfx_encoder_submit_indirect",
	args=[POINTER(bgfx_encoder), bgfx_view_id, bgfx_program_handle, bgfx_indirect_buffer_handle, c_uint16, c_uint16, c_uint32, c_bool],
	returns=None)

encoder_set_image = _bind("bgfx_encoder_set_image",
	args=[POINTER(bgfx_encoder), c_uint8, bgfx_texture_handle, c_uint8, bgfx_access, bgfx_texture_format],
	returns=None)

encoder_set_compute_index_buffer = _bind("bgfx_encoder_set_compute_index_buffer",
	args=[POINTER(bgfx_encoder), c_uint8, bgfx_index_buffer_handle, bgfx_access],
	returns=None)

encoder_set_compute_vertex_buffer = _bind("bgfx_encoder_set_compute_vertex_buffer",
	args=[POINTER(bgfx_encoder), c_uint8, bgfx_vertex_buffer_handle, bgfx_access],
	returns=None)

encoder_set_compute_dynamic_index_buffer = _bind("bgfx_encoder_set_compute_dynamic_index_buffer",
	args=[POINTER(bgfx_encoder), c_uint8, bgfx_dynamic_index_buffer_handle, bgfx_access],
	returns=None)

encoder_set_compute_dynamic_vertex_buffer = _bind("bgfx_encoder_set_compute_dynamic_vertex_buffer",
	args=[POINTER(bgfx_encoder), c_uint8, bgfx_dynamic_vertex_buffer_handle, bgfx_access],
	returns=None)

encoder_set_compute_indirect_buffer = _bind("bgfx_encoder_set_compute_indirect_buffer",
	args=[POINTER(bgfx_encoder), c_uint8, bgfx_indirect_buffer_handle, bgfx_access],
	returns=None)

encoder_dispatch = _bind("bgfx_encoder_dispatch",
	args=[POINTER(bgfx_encoder), bgfx_view_id, bgfx_program_handle, c_uint32, c_uint32, c_uint32],
	returns=None)

encoder_dispatch_indirect = _bind("bgfx_encoder_dispatch_indirect",
	args=[POINTER(bgfx_encoder), bgfx_view_id, bgfx_program_handle, bgfx_indirect_buffer_handle, c_uint16, c_uint16],
	returns=None)

encoder_discard = _bind("bgfx_encoder_discard",
	args=[POINTER(bgfx_encoder)],
	returns=None)

encoder_blit = _bind("bgfx_encoder_blit",
	args=[POINTER(bgfx_encoder), bgfx_view_id, bgfx_texture_handle, c_uint8, c_uint16, c_uint16, c_uint16, bgfx_texture_handle, c_uint8, c_uint16, c_uint16, c_uint16, c_uint16, c_uint16, c_uint16],
	returns=None)

request_screen_shot = _bind("bgfx_request_screen_shot",
	args=[bgfx_frame_buffer_handle, POINTER(c_char)],
	returns=None)

class BGFX_PLATFORM_DATA(Structure):
    _fields_ = [("ndt", c_void_p),
                ("nwh", c_void_p),
                ("context", c_void_p),
                ("backBuffer", c_void_p),
                ("backBufferDS", c_void_p)]
_bgfx.bgfx_set_platform_data.argtypes = [POINTER(BGFX_PLATFORM_DATA)]

def set_platform_data(handle):
    platform_data = BGFX_PLATFORM_DATA(None, handle, None, None, None)
    _bgfx.bgfx_set_platform_data(byref(platform_data))


text:
<#
Copyright 2015 Brandon Olin

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
#>

function Get-NSFeature {
    <#
    .SYNOPSIS
        Gets the feature status for the NetScaler appliance.

    .DESCRIPTION
        Gets the feature status for the NetScaler appliance.

    .EXAMPLE
        Get-NSFeature

        Get status for all the NetScaler features.

    .EXAMPLE
        Get-NSFeature -Name 'sslvpn'

        Get the status of NetScaler feature 'sslvpn'.

    .EXAMPLE
        'sslvpn', 'lb' | Get-NSFeature

        Get the status of NetScaler feature 'sslvpn' and 'lb'.

    .PARAMETER Session
        The NetScaler session object.

    .PARAMETER Name
        The name or names of NetScaler features to get.
    #>
    [cmdletbinding()]
    param(
        $Session = $script:session,

        [parameter(ValueFromPipeline = $true, Position = 0, ValueFromPipelineByPropertyName)]
        [string[]]$Name = @()
    )

    begin {
        _AssertSessionActive
        $features = @()
    }

    process {
        if ($Name.Count -gt 0) {
            $all = _InvokeNSRestApi -Session $Session -Method Get -Type nsfeature -Action Get
            foreach ($item in $Name) {
                $features += $all.nsfeature.$item
            }
            return $features
        } else {
            $features = _InvokeNSRestApi -Session $Session -Method Get -Type nsfeature -Action Get
            return $features.nsfeature
        }
    }
}

redpajama_set_name: RedPajamaGithub


text:
/*
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
 * in compliance with the License. You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License
 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 * or implied. See the License for the specific language governing permissions and limitations under
 * the License.
 */
/*
 * This code was generated by https://github.com/googleapis/google-api-java-client-services/
 * Modify at your own risk.
 */

package com.google.api.services.dfareporting.model;

/**
 * Represents the list of user profiles.
 *
 *
 This is the Java data model class that specifies how to parse/serialize into the JSON that is
 * transmitted over HTTP when working with the Campaign Manager 360 API. For a detailed explanation
 * see:
 * https://developers.google.com/api-client-library/java/google-http-java-client/json
 *

 *
 * @author Google, Inc.
 */
@SuppressWarnings("javadoc")
public final class UserProfileList extends com.google.api.client.json.GenericJson {

  /**
   * Etag of this resource.
   * The value may be {@code null}.
   */
  @com.google.api.client.util.Key
  private java.lang.String etag;

  /**
   * The user profiles returned in this response.
   * The value may be {@code null}.
   */
  @com.google.api.client.util.Key
  private java.util.List<UserProfile> items;

  static {
    // hack to force ProGuard to consider UserProfile used, since otherwise it would be stripped out
    // see https://github.com/google/google-api-java-client/issues/543
    com.google.api.client.util.Data.nullOf(UserProfile.class);
  }

  /**
   * Identifies what kind of resource this is. Value: the fixed string
   * "dfareporting#userProfileList".
   * The value may be {@code null}.
   */
  @com.google.api.client.util.Key
  private java.lang.String kind;

  /**
   * Etag of this resource.
   * @return value or {@code null} for none
   */
  public java.lang.String getEtag() {
    return etag;
  }

  /**
   * Etag of this resource.
   * @param etag etag or {@code null} for none
   */
  public UserProfileList setEtag(java.lang.String etag) {
    this.etag = etag;
    return this;
  }

  /**
   * The user profiles returned in this response.
   * @return value or {@code null} for none
   */
  public java.util.List<UserProfile> getItems() {
    return items;
  }

  /**
   * The user profiles returned in this response.
   * @param items items or {@code null} for none
   */
  public UserProfileList setItems(java.util.List<UserProfile> items) {
    this.items = items;
    return this;
  }

  /**
   * Identifies what kind of resource this is. Value: the fixed string
   * "dfareporting#userProfileList".
   * @return value or {@code null} for none
   */
  public java.lang.String getKind() {
    return kind;
  }

  /**
   * Identifies what kind of resource this is. Value: the fixed string
   * "dfareporting#userProfileList".
   * @param kind kind or {@code null} for none
   */
  public UserProfileList setKind(java.lang.String kind) {
    this.kind = kind;
    return this;
  }

  @Override
  public UserProfileList set(String fieldName, Object value) {
    return (UserProfileList) super.set(fieldName, value);
  }

  @Override
  public UserProfileList clone() {
    return (UserProfileList) super.clone();
  }

}


text:
package com.cyanogenmod.filemanager.commands;

/**
 * An interface that represents an executable for change the owner of
 * a file system object.
 */
public interface ChangeOwnerExecutable extends WritableExecutable {

    /**
     * {@inheritDoc}
     */
    @Override
    Boolean getResult();
}


text:
import {createStore, combineReducers} from 'redux';
import chess from './chess';

const reducers = combineReducers({
  chess
});

export default createStore(reducers);


text:
UartManager::UartManager() :
    current_port("None")
{
    com_port_.setBaudRate(BAUD115200);
    com_port_.setDataBits(DATA_8);
    com_port_.setFlowControl(FLOW_OFF);
    com_port_.setParity(PAR_NONE);
}

UartManager::~UartManager()
{
    if (com_port_.isOpen()) {
        com_port_.close();
    }
}

void UartManager::set_port(QString port_name)
{
    if (com_port_.isOpen()) {
        if (current_port == port_name) {
            return;
        }
        com_port_.close();
    }
    com_port_.setPortName(port_name);
    com_port_.open(QIODevice::ReadWrite);
    current_port = port_name;
}


text:
"""Defines various classes and definitions that provide assistance for
unit testing Actors in an ActorSystem."""

import unittest
import pytest
import logging
import time
from thespian.actors import ActorSystem

def simpleActorTestLogging():
    """This function returns a logging dictionary that can be passed as
       the logDefs argument for ActorSystem() initialization to get
       simple stdout logging configuration.  This is not necessary for
       typical unit testing that uses the simpleActorSystemBase, but
       it can be useful for multiproc.. ActorSystems where the
       separate processes created should have a very simple logging
       configuration.
    """
    import sys
    if sys.platform == 'win32':
        # Windows will not allow sys.stdout to be passed to a child
        # process, which breaks the startup/config for some of the
        # tests.
        handler = { 'class': 'logging.handlers.RotatingFileHandler',
                    'filename': 'nosetests.log',
                    'maxBytes': 256*1024,
                    'backupCount':3,
        }
    else:
        handler = { 'class': 'logging.StreamHandler',
                    'stream': sys.stdout,
        }
    return {
        'version' : 1,
        'handlers': { #'discarder': {'class': 'logging.NullHandler' },
            'testStream' : handler,
        },
        'root': { 'handlers': ['testStream'] },
        'disable_existing_loggers': False,
    }

class LocallyManagedActorSystem(object):

    def setSystemBase(self, newBase='simpleSystemBase', systemCapabilities=None, logDefs='BestForBase'):
        newBaseStr = str(newBase)
        if not hasattr(self, 'currentBase') or self.currentBase != newBaseStr:
            ldefs = logDefs if logDefs != 'BestForBase' else (simpleActorTestLogging() if newBase.startswith('multiproc') else False)
            # In case the ActorSystem was *already* setup, break the singleton aspect and re-init
            ActorSystem(logDefs = ldefs).shutdown()
            ActorSystem(newBase, systemCapabilities, logDefs = ldefs)
            self.currentBase = newBaseStr

class ActorSystemTestCase(unittest.TestCase, LocallyManagedActorSystem):

    """The ActorSystemTestCase is a wrapper for the unittest TestCase
       class that will startup a default ActorSystem in the provided
       setUp() and tearDown() any active ActorSystem after testing.

       If a non-default ActorSystem is to be used, the setSystemBase()
       method should be called with that system base.

       It also provides some additional methods for assistance in testing Actors.

    """
    def setUp(self):
        if not hasattr(self, 'currentBase'):
            self.setSystemBase()

    def tearDown(self):
        if hasattr(self, 'currentBase'):
            ActorSystem().shutdown()
            delattr(self, 'currentBase')
            import time
            time.sleep(0.02)

    @staticmethod
    def actualActorObject(actorClass):
        """Normally an Actor is only instantiated in the context of an
           ActorSystem, and then only responds to messages delivered
           via that system.  For testing purposes *only*, it may be
           desireable to have the actual Actor instance to test
           methods on that Actor directly.  This method will return
           that actual Actor instance after instantiating the actor in
           an ActorSystem.

           This method can ONLY be used with an ActorSystem that will
           instantiate the Actor in the context of the current process
           (e.g. simpleSystemBase) and the methods tested on the
           resulting Actor CANNOT perform any Actor-related actions
           (e.g. self.createActor(), self.send()).

           This method is for TESTING only under very special
           circumstances; if you're not sure you need this, then you
           probably don't.
        """
        # Create the Actor within the system.
        aAddr = ActorSystem().createActor(actorClass)
        # This depends on the internals of the systemBase
        return ActorSystem()._systemBase.actorRegistry[aAddr.actorAddressString].instance

###
### pytest fixtures and helpers
###

testAdminPort = None

def get_free_admin_port_random():
    global testAdminPort
    if testAdminPort is None:
        import random
        # Reserved system ports are typically below 1024. Ephemeral
        # ports typically start at either 32768 (Linux) or 49152
        # (IANA), or range from 1024-5000 (older Windows).  Pick
        # something unused outside those ranges for the admin.
        testAdminPort = random.randint(10000, 30000)
        #testAdminPort = random.randint(5,60) * 1000
    else:
        testAdminPort = testAdminPort + 1
    return testAdminPort

def get_free_admin_port():
    import socket
    import random
    for tries in range(100):
        port = random.randint(5000, 30000)
        try:
            for m,p in [ (socket.SOCK_STREAM, socket.IPPROTO_TCP),
                         (socket.SOCK_DGRAM, socket.IPPROTO_UDP),
            ]:
                s = socket.socket(socket.AF_INET, m, p)
                s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
                s.bind(('',port))
                s.close()
            return port
        except Exception:
            pass
    return get_free_admin_port_random()

@pytest.fixture(params=['simpleSystemBase',
                        'multiprocQueueBase',
                        'multiprocUDPBase',
                        'multiprocTCPBase',
                        'multiprocTCPBase-AdminRouting',
                        'multiprocTCPBase-AdminRoutingTXOnly',
])
def asys(request):
    caps = {'Foo Allowed': True,
            'Cows Allowed': True,
            'Dogs Allowed': True,
            'dog': 'food'}
    if request.param.startswith('multiprocTCP') or \
       request.param.startswith('multiprocUDP'):
        caps['Admin Port'] = get_free_admin_port()
        caps['Convention Address.IPv4'] = '', caps['Admin Port']
    if request.param.endswith('-AdminRouting'):
        caps['Admin Routing'] = True
    if request.param.endswith('-AdminRoutingTXOnly'):
        caps['Admin Routing'] = True
        caps['Outbound Only'] = True
    asys = ActorSystem(systemBase=request.param.partition('-')[0],
                       capabilities=caps,
                       logDefs=(simpleActorTestLogging()
                                if request.param.startswith('multiproc')
                                else False),
                       transientUnique=True)
    asys.base_name = request.param
    asys.port_num  = caps.get('Admin Port', None)
    asys.txonly = request.param.endswith('-AdminRoutingTXOnly')
    request.addfinalizer(lambda asys=asys: asys.shutdown())
    return asys

def similar_asys(asys, in_convention=True, start_wait=True, capabilities=None):
    caps = capabilities or {}
    if asys.base_name.startswith('multiprocTCP') or \
       asys.base_name.startswith('multiprocUDP'):
        caps['Admin Port'] = get_free_admin_port()
        if in_convention:
            caps['Convention Address.IPv4'] = '', asys.port_num
    if asys.base_name.endswith('-AdminRouting'):
        caps['Admin Routing'] = True
    asys2 = ActorSystem(systemBase=asys.base_name.partition('-')[0],
                        capabilities=caps,
                        logDefs=(simpleActorTestLogging()
                                if asys.base_name.startswith('multiproc')
                                else False),
                       transientUnique=True)
    asys2.base_name = asys.base_name
    asys2.port_num  = caps.get('Admin Port', None)
    if in_convention and start_wait:
        time.sleep(0.25)  # Wait for Actor Systems to start and connect together
    return asys2

@pytest.fixture
def asys2(request, asys):
    asys2 = similar_asys(asys, in_convention=False)
    # n.b. shutdown the second actor system first:
    #   1. Some tests ask asys1 to create an actor
    #   2. That actor is actually supported by asys2
    #   3. There is an external port the tester uses for each asys
    #   4. When asys2 is shutdown, it will attempt to notify the
    #      parent of the actor that the actor is dead
    #   5. This parent is the external port for asys1.
    #   6. If asys1 is shutdown first, then asys2 must time out
    #      on the transmit attempt (usually 5 minutes) before
    #      it can exit.
    #   7. If the test is re-run within this 5 minute period, it will fail
    #      because the old asys2 is still existing but in shutdown state
    #      (and will therefore rightfully refuse new actions).
    # By shutting down asys2 first, the parent notification can be
    # performed and subsequent runs don't encounter the lingering
    # asys2.
    request.addfinalizer(lambda asys=asys2: asys2.shutdown())
    return asys2

@pytest.fixture
def asys_pair(request, asys):
    asys2 = similar_asys(asys, in_convention=True)
    # n.b. shutdown the second actor system first:
    #   1. Some tests ask asys1 to create an actor
    #   2. That actor is actually supported by asys2
    #   3. There is an external port the tester uses for each asys
    #   4. When asys2 is shutdown, it will attempt to notify the
    #      parent of the actor that the actor is dead
    #   5. This parent is the external port for asys1.
    #   6. If asys1 is shutdown first, then asys2 must time out
    #      on the transmit attempt (usually 5 minutes) before
    #      it can exit.
    #   7. If the test is re-run within this 5 minute period, it will fail
    #      because the old asys2 is still existing but in shutdown state
    #      (and will therefore rightfully refuse new actions).
    # By shutting down asys2 first, the parent notification can be
    # performed and subsequent runs don't encounter the lingering
    # asys2.
    request.addfinalizer(lambda asys=asys2: asys2.shutdown())
    return (asys, asys2)

@pytest.fixture
def run_unstable_tests(request):
    return request.config.getoption('unstable', default=False)

def unstable_test(run_unstable_tests, asys, *unstable_bases):
    if asys.base_name in unstable_bases and not run_unstable_tests:
        pytest.skip("Test unstable for %s system base"%asys.base_name)

def actor_system_unsupported(asys, *unsupported_bases):
    if asys.base_name in unsupported_bases:
        pytest.skip("Functionality not supported for %s system base"%asys.base_name)

from thespian.system.timing import timePeriodSeconds
import time

inTestDelay = lambda period: time.sleep(timePeriodSeconds(period))

def delay_for_next_of_kin_notification(system):
    if system.base_name == 'multiprocQueueBase':
        # The multiprocQueueBase signal processor cannot interrupt a
        # sleeping Queue.get(), so for this base it is necessary to
        # wait for the timeout on the Queue.get() to allow it time to
        # notice and process the child exit.
        time.sleep(2.5)
    elif system.base_name == 'multiprocUDPBase':
        time.sleep(0.6)
    else:
        time.sleep(0.1)


text:
package org.jivesoftware.openfire.muc.spi;

import java.util.Map;

/**
 * Interface to listen for property events. Use the
 * {@link org.jivesoftware.openfire.muc.spi.MUCServicePropertyEventDispatcher#addListener(MUCServicePropertyEventListener)}
 * method to register for events.
 *
 * @author Daniel Henninger
 */
public interface MUCServicePropertyEventListener {

    /**
     * A property was set. The parameter map {@code params} will contain the
     * the value of the property under the key {@code value}.
     *
     * @param service the subdomain of the service the property was set on.
     * @param property the name of the property.
     * @param params event parameters.
     */
    void propertySet( String service, String property, Map<String, Object> params );

    /**
     * A property was deleted.
     *
     * @param service the subdomain of the service the property was deleted from.
     * @param property the name of the property deleted.
     * @param params event parameters.
     */
    void propertyDeleted( String service, String property, Map<String, Object> params );

}


text:
using System;
using System.Collections.Generic;

namespace Naxis.Core.Data
{
    /// <summary>
    /// 实体基类
    /// </summary>
    /// <typeparam name="TEntityIdType"></typeparam>
    public abstract class BaseEntity<TEntityIdType> : IPersistable<TEntityIdType, int>, IEquatable<BaseEntity<TEntityIdType>> where TEntityIdType : IComparable
    {
        /// <summary>
        /// 实体Id
        /// </summary>
        public virtual TEntityIdType Id { get; set; }

        /// <summary>
        /// 版本号
        /// </summary>
        public virtual int Version { get; set; }

        /// <summary>
        /// ==
        /// </summary>
        /// <param name="persistableObject1"></param>
        /// <param name="persistableObject2"></param>
        /// <returns></returns>
        public static bool operator ==(BaseEntity<TEntityIdType> persistableObject1, BaseEntity<TEntityIdType> persistableObject2)
        {
            object obj1 = persistableObject1;
            object obj2 = persistableObject2;

            if (obj1 == null && obj2 == null)
            {
                return true;
            }
            if (obj1 == null || obj2 == null || persistableObject1.Id == null || persistableObject2.Id == null)
            {
                return false;
            }

            if (persistableObject1.Id.Equals(default(TEntityIdType)) == false && persistableObject2.Id.Equals(default(TEntityIdType)) == false)
            {
                return persistableObject1.Id.CompareTo(persistableObject2.Id) == 0;
            }

            if (persistableObject1.GetType() != persistableObject2.GetType())
            {
                return false;
            }

            return obj1 == obj2;
        }

        /// <summary>
        /// !=
        /// </summary>
        /// <param name="persistableObject1"></param>
        /// <param name="persistableObject2"></param>
        /// <returns></returns>
        public static bool operator !=(BaseEntity<TEntityIdType> persistableObject1, BaseEntity<TEntityIdType> persistableObject2)
        {
            return !(persistableObject1 == persistableObject2);
        }

        /// <summary>
        /// 重写Equals
        /// </summary>
        /// <param name="persistableObject"></param>
        /// <returns></returns>
        public override bool Equals(object persistableObject)
        {
            return persistableObject is BaseEntity<TEntityIdType> ? this == (BaseEntity<TEntityIdType>)persistableObject : false;
        }

        /// <summary>
        /// 重写GetHashCode
        /// </summary>
        /// <returns></returns>
        public override int GetHashCode()
        {
            return EqualityComparer<TEntityIdType>.Default.Equals(Id, default(TEntityIdType)) == false ? Id.GetHashCode() : 0;
        }

        /// <summary>
        /// 判断两个实体是否相同
        /// </summary>
        /// <param name="other"></param>
        /// <returns></returns>
        public virtual bool Equals(BaseEntity<TEntityIdType> other)
        {
            return other == null ? false : this == other;
        }

        /// <summary>
        /// 重写ToString
        /// </summary>
        /// <returns></returns>
        public override string ToString()
        {
            return Id.Equals(default(TEntityIdType)) == false ? Id.ToString() : base.ToString();
        }
    }
}


text:
import { ElevatorController } from './controllers/elevatorController';
import { PassengerController } from './controllers/passengerController';
import { Output } from './services/output';
import { speed } from './constants';

let start = function () {
	const NUM_FLOORS: number = 10;
	const TOTAL_ELEVATORS: number = 4;
	const NUM_PASSENGERS_PER: number = 10;
	const TOTAL_PASSENGERS: number = 10;
	const HTML_ELEMENTS: string[] = [
		"content","avgWaitTime", "avgTripTime", "longestWait", "longestTrip","totalTrips","totalWaitTime", "totalTripTime"
	];

	let self = this;
	let output = new Output(HTML_ELEMENTS);
	let elevatorController: ElevatorController = new ElevatorController(NUM_PASSENGERS_PER, TOTAL_ELEVATORS, NUM_FLOORS, output);
	let passengerController: PassengerController = new PassengerController(TOTAL_PASSENGERS, NUM_FLOORS, output);

	let run = function(): void {
		elevatorController.tick();
		passengerController.tick(elevatorController);
	}

	setInterval(function(){ run() }, speed);
};
start();


text:
## Mongolass

[![NPM version][npm-image]][npm-url]
[![Build status][travis-image]][travis-url]
[![Dependency Status][david-image]][david-url]
[![License][license-image]][license-url]
[![Downloads][downloads-image]][downloads-url]

Elegant MongoDB driver for Node.js.

## Installation

```bash
$ npm i mongolass --save
```

## Usage

Just like mongoose:

```javascript
'use strict';

const Mongolass = require('mongolass');
const mongolass = new Mongolass();
mongolass.connect('mongodb://localhost:27017/test');// const mongolass = new Mongolass('mongodb://localhost:27017/test');

const User = mongolass.model('User');

User
  .find()
  .select({ name: 1, age: 1 })
  .sort({ name: -1 })
  .exec()
  .then(console.log)
  .catch(console.error);
```

or use optional schema:

```javascript
'use strict';

const Mongolass = require('mongolass');
const Schema = Mongolass.Schema;
const mongolass = new Mongolass('mongodb://localhost:27017/test');

const UserSchema = new Schema('UserSchema', {
  name: { type: 'string' },
  age: { type: 'number' }
});
const User = mongolass.model('User', UserSchema);

/*
equal to:
const User = mongolass.model('User', {
  name: { type: 'string' },
  age: { type: 'number' }
});
will create inner schema named `UserSchema`.
*/

User
  .insertOne({ name: 'nswbmw', age: 'wrong age' })
  .exec()
  .then(console.log)
  .catch(console.error);
/*
{ TypeError: ($.age: "wrong age") ✖ (type: number)
    at Model.insertOne (/Users/nswbmw/Desktop/test/node_modules/mongolass/lib/query.js:105:16)
    at Object.<anonymous> (/Users/nswbmw/Desktop/test/app.js:23:4)
    at Module._compile (module.js:573:30)
    at Object.Module._extensions..js (module.js:584:10)
    at Module.load (module.js:507:32)
    at tryModuleLoad (module.js:470:12)
    at Function.Module._load (module.js:462:3)
    at Function.Module.runMain (module.js:609:10)
    at startup (bootstrap_node.js:158:16)
    at bootstrap_node.js:598:3
  validator: 'type',
  actual: 'wrong age',
  expected: { type: 'number' },
  path: '$.age',
  schema: 'UserSchema',
  model: 'User',
  op: 'insertOne',
  args: [ { name: 'nswbmw', age: 'wrong age' } ],
  pluginName: 'MongolassSchema',
  pluginOp: 'beforeInsertOne',
  pluginArgs: [] }
*/
```

ObjectId schema:

```javascript
'use strict';

const Mongolass = require('mongolass');
const Schema = Mongolass.Schema;
const mongolass = new Mongolass('mongodb://localhost:27017/test');

const Post = mongolass.model('Post', {
  author: { type: Mongolass.Types.ObjectId }
}, { collName: 'post' });

Post.insertOne({ author: '111111111111111111111111' })
  .then(function () {
    return Post.find({ author: '111111111111111111111111' });
  })
  .then(console.log);
/*
[ { _id: 57caed24ecda6ffb15962591,
    author: 111111111111111111111111 } ]
 */
```
**NB:** You can pass `collName` as collection name.

## API

Same as [node-mongodb-native](http://mongodb.github.io/node-mongodb-native/2.2/api/Collection.html).

## Mongolass vs Mongoose

知乎:[从零开始写一个 Node.js 的 MongoDB 驱动库](https://zhuanlan.zhihu.com/p/24308524)

----------------------------

I've been using Mongoose for years, it's great but complex sucks, so i wrote Mongolass. Mongolass is not simply mimicking Mongoose, but rather draw on the advantages of mongoose redesigned the architecture. Mongolass has some exciting features different from Mongoose:

1. Pure Schema. In Mongoose, Schema and Model and Entity are confused.

  > Schemas not only define the structure of your document and casting of properties, they also define document instance methods, static Model methods, compound indexes and document lifecycle hooks called middleware.

  In Mongolass, Schema is only used for defining the structure of your document and casting of properties, Model used for retrievaling data from mongodb and register plugins, Entity(as result) is plain object. Schema is also optional.

2. Awesome plugin system. Mongoose plugin system is not strong enough, eg: `.pre`, `.post`, use async `next()`. In Mongolass, we can register a plugin for Model or global mongolass instance. like:

  ```javascript
  User.plugin('xx', {
    beforeFind: function (...args) {},// or function return Promise
    afterFind: function* (result, ...args) {
      console.log(result, args);
      ...
    },
    // afterFind: async function (result, ...args) {
    //   console.log(result, args);
    //   ...
    // }
  });
  ```

  Above added two hook functions for `User`, when `User.find().xx().exec()` is called, the execution order is as follows:

  ```
  beforeFind(handle query args) -> retrieve data from mongodb -> afterFind(handle query result)
  ```

  Mongolass's plugins could be substituted for Mongoose's (document instance methods + static Model methods + plugins).

3. Detailed error informations. see [usage](https://github.com/mongolass/mongolass#usage).

  ```javascript
  User
    .insertOne({ name: 'nswbmw', age: 'wrong age' })
    .exec()
    .then(console.log)
    .catch(console.error);
  /*
  { TypeError: ($.age: "wrong age") ✖ (type: number)
      at Model.insertOne (/Users/nswbmw/Desktop/test/node_modules/mongolass/lib/query.js:105:16)
      at Object.<anonymous> (/Users/nswbmw/Desktop/test/app.js:23:4)
      at Module._compile (module.js:573:30)
      at Object.Module._extensions..js (module.js:584:10)
      at Module.load (module.js:507:32)
      at tryModuleLoad (module.js:470:12)
      at Function.Module._load (module.js:462:3)
      at Function.Module.runMain (module.js:609:10)
      at startup (bootstrap_node.js:158:16)
      at bootstrap_node.js:598:3
    validator: 'type',
    actual: 'wrong age',
    expected: { type: 'number' },
    path: '$.age',
    schema: 'UserSchema',
    model: 'User',
    op: 'insertOne',
    args: [ { name: 'nswbmw', age: 'wrong age' } ],
    pluginName: 'MongolassSchema',
    pluginOp: 'beforeInsertOne',
    pluginArgs: [] }
  */
  ```

  According to the error instance, esay to know `age` expect a number but got a string, from error stack know it's broken on `app.js:23:4` and the operator is `Model.insertOne`.

## Schema

see [another-json-schema](https://github.com/nswbmw/another-json-schema).

## Plugins

```
mongolass.plugin(pluginName, hooks);// register global plugin
User.plugin(pluginName, hooks);// register model plugin
```

example:

```javascript
const co = require('co');
const moment = require('moment');
const Mongolass = require('mongolass');
const mongolass = new Mongolass('mongodb://localhost:27017/test');
const User = mongolass.model('User');

mongolass.plugin('addCreatedAt', {
  beforeInsert: function (format) {
    console.log('beforeInsert', this._op, this._args, format);
    // beforeInsert insert [ { firstname: 'san', lastname: 'zhang' } ] YYYY-MM-DD
    this._args[0].createdAt = moment().format(format);
  }
});

User.plugin('addFullname', {
  afterFindOne: function* (user, opt) {
    console.log('afterFindOne', this._op, this._args, opt);
    // afterFindOne findOne [] { sep: '-' }
    if (!user) return user;
    user.fullname = user.firstname + opt.sep + user.lastname;
    return user;
  },
  afterFind: async function (users, opt) {
    console.log('afterFind', this._op, this._args, opt);
    // afterFind find [ { firstname: 'san' } ] { sep: ' ' }
    if (!users.length) return users;
    return users.map(user => {
      user.fullname = user.firstname + opt.sep + user.lastname;
      return user;
    });
  }
});

co(function* () {
  // when use yield, .exec() is omissible.
  yield User.insert({ firstname: 'san', lastname: 'zhang' }).addCreatedAt('YYYY-MM-DD');
  console.log(yield User.findOne().addFullname({ sep: '-' }));
  // { _id: 5850186544c3b82d23a82e45,
  //   firstname: 'san',
  //   lastname: 'zhang',
  //   createdAt: '2016-12-13',
  //   fullname: 'san-zhang' }
  console.log(yield User.find({ firstname: 'san' }).addFullname({ sep: ' ' }));
  // [ { _id: 5850186544c3b82d23a82e45,
  //     firstname: 'san',
  //     lastname: 'zhang',
  //     createdAt: '2016-12-13',
  //     fullname: 'san zhang' } ]
}).catch(console.error.bind(console));
```

**NOTE**: Different order of calling plugins will output different results. The priority of Model's plugins is greater than global's.

example:

```javascript
const co = require('co');
const Mongolass = require('mongolass');
const mongolass = new Mongolass('mongodb://localhost:27017/test');
const User = mongolass.model('User');

User.plugin('add2', {
  afterFindOne: function* (user) {
    if (!user) return user;
    user.name = `${user.name}2`;
    return user;
  }
});
User.plugin('add3', {
  afterFindOne: function* (user) {
    if (!user) return user;
    user.name = `${user.name}3`;
    return user;
  }
});

co(function* () {
  yield User.insert({ name: '1' });
  console.log(yield User.findOne().add2().add3());
  // { _id: 58501a8a7cc264af259ca691, name: '123' }
  console.log(yield User.findOne().add3().add2());
  // { _id: 58501a8a7cc264af259ca691, name: '132' }
}).catch(console.error.bind(console));
```

see [mongolass-plugin-populate](https://github.com/mongolass/mongolass-plugin-populate).

### Built-in plugins

Mongolass has some built-in plugins, only for `find` and `findOne`.

- [limit](http://mongodb.github.io/node-mongodb-native/2.1/api/Collection.html#findOne)
- [sort](http://mongodb.github.io/node-mongodb-native/2.1/api/Collection.html#findOne)
- [fields(alias: select)](http://mongodb.github.io/node-mongodb-native/2.1/api/Collection.html#findOne)
- [skip](http://mongodb.github.io/node-mongodb-native/2.1/api/Collection.html#findOne)
- [hint](http://mongodb.github.io/node-mongodb-native/2.1/api/Collection.html#findOne)
- [populate]()
- [explain](http://mongodb.github.io/node-mongodb-native/2.1/api/Collection.html#findOne)
- [snapshot](http://mongodb.github.io/node-mongodb-native/2.1/api/Collection.html#findOne)
- [timeout](http://mongodb.github.io/node-mongodb-native/2.1/api/Collection.html#findOne)
- [tailable](http://mongodb.github.io/node-mongodb-native/2.1/api/Collection.html#findOne)
- [tailableRetryInterval](http://mongodb.github.io/node-mongodb-native/2.1/api/Collection.html#findOne)
- [numberOfRetries](http://mongodb.github.io/node-mongodb-native/2.1/api/Collection.html#findOne)
- [awaitdata](http://mongodb.github.io/node-mongodb-native/2.1/api/Collection.html#findOne)
- [oplogReplay](http://mongodb.github.io/node-mongodb-native/2.1/api/Collection.html#findOne)
- [exhaust](http://mongodb.github.io/node-mongodb-native/2.1/api/Collection.html#findOne)
- [batchSize](http://mongodb.github.io/node-mongodb-native/2.1/api/Collection.html#findOne)
- [returnKey](http://mongodb.github.io/node-mongodb-native/2.1/api/Collection.html#findOne)
- [maxScan](http://mongodb.github.io/node-mongodb-native/2.1/api/Collection.html#findOne)
- [min](http://mongodb.github.io/node-mongodb-native/2.1/api/Collection.html#findOne)
- [max](http://mongodb.github.io/node-mongodb-native/2.1/api/Collection.html#findOne)
- [showDiskLoc](http://mongodb.github.io/node-mongodb-native/2.1/api/Collection.html#findOne)
- [comment](http://mongodb.github.io/node-mongodb-native/2.1/api/Collection.html#findOne)
- [raw](http://mongodb.github.io/node-mongodb-native/2.1/api/Collection.html#findOne)
- [readPreference](http://mongodb.github.io/node-mongodb-native/2.1/api/Collection.html#findOne)
- [partial](http://mongodb.github.io/node-mongodb-native/2.1/api/Collection.html#findOne)
- [maxTimeMS](http://mongodb.github.io/node-mongodb-native/2.1/api/Collection.html#findOne)

example:

```javascript
const co = require('co');
const Mongolass = require('mongolass');
const mongolass = new Mongolass('mongodb://localhost:27017/test');
const User = mongolass.model('User');

co(function* () {
  yield User.insert({ name: '1' });
  yield User.insert({ name: '2' });
  const result = yield User
    .find()
    .skip(1)
    .limit(1);
  console.log(result);
  // [ { _id: 58501c1281ea915a2760a2ee, name: '2' } ]
}).catch(console.error.bind(console));
```

## Test

```bash
$ npm test (coverage 100%)
```

## License

MIT

[npm-image]: https://img.shields.io/npm/v/mongolass.svg?style=flat-square
[npm-url]: https://npmjs.org/package/mongolass
[travis-image]: https://img.shields.io/travis/mongolass/mongolass.svg?style=flat-square
[travis-url]: https://travis-ci.org/mongolass/mongolass
[david-image]: http://img.shields.io/david/mongolass/mongolass.svg?style=flat-square
[david-url]: https://david-dm.org/mongolass/mongolass
[license-image]: http://img.shields.io/npm/l/mongolass.svg?style=flat-square
[license-url]: LICENSE
[downloads-image]: http://img.shields.io/npm/dm/mongolass.svg?style=flat-square
[downloads-url]: https://npmjs.org/package/mongolass