- Main Page
- Namespaces
- Classes
- Files
- Related Pages

# /Users/fpy/titan-1-5-MBARI/Mers/Executives/DeductiveController/Command/CommandDispatcherEntry.cpp

    00001
    00002 //
    00003 // Copyright (c) 2004           Model-based Embedded and Robotic Systems Group
    00004 // All Rights Reserved          Massachusetts Institute of Technology
    00005 //
    00006 // This software is provided as is. Model-based Embedded and Robotic Systems
    00007 // (MERS) group does not assume any responsibility.
    00008 //
    00010
    00011 #include "CommandDispatcherEntry.h"
    00012
    00014
    00015 using namespace Mers::Executives::DeductiveController::Command;
    00016 using Mers::Utils::Lang::BaseModelObject;
    00017 using Mers::Utils::Lang::String;
    00018 using Mers::Utils::Exceptions::IllegalArgumentException;
    00019 using Mers::Utils::Exceptions::RuntimeException;
    00020 using std::vector;
    00021 using std::ostream;
    00022 using std::endl;
    00023
    00025 // Construction/Destruction
    00027
    00028 CommandDispatcherEntry::CommandDispatcherEntry(const char * fromFunctionName,
    00029         unsigned int numFromParams, const char * toFunctionName )
    00030         : BaseModelObject( BaseModelObject::oHCAFunctionCall ),
    00031                 m_fromFuncName( fromFunctionName ), m_numFromParams( numFromParams ),
    00032                 m_toFuncName( toFunctionName ), m_toFuncParams( true ),
    00033                 m_fromFuncParams( true )
    00034 {
    00035 }
    00036
    00038
    00039 CommandDispatcherEntry::CommandDispatcherEntry(
    00040                                                 const String &fromFunctionName,
    00041                                                 unsigned int numFromParams,
    00042                                                 const String &toFunctionName )
    00043         : BaseModelObject( BaseModelObject::oHCAFunctionCall ),
    00044                 m_fromFuncName( fromFunctionName ), m_numFromParams( numFromParams ),
    00045                 m_toFuncName( toFunctionName ), m_toFuncParams( true ),
    00046                 m_fromFuncParams( true )
    00047 {
    00048 }
    00049
    00051
    00052 CommandDispatcherEntry::~CommandDispatcherEntry()
    00053 {
    00054 }
    00055
    00057
    00058 void CommandDispatcherEntry::addToParameter( const char * str )
    00059 {
    00060         // Construct a new parameter.
    00061         ParameterLiteral * param = new ParameterLiteral( str );
    00062
    00063         // Record this parameter.
    00064         m_toFuncParams.pushBack( param );
    00065 }
    00066
    00068
    00069 void CommandDispatcherEntry::addToParameter( const String &str )
    00070 {
    00071         // Construct a new parameter.
    00072         ParameterLiteral * param = new ParameterLiteral( str );
    00073
    00074         // Record this parameter.
    00075         m_toFuncParams.pushBack( param );
    00076 }
    00077
    00079
    00080 void CommandDispatcherEntry::addToParameter( unsigned int index )
    00081         throw(IllegalArgumentException *)
    00082 {
    00083         // Make sure our index is less than m_numFromParams;
    00084         if( index >= m_numFromParams )
    00085                 throw new IllegalArgumentException( "Parameter index was greater than the"
    00086                         " number of parameters.", __FILE__, __LINE__ );
    00087
    00088         // Construct a new parameter.
    00089         ParameterReference * param = new ParameterReference( index );
    00090
    00091         // Record this parameter.
    00092         m_toFuncParams.pushBack( param );
    00093 }
    00094
    00096
    00097 void CommandDispatcherEntry::setFromParameter( unsigned int index,
    00098         const char * str )
    00099 {
    00100         // Clear what was there before, if anything, and make sure we have
    00101         // space for this index.  This will also verify index is in range
    00102         // for the function.
    00103         clearFromParameter( index );
    00104
    00105         if( str != NULL ) {
    00106                 // Construct a new parameter.
    00107                 ParameterLiteral * param = new ParameterLiteral( str );
    00108
    00109                 // Record this parameter.
    00110                 m_fromFuncParams[index] = param;
    00111         }
    00112 }
    00113
    00115
    00116 void CommandDispatcherEntry::setFromParameter( unsigned int index,
    00117         const String &str )
    00118 {
    00119         // Clear what was there before, if anything, and make sure we have
    00120         // space for this index.  This will also verify index is in range
    00121         // for the function.
    00122         clearFromParameter( index );
    00123
    00124         // Construct a new parameter.
    00125         ParameterLiteral * param = new ParameterLiteral( str );
    00126
    00127         // Record this parameter.
    00128         m_fromFuncParams[index] = param;
    00129 }
    00130
    00132
    00133 void CommandDispatcherEntry::clearFromParameter( unsigned int index )
    00134 {
    00135         // Verify index is in range.
    00136         if( index >= m_numFromParams ) {
    00137                 throw IllegalArgumentException( (String("Index of parameter to be set was "
    00138                         "out of range: ") + index + " was not less than " +
    00139                         m_numFromParams).c_str(), __FILE__, __LINE__ );
    00140         }
    00141
    00142         // Make sure we have enough space.
    00143         if( m_fromFuncParams.size() < index ) {
    00144                 m_fromFuncParams.resize( index + 1 );
    00145         } else if( m_fromFuncParams.size() == index ) {
    00146                 m_fromFuncParams.pushBack( NULL );
    00147         }
    00148
    00149         // Remove the previous reference.
    00150         if( m_fromFuncParams[index] != NULL ) {
    00151                 delete m_fromFuncParams[index];
    00152                 m_fromFuncParams[index] = NULL;
    00153         }
    00154 }
    00155
    00157
    00158 bool CommandDispatcherEntry::matches( const String &funcName,
    00159         const vector< const String * > &params ) const
    00160 {
    00161         // If our number of parameters or function name differ, we don't match.
    00162         if( params.size() != m_numFromParams || m_fromFuncName != funcName )
    00163                 return false;
    00164
    00165         // Look at parameter string matches.
    00166         for( unsigned int i = 0; i < m_fromFuncParams.size(); ++i ) {
    00167                 // Get our parameter
    00168                 const ParameterLiteral * lit = m_fromFuncParams[i];
    00169
    00170                 // Compare it if it's non-null.
    00171                 if( lit != NULL && lit->getLiteral() != *(params[i]) )
    00172                         return false;
    00173         }
    00174
    00175         // Everything matches.
    00176         return true;
    00177 }
    00178
    00180
    00181 void CommandDispatcherEntry::convertParameters(
    00182         const std::vector< const String * > &fromParams,
    00183         std::vector< const String * > &out_toParams ) const
    00184         throw(IllegalArgumentException *)
    00185 {
    00186         // Make sure that we get the right number of parameters.
    00187         if( fromParams.size() != m_numFromParams ) {
    00188                 throw new IllegalArgumentException( "The number of parameters of the "
    00189                         "calling function did not match the expected number of parameters.",
    00190                         __FILE__, __LINE__);
    00191         }
    00192
    00193         // Make our list of parameters empty so we get the right number.
    00194         out_toParams.clear();
    00195
    00196         // Copy our paramters over, static or reference.
    00197         for( unsigned int i = 0; i < m_toFuncParams.size(); ++i ) {
    00198                 // Get this parameter information.
    00199                 const Parameter * param = m_toFuncParams[i];
    00200
    00201                 // Add the appropriate parameter.
    00202                 if( param->isLiteral() ) {
    00203                         // Insert a constant string.
    00204                         out_toParams.push_back( &param->getLiteral() );
    00205                 } else {
    00206                         // Get the reference parameter.
    00207                         const String * str = fromParams[ param->getReference() ];
    00208
    00209                         // Insert the reference parameter.
    00210                         out_toParams.push_back( str );
    00211                 }
    00212         }
    00213 }
    00214
    00216
    00217 #ifndef EMBEDDED
    00218
    00219 void CommandDispatcherEntry::print( ostream &out ) const
    00220 {
    00221         // Print out our initial function name.
    00222         out << m_fromFuncName << "( ";
    00223         // Print out the initial parameters.
    00224         if( m_numFromParams > 0 ) {
    00225                 // Print out the first parameter
    00226                 out << "s0";
    00227                 if( m_numFromParams > 3 )
    00228                         // Print out the "lots" more parameters.
    00229                         out << ", ...";
    00230                 else if( m_numFromParams == 3 )
    00231                         out << ", s1";
    00232
    00233                 if( m_numFromParams > 1 )
    00234                         // Print out the last parameter.
    00235                         out << ", s" << ( m_numFromParams - 1 );
    00236         }
    00237         // Close the function, and start the next one.
    00238         out << " ) -> " << m_toFuncName << "( ";
    00239
    00240         // Print out our destination function's parameters.  Note that we
    00241         // would like to use m_toFunctionParams.printArray<PrintAll>(", "),
    00242         // but that kind of functionality isn't supported yet.
    00243         for( unsigned int i = 0; i < m_toFuncParams.size(); ++i ) {
    00244                 // Get this parameter.
    00245                 const Parameter * param = m_toFuncParams[i];
    00246
    00247                 // Print seperator, if needed.
    00248                 if( i != 0 )
    00249                         out << ", ";
    00250
    00251                 // Print out the parameter.
    00252                 param->print( out, "&s", "\"", "\"" );
    00253         }
    00254
    00255         // Close our function.
    00256         out << " )";
    00257 }
    00258
    00259 #endif
    00260
    00262
    00263 CommandDispatcherEntry * CommandDispatcherEntry::duplicate() const
    00264 {
    00265         return NULL;
    00266 }
    00267
    00269
    00270 bool
    00271 CommandDispatcherEntry::operator ==(CommandDispatcherEntry const &other) const
    00272 {
    00273         return
    00274                 // Do the cheap check first - they're the same if they're the same object.
    00275                 (this == &other) ||
    00276                 // Check each member, from cheapest to most expensive (int, string, array)
    00277                 (m_numFromParams == other.m_numFromParams &&
    00278                 m_fromFuncName == other.m_fromFuncName &&
    00279                 m_toFuncName == other.m_toFuncName &&
    00280                 m_toFuncParams == other.m_toFuncParams);
    00281 }
    00282
    00284
    00285 #ifndef EMBEDDED
    00286
    00287 void CommandDispatcherEntry::Parameter::print(std::ostream &out) const
    00288 {
    00289         // Print out the string, if we're a string, and a reference if we're
    00290         // a reference.
    00291         print( out, "&", "\"", "\"" );
    00292 }
    00293
    00295
    00296 void CommandDispatcherEntry::Parameter::print( std::ostream &out,
    00297         const char * refVarName, const char * litPrefix, const char * litPostfix )
    00298         const
    00299 {
    00300         if( isLiteral() ) {
    00301                 out << litPrefix << getLiteral() << litPostfix;
    00302         } else {
    00303                 out << refVarName << getReference();
    00304         }
    00305 }
    00306
    00307 #endif
    00308
    00310
    00311 bool CommandDispatcherEntry::Parameter::operator ==( const Parameter &other )
    00312         const
    00313 {
    00314         return ( isLiteral() && other.isLiteral() &&
    00315                                          getLiteral() == other.getLiteral() ) ||
    00316                 ( !isLiteral() && !other.isLiteral() &&
    00317                         getReference() == other.getReference() );
    00318 }
    00319
    00321
    00322 unsigned int CommandDispatcherEntry::ParameterLiteral::getReference() const
    00323         throw(RuntimeException *)
    00324 {
    00325         throw new RuntimeException("Attempt to get a reference from a literal.",
    00326                 __FILE__, __LINE__ );
    00327 }
    00328
    00330
    00331 const String & CommandDispatcherEntry::ParameterReference::getLiteral()
    00332         const throw(RuntimeException *)
    00333 {
    00334         throw new RuntimeException("Attempt to get a literal from a reference.",
    00335                 __FILE__, __LINE__ );
    00336 }
    00337
    00339
    00340