Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changeset 7403 in orxonox.OLD for trunk/src/lib


Ignore:
Timestamp:
Apr 27, 2006, 5:49:12 PM (19 years ago)
Author:
bensch
Message:

orxonox/trunk: more exists functions to ClassList, and improved shellcompletion

Location:
trunk/src/lib
Files:
9 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/lib/lang/class_list.cc

    r7396 r7403  
    249249    if (cl != NULL)
    250250    {
    251       std::list<BaseObject*>::const_iterator bo = find (cl->objectList.begin(), cl->objectList.end(), object);
     251      std::list<BaseObject*>::const_iterator bo = std::find (cl->objectList.begin(), cl->objectList.end(), object);
    252252      return (bo != cl->objectList.end());
    253253    }
     
    258258    for (cl = ClassList::classList->begin(); cl != ClassList::classList->end(); cl++)
    259259    {
    260       std::list<BaseObject*>::const_iterator bo = find ((*cl).objectList.begin(), (*cl).objectList.end(), object);
     260      std::list<BaseObject*>::const_iterator bo = std::find ((*cl).objectList.begin(), (*cl).objectList.end(), object);
    261261      if (bo != (*cl).objectList.end())
    262262        return true;
     
    265265  return false;
    266266}
     267
     268bool ClassList::exists(const std::string& className, const std::string& objectName)
     269{
     270  ClassList* cl = ClassList::getClassList(className);
     271  if (likely(cl != NULL))
     272  {
     273    std::list<BaseObject*>::iterator bo;
     274    for (bo = cl->objectList.begin(); bo != cl->objectList.end(); bo++)
     275      if ((*bo)->getName() != NULL && objectName == (*bo)->getName())
     276        return true;
     277  }
     278  return false;
     279}
     280
    267281
    268282/**
  • trunk/src/lib/lang/class_list.h

    r7221 r7403  
    4747    static BaseObject*                    getObject(const std::string& name, ClassID classID = CL_NULL);
    4848    static bool                           exists(const BaseObject* object, ClassID classID = CL_NULL);
     49    static bool                           exists(const std::string& className, const std::string& objectName);
    4950
    5051    void                                  sendBack(std::list<BaseObject*>::const_iterator it);
  • trunk/src/lib/shell/shell_command.cc

    r7401 r7403  
    7070  ShellCommand* ShellCommand::registerCommand(const std::string& commandName, const std::string& className, const Executor& executor)
    7171  {
    72     if (ShellCommand::isRegistered(commandName, className))
     72    if (ShellCommand::exists(commandName, className))
    7373      return NULL;
    7474    else
     
    104104   * This is checked in the registerCommand-function.
    105105   */
    106   bool ShellCommand::isRegistered(const std::string& commandName, const std::string& className)
    107   {
    108     const ShellCommandClass* checkClass = ShellCommandClass::isRegistered(className);
    109     if (checkClass != NULL)
     106  bool ShellCommand::exists(const std::string& commandName, const std::string& className)
     107  {
     108    const ShellCommandClass* checkClass = ShellCommandClass::exists(className);
     109    if (likely(checkClass != NULL))
    110110    {
    111111      std::vector<ShellCommand*>::const_iterator elem;
     
    139139    bool emptyComplete = false;                  //< if the completion input is empty string. e.g ""
    140140    //  long completeType = SHELLC_NONE;           //< the Type we'd like to complete.
     141
    141142    SubString inputSplits(executionString, SubString::WhiteSpacesWithComma);
    142143
     
    147148
    148149    // if we only have one input (!MUST BE AN ALIAS)
    149     if (inputSplits.size() >= 1)
    150     {
    151       // CHECK FOR ALIAS
    152       std::vector<ShellCommandAlias*>::const_iterator alias;
    153       for (alias = ShellCommandAlias::getAliases().begin(); alias != ShellCommandAlias::getAliases().end(); alias++ )
    154       {
    155         if (inputSplits.getString(0) == (*alias)->getName() && (*alias)->getCommand() != NULL &&
    156             (*alias)->getCommand()->shellClass != NULL )
     150    // CHECK FOR ALIAS
     151    std::vector<ShellCommandAlias*>::const_iterator alias;
     152    for (alias = ShellCommandAlias::getAliases().begin(); alias != ShellCommandAlias::getAliases().end(); alias++ )
     153    {
     154      if (inputSplits.getString(0) == (*alias)->getName() && (*alias)->getCommand() != NULL &&
     155          (*alias)->getCommand()->shellClass != NULL )
     156      {
     157        objectList = ClassList::getList((*alias)->getCommand()->shellClass->getName());
     158        if (objectList != NULL)
    157159        {
    158           objectList = ClassList::getList((*alias)->getCommand()->shellClass->getName());
    159           if (objectList != NULL)
     160          (*(*alias)->getCommand()->executor)(objectList->front(), inputSplits.getSubSet(1).join());
     161          return true;
     162        }
     163      }
     164    }
     165
     166    // looking for a Matching Class (in the First Argument)
     167    std::vector<ShellCommandClass*>::iterator commandClassIT;
     168    for (commandClassIT = ShellCommandClass::commandClassList.begin(); commandClassIT != ShellCommandClass::commandClassList.end(); commandClassIT++)
     169    {
     170      if (inputSplits[0] == (*commandClassIT)->getName())
     171      {
     172        //elemCL->getName();
     173        classID = ClassList::StringToID((*commandClassIT)->getName());
     174        commandClass = (*commandClassIT);
     175        objectList = ClassList::getList((ClassID)classID);
     176        break;
     177      }
     178    }
     179
     180    // Second Agument. (either Object, or Function)
     181    if (commandClass != NULL && inputSplits.size() >= 2)
     182    {
     183      int fktPos = 1; // The position of the Function (either at pos 1(if object given), or 2)
     184      // If we have an ObjectList.
     185      if (objectList != NULL)
     186      {
     187        // Checking for a Match in the Objects of classID (else take the first)
     188        std::list<BaseObject*>::const_iterator object;
     189        for (object = objectList->begin(); object != objectList->end(); object++)
     190        {
     191          if ((*object)->getName() != NULL && inputSplits[1] == (*object)->getName())
    160192          {
    161             (*(*alias)->getCommand()->executor)(objectList->front(), inputSplits.getSubSet(1).join());
    162             return true;
     193            /// TODO make this work for multiple Objects at once.
     194            objectPointer = (*object);
     195            fktPos = 2;
     196            break;
    163197          }
    164198        }
    165       }
    166 
    167       // looking for a Matching Class
    168       std::vector<ShellCommandClass*>::iterator commandClassIT;
    169       for (commandClassIT = ShellCommandClass::commandClassList.begin(); commandClassIT != ShellCommandClass::commandClassList.end(); commandClassIT++)
    170       {
    171         if ((*commandClassIT)->getName() && inputSplits[0] == (*commandClassIT)->getName())
     199
     200        // if we did not find an Object with matching name, take the first.
     201        if (objectPointer == NULL)
     202          objectPointer = objectList->front();
     203      }
     204
     205      // match a function.
     206      if (commandClass != NULL && (fktPos == 1 || (fktPos == 2 && inputSplits.size() >= 3)))
     207      {
     208        std::vector<ShellCommand*>::iterator cmdIT;
     209        for (cmdIT = commandClass->commandList.begin(); cmdIT != commandClass->commandList.end(); cmdIT++)
    172210        {
    173           //elemCL->getName();
    174           classID = ClassList::StringToID((*commandClassIT)->getName());
    175           commandClass = (*commandClassIT);
    176           objectList = ClassList::getList((ClassID)classID);
    177           break;
    178         }
    179       }
    180 
    181       // Second Agument. (either Object, or Function)
    182       if (commandClass != NULL && inputSplits.size() >= 2)
    183       {
    184         int fktPos = 1; // The position of the Function (either at pos 1, or 2)
    185         // If we have an ObjectList.
    186         if (objectList != NULL)
    187         {
    188           // Checking for a Match in the Objects of classID (else take the first)
    189           std::list<BaseObject*>::const_iterator object;
    190           for (object = objectList->begin(); object != objectList->end(); object++)
     211          if (inputSplits[fktPos] == (*cmdIT)->getName())
    191212          {
    192             if ((*object)->getName() != NULL && inputSplits[1] == (*object)->getName())
     213            if (objectPointer == NULL && (*cmdIT)->executor->getType() & Executor_Objective)
     214              return false;
     215            else
    193216            {
    194               objectPointer = (*object);
    195               fktPos = 2;
    196               break;
    197             }
    198           }
    199 
    200           // if we did not find an Object with matching name, take the first.
    201           if (objectPointer == NULL)
    202             objectPointer = objectList->front();
    203         }
    204 
    205         // match a function.
    206         if (commandClass != NULL && (fktPos == 1 || (fktPos == 2 && inputSplits.size() >= 3)))
    207         {
    208           std::vector<ShellCommand*>::iterator cmdIT;
    209           for (cmdIT = commandClass->commandList.begin(); cmdIT != commandClass->commandList.end(); cmdIT++)
    210           {
    211             if (inputSplits[fktPos] == (*cmdIT)->getName())
    212             {
    213               if (objectPointer == NULL && (*cmdIT)->executor->getType() & Executor_Objective)
    214                 return false;
    215               else
    216               {
    217                 (*(*cmdIT)->executor)(objectPointer, inputSplits.getSubSet(fktPos+1).join()); /// TODO CHECK IF OK
    218                 return true;
    219               }
     217              (*(*cmdIT)->executor)(objectPointer, inputSplits.getSubSet(fktPos+1).join()); /// TODO CHECK IF OK
     218              return true;
    220219            }
    221220          }
     
    234233    if (this == NULL)
    235234      return NULL;
    236     else
    237     {
    238       this->description = description;
    239       return this;
    240     }
     235    this->description = description;
     236    return this;
    241237  }
    242238
     
    344340  * @returns true on success, false otherwise
    345341   */
     342
    346343  bool ShellCommandAlias::getCommandListOfAlias(std::list<std::string>& stringList)
    347344  {
  • trunk/src/lib/shell/shell_command.h

    r7401 r7403  
    7272
    7373    static ShellCommand* registerCommand(const std::string& commandName, const std::string& className, const Executor& executor);
    74 
    7574    static void unregisterCommand(const std::string& commandName, const std::string& className);
     75    static bool exists(const std::string& commandName, const std::string& className);
    7676
    7777    static void debug();
     
    8181    virtual ~ShellCommand();
    8282
    83     static bool isRegistered(const std::string& commandName, const std::string& className);
    8483    static const std::string& paramToString(long parameter);
    8584
  • trunk/src/lib/shell/shell_command_class.cc

    r7399 r7403  
    110110   * @returns the CommandClass if found, NULL otherwise
    111111   */
    112   const ShellCommandClass* ShellCommandClass::isRegistered(const std::string& className)
     112  const ShellCommandClass* ShellCommandClass::exists(const std::string& className)
    113113  {
    114114    std::vector<ShellCommandClass*>::const_iterator classIT;
  • trunk/src/lib/shell/shell_command_class.h

    r7394 r7403  
    3030    static ShellCommandClass* getCommandClass(const std::string& className);
    3131    static void unregisterAllCommands();
     32    static const ShellCommandClass* exists(const std::string& className);
    3233
    3334    static void help (const std::string& className);
     
    3738    virtual ~ShellCommandClass();
    3839
    39     static const ShellCommandClass* isRegistered(const std::string& className);
    4040
    4141    void registerCommand(ShellCommand* command);
  • trunk/src/lib/shell/shell_completion.cc

    r7393 r7403  
    5151  bool ShellCompletion::autoComplete(std::string& input)
    5252  {
    53     long classID;                               //< the classID retrieved from the Class.
    54     const std::list<BaseObject*>* objectList;   //< the list of Objects stored in classID
     53    long classID = CL_NULL;                     //< the classID retrieved from the Class.
     54    const std::list<BaseObject*>* objectList;   //< the list of Objects stored in classID's ClassList
    5555    bool emptyComplete = false;                 //< if the completion input is empty string. e.g ""
    5656    long completeType = NullCompletion;         //< the Type we'd like to complete.
    57     std::string completeString;                 //< the string to complete.
     57    std::string completeString = "";            //< the string to complete.
    5858
    5959
     
    6262
    6363    // Check if we are in a input. eg. the supplied string "class " and now we complete either function or object
    64     if (input[input.size()-1] == ' ')
     64    if (input.empty() || input[input.size()-1] == ' ')
    6565      emptyComplete = true;
    6666
     
    6969
    7070    // What String will be completed
    71     if (emptyComplete == true)
    72       completeString = "";
    73     else
     71    if (!emptyComplete && inputSplits.size() >= 1)
    7472      completeString = inputSplits.getString(inputSplits.size()-1);
    7573
    76     // CLASS COMPLETION
    77     if (inputSplits.size() == 0)
     74    // CLASS/ALIAS COMPLETION (on first argument)
     75    if (inputSplits.size() == 0 || (!emptyComplete && inputSplits.size() == 1))
    7876    {
    7977      completeType |= ClassCompletion;
    8078      completeType |= AliasCompletion;
    8179    }
    82     else if (inputSplits.size() == 1 && emptyComplete == false)
    83     {
    84       completeType |= ClassCompletion;
    85       completeType |= AliasCompletion;
    86     }
    8780
    8881    // OBJECT/FUNCTION COMPLETIONS
    89     else if ((inputSplits.size() == 1 && emptyComplete == true) ||
    90              (inputSplits.size() == 2 && emptyComplete == false))
     82    else if ((emptyComplete && inputSplits.size() == 1) ||
     83              (!emptyComplete && inputSplits.size() == 2))
    9184    {
    9285      classID = ClassList::StringToID(inputSplits.getString(0));
     
    9487      if (classID != CL_NULL)
    9588        completeType |= ObjectCompletion;
    96       //if (objectList != NULL && objectList->getSize() == 1)
    9789      completeType |= FunctionCompletion;
    9890    }
    99     else if ((inputSplits.size() == 2 && emptyComplete == true) ||
    100              (inputSplits.size() == 3 && emptyComplete == false))
    101     {
    102       classID = ClassList::StringToID(inputSplits.getString(0));
    103       if (classID == CL_NULL)
    104         return false;
    105       else
     91    else if ((emptyComplete && inputSplits.size() == 2 ) ||
     92             (!emptyComplete && inputSplits.size() == 3))
     93    {
     94      if (ClassList::exists(inputSplits[0], inputSplits[1]))
    10695        completeType |= FunctionCompletion;
    10796    }
  • trunk/src/lib/shell/shell_completion.h

    r7393 r7403  
    2222  class ShellCompletion
    2323  {
    24     //! an enumerator for different types the Shell can complete.
    25     typedef enum {
    26       NullCompletion         = 0,
    27       ClassCompletion        = 1,
    28       ObjectCompletion       = 2,
    29       FunctionCompletion     = 4,
    30       AliasCompletion        = 8,
     24      //! an enumerator for different types the Shell can complete.
     25      typedef enum {
     26        NullCompletion         = 0,
     27        ClassCompletion        = 1,
     28        ObjectCompletion       = 2,
     29        FunctionCompletion     = 4,
     30        AliasCompletion        = 8,
    3131    } CompletionType;
    3232
    33     //! A struct for ShellElements (these are used as containers to identify an Input for what it is)
    34     struct CompletionElement
    35     {
    36       std::string     name;     //!< the Name of the Element to be completed.
    37       CompletionType  type;     //!< the type of the Element
    38     };
     33      //! A struct for ShellElements (these are used as containers to identify an Input for what it is)
     34      struct CompletionElement
     35      {
     36        std::string     name;     //!< the Name of the Element to be completed.
     37        CompletionType  type;     //!< the type of the Element
     38      };
    3939
    40   public:
    41     ShellCompletion();
    42     virtual ~ShellCompletion();
     40    public:
     41      ShellCompletion();
     42      virtual ~ShellCompletion();
    4343
    4444
    45     // Functions to produce the Complete Lists.
    46     bool autoComplete(std::string& input);
    47     //  long classMatch(const char* input, unsigned int* length);
    48     bool objectComplete(const std::string& objectBegin, long classID);
    49     //  bool objectMatch(const char* objectBegin, long classID, unsigned int* length);
    50     bool commandComplete(const std::string& commandBegin, const std::string& className);
    51     //  bool functionMatch(const char* functionBegin, long classID, unsigned int* length);
    52     bool aliasComplete(const std::string& aliasBegin);
     45      // Functions to produce the Complete Lists.
     46      bool autoComplete(std::string& input);
    5347
    54     // Generally Completes.
    55     bool generalComplete(std::string& input,
    56                          const std::string& begin, const std::string& displayAs = "%s",
    57                          const std::string& addBack = "", const std::string& addFront = "");
     48      static const std::string& ShellCompletion::typeToString(ShellCompletion::CompletionType type);
     49
     50    private:
     51      //  long classMatch(const char* input, unsigned int* length);
     52      bool objectComplete(const std::string& objectBegin, long classID);
     53      //  bool objectMatch(const char* objectBegin, long classID, unsigned int* length);
     54      bool commandComplete(const std::string& commandBegin, const std::string& className);
     55      //  bool functionMatch(const char* functionBegin, long classID, unsigned int* length);
     56      bool aliasComplete(const std::string& aliasBegin);
     57
     58      // Generally Completes.
     59      bool generalComplete(std::string& input,
     60                           const std::string& begin, const std::string& displayAs = "%s",
     61                           const std::string& addBack = "", const std::string& addFront = "");
    5862
    5963
    60     bool addToCompleteList(const std::list<std::string>& inputList, const std::string& completionBegin, ShellCompletion::CompletionType type);
    61     bool addToCompleteList(const std::list<BaseObject*>& inputList, const std::string& completionBegin, ShellCompletion::CompletionType type);
     64      bool addToCompleteList(const std::list<std::string>& inputList, const std::string& completionBegin, ShellCompletion::CompletionType type);
     65      bool addToCompleteList(const std::list<BaseObject*>& inputList, const std::string& completionBegin, ShellCompletion::CompletionType type);
    6266
     67      // Helpers.
     68      void clearCompletionList();
    6369
    64     // Helpers.
    65     void clearCompletionList();
    66     static const std::string& ShellCompletion::typeToString(ShellCompletion::CompletionType type);
     70    private:
     71      std::vector<CompletionElement>  completionList;          //!< A list of completions, that are io.
    6772
    68   private:
    69     std::vector<CompletionElement>  completionList;          //!< A list of completions, that are io.
    70 
    71     static const std::string        typeNames[];             //!< A list of Completion-Type-Names.
     73      static const std::string        typeNames[];             //!< A list of Completion-Type-Names.
    7274  };
    7375
  • trunk/src/lib/shell/shell_input.h

    r7393 r7403  
    3939
    4040    // InputLine
    41     void flush();
    4241    void setInputText(const std::string& text);
    4342    void addCharacter(char character);
    4443    void addCharacters(const std::string& characters);
    4544    void removeCharacters(unsigned int characterCount = 1);
     45    void flush();
     46
    4647    void setRepeatDelay(float repeatDelay, float repeatRate);
    4748
Note: See TracChangeset for help on using the changeset viewer.