comparison 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
comparison
equal deleted inserted replaced
-1:000000000000 0:3425707ddbf6
1 /**
2 * Hoofbaby -- http://www.dsource.org/projects/hoofbaby
3 * Copyright (C) 2009 Robert Fraser
4 *
5 * This program is free software; you can redistribute it andor
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 */
15
16 module hoofbaby.util.conv;
17
18 //------------------------------------------------------------------------------
19 // Fast casting
20
21 /**
22 * Cast without a typecheck. Use only if you're certain T is actually a U.
23 *
24 * Params:
25 * v = The value to cast from
26 * Returns: v as a T
27 */
28 public T as(T, U)(U v)
29 {
30 static assert(is(T : U));
31 return cast(T) (cast(void*) v);
32 }
33
34 public T checkedCast(T, U)(U v)
35 {
36 static assert(is(T : U));
37 if(v is null)
38 return null;
39 T ret = cast(T) v;
40 assert(ret !is null, "Expected " ~ T.stringof ~ ", but got " ~ v.classinfo.name);
41 return ret;
42 }
43
44 //------------------------------------------------------------------------------
45 // Mixin generation
46