MercuryCommands.cpp

Go to the documentation of this file.
00001 #include "MercuryCommands.h"
00002 #include "MercuryLog.h"
00003 #include "MercuryObject.h"
00004 #include "MercuryUtil.h"
00005 #include "PStack.h"
00006 #include "MercuryObjectFactory.h"
00007 
00008 using namespace std;
00009 
00010 bool MercuryCommandHandler::Command( PStack & ret, const char * command, PStack & args )
00011 {
00012     ret.PushItem( MercuryObjectFactory::GetInstance().GetCallback( GetType(), command )( this, args) );
00013     return true;
00014 }
00015 
00016 void MercuryCommandHandler::EnumerateCommands( MVector< MString > & toAdd )
00017 {
00018     MercuryObjectFactory::GetInstance().GetAllFunctions( GetType(), toAdd );
00019 }
00020 
00021 void MercuryCommandRegisteringSystem::CheckInit()
00022 {
00023     if ( !bInit )
00024     {
00025         mCommands = new std::map< MString, CMDCallback >;
00026         bInit = true;
00027     }
00028 }
00029 
00030 int MercuryCommandRegisteringSystem::Register( const MString & command, CMDCallback reg )
00031 {
00032     CheckInit();
00033     if ( mCommands->find( command ) == mCommands->end() )
00034         (*mCommands)[command] = reg;
00035     else
00036     {
00037         LOG.Warn("Double registeration of command \""+command+"\"!");
00038         return 0;
00039     }
00040     return 1;
00041 }
00042 
00043 bool MercuryCommandRegisteringSystem::ExecuteCommand( PStack & ret, const MString & command, PStack & args, void * source )
00044 {
00045     CheckInit();
00046     if ( mCommands->find( command ) == mCommands->end() )
00047     {
00048         LOG.Warn( "There is no command by the name: "+command );
00049         return false;
00050     }
00051     return (*(*mCommands)[command])( ret, command, args, source );
00052 }
00053 
00054 bool MercuryCommandRegisteringSystem::ExecuteObjectCommand( PStack & ret, const MString & object, const MString & command, PStack & args )
00055 {
00056     CheckInit();
00057     return OBJECTREGISTER.GetObjectFromName( object )->Command( ret, command.c_str(), args );
00058 }
00059 
00060 bool MercuryCommandRegisteringSystem::ExecuteCommandString( const MString & sCommand )
00061 {
00062     return false;
00063 }
00064 
00065 MercuryCommandRegisteringSystem CMDMAN;
00066 
00067 /***************MERCURY COMMAND-MESSAGE BRIDGE INTERFACE*****************/
00068 
00069 bool ToMessage( PStack & ret, const char * cmd, PStack & args, void * info )
00070 {
00071     PSElement g = args.PopItem();
00072     if ( g.GetType() == PSElement::STRING )
00073     {
00074         MESSAGEMAN->BroadcastMessage( g.GetValueS(), args );
00075         return true;
00076     }
00077     else
00078     {
00079         LOG.Warn( "Message command was not a string!" );
00080         return false;
00081     }
00082 }
00083 
00084 
00085 
00086 
00087 
00088 
00089 
00090 
00091 
00092 REGISTER_FUNCTION_TO_COMMAND( Message, ToMessage );
00093 
00094 bool HGEXPORT RunCommandOnObject( const char * sObjectName, const char * sCommandName, int iMPStackArgs, int iMPStackRet )
00095 {
00096     PStack * args = GetMPStack( iMPStackArgs );
00097     PStack * ret = GetMPStack( iMPStackRet );
00098     if ( !args || !ret )
00099         return false;
00100     MercuryObject * obj = OBJECTREGISTER.GetObjectFromName( MString( sObjectName ) );
00101     if ( obj )
00102         return obj->Command( *ret, sCommandName, *args );
00103     else
00104         return false;
00105 }
00106 
00107 /* 
00108  * Copyright (c) 2006, Charles Lohr
00109  * All rights reserved.
00110  *
00111  * Redistribution and use in source and binary forms, with or
00112  * without modification, are permitted provided that the following
00113  * conditions are met:
00114  *  -   Redistributions of source code must retain the above
00115  *      copyright notice, this list of conditions and the following disclaimer.
00116  *  -   Redistributions in binary form must reproduce the above copyright
00117  *      notice, this list of conditions and the following disclaimer in
00118  *      the documentation and/or other materials provided with the distribution.
00119  *  -   Neither the name of the <ORGANIZATION> nor the names of its
00120  *      contributors may be used to endorse or promote products derived from
00121  *      this software without specific prior written permission.
00122  *
00123  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00124  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00125  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00126  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
00127  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00128  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
00129  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00130  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
00131  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
00132  * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00133  */
00134 

Hosted by SourceForge.net Logo