# Path: src/zugbruecke/core/const.py
# GROUP_VOID = 1
# 
# Path: src/zugbruecke/core/data/memory.py
# def generate_pointer_from_bytes(in_bytes):
# 
# 	return ctypes.cast(ctypes.pointer((ctypes.c_ubyte * len(in_bytes)).from_buffer_copy(in_bytes)), ctypes.c_void_p)
# 
# def is_null_pointer(ctypes_pointer):
# 
# 	try:
# 		return ctypes.cast(ctypes_pointer, ctypes.c_void_p).value is None
# 	except ctypes.ArgumentError: # catch non-pointer arguments
# 		return False
# 
# def overwrite_pointer_with_bytes(ctypes_pointer, in_bytes):
# 
# 	ctypes.memmove(ctypes_pointer, ctypes.pointer((ctypes.c_ubyte * len(in_bytes)).from_buffer_copy(in_bytes)), len(in_bytes))
# 
# def serialize_pointer_into_bytes(ctypes_pointer, size_bytes):
# 
# 	return bytes(ctypes.cast(ctypes_pointer, ctypes.POINTER(ctypes.c_ubyte * size_bytes)).contents)


import ctypes
from pprint import pformat as pf
from ..const import GROUP_VOID
from .memory import (
	generate_pointer_from_bytes,
	is_null_pointer,
	overwrite_pointer_with_bytes,
	serialize_pointer_into_bytes
	)
		if '_f' not in memsync_d.keys():

			# Search for length
			length = self.__get_argument_by_memsync_path__(memsync_d['l'], args_tuple, return_value)

			# Length might come from ctypes or a Python datatype
			return getattr(length, 'value', length)

		# Make sure length can be computed from a tuple of arguments
		assert isinstance(memsync_d['l'], tuple)

		# Compute length from arguments and return
		return memsync_d['_f'](*(
			self.__get_argument_by_memsync_path__(item, args_tuple, return_value) for item in memsync_d['l']
			))


	def __pack_memory_item__(self, memsync_d, args_tuple, return_value = None):

		# Search for pointer
		pointer = self.__get_argument_by_memsync_path__(memsync_d['p'], args_tuple, return_value)

		# Convert argument into ctypes datatype TODO more checks needed!
		if '_c' in memsync_d.keys():
			pointer = ctypes.pointer(memsync_d['_c'].from_param(pointer))

		# Unicode char size if relevant
		w = WCHAR_BYTES if memsync_d['w'] else None

		# Check for NULL pointer

# Path: src/zugbruecke/core/const.py
# GROUP_VOID = 1
# 
# Path: src/zugbruecke/core/data/memory.py
# def generate_pointer_from_bytes(in_bytes):
# 
# 	return ctypes.cast(ctypes.pointer((ctypes.c_ubyte * len(in_bytes)).from_buffer_copy(in_bytes)), ctypes.c_void_p)
# 
# def is_null_pointer(ctypes_pointer):
# 
# 	try:
# 		return ctypes.cast(ctypes_pointer, ctypes.c_void_p).value is None
# 	except ctypes.ArgumentError: # catch non-pointer arguments
# 		return False
# 
# def overwrite_pointer_with_bytes(ctypes_pointer, in_bytes):
# 
# 	ctypes.memmove(ctypes_pointer, ctypes.pointer((ctypes.c_ubyte * len(in_bytes)).from_buffer_copy(in_bytes)), len(in_bytes))
# 
# def serialize_pointer_into_bytes(ctypes_pointer, size_bytes):
# 
# 	return bytes(ctypes.cast(ctypes_pointer, ctypes.POINTER(ctypes.c_ubyte * size_bytes)).contents)


# Path: src/zugbruecke/core/data/mem_contents.py
import ctypes
from pprint import pformat as pf
from ..const import GROUP_VOID
from .memory import (
	generate_pointer_from_bytes,
	is_null_pointer,
	overwrite_pointer_with_bytes,
	serialize_pointer_into_bytes
	)

		if '_f' not in memsync_d.keys():

			# Search for length
			length = self.__get_argument_by_memsync_path__(memsync_d['l'], args_tuple, return_value)

			# Length might come from ctypes or a Python datatype
			return getattr(length, 'value', length)

		# Make sure length can be computed from a tuple of arguments
		assert isinstance(memsync_d['l'], tuple)

		# Compute length from arguments and return
		return memsync_d['_f'](*(
			self.__get_argument_by_memsync_path__(item, args_tuple, return_value) for item in memsync_d['l']
			))


	def __pack_memory_item__(self, memsync_d, args_tuple, return_value = None):

		# Search for pointer
		pointer = self.__get_argument_by_memsync_path__(memsync_d['p'], args_tuple, return_value)

		# Convert argument into ctypes datatype TODO more checks needed!
		if '_c' in memsync_d.keys():
			pointer = ctypes.pointer(memsync_d['_c'].from_param(pointer))

		# Unicode char size if relevant
		w = WCHAR_BYTES if memsync_d['w'] else None

		# Check for NULL pointer

		if pointer is None or is_null_pointer(pointer):