Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

removed debug output

  • Property svn:eol-style set to native
File size: 5.1 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            {
67                evaluation = CommandExecutor::evaluate(command);
68                evaluation.evaluateParams();
69                CommandExecutor::getInstance().cache(command, evaluation);
70            }
71
72            return evaluation.query(error);
73        }
74    }
75
76    /* static */ std::string CommandExecutor::query(const std::string& command, int* error, bool useTcl)
77    {
78        return CommandExecutor::queryMT(command, error, useTcl).getString();
79    }
80
81    /* static */ CommandEvaluation CommandExecutor::evaluate(const std::string& command)
82    {
83        CommandEvaluation evaluation;
84        evaluation.initialize(command);
85
86        evaluation.hintCommand_ = _ConsoleCommand::getCommand(__CC_CommandExecutor_name, __CC_autocomplete_name);
87
88        if (evaluation.getNumberOfArguments() >= 1)
89        {
90            evaluation.execCommand_ = _ConsoleCommand::getCommandLC(evaluation.getToken(0));
91            if (evaluation.execCommand_)
92                evaluation.execArgumentsOffset_ = 1;
93            else if (evaluation.getNumberOfArguments() >= 2)
94            {
95                evaluation.execCommand_ = _ConsoleCommand::getCommandLC(evaluation.getToken(0), evaluation.getToken(1));
96                if (evaluation.execCommand_)
97                    evaluation.execArgumentsOffset_ = 2;
98            }
99        }
100
101        if (evaluation.execCommand_ && evaluation.getNumberOfArguments() > evaluation.execArgumentsOffset_)
102        {
103            evaluation.hintCommand_ = evaluation.execCommand_;
104            evaluation.hintArgumentsOffset_ = evaluation.execArgumentsOffset_;
105        }
106
107        return evaluation;
108    }
109
110    bool CommandExecutor::getCached(const std::string& command, CommandEvaluation& evaluation)
111    {
112        if (Shell::getCacheSize() == 0)
113            return false;
114
115        std::map<std::string, CacheEntry>::iterator it = this->cache_.find(command);
116        if (it != this->cache_.end())
117        {
118            // update ordered list of cached commands (move it to the front)
119            this->cachelist_.erase(it->second.iterator_);
120            this->cachelist_.push_front(command);
121            it->second.iterator_ = this->cachelist_.begin();
122
123            // assign the cached evaluation
124            evaluation = it->second.evaluation_;
125            return true;
126        }
127        return false;
128    }
129
130    void CommandExecutor::cache(const std::string& command, const CommandEvaluation& evaluation)
131    {
132        if (Shell::getCacheSize() == 0)
133            return;
134
135        // push command to the front of the ordered list
136        this->cachelist_.push_front(command);
137
138        // create a cache entry and store it in the cache
139        CacheEntry entry;
140        entry.evaluation_ = evaluation;
141        entry.iterator_ = this->cachelist_.begin();
142        this->cache_[command] = entry;
143
144        // remove the last command in the ordered list from the cache if it exceeds the maximum size of the cache
145        if (this->cachelist_.size() > Shell::getCacheSize())
146        {
147            this->cache_.erase(this->cachelist_.back());
148            this->cachelist_.pop_back();
149        }
150    }
151}
Note: See TracBrowser for help on using the repository browser.