1 /**
2  * Copyright: Copyright (c) 2011 Jacob Carlborg. All rights reserved.
3  * Authors: Jacob Carlborg
4  * Version: Initial created: Oct 1, 2011
5  * License: $(LINK2 http://www.boost.org/LICENSE_1_0.txt, Boost Software License 1.0)
6  */
7 module clang.Util;
8 
9 import mambo.core._;
10 
11 import clang.c.Index;
12 
13 import std.conv;
14 
15 immutable(char*)* strToCArray (string[] arr)
16 {
17     if (!arr)
18         return null;
19 
20     immutable(char*)[] cArr;
21     cArr.reserve(arr.length);
22 
23     foreach (str ; arr)
24         cArr ~= str.toStringz;
25 
26     return cArr.ptr;
27 }
28 
29 string toD (CXString cxString)
30 {
31     auto cstr = clang_getCString(cxString);
32     auto str = to!(string)(cstr).idup;
33     clang_disposeString(cxString);
34 
35     return str;
36 }
37 
38 template isCX (T)
39 {
40     enum bool isCX = __traits(hasMember, T, "cx");
41 }
42 
43 template cxName (T)
44 {
45     enum cxName = "CX" ~ T.stringof;
46 }
47 
48 U* toCArray (U, T) (T[] arr)
49 {
50     if (!arr)
51         return null;
52 
53     static if (is(typeof(T.init.cx)))
54         return arr.map!(e => e.cx).toArray.ptr;
55 
56     else
57         return arr.ptr;
58 }
59 
60 mixin template CX ()
61 {
62     mixin("private alias " ~ cxName!(typeof(this)) ~ " CType;");
63 
64     CType cx;
65     alias cx this;
66 
67     void dispose ()
68     {
69         enum methodCall = "clang_dispose" ~ typeof(this).stringof ~ "(cx);";
70 
71         static if (false && __traits(compiles, methodCall))
72             mixin(methodCall);
73     }
74 
75     @property bool isValid ()
76     {
77         return cx !is CType.init;
78     }
79 }