Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

implemented command cache in CommandExecutor

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