mirror of https://github.com/mono/CppSharp.git
Browse Source
Instead of keeping separate lists for different types of declarations, which limits future extensibility, provide a generalized declaration list like Clang. This also introduces a new DeclIterator<T> type to help migrating the rest of the codebase. This is the first step of a progressive cleanup to modernize the AST/parser layer.pull/404/head
3 changed files with 117 additions and 33 deletions
@ -0,0 +1,67 @@
@@ -0,0 +1,67 @@
|
||||
using System; |
||||
using System.Collections; |
||||
using System.Collections.Generic; |
||||
using System.Linq; |
||||
|
||||
namespace CppSharp.AST |
||||
{ |
||||
public struct DeclIterator<T> : IEnumerable<T> where T : Declaration |
||||
{ |
||||
private readonly List<Declaration> Declarations; |
||||
|
||||
public DeclIterator(List<Declaration> declarations) |
||||
{ |
||||
Declarations = declarations; |
||||
} |
||||
|
||||
public int Count |
||||
{ |
||||
get { return Declarations.OfType<T>().ToArray().Length; } |
||||
} |
||||
|
||||
public T this[int index] |
||||
{ |
||||
get { return Declarations.OfType<T>().ToArray()[index]; } |
||||
} |
||||
|
||||
public void Add(T declaration) |
||||
{ |
||||
Declarations.Add(declaration); |
||||
} |
||||
|
||||
public void AddRange(IEnumerable<T> range) |
||||
{ |
||||
Declarations.AddRange(range); |
||||
} |
||||
|
||||
public T Find(Func<T, bool> predicate) |
||||
{ |
||||
return Declarations.OfType<T>().SingleOrDefault<T>(predicate); |
||||
} |
||||
|
||||
public int FindIndex(Predicate<T> predicate) |
||||
{ |
||||
return Declarations.OfType<T>().ToList().FindIndex(predicate); |
||||
} |
||||
|
||||
public bool Exists(Func<T, bool> predicate) |
||||
{ |
||||
return Declarations.OfType<T>().Any(predicate); |
||||
} |
||||
|
||||
public IEnumerator<T> GetEnumerator() |
||||
{ |
||||
return Declarations.OfType<T>().GetEnumerator(); |
||||
} |
||||
|
||||
IEnumerator IEnumerable.GetEnumerator() |
||||
{ |
||||
return GetEnumerator(); |
||||
} |
||||
|
||||
public void Replace(T decl, T newDecl) |
||||
{ |
||||
Declarations[Declarations.FindIndex(d => d == decl)] = newDecl; |
||||
} |
||||
} |
||||
} |
Loading…
Reference in new issue