view dstep/foundation/NSFileHandle.d @ 14:89f3c3ef1fd2

Added the Foundation framework
author Jacob Carlborg <doob@me.com>
date Mon, 03 Aug 2009 15:23:15 +0200
parents
children 19885b43130e
line wrap: on
line source

/**
 * Copyright: Copyright (c) 2009 Jacob Carlborg.
 * Authors: Jacob Carlborg
 * Version: Initial created: Aug 3, 2009 
 * License: $(LINK2 http://www.boost.org/LICENSE_1_0.txt, Boost Software License 1.0)
 */
module dstep.foundation.NSFileHandle;

import dstep.foundation.NSArray;
import dstep.foundation.NSData;
import dstep.foundation.NSObject;
import dstep.foundation.NSRange;
import dstep.foundation.NSString;
import dstep.objc.bridge.Bridge;
import dstep.objc.objc : id;

import bindings = dstep.foundation.NSFileHandle_bindings;

const NSString NSFileHandleOperationException;
const NSString NSFileHandleReadCompletionNotification;
const NSString NSFileHandleReadToEndOfFileCompletionNotification;
const NSString NSFileHandleConnectionAcceptedNotification;
const NSString NSFileHandleDataAvailableNotification;
const NSString NSFileHandleNotificationDataItem;
const NSString NSFileHandleNotificationFileHandleItem;
const NSString NSFileHandleNotificationMonitorModes;

static this ()
{
	NSFileHandleOperationException = new NSString(bindings.NSFileHandleOperationException);
	NSFileHandleReadCompletionNotification = new NSString(bindings.NSFileHandleReadCompletionNotification);
	NSFileHandleReadToEndOfFileCompletionNotification = new NSString(bindings.NSFileHandleReadToEndOfFileCompletionNotification);
	NSFileHandleConnectionAcceptedNotification = new NSString(bindings.NSFileHandleConnectionAcceptedNotification);
	NSFileHandleDataAvailableNotification = new NSString(bindings.NSFileHandleDataAvailableNotification);
	NSFileHandleNotificationDataItem = new NSString(bindings.NSFileHandleNotificationDataItem);
	NSFileHandleNotificationFileHandleItem = new NSString(bindings.NSFileHandleNotificationFileHandleItem);
	NSFileHandleNotificationMonitorModes = new NSString(bindings.NSFileHandleNotificationMonitorModes);
}

class NSPipe : NSObject
{
	mixin ObjcWrap;

	NSFileHandle fileHandleForReading ()
	{
		return invokeObjcSelf!(NSFileHandle, "fileHandleForReading");
	}

	NSFileHandle fileHandleForWriting ()
	{
		return invokeObjcSelf!(NSFileHandle, "fileHandleForWriting");
	}

	Object init ()
	{
		return invokeObjcSelf!(Object, "init");
	}

	this ()
	{
		objcObject = Bridge.invokeObjcClassMethod!(id, "alloc")(objcClass);
		id result = Bridge.invokeObjcMethod!(id, "init")(objcObject);

		if (result)
			objcObject = ret;

		dObject = this;
	}

	static Object pipe ()
	{
		return invokeObjcSelfClass!(Object, "pipe");
	}
}

class NSFileHandle : NSObject
{
	mixin ObjcWrap;
	mixin TNSFileHandleAsynchronousAccess;
	mixin TNSFileHandleCreation;
	mixin TNSFileHandlePlatformSpecific;

	NSData availableData ()
	{
		return invokeObjcSelf!(NSData, "availableData");
	}

	NSData readDataToEndOfFile ()
	{
		return invokeObjcSelf!(NSData, "readDataToEndOfFile");
	}

	NSData readDataOfLength (NSUInteger length)
	{
		return invokeObjcSelf!(NSData, "readDataOfLength:", NSUInteger)(length);
	}

	void writeData (NSData data)
	{
		return invokeObjcSelf!(void, "writeData:", NSData)(data);
	}

	ulong offsetInFile ()
	{
		return invokeObjcSelf!(ulong, "offsetInFile");
	}

	ulong seekToEndOfFile ()
	{
		return invokeObjcSelf!(ulong, "seekToEndOfFile");
	}

	void seekToFileOffset (ulong offset)
	{
		return invokeObjcSelf!(void, "seekToFileOffset:", ulong)(offset);
	}

