IScript.cpp

00001 /***************************************************************************
00002  *                                                                         *
00003  *   (c) Art Tevs, MPI Informatik Saarbruecken                             *
00004  *       mailto: <tevs@mpi-sb.mpg.de>                                      *
00005  *                                                                         *
00006  *   This program is free software; you can redistribute it and/or modify  *
00007  *   it under the terms of the GNU General Public License as published by  *
00008  *   the Free Software Foundation; either version 2 of the License, or     *
00009  *   (at your option) any later version.                                   *
00010  *                                                                         *
00011  ***************************************************************************/
00012 
00013 
00014 //----------------------------------------------------------------------------------
00015 // Includes
00016 //----------------------------------------------------------------------------------
00017 #include "IScript.h"
00018 #include "Log.h"
00019 #include "Kernel.h"
00020 
00021 namespace nrEngine{
00022 
00023 
00024         //--------------------------------------------------------------------
00025         void IScript::notifyLoaded()
00026         {
00027                 markResourceLoaded();
00028         }
00029 
00030         //--------------------------------------------------------------------
00031         Result IScript::onStartTask()
00032         {
00033                 // notice derived classes about starting
00034                 onStartScript();
00035 
00036                 return OK;
00037         }
00038 
00039         //--------------------------------------------------------------------
00040         Result IScript::execute(bool runOnce)
00041         {
00042                 // set running counter parameter
00043                 mbRunOnce = runOnce;
00044 
00045                 // if task is already running, so do not do anything
00046                 if (getTaskState() == TASK_RUNNING) return SCRIPT_ALREADY_RUNNING;
00047 
00048                 // add the task to the kernel
00049                 // also kernel is not allowed to remove this object, because
00050                 // we created it from this, so no reference counting is used
00051                 SharedPtr<ITask> task (this, null_deleter());
00052                 Engine::sKernel()->AddTask(task, ORDER_NORMAL);
00053 
00054                 return OK;
00055         }
00056 
00057         //--------------------------------------------------------------------
00058         Result IScript::forceExecute(bool runOnce)
00059         {
00060                 mbRunOnce = runOnce;
00061 
00062                 // we only run the script nothing more
00063                 if (runOnce) return run();
00064 
00065                 // we add it to the kernel and run it first
00066                 return execute(runOnce);
00067         }
00068 
00069         //--------------------------------------------------------------------
00070         Result IScript::updateTask()
00071         {
00072                 // run the script
00073                 Result ret = run();
00074 
00075                 // if script should run once, so check if it has still commands
00076                 if (shouldRunOnce())
00077                 {
00078                         // check if script still have some commands
00079                         if (!hasCommands())
00080                         {
00081                                 Engine::sKernel()->RemoveTask(this->getTaskID());
00082                         }
00083                 }
00084 
00085                 // return result
00086                 return ret;
00087         }
00088 
00089         //--------------------------------------------------------------------
00090         IScript::~IScript()
00091         {
00092 
00093         }
00094 
00095         //--------------------------------------------------------------------
00096         IScript::IScript(const std::string& name) : IResource(name)
00097         {
00098                 mbRunOnce = true;
00099                 setTaskName(std::string("Script_") + name);
00100         }
00101 
00102         //--------------------------------------------------------------------
00103         uint32 IScript::pushArgument(const std::string& funcName, const std::string& type, const std::string& value)
00104         {
00105                 // store the new argument in the database
00106                 mArguments[funcName].push_back(std::pair<std::string,std::string>(type, value));
00107 
00108                 // get the number of arguments for the function
00109                 return mArguments[funcName].size();
00110         }
00111 
00112         //--------------------------------------------------------------------
00113         uint32 IScript::setArguments(const std::string& funcName, const ArgumentList& args)
00114         {
00115                 mArguments[funcName] = args;
00116                 return mArguments[funcName].size();
00117         }
00118 
00119         //--------------------------------------------------------------------
00120         int32 IScript::popArgument(const std::string& funcName, std::string& type, std::string& value)
00121         {
00122                 // first check if such a function does exists
00123                 ArgumentDatabase::iterator it = mArguments.find(funcName);
00124                 if (it == mArguments.end()) return -1;
00125 
00126                 // check the number of elements
00127                 if (it->second.size() == 0) return -1;
00128 
00129                 // pop the element and return the number of remaining elements
00130                 const std::pair<std::string, std::string>& first = it->second.front();
00131                 type = first.first;
00132                 value = first.second;
00133 
00134                 it->second.pop_front();
00135 
00136                 return it->second.size();
00137         }
00138 
00139         //----------------------------------------------------------------------------------
00140         ScriptResult IScript::call(const std::string& funcName)
00141         {
00142                 // call a certain function
00143                 ScriptResult res;
00144                 Result ret = callfunc(funcName, res);
00145                 if (ret == OK) return res;
00146 
00147                 // we got an error, so throw an error
00148                 NR_EXCEPT(ret, mErrorMsg.front(), "IScript::call()");
00149         }
00150 
00151         //----------------------------------------------------------------------------------
00152         Result IScript::callfunc(const std::string& funcName, ScriptResult& result)
00153         {
00154                 return OK;
00155         }
00156 
00157         //----------------------------------------------------------------------------------
00158         std::string IScript::popLastError()
00159         {
00160                 if (mErrorMsg.size() == 0) return std::string();
00161                 std::string msg = mErrorMsg.front();
00162                 mErrorMsg.pop_front();
00163                 return msg;
00164         }
00165 
00166         //----------------------------------------------------------------------------------
00167         void IScript::pushErrorMessage(const std::string& sh)
00168         {
00169                 mErrorMsg.push_front(sh);
00170                 NR_Log(Log::LOG_CONSOLE, Log::LL_ERROR, "%s", sh.c_str());
00171         }
00172 
00173         //----------------------------------------------------------------------------------
00174         // ----------------- Implementation of the empty script object ---------------------
00175         //----------------------------------------------------------------------------------
00176         //----------------------------------------------------------------------------------
00177         EmptyScript::EmptyScript(const std::string& typeName) : IScript(typeName + "Empty")
00178         {
00179 
00180         }
00181 
00182         //----------------------------------------------------------------------------------
00183         EmptyScript::~EmptyScript()
00184         {
00185 
00186         }
00187 
00188         //----------------------------------------------------------------------------------
00189         Result EmptyScript::loadFromString(const std::string& str)
00190         {
00191                 return OK;
00192         }
00193 
00194         //----------------------------------------------------------------------------------
00195         Result EmptyScript::run()
00196         {
00197                 return OK;
00198         }
00199 
00200 
00201 }; // end namespace
00202 

Generated on Wed Sep 12 23:19:42 2007 for nrEngine by  doxygen 1.5.1