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

# /Users/fpy/titan-1-5-MBARI/Mers/Interfaces/StaticTaskFactory.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 //
    00009 // Organization:        Model-based Embedded and Robotic Systems Group
    00010 //                                      Massachusetts Institute of Technology
    00011 //
    00013
    00014 #include "StaticTaskFactory.h"
    00015
    00016 #include "Task.h"
    00017 #include <vector>
    00018
    00020
    00021 using namespace Mers::Interfaces;
    00022 using Mers::Utils::Lang::String;
    00023 using Mers::Utils::Containers::SharedPointer;
    00024 using std::vector;
    00025 using log4cplus::Logger;
    00026
    00028
    00029 Logger StaticTaskFactory::m_logger(
    00030         Logger::getInstance("Mers.Interfaces.StaticTaskFactory") );
    00031
    00033
    00034 StaticTaskFactory::StaticTaskFactory()
    00035   : Mers::Interfaces::TaskFactory(),
    00036     m_Tasks(NULL), m_NumTasks(0)
    00037 {
    00038
    00039 }
    00040
    00042
    00043 StaticTaskFactory::~StaticTaskFactory()
    00044 {
    00045   delete[] m_Tasks;
    00046 }
    00047
    00049
    00050 TaskFactory::t_TaskSharedPointer
    00051 StaticTaskFactory::createTask( const TaskFactory::t_TaskName &taskName )
    00052 {
    00053         // We just match tasks by assuming we have parameters of length zero.
    00054         vector< const TaskFactory::t_TaskParameter * > parameters;
    00055
    00056         return createTask( taskName, parameters );
    00057 }
    00058
    00060
    00061 TaskFactory::t_TaskSharedPointer
    00062 StaticTaskFactory::createTask( const TaskFactory::t_TaskName &taskName,
    00063         vector< const TaskFactory::t_TaskParameter * > const &parameters )
    00064 {
    00065   // Cache the number of parameters we're looking for.
    00066   unsigned int numParams = parameters.size();
    00067
    00068   // Look for a task with no parameters named taskName.
    00069   for( unsigned int i = 0; i < m_NumTasks; ++i )
    00070   {
    00071     // Get the current task we're looking at.
    00072     t_TaskSharedPointer task = m_Tasks[i];
    00073
    00074     // If it isn't NULL, check to see if it matches.
    00075     if( task ) {
    00076
    00077       // Check the number of parameters first since it's less
    00078       // expensive than checking that the name matches.
    00079       const vector< const t_TaskParameter * > & taskParams =
    00080                                 task->getParameters();
    00081       if(taskParams.size() == numParams) {
    00082
    00083         // See if the name matches.
    00084         if( task->getName() == taskName) {
    00085           // Now verify that the parameters are the same
    00086           bool matches = true;
    00087           for( unsigned int j = 0; j < numParams && matches; ++j ) {
    00088             // Compare these two - using (String == char *)
    00089             matches = *parameters[j] == *taskParams[j];
    00090
    00091 #if !defined(LOG4CPLUS_DISABLE_TRACE)
    00092                                                 if( !matches ) {
    00093                                                         LOG4CPLUS_TRACE( m_logger, "Param " << j << ": " <<
    00094                                                                 *parameters[j] << " != " << *taskParams[j] );
    00095                                                 }
    00096 #endif
    00097           }
    00098
    00099           if( matches )
    00100             // We found our task.
    00101             return task;
    00102         }
    00103       } else {
    00104                                 LOG4CPLUS_TRACE( m_logger, "Task has " << numParams << " and not " <<
    00105                                         taskParams.size() );
    00106                         }
    00107     }
    00108   }
    00109
    00110 #if !defined(LOG4CPLUS_DISABLE_DEBUG)
    00111         // Log the failed match as a debug message (this is sometimes
    00112         // desirable behaviour).
    00113         if(m_logger.isEnabledFor(log4cplus::DEBUG_LOG_LEVEL)) {
    00114                 log4cplus::tostringstream log4cplus_buf;
    00115
    00116                 log4cplus_buf << "Failed to find task " << taskName << '(';
    00117                 for( unsigned int i = 0; i < parameters.size(); ++i ) {
    00118                         if( i != 0 )
    00119                                 log4cplus_buf << ", ";
    00120                         log4cplus_buf << *(parameters[i]);
    00121                 }
    00122                 log4cplus_buf << ')';
    00123
    00124                 m_logger.forcedLog(log4cplus::DEBUG_LOG_LEVEL, log4cplus_buf.str(),
    00125                         __FILE__, __LINE__);
    00126         }
    00127         // Log what the options were.
    00128         logTasks( log4cplus::TRACE_LOG_LEVEL );
    00129 #endif
    00130
    00131   // Didn't find a match, return no-match.
    00132   return t_TaskSharedPointer();
    00133 }
    00134
    00136
    00137 void StaticTaskFactory::logTasks( log4cplus::LogLevel logLevel ) const
    00138 {
    00139 #if !defined(LOG4CPLUS_DISABLE_FATAL)
    00140         if( !m_logger.isEnabledFor(logLevel) )
    00141                 return;
    00142
    00143         m_logger.forcedLog(logLevel, "Available Tasks:", __FILE__, __LINE__);
    00144         // Log all the tasks
    00145         for( unsigned int t = 0; t < m_NumTasks; ++t ) {
    00146     // Get the current task we're looking at.
    00147     t_TaskSharedPointer task = m_Tasks[t];
    00148
    00149     // If it isn't NULL, print it.
    00150     if( task ) {
    00151                         m_logger.forcedLog(logLevel, task->toString().c_str(), __FILE__, __LINE__);
    00152                 } else {
    00153                         m_logger.forcedLog(logLevel, "NULL", __FILE__, __LINE__);
    00154                 }
    00155         }
    00156 #endif
    00157 }
    00158
    00160