	void truncateFileAtOffset (ulong offset)
	{
		return invokeObjcSelf!(void, "truncateFileAtOffset:", ulong)(offset);
	}

	void synchronizeFile ()
	{
		return invokeObjcSelf!(void, "synchronizeFile");
	}

	void closeFile ()
	{
		return invokeObjcSelf!(void, "closeFile");
	}
}

template TNSFileHandleAsynchronousAccess ()
{
	void readInBackgroundAndNotifyForModes (NSArray modes)
	{
		return invokeObjcSelf!(void, "readInBackgroundAndNotifyForModes:", NSArray)(modes);
	}

	void readInBackgroundAndNotify ()
	{
		return invokeObjcSelf!(void, "readInBackgroundAndNotify");
	}

	void readToEndOfFileInBackgroundAndNotifyForModes (NSArray modes)
	{
		return invokeObjcSelf!(void, "readToEndOfFileInBackgroundAndNotifyForModes:", NSArray)(modes);
	}

	void readToEndOfFileInBackgroundAndNotify ()
	{
		return invokeObjcSelf!(void, "readToEndOfFileInBackgroundAndNotify");
	}

	void acceptConnectionInBackgroundAndNotifyForModes (NSArray modes)
	{
		return invokeObjcSelf!(void, "acceptConnectionInBackgroundAndNotifyForModes:", NSArray)(modes);
	}

	void acceptConnectionInBackgroundAndNotify ()
	{
		return invokeObjcSelf!(void, "acceptConnectionInBackgroundAndNotify");
	}

	void waitForDataInBackgroundAndNotifyForModes (NSArray modes)
	{
		return invokeObjcSelf!(void, "waitForDataInBackgroundAndNotifyForModes:", NSArray)(modes);
	}

	void waitForDataInBackgroundAndNotify ()
	{
		return invokeObjcSelf!(void, "waitForDataInBackgroundAndNotify");
	}
}

template TNSFileHandleCreation ()
{
	static Object fileHandleWithStandardInput ()
	{
		return invokeObjcSelfClass!(Object, "fileHandleWithStandardInput");
	}

	static Object fileHandleWithStandardOutput ()
	{
		return invokeObjcSelfClass!(Object, "fileHandleWithStandardOutput");
	}

	static Object fileHandleWithStandardError ()
	{
		return invokeObjcSelfClass!(Object, "fileHandleWithStandardError");
	}

	static Object fileHandleWithNullDevice ()
	{
		return invokeObjcSelfClass!(Object, "fileHandleWithNullDevice");
	}

	static Object fileHandleForReadingAtPath (NSString path)
	{
		return invokeObjcSelfClass!(Object, "fileHandleForReadingAtPath:", NSString)(path);
	}

	static Object fileHandleForWritingAtPath (NSString path)
	{
		return invokeObjcSelfClass!(Object, "fileHandleForWritingAtPath:", NSString)(path);
	}

	static Object fileHandleForUpdatingAtPath (NSString path)
	{
		return invokeObjcSelfClass!(Object, "fileHandleForUpdatingAtPath:", NSString)(path);
	}
}

template TNSFileHandlePlatformSpecific ()
{
	Object initWithFileDescriptor (int fd, bool closeopt)
	{
		return invokeObjcSelf!(Object, "initWithFileDescriptor:closeOnDealloc:", int, bool)(fd, closeopt);
	}

	this (int fd, bool closeopt)
	{
		objcObject = Bridge.invokeObjcClassMethod!(id, "alloc")(objcClass);
		id result = Bridge.invokeObjcMethod!(id, "initWithFileDescriptor:closeOnDealloc:", int, bool)(objcObject, fd, closeopt);

		if (result)
			objcObject = ret;

		dObject = this;
	}

	Object initWithFileDescriptor (int fd)
	{
		return invokeObjcSelf!(Object, "initWithFileDescriptor:", int)(fd);
	}

	this (int fd)
	{
		objcObject = Bridge.invokeObjcClassMethod!(id, "alloc")(objcClass);
		id result = Bridge.invokeObjcMethod!(id, "initWithFileDescriptor:", int)(objcObject, fd);

		if (result)
			objcObject = ret;

		dObject = this;
	}

	int fileDescriptor ()
	{
		return invokeObjcSelf!(int, "fileDescriptor");
	}
}