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 clang.c.index; 10 11 import std..string; 12 import std.conv; 13 14 immutable(char*)* strToCArray (string[] arr) 15 { 16 if (!arr) 17 return null; 18 19 immutable(char*)[] cArr; 20 cArr.reserve(arr.length); 21 22 foreach (str ; arr) 23 cArr ~= str.toStringz; 24 25 return cArr.ptr; 26 } 27 28 string toD (CXString cxString) 29 { 30 auto cstr = clang_getCString(cxString); 31 auto str = to!(string)(cstr).idup; 32 clang_disposeString(cxString); 33 34 return str; 35 } 36 37 template isCX (T) 38 { 39 enum bool isCX = __traits(hasMember, T, "cx"); 40 } 41 42 template cxName (T) 43 { 44 enum cxName = "CX" ~ T.stringof; 45 } 46 47 U* toCArray (U, T) (T[] arr) 48 { 49 if (!arr) 50 return null; 51 52 U[] cArr; 53 cArr.reserve(arr.length); 54 55 foreach (e ; arr) 56 cArr ~= e.cx; 57 58 return cArr.ptr; 59 } 60 61 mixin template CX () 62 { 63 mixin("private alias " ~ cxName!(typeof(this)) ~ " CType;"); 64 65 CType cx; 66 alias cx this; 67 68 void dispose () 69 { 70 enum methodCall = "clang_dispose" ~ typeof(this).stringof ~ "(cx);"; 71 72 static if (false && __traits(compiles, methodCall)) 73 mixin(methodCall); 74 } 75 76 @property bool isValid () 77 { 78 return cx !is CType.init; 79 } 80 }