/// <summary> /// Creates a ClassDeclaration from C++ header source. /// Warning: Very brittle at present, will choke on headers with implementations rather than solely signatures. /// </summary> /// <param name="declaration"></param> /// <returns></returns> public static ClassDeclaration Create(string declaration) { // TODO: Make this less brittle with multiline support using(var reader = new StringReader(declaration)) { ClassDeclaration cppClass = null; string line; var level = AccessLevel.Private; while((line = reader.ReadLine()) != null) { if(line.StartsWith("class", StringComparison.OrdinalIgnoreCase)) cppClass = new ClassDeclaration(line.Replace("class ", "")); else if(line.StartsWith("public", StringComparison.OrdinalIgnoreCase)) level = AccessLevel.Public; else if(line.StartsWith("private", StringComparison.OrdinalIgnoreCase)) level = AccessLevel.Private; else if(line.StartsWith("protected", StringComparison.OrdinalIgnoreCase)) level = AccessLevel.Protected; else if(line.Contains("(") || line.Contains(")")) cppClass.Functions.Add(FunctionDeclaration.Create(line, level)); else if(!string.IsNullOrWhiteSpace(line) && !line.Contains("{") && !line.Contains("}")) cppClass.Fields.Add(FieldDeclaration.Create(line.Replace(";", ""), level)); } return cppClass; } }
static void Main(string[] args) { var myClass = new ClassDeclaration("CMyClass"); // Add a private field myClass.Fields.Add(new FieldDeclaration("m_someInt", Primitive.Int, AccessLevel.Private)); // Define our custom type var myType = new TypeReference("CSomeOtherClass").MakePointer(); // And store a private pointer with a basic comment myClass.Fields.Add(new FieldDeclaration("m_pSomePointer", myType, AccessLevel.Private, new Comment("Test comment"))); // Add getters myClass.Functions.Add(new FunctionDeclaration("GetInteger", Primitive.Int)); myClass.Functions.Add(new FunctionDeclaration("GetOtherClass", myType)); // And setters var intArg = new VariableDeclaration("value", Primitive.Int); var classPtrArg = new VariableDeclaration("value", myType); myClass.Functions.Add(new FunctionDeclaration("SetInteger", Primitive.Void, AccessLevel.Public, false, intArg)); myClass.Functions.Add(new FunctionDeclaration("SetOtherClass", Primitive.Void, AccessLevel.Public, false, classPtrArg)); // Add a virtual function with a multiline comment var comment = new MultilineComment("This is a function", "that should be overridden", "just because it can be"); var function = new FunctionDeclaration("OverrideMe", Primitive.Char.MakePointer().MakeConst(), AccessLevel.Public, true); function.Comment = comment; myClass.Functions.Add(function); var classSource = myClass.CreateSource(); Console.Write(myClass.CreateSource()); Console.ReadLine(); }