Virtual files

H. S. Teoh hsteoh at quickfur.ath.cx
Thu Dec 6 16:11:55 UTC 2018


On Thu, Dec 06, 2018 at 02:48:59AM +0000, solidstate1991 via Digitalmars-d wrote:
> While working on a lot of enhancements for emesx'es tga library
> (almost a rewrite), I was thinking on implementing a way for handling
> memory locations as files, which would make handling archives mush
> easier, since I wouldn't have to write a separate overload (or more)
> for loading files from memory every time I need access to a new type.
[...]

There's no need for a separate overload.  All you need to do is:

	import std.stdio;
	auto myfunc(File = std.stdio.File, Args...)(File fp, Args args)
	{
		...
		fp.rawRead(...);
		...
		fp.rawWrite(...);
		... // etc
	}

	// uses std.stdio.File
	myfunc(File("realfile.txt", "r"), ...);

	struct InMemoryFile {
		...
		void[] rawRead(void[]) { ... }
		void rawWrite(void[]) { ... }
		...
	}

	// uses InMemoryFile
	myfunc(InMemoryFile(...), ...);

As long as the type you pass in has the same API as std.stdio.File (or a
subset thereof, as long as that subset is the only thing you use in the
called function), the code will work as-is with no additional work.

Writing it this way also makes it easy to test your code without having
your unittests pollute the filesystem with temporaries (and needing to
clean up the mess afterwards).

If you're concerned about template bloat, you could factor out the
common API into an interface, then write a wrapper for std.stdio.File.


T

-- 
"A man's wife has more power over him than the state has." -- Ralph Emerson


More information about the Digitalmars-d mailing list