Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/consolecommands3/src/libraries/core/command/CommandExecutor.cc @ 7230

Last change on this file since 7230 was 7230, checked in by landauf, 14 years ago

re-implemented parameter evaluation in CommandEvaluation and simplified parse() and evaluateParams() in Executor.

  • Property svn:eol-style set to native
File size: 5.3 KB
Line 
1/*
2 *   ORXONOX - the hottest 3D action shooter ever to exist
3 *                    > www.orxonox.net <
4 *
5 *
6 *   License notice:
7 *
8 *   This program is free software; you can redistribute it and/or
9 *   modify it under the terms of the GNU General Public License
10 *   as published by the Free Software Foundation; either version 2
11 *   of the License, or (at your option) any later version.
12 *
13 *   This program is distributed in the hope that it will be useful,
14 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
15 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 *   GNU General Public License for more details.
17 *
18 *   You should have received a copy of the GNU General Public License
19 *   along with this program; if not, write to the Free Software
20 *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
21 *
22 *   Author:
23 *      Fabian 'x3n' Landau
24 *   Co-authors:
25 *      ...
26 *
27 */
28
29#include "CommandExecutor.h"
30
31#include "ConsoleCommand.h"
32#include "TclBind.h"
33#include "Shell.h"
34
35namespace orxonox
36{
37    static const std::string __CC_CommandExecutor_name = "CommandExecutor";
38    static const std::string __CC_autocomplete_name = "autocomplete";
39
40    _SetConsoleCommand(__CC_CommandExecutor_name, __CC_autocomplete_name, &CommandExecutor::_autocomplete)
41        .hide()
42        .argumentCompleter(0, autocompletion::groupsandcommands())
43        .argumentCompleter(1, autocompletion::subcommands());
44
45    /* static */ CommandExecutor& CommandExecutor::getInstance()
46    {
47        static CommandExecutor instance;
48        return instance;
49    }
50
51    /* static */ int CommandExecutor::execute(const std::string& command, bool useTcl)
52    {
53        int error;
54        CommandExecutor::queryMT(command, &error, useTcl);
55        return error;
56    }
57
58    /* static */ MultiType CommandExecutor::queryMT(const std::string& command, int* error, bool useTcl)
59    {
60        if (useTcl)
61            return TclBind::eval(command, error);
62        else
63        {
64            CommandEvaluation evaluation;
65            if (!CommandExecutor::getInstance().getCached(command, evaluation))
66            {
67COUT(0) << "evaluate" << std::endl;
68                evaluation = CommandExecutor::evaluate(command);
69                evaluation.evaluateParams();
70                CommandExecutor::getInstance().cache(command, evaluation);
71            }
72            else
73            {
74COUT(0) << "cached" << std::endl;
75            }
76
77            return evaluation.query(error);
78        }
79    }
80
81    /* static */ std::string CommandExecutor::query(const std::string& command, int* error, bool useTcl)
82    {
83        return CommandExecutor::queryMT(command, error, useTcl).getString();
84    }
85
86    /* static */ CommandEvaluation CommandExecutor::evaluate(const std::string& command)
87    {
88        CommandEvaluation evaluation;
89        evaluation.initialize(command);
90
91        evaluation.hintCommand_ = _ConsoleCommand::getCommand(__CC_CommandExecutor_name, __CC_autocomplete_name);
92
93        if (evaluation.getNumberOfArguments() >= 1)
94        {
95            evaluation.execCommand_ = _ConsoleCommand::getCommandLC(evaluation.getToken(0));
96            if (evaluation.execCommand_)
97                evaluation.execArgumentsOffset_ = 1;
98            else if (evaluation.getNumberOfArguments() >= 2)
99            {
100                evaluation.execCommand_ = _ConsoleCommand::getCommandLC(evaluation.getToken(0), evaluation.getToken(1));
101                if (evaluation.execCommand_)
102                    evaluation.execArgumentsOffset_ = 2;
103            }
104        }
105
106        if (evaluation.execCommand_ && evaluation.getNumberOfArguments() > evaluation.execArgumentsOffset_)
107        {
108            evaluation.hintCommand_ = evaluation.execCommand_;
109            evaluation.hintArgumentsOffset_ = evaluation.execArgumentsOffset_;
110        }
111
112        return evaluation;
113    }
114
115    bool CommandExecutor::getCached(const std::string& command, CommandEvaluation& evaluation)
116    {
117        if (Shell::getCacheSize() == 0)
118            return false;
119
120        std::map<std::string, CacheEntry>::iterator it = this->cache_.find(command);
121        if (it != this->cache_.end())
122        {
123            // update ordered list of cached commands (move it to the front)
124            this->cachelist_.erase(it->second.iterator_);
125            this->cachelist_.push_front(command);
126            it->second.iterator_ = this->cachelist_.begin();
127
128            // assign the cached evaluation
129            evaluation = it->second.evaluation_;
130            return true;
131        }
132        return false;
133    }
134
135    void CommandExecutor::cache(const std::string& command, const CommandEvaluation& evaluation)
136    {
137        if (Shell::getCacheSize() == 0)
138            return;
139
140        // push command to the front of the ordered list
141        this->cachelist_.push_front(command);
142
143        // create a cache entry and store it in the cache
144        CacheEntry entry;
145        entry.evaluation_ = evaluation;
146        entry.iterator_ = this->cachelist_.begin();
147        this->cache_[command] = entry;
148
149        // remove the last command in the ordered list from the cache if it exceeds the maximum size of the cache
150        if (this->cachelist_.size() > Shell::getCacheSize())
151        {
152            this->cache_.erase(this->cachelist_.back());
153            this->cachelist_.pop_back();
154        }
155    }
156}
Note: See TracBrowser for help on using the repository browser.