コード例 #1
0
        /// <summary>
        /// Gets or creates the <see cref="CommandGrammar"/> corresponding to the <see cref="GlobalState"/>
        /// </summary>
        public static CommandGrammar From(GlobalState globals)
        {
            // if this is same set of commands as the default set, then just return the default grammar
            if (globals.Commands == GlobalState.Default.Commands)
            {
                return(GetDefaultCommandGrammar());
            }

            CommandGrammar grammar      = null;
            bool           foundInCache = false;

            // if the globals has a cache, look in the cache
            if (globals.Cache != null)
            {
                foundInCache = globals.Cache.TryGetValue <CommandGrammar>(out grammar);
            }

            // if we still don't have a grammar check the recently created grammar
            // or create a new one
            if (grammar == null)
            {
                var recent = s_recentGrammar;
                if (recent != null && recent.Commands == globals.Commands)
                {
                    grammar = recent.Grammar;
                }
                else
                {
                    grammar = Create(globals);

                    // remember this grammar for next time
                    var newRecent = new RecentGrammar(globals.Commands, grammar);
                    Interlocked.CompareExchange(ref s_recentGrammar, newRecent, recent);
                }
            }

            // if the grammar did not come from the cache, put it into the cache if there is one.
            if (globals.Cache != null && !foundInCache)
            {
                grammar = globals.Cache.GetOrCreate <CommandGrammar>(() => grammar);
            }

            return(grammar);
        }
コード例 #2
0
 public RecentGrammar(IReadOnlyList <CommandSymbol> commands, CommandGrammar grammar)
 {
     this.Commands = commands;
     this.Grammar  = grammar;
 }