Paradox Game Engine  v1.0.0 beta06
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
SearchVisitor.cs
Go to the documentation of this file.
1 // Copyright (c) 2014 Silicon Studio Corp. (http://siliconstudio.co.jp)
2 // This file is distributed under GPL v3. See LICENSE.md for details.
3 using System;
4 using SiliconStudio.Shaders.Ast;
5 
6 namespace SiliconStudio.Shaders.Visitor
7 {
8  /// <summary>
9  /// A visitor that takes a filter function to apply to each node.
10  /// </summary>
12  {
13  /// <summary>
14  /// Initializes a new instance of the <see cref="SearchVisitor"/> class.
15  /// </summary>
16  /// <param name="filterFunction">The filter function.</param>
17  /// <param name="buildScopeDeclaration">if set to <c>true</c> [build scope declaration].</param>
18  /// <param name="useNodeStack">if set to <c>true</c> [use node stack].</param>
19  protected SearchVisitor(Func<Node, Node> filterFunction, bool buildScopeDeclaration = false, bool useNodeStack = false)
20  : base(buildScopeDeclaration, useNodeStack)
21  {
22  FilterFunction = filterFunction;
23  }
24 
25  /// <summary>
26  /// Gets or sets the filter function.
27  /// </summary>
28  /// <value>
29  /// The filter function.
30  /// </value>
31  protected Func<Node, Node> FilterFunction { get; set; }
32 
33  /// <summary>
34  /// Visits the specified node.
35  /// </summary>
36  /// <param name="node">The node.</param>
37  /// <returns>The filtered node</returns>
38  [Visit]
39  protected override Node Visit(Node node)
40  {
41  node = FilterFunction(node);
42  return (node != null) ? base.Visit(node) : null;
43  }
44 
45  /// <summary>
46  /// Searches from the specified node.
47  /// </summary>
48  /// <param name="node">The node.</param>
49  /// <param name="filter">The filter function to apply to each node.</param>
50  /// <param name="buildScopeDeclaration">if set to <c>true</c> [build scope declaration].</param>
51  /// <param name="useNodeStack">if set to <c>true</c> [use node stack].</param>
52  public static void Run(Node node, Func<Node, Node> filter, bool buildScopeDeclaration = false, bool useNodeStack = false)
53  {
54  var visitor = new SearchVisitor(filter, buildScopeDeclaration, useNodeStack);
55  visitor.Visit(node);
56  }
57  }
58 }
override Node Visit(Node node)
Visits the specified node.
A visitor that takes a filter function to apply to each node.
Abstract node.
Definition: Node.cs:15
SearchVisitor(Func< Node, Node > filterFunction, bool buildScopeDeclaration=false, bool useNodeStack=false)
Initializes a new instance of the SearchVisitor class.
static void Run(Node node, Func< Node, Node > filter, bool buildScopeDeclaration=false, bool useNodeStack=false)
Searches from the specified node.