Mercurial > projects > hoofbaby
diff src/impl/hoofbaby/util/conv.d @ 0:3425707ddbf6
Initial import (hopefully this mercurial stuff works...)
author | fraserofthenight |
---|---|
date | Mon, 06 Jul 2009 08:06:28 -0700 |
parents | |
children |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/impl/hoofbaby/util/conv.d Mon Jul 06 08:06:28 2009 -0700 @@ -0,0 +1,46 @@ +/** + * Hoofbaby -- http://www.dsource.org/projects/hoofbaby + * Copyright (C) 2009 Robert Fraser + * + * This program is free software; you can redistribute it andor + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + */ + +module hoofbaby.util.conv; + +//------------------------------------------------------------------------------ +// Fast casting + +/** + * Cast without a typecheck. Use only if you're certain T is actually a U. + * + * Params: + * v = The value to cast from + * Returns: v as a T + */ +public T as(T, U)(U v) +{ + static assert(is(T : U)); + return cast(T) (cast(void*) v); +} + +public T checkedCast(T, U)(U v) +{ + static assert(is(T : U)); + if(v is null) + return null; + T ret = cast(T) v; + assert(ret !is null, "Expected " ~ T.stringof ~ ", but got " ~ v.classinfo.name); + return ret; +} + +//------------------------------------------------------------------------------ +// Mixin generation